首页 文章

PdfSharp:在现有pdf中嵌入字体

提问于
浏览
0

我们有一个申请,在某些点上,报告以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;
        }

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

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

编辑:这是我使用该应用程序制作的测试pdf . 因为我在模板中放了多个字体,我注意到它将所有字体变成了Windows上的Calibri( Headers 是Arial Black,页脚是Verdana,主要文字是Times New Roman)

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

还要感谢你的建议,我试过了两个,但似乎都没有帮助 . 为了关闭流,我甚至可以将其注释掉并获得相同的错误 .

1 回答

  • 0

    看起来您尝试修改的PDF文件存在问题 .

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

相关问题