Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate PDF files with PDFSharp return blank pages

I try to concatenate 2 PDF data from rdlc report.

The problem is the result is blank pages.

I don't know why, could someone help me please.

here is my code:

private ActionResult ConcatPdf(byte[] pdfData1, byte[] pdfData2)
{
    MemoryStream ms1 = new MemoryStream(pdfData1);
    MemoryStream ms2 = new MemoryStream(pdfData2);

    PdfDocument inputDoc1 = PdfReader.Open(ms1, PdfDocumentOpenMode.Import);
    PdfDocument inputDoc2 = PdfReader.Open(ms2, PdfDocumentOpenMode.Import);

    PdfDocument outputDoc = new PdfDocument();

    foreach (PdfPage page in inputDoc1.Pages)
    {
        outputDoc.AddPage(page);
    }

    foreach (PdfPage page in inputDoc2.Pages)
    {
        outputDoc.AddPage(page);
    }

    MemoryStream outputMs = new MemoryStream();
    outputDoc.Save(outputMs);

    return File(outputMs.ToArray(), "application/pdf");
}

In generate report function look like this:

public ActionResult TestPDF(int id)
{
    // Set report path.
    LocalReport rep = viewer.LocalReport;
    rep.ReportPath = Server.MapPath("~/Reports/rptExternalTransferIndividual.rdlc");
    rep.DataSources.Clear();


    //
    // Set data and parameter to report.
    //
    ...
    ...

    return ConcatPdf(viewer.LocalReport.Render("PDF"), viewer.LocalReport.Render("PDF"));
}
like image 896
TaeV Avatar asked Oct 15 '11 18:10

TaeV


2 Answers

I know this is old, but add HumanReadablePDF:

 string deviceInfo = "<DeviceInfo>" +
                    "  <OutputFormat>PDF</OutputFormat>" +
                    "  <PageWidth>29.7cm</PageWidth>" +
                    "  <PageHeight>21cm</PageHeight>" +
                    "  <MarginTop>0cm</MarginTop>" +
                    "  <MarginLeft>0cm</MarginLeft>" +
                    "  <MarginRight>0cm</MarginRight>" +
                    "  <MarginBottom>0cm</MarginBottom>" +
                    "  <HumanReadablePDF>True</HumanReadablePDF>" +
                    "</DeviceInfo>";

  byte[] reportBytes = LocalReport.Render(
                "PDF", deviceInfo, out mimeType, out encoding,
                out extension,
                out streamids, out warnings);

Then return the byte array to PdfSharp.

like image 93
nik0lai Avatar answered Sep 28 '22 00:09

nik0lai


Maybe there's something unusual about the PDF files generated from Report Viewer. We need sample files to check it.

See also:

http://forum.pdfsharp.net/viewtopic.php?f=3&t=1818&p=5174

http://forum.pdfsharp.net/viewtopic.php?f=3&t=1730

like image 24
I liked the old Stack Overflow Avatar answered Sep 27 '22 22:09

I liked the old Stack Overflow