Print a PDF document using Aspose.PDF for .NET - Printing to file results in corrupt pdf

We have an API that on one of its endpoints is supposed to print out a pdf that is a byte array. The method that we have for this is the following:

public void Print(byte[] docBytes, string printerName, string printJobName, string printTargetDirectory)
{
    using (MemoryStream originalPdfStream = new MemoryStream(docBytes))
    {
        PdfViewer pdfViewer = new PdfViewer();
        pdfViewer.BindPdf(originalPdfStream);

        int a4PaperWidthAsHundrethOfInch = 827;
        int a4PaperHeightAsHundrethOfInch = 1169;

        PrinterSettings printerSettings = new PrinterSettings
        {
            Copies = 1,
            PrinterName = printerName,
            PrintToFile = true,
            PrintFileName = Path.Combine(printTargetDirectory, printJobName)
        };

        PageSettings pgs = new PageSettings
        {
            Margins = new Margins(0, 0, 0, 0),
            PaperSize = new PaperSize("A4", a4PaperWidthAsHundrethOfInch, a4PaperHeightAsHundrethOfInch),
            PrinterSettings = printerSettings
        };

        if (!printerSettings.IsValid)
        {
            throw new BadRequestException("Printer invalid");
        }

        pdfViewer.PrinterJobName = printJobName;
        pdfViewer.PrintPageDialog = false;

        pdfViewer.PrintDocumentWithSettings(pgs, printerSettings);

        if (pdfViewer.PrintStatus == null) return;

        Exception e = pdfViewer.PrintStatus as Exception;
        throw e;
    }
}

When I run this code, a file will be generated. An interesting thing is that this file size is significantly smaller than the original file, so I am guessing that Aspose is doing something here to reduce the file size. Anyway, I have tried opening this file with Adobe Acrobat Reader and it fails to open. So something is corrupt with the file. I also have another PDF viewer though, Sumatra PDF, and this actually can open the file. The file looks a bit compressed compared to the original.

Can you look into this issue?

Also I have tried setting PrintToFile = false but this results in a file dialog opening and I do not want that.

@stebe95

Would you kindly share your sample source PDF file with us with which we can run above code and try to replicate the issue at our end. We will further share our feedback with you accordingly.

You can use whatever pdf you have, the file does not matter.

@stebe95

We used following code snippet (similar to yours except usage of file stream) in our environment with Aspose.PDF for .NET 20.10 and printed a sample file. We did not notice any issue in the printed file.

using (FileStream originalPdfStream = new FileStream(dataDir + "Sample.pdf", FileMode.Open))
{
 Facades.PdfViewer pdfViewer = new Facades.PdfViewer();
 pdfViewer.BindPdf(originalPdfStream);

 int a4PaperWidthAsHundrethOfInch = 827;
 int a4PaperHeightAsHundrethOfInch = 1169;

 System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings
 {
  Copies = 1,
  PrinterName = "Microsoft Print to PDF",
  PrintToFile = true,
  PrintFileName = dataDir + "printotfile.pdf"
 };

 System.Drawing.Printing.PageSettings pgs = new System.Drawing.Printing.PageSettings
 {
  Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0),
  PaperSize = new System.Drawing.Printing.PaperSize("A4", a4PaperWidthAsHundrethOfInch, a4PaperHeightAsHundrethOfInch),
  PrinterSettings = printerSettings
 };

 if (!printerSettings.IsValid)
 {
                    
 }

 //pdfViewer.PrinterJobName = printJobName;
 pdfViewer.PrintPageDialog = false;

 pdfViewer.PrintDocumentWithSettings(pgs, printerSettings);

 if (pdfViewer.PrintStatus == null) return;

 Exception e = pdfViewer.PrintStatus as Exception;
 throw e;
}

printotfile.pdf (43.2 KB)
sample.pdf (3.0 KB)

Would you please try to save the incoming bytes directly to a .pdf file and share that PDF with us. We will again try to replicate the issue at our end and share our feedback with you.

Hi, thanks for your reply. I tried your code . The interesting thing is that it works when I run it locally. Before I call this printing method, I am actually merging multiple documents together. When I run this locally, my merged file is 142Kb. When I print it, the resulting file is 113KB and opens in Adobe Acrobat just fine. But when I run this code on the server, the and the resulting file is 99KB and cannot be opend.

I am using the exact same file in both environments and the same Aspose version and settings. How can it be so different?

I’m gonna see if I can use some other library or method of printing because there are clearly something wrong with the Aspose lib for this.

Edit: I ended up using this instead, works perfectly fine: https://github.com/frogmorecs/RawPrint

@stebe95

There may be something related to specific environment which is causing the issue at your side when code runs on server. We need to further investigate it in order to determine the cause. Could you please share the server details with us i.e. OS Name and Version, Application Type, Sample PDFs, etc. We will log a ticket against the case and share the ID with you.

It is nice to hear that you were able to sort out your issue.