Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating PDF using PDFsharp returns empty PDF

I have a list of PDF stored as list<byte[]>. I try to concatenate all these PDF files using PDFsharp, but after my operation I get a PDF with proper page count, but all pages are blank. Looks like I lose some header or something but I can't find where.

My code:

        PdfDocument output = new PdfDocument();
        try
        {
            foreach (var report in reports)
            {
                using (MemoryStream stream = new MemoryStream(report))
                {

                    PdfDocument input = PdfReader.Open(stream, PdfDocumentOpenMode.Import);

                    foreach (PdfPage page in input.Pages)
                    {
                        output.AddPage(page);
                    }
                }
            }


            if (output.Pages.Count <= 0)
            {
                throw new Exception("Empty Document");
            }
            MemoryStream final = new MemoryStream();
            output.Save(final);
            output.Close();
            return final.ToArray();
        }
        catch (Exception e)
        {
            throw new Exception(e.ToString());
        }

I want to return it as byte[] because I use them later:

return File(report, System.Net.Mime.MediaTypeNames.Application.Octet, "test.pdf");

This returns PDF with proper page count, but all blank.

like image 499
Aht Avatar asked Feb 12 '26 09:02

Aht


1 Answers

You tell in a comment that the files come from SSRS.

Older versions of PDFsharp require a special SSRS setting:

For the DeviceSettings parameter for the Render method on the ReportExecutionService object, pass this value:

theDeviceSettings = "<DeviceInfo><HumanReadablePDF>True</HumanReadablePDF></DeviceInfo>"; 

Source:
http://forum.pdfsharp.net/viewtopic.php?p=1613#p1613

like image 73
I liked the old Stack Overflow Avatar answered Feb 15 '26 20:02

I liked the old Stack Overflow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!