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

发布于 2024-12-10 15:41:51 字数 1245 浏览 0 评论 0原文

我尝试连接 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"));
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫卆 2024-12-17 15:41:51

我知道这很旧,但添加 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。

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.

偏爱自由 2024-12-17 15:41:51

也许从报表查看器生成的 PDF 文件有一些不寻常的地方。
我们需要样本文件来检查它。

另请参阅:

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

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

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

破晓 2024-12-17 15:41:51

我使用 iTextSharp 来做同样的事情。

传递相同的参数-->查看器.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");
}

I use iTextSharp to do the same thing.

Passing same parameter --> viewer.LocalReport.Render("PDF"). It work well.

Here is my code:

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文