Documents for PDF .NET Edition
Features / Print
In This Topic
    Print
    In This Topic

    Printing a PDF document is a basic and one of the most commonly used feature. In most cases, printing is supported through a PDF Viewer, that is, you can open your PDF document in a viewer and give a print document from viewer itself. However, in some cases you might want to print a document directly without using a viewer. In case of macOS and Linux, operating systems provide built-in print functionality and can print simply by using a command line. However in case of Windows, there is no such built-in feature available. Hence, we have created a product sample that uses Direct2D technology to print the document without using a viewer.

    Print on Windows OS

    To demonstrate direct printing through GcPdf on Windows operating system, a sample named "Print PDF on Windows using Direct2D" is hosted as a part of the online demo sample. The sample has been built up on GcD2DBitmap and GcD2DGraphics classes which reside in GrapeCity.Documents.Imaging.Windows package.

    The sample includes following ready to use utilities required to print a document and implement print settings. These can be used as it is in your application.

    The online demo sample has been designed to create a single-page PDF and return it. The actual code for printing the generated PDF on a local printer has been blocked inside a false condition as shown in the code below.

    C#
    Copy Code
    public void CreatePDF(Stream stream)
    {
        GcPdfDocument doc = new GcPdfDocument();
    
        Common.Util.AddNote(
            "This sample uses Direct2D to implement direct printing on Windows. ",
            doc.NewPage());
    
        // Include this block to print the PDF on Windows:
        if (false)
        {
            GcPdfPrintManager pm = new GcPdfPrintManager();
            pm.Doc = doc;
            pm.PrinterSettings = new PrinterSettings();
            // Set the printer name and/or other settings if not using the defaults.
            // pm.PrinterSettings.PrinterName = "my printer";
            pm.PageScaling = PageScaling.FitToPrintableArea;
            pm.Print();
        }
    
        // Save the PDF:
        doc.Save(stream);
    }
    

    To print a PDF, you need to download the demo sample, edit the source to remove the false condition and call the GcPdfPrintManager.Print() method. You can adjust the GcPdfPrintManager and PrinterSettings properties to get the desired output.

    Print on Linux and macOS

    Command line to print a PDF document on Linux:

    lp filename.pdf

     Command line to print a PDF document on macOS:

    lp filename.pdf

    OR

    #! /bin/bash
    cd /path/to/your/PDFfiles
    for pdffile in *.pdf;
    lpr -P MY_PRINTER -o media=A4 -o sides=two-sided-long-edge -o InputSlot=tray-3 "$pdffile";
    done