使用PDFSharp连接PDF文件后返回空白页面

6

我尝试从rdlc报告中连接2个PDF数据。

问题是结果是空白页。

我不知道为什么,请有人帮忙。

这是我的代码:

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");
}

在“生成报告”函数中,代码看起来像这样:
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"));
}
3个回答

5
我知道这篇文章有点老了,但是请添加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);

然后将字节数组返回给PdfSharp。

1
如果您不想修改所有的尺寸,只需使用 "<DeviceInfo><OutputFormat>PDF</OutputFormat><HumanReadablePDF>True</HumanReadablePDF></DeviceInfo>" 即可。 - Dan Jagnow

1

谢谢,我也遇到了Report Viewer生成的PDF文件相同的问题,第二个链接中的信息解决了它...顺便问一下,这个修复将会被纳入未来版本的库中吗? - wheelibin

0

我使用iTextSharp来完成同样的事情。

传递相同的参数 --> viewer.LocalReport.Render("PDF")。它工作得很好。

这是我的代码:

private ActionResult ConcatPdf(List<byte[]> pdfDataList)
{
    MemoryStream outputMS = new MemoryStream();
    Document document = new Document();
    PdfCopy writer = new PdfCopy(document, outputMS);
    PdfImportedPage page = null;

    document.Open();

    foreach (byte[] pdfData in pdfDataList)
    {
        PdfReader reader = new PdfReader(pdfData);
        int n = reader.NumberOfPages;

        for (int i = 1; i <= n; i++)
        {
            page = writer.GetImportedPage(reader, i);
            writer.AddPage(page);
        }

        PRAcroForm form = reader.AcroForm;
        if (form != null)
            writer.CopyAcroForm(reader);
    }

    document.Close();

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接