How to print a PDF with C#

前端 未结 11 568
抹茶落季
抹茶落季 2020-12-14 17:58

I´ve trying to solve this problem for nearly 2 days. There are a lot of more or fewer good solutions on the net, but not a single one fits my task perfectly.

相关标签:
11条回答
  • 2020-12-14 18:23

    The most flexible, easiest and best performing method I could find was using GhostScript. It can print to windows printers directly by printer name.

    "C:\Program Files\gs\gs9.07\bin\gswin64c.exe" -dPrinted -dBATCH -dNOPAUSE -sDEVICE=mswinpr2 -dNoCancel -sOutputFile="%printer%printer name" "pdfdocument.pdf"

    Add these switches to shrink the document to an A4 page.

    -sPAPERSIZE=a4 -dPDFFitPage

    0 讨论(0)
  • 2020-12-14 18:25

    What about using the PrintDocument class?

    http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx

    You just need to pass the filename of the file you want to print (based on the example).

    HTH

    0 讨论(0)
  • 2020-12-14 18:27

    If a commercial library is an option, you can try with Amyuni PDF Creator. Net.

    Printing directly with the library:
    For opening a PDF file and send it to print directly you can use the method IacDocument.Print. The code in C# will look like this:

    // Open PDF document from file<br>
    FileStream file1 = new FileStream ("test.pdf", FileMode.Open, FileAccess.Read);
    IacDocument doc1 = new IacDocument (null);
    doc1.Open (file1, "" );
    // print document to a specified printer with no prompt
    doc1.Print ("My Laser Printer", false);
    

    Exporting to images (then printing if needed):
    Choice 1: You can use the method IacDocument.ExportToJPeg for converting all pages in a PDF to JPG images that you can print or display using Drawing.Image

    Choice 2: You can draw each page into a bitmap using the method IacDocument.DrawCurrentPage with the method System.Drawing.Graphics.FromImage. The code in C# should look like this:

    FileStream myFile = new FileStream ("test.pdf", FileMode.Open, FileAccess.Read);
    IacDocument doc = new IacDocument(null);
    doc.Open(myFile);
    doc.CurrentPage = 1;
    Image img = new Bitmap(100,100);
    Graphics gph = Graphics.FromImage(img);
    IntPtr hdc = gph.GetHDC();
    doc.DrawCurrentPage(hdc, false);
    gph.ReleaseHdc( hdc );
    

    Disclaimer: I work for Amyuni Technologies

    0 讨论(0)
  • 2020-12-14 18:28

    You can use ghostscript to convert PDF into image formats.

    The following example converts a single PDF into a sequence of PNG-Files:

    private static void ExecuteGhostscript(string input, string tempDirectory)
    {
        // %d will be replaced by ghostscript with a number for each page
        string filename = Path.GetFileNameWithoutExtension(input) + "-%d.png";
        string output = Path.Combine(tempDirectory, filename);
    
        Process ghostscript = new Process();
        ghostscript.StartInfo.FileName = _pathToGhostscript;
        ghostscript.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    
        ghostscript.StartInfo.Arguments = string.Format(
            "-dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r300 -sOutputFile=\"{0}\" \"{1}\"", output, input);
    
        ghostscript.Start();
        ghostscript.WaitForExit();
    }
    

    If you prefer to use Adobe Reader instead you can hide its window:

    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    
    0 讨论(0)
  • 2020-12-14 18:29

    I know that the tag has Windows Forms; however, due to the general title, some people might be wondering if they may use that namespace with a WPF application -- they may.

    Here's code:

    var file = File.ReadAllBytes(pdfFilePath);
    var printQueue = LocalPrintServer.GetDefaultPrintQueue();
    
    using (var job = printQueue.AddJob())
    using (var stream = job.JobStream)
    {
        stream.Write(file, 0, file.Length);
    }
    

    Now, this namespace must be used with a WPF application. It does not play well with ASP.NET or Windows Service. It should not be used with Windows Forms, as it has System.Drawing.Printing. I don't have a single issue with my PDF printing using the above code.

    Note that if your printer does not support Direct Printing for PDF files, this won't work.

    0 讨论(0)
  • 2020-12-14 18:31

    I found a slightly different version of your code that uses the printto verb. I didn't try it, but maybe it helps you:

    http://vbcity.com/forums/t/149141.aspx

    0 讨论(0)
提交回复
热议问题