Search code examples
c#pdfacrobat

How to save a pdf silently in c#


I have a PDF that is protected.

If you open this PDF, open the print dialog and choose "Print to PDF" but if in that moment you save in your PC it will be saved as unprotected.

I want to do that in C#. I have this code:

Process proc = new Process();
proc.StartInfo.FileName = @"C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe";
proc.StartInfo.Arguments = String.Format(@"/o /p /h C:\Users\itsvan.moreno\Desktop\1.pdf", @"C:\Users\itsvan.moreno\Desktop\");
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = false;
proc.EnableRaisingEvents = true;
proc.Start();
if (proc.HasExited == false)
{
    proc.WaitForExit(10000);
}
proc.Close();

All the process is correct but a popup window is displayed to put the name at the new pdf file.

Save Print Output dialog

How can I save as without a popup?


Solution

  • Your code doesn't really unprotect it "in C#". You're unprotecting it with Acrobat Reader.

    Your code opens Acrobat Reader and tells it (with the /o /p /h paramters) to open a file and go directly to the print dialog. If you can't get further than that, it is a limitation of Acrobat Reader.

    You might be able to experiment with the /t option. See here. But I suspect it just doesn't give you a way to feed a filename to the Print to PDF driver. If not, then you just can't do it this way.

    You can look into a PDF library for .NET so that you are, actually, modifying the PDF in .NET, but the good ones aren't free. Aspose.PDF is one, for example.