8

Printing pdf document with Microsoft XPS Document Writer:

string filename = "C:\\1.pdf";

Process process = new Process();
process.StartInfo.Verb = "PrintTo";

process.StartInfo.FileName = @"C:\Program Files\Adobe\Reader 9.0\Reader\acrord32.exe";

process.StartInfo.Arguments = 
    "/t \"C:\\1.pdf\" \"Microsoft XPS Document Writer\" \"xps\"  XPSPort:";

process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;

process.StartInfo.UseShellExecute = false;

process.Start();
process.StandardOutput.ReadToEnd();

process.WaitForExit();

The only problem is Save Dialog, which requests file name (*.xps) where to save result. Everbody advices DOCINFO to solve this problem, but I didn't find any example of using. I need programatically print PDF File via Microsoft XPS Document Writer with default output file name. How should I use DOCINFO in this situation?

Can you help me?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mykhail Galushko
  • 515
  • 2
  • 5
  • 14
  • The solution is not so simple (in 10+ lines). Anyway there is a classic problem with Adobe Reader - one copy remains open which looks bad for the user. Better use pdf to xps conversion tool like this: https://www.pdftron.com/pdf-sdk/conversion-library/ There are 2-3 alternatives. – i486 Nov 21 '18 at 08:40

1 Answers1

1

You can't reliably print by spawning Acrobat Reader unless you give it a desktop session and there will be a user there, because it sometimes pops up dialogs that need user attention.

Also it violates Adobe's licence if used unattended.

You can, however print using Ghostscript.

There is a C# interface to Ghostscript called Ghostscript.Net that I've used successfully on some very large projects. Both Ghostscript and Ghostcript.Net are free & open source.

Terry Carmen
  • 3,720
  • 1
  • 16
  • 32
  • 1
    It's very, VERY important to read Ghostscript.Net's licensing terms before using it. It uses "Affero GPL", which is a very dangerous license if you're working on commercial software. – Jamie Sep 06 '22 at 01:37