1

我们有一个应用程序,其中在某些时候以 pdf 格式生成报告。虽然一开始一切似乎都很完美,但后来我们发现我们使用的字体并没有嵌入到我们创建的 pdf 中。因此,我们可以自己查看 pdf,但是当其他人使用我们的应用程序时,他们会得到一个空白文档。

我们尝试使用 PdfSharp 使用以下代码嵌入字体:

        public byte[] EmbedPdf (byte[] originalPdf)
        {
            //open existing pdf through stream
            MemoryStream preStream = new MemoryStream();
            preStream.Write(originalPdf, 0, originalPdf.Length);
            PdfDocument pdf = PdfReader.Open(preStream, PdfDocumentOpenMode.Import);
            preStream.Close();

            //open second document to paste the original data into which should be including embedding
            PdfDocument nieuwePdf = new PdfDocument();
            var options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

            //loop through pages to insert them
            for (int page = 0; page < pdf.Pages.Count; page++)
            {
                PdfPage pdfPage = nieuwePdf.AddPage(pdf.Pages[page]);

                //the following line is the point where the application throws the error
                XGraphics xgr = XGraphics.FromPdfPage(pdfPage);
                XFont font = new XFont("Times New Roman", 12, XFontStyle.Regular, options);
            }

            //put new pdf back to the stream so it can be returned in the same format the original came in
            MemoryStream postStream = new MemoryStream();
            nieuwePdf.Save(postStream);
            originalPdf = postStream.ToArray();

            //end the stream
            postStream.Close();

            return originalPdf;
        }

我们得到的错误是:“放气的流提前结束”在新的 pdf 应该从原始页面获取页面的行处。我们认为该错误意味着读取流中的某些内容出错,但我们无法发现如何解决该错误。你能帮我们解决这个问题吗?

作为参考,以下链接显示了另一个有类似问题的人,并作为上述代码的示例: PDFsharp edit a pdf file



编辑:这是我使用该应用程序制作的测试 pdf。由于我为此在模板中放置了多种字体,我注意到它会将所有字体转换为 windows 上的 Calibri(页眉是 Arial Black,页脚是 Verdana,正文是 Times New Roman)

从这里下载:http: //www.wikiupload.com/L5GMAN88J9JXAYR

也感谢您的建议,我都尝试了,但似乎都没有帮助。为了关闭流,我什至可以将其注释掉并得到相同的错误。

4

1 回答 1

0

您尝试修改的 PDF 文件似乎存在问题。

所以回答你的问题“你能帮我们解决这个问题吗?” 是:不是没有允许我们复制问题的 PDF 文件。

于 2014-06-20T10:32:14.347 回答