Vinicius Leandro
Vinicius Leandro

Reputation: 7

I can't print a PDF using PDFsharp

I have a problem when printing a PDF using the PDFsharp library.

I am using the Stream property of the PdfPage class to draw the page. However, it is showing a null reference error, as the stream is empty.

I couldn't understand why, because the PDF exists and the path is correct.

Below is the code:

public static void PrintPdfWithPrintDocument(string pdfPath)
{
    if (!System.IO.File.Exists(pdfPath))
    {
        MessageBox.Show("Not Found.");
        return;
    }

    pdfDoc = Pdf.IO.PdfReader.Open(pdfPath, Pdf.IO.PdfDocumentOpenMode.Modify);
    CurrentPage = 0;

    PrintDocument prnPdf = new PrintDocument();
    prnPdf.PrintPage += OnPrintPage;

    PrintDialog ptdPrinter = new PrintDialog();
    ptdPrinter.AllowSomePages = true;
    ptdPrinter.Document = prnPdf;

    if (ptdPrinter.ShowDialog() == DialogResult.OK)
    {
        prnPdf.PrinterSettings = ptdPrinter.PrinterSettings;
        prnPdf.Print();
    }
}

private static void OnPrintPage(object sender, PrintPageEventArgs e)
{
    if (CurrentPage < pdfDoc.PageCount)
    {
        pdfPage = pdfDoc.Pages(CurrentPage);
        pdfGraphics = XGraphics.FromPdfPage(pdfPage, XGraphicsUnit.Millimeter);
        pdfGraphics.MUH = Pdf.PdfFontEncoding.Unicode;

        PdfSharp.Pdf.PdfDictionary.PdfStream pdfStream = (PdfSharp.Pdf.PdfDictionary.PdfStream)pdfPage.Stream;
        MemoryStream imageStream = new MemoryStream(pdfStream.Value);
        XRect xRect = new XRect(0, 0, e.PageBounds.Width, e.PageBounds.Height);

        pdfGraphics.DrawImage(XImage.FromStream(imageStream), xRect);

        CurrentPage += 1;
        e.HasMorePages = CurrentPage < pdfDoc.PageCount;
    }
    else
        e.HasMorePages = false;
}

I didn't want to use another library or Acrobat Reader.

I've tried to find other ways to collect the bytes from the page but I haven't found any.

I installed x86 and x64 libraries from Pdfiumviewer

Solution:

public static void PrintPDF(string path)
  {
    try
    {
    PrintDialog printDialog = new PrintDialog();
    printDialog.PrinterSettings = new PrinterSettings();

    if (printDialog.ShowDialog() == DialogResult.OK)
    {
        PrinterSettings printerSettings = new PrinterSettings();
        printerSettings.PrinterName = printDialog.PrinterSettings.PrinterName;
        printerSettings.Copies = 1;

        PageSettings pageSettings = new PageSettings(printerSettings);
        pageSettings.Margins = new Margins(0, 0, 0, 0);

        using (var document = PdfDocument.Load(path))
        {
            using (var printDocument = document.CreatePrintDocument())
            {
                printDocument.PrinterSettings = printerSettings;
                printDocument.DefaultPageSettings = pageSettings;
                printDocument.PrintController = new StandardPrintController();
                printDocument.Print();
            }
        }
    }
}
catch (Exception ex)
{
    Console.WriteLine($"An error occurred while printing the file: {ex.Message}");
}
}

Upvotes: 0

Views: 233

Answers (1)

Where to start?

PDFsharp FAQ say that you cannot print PDFs using PDFsharp.

You create a prnPdf that can be used to draw on pages to be printed. But your code does not draw anything there.

You create a pdfGraphics that can be used to modify pages of the PDF file. Not your intention.

The streams of PDF files seldom contain image files that can be loaded by Windows and drawn anywhere.

Using Adobe Reader is an option to print PDF files, but PDFsharp is not.

Upvotes: 0

Related Questions