首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用PDFsharp控制PDF返回空PDF

使用PDFsharp控制PDF返回空PDF
EN

Stack Overflow用户
提问于 2016-05-09 06:40:04
回答 2查看 1.5K关注 0票数 3

我有一个PDF列表存储为list<byte[]>。我尝试用PDFsharp连接所有这些PDF文件,但是在我的操作之后,我得到了一个具有适当页面计数的PDF,但是所有的页面都是空的。看上去我丢了一些头球什么的,但我找不到哪里。

我的代码:

代码语言:javascript
运行
复制
        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());
        }

我想以byte[]的形式返回它,因为我稍后使用它们:

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

这返回PDF与适当的页面计数,但全部为空白。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-09 12:30:22

您可以在评论中告诉您,这些文件来自SSRS。

较早版本的PDFsharp需要特殊的SSRS设置:

对于ReportExecutionService对象上的Render方法的ReportExecutionService参数,传递以下值:

代码语言:javascript
运行
复制
theDeviceSettings = "<DeviceInfo><HumanReadablePDF>True</HumanReadablePDF></DeviceInfo>"; 

来源:

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

票数 2
EN

Stack Overflow用户

发布于 2016-05-09 08:41:31

我使用iTextSharp,看看这个saple代码(它能工作)

代码语言:javascript
运行
复制
public static byte[] PdfJoin(List<String> pdfs)
    {
        byte[] mergedPdf = null;
        using (MemoryStream ms = new MemoryStream())
        {
            using (iTextSharp.text.Document document = new iTextSharp.text.Document())
            {
                using (iTextSharp.text.pdf.PdfCopy copy = new iTextSharp.text.pdf.PdfCopy(document, ms))
                {
                    document.Open();

                    for (int i = 0; i < pdfs.Count; ++i)
                    {
                        iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(pdfs[i]);
                        // loop over the pages in that document
                        int n = reader.NumberOfPages;
                        for (int page = 0; page < n; )
                        {
                            copy.AddPage(copy.GetImportedPage(reader, ++page));
                        }
                    }
                }
            }
            mergedPdf = ms.ToArray();
        }
        return mergedPdf;
    }




 public static byte[] PdfJoin(List<byte[]> pdfs)
    {
        byte[] mergedPdf = null;
        using (MemoryStream ms = new MemoryStream())
        {
            using (iTextSharp.text.Document document = new iTextSharp.text.Document())
            {
                using (iTextSharp.text.pdf.PdfCopy copy = new iTextSharp.text.pdf.PdfCopy(document, ms))
                {
                    document.Open();

                    for (int i = 0; i < pdfs.Count; ++i)
                    {
                        iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(pdfs[i]);
                        // loop over the pages in that document
                        int n = reader.NumberOfPages;
                        for (int page = 0; page < n; )
                        {
                            copy.AddPage(copy.GetImportedPage(reader, ++page));
                        }
                    }
                }
            }
            mergedPdf = ms.ToArray();
        }
        return mergedPdf;
    }
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37109232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档