PDFsharp PdfDocument.Save 在任何其他保存时导致空白页 [英] PDFsharp PdfDocument.Save causing blank pages on any additional saves

查看:33
本文介绍了PDFsharp PdfDocument.Save 在任何其他保存时导致空白页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我有解决此问题的方法,但我更愿意按照遇到此问题的方式来解决.在解决问题之前,让我解释一下上下文:

While I have a workaround for this problem, I'd prefer to do it the way that I ran into this problem. Let me explain the context before getting to the problem:

我正在使用扫描仪获取虚拟图像,我想用这些图像来创建每个页面都是一个图像的 PDF.我使用 PDFsharp 创建 PDF 工作得很好.

I'm using a scanner to get virtual images which I want to use to create a PDF with each page being one single image. I got the working just fine using PDFsharp for creation of the PDF.

然而;如果我尝试重新保存 PDFsharp 文档,它最终会清空所有以前的页面,然后添加新图像.

However; if I try to re-save the PDFsharp document, it ends up blanking all the previous pages, and then adding the new image.

关于更多上下文,我使用 ASP.NET Windows 窗体,NTwain 用于扫描软件,PDFsharp 用于创建 PDF,PdfiumViewer 用于在 Windows 窗体上查看 PDF.

For a little more context, I'm using ASP.NET Windows Forms, with NTwain for the scanning software, PDFsharp for creating the PDF, and PdfiumViewer to view the PDF on the Windows form.

我有一个文档的类级变量PdfDocument document = new PdfDocument();

I have a class-level variable for the document PdfDocument document = new PdfDocument();

在图片保存时,我只是将图片保存到新页面

On image save, I simply save the image to a new page

var img = pictureBox1.Image;
PdfPage page = document.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromGdiPlusImage(img);
gfx.DrawImage(image, 0, 0);

为了渲染文档,我将文档(认为这可能会解决问题)复制到一个新文档,将内容保存到新的内存流,然后简单地查看 PDF

To render out the document, I copy the document (thought this might fix the problem) to a new document, save the contents to a new memory stream, and simply view the PDF

var viewDocument = (PdfDocument)document.Clone();
MemoryStream ms = new MemoryStream();
viewDocument.Save(ms, false);
ms.Position = 0;
var pdf = PdfiumViewer.PdfDocument.Load(ms);
pdfRenderer1.Load(pdf);

我通过将图像保存到列表而不是作为新页面来实现它,并且每次我想显示它时都重新制作文档.然而,这个问题真的很奇怪.

I got it working by saving the image to a list, instead of as a new page, and remade the document each time I wanted to display it. The issue is really strange, however.

任何帮助理解为什么这样做的帮助表示赞赏.

Any help understanding why it does that is appreciated.

推荐答案

AFAIK 这是实现的状态:推荐的方法是创建一个 PdfDocument 或从文件中打开它,使您需要的更改,然后保存一次.
这就是您在适合您的实施中所做的.

AFAIK this is the state of the implementation: The recommended way is to create a PdfDocument or open it from a file, make the changes you need, then save it once.
This is what you do in the implementation that works for you.

保存文档、打开它、添加更多页面、再次保存它也可以.这样您就不需要所有图像的列表.但恕我直言,使用列表并在每次保存时创建一个新的 PdfDocument 是最干净的方法.

Saving the document, opening it, adding more pages, saving it again would also work. This way you would not need a list of all images. But IMHO using the list and creating a new PdfDocument each time you save is the cleanest way.

在进一步更改后再次保存时出现意外结果.这是一个已知的问题.请随时调查此问题并修复它.也许只是一个简单的改变,也许是复杂的.

Unexpected results occur when saving again after further changes. This is a known issue. Feel free to investigate this issue and fix it. Maybe it is just a simple change, maybe it is complicated.

这篇关于PDFsharp PdfDocument.Save 在任何其他保存时导致空白页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆