将 PDFsharp 文档发送到 WCF 服务并保存 returns 错误 "Cannot save a PDF document with no pages"

Sending a PDFsharp document to WCF service and saving returns error "Cannot save a PDF document with no pages"

我有一个由 PDFsharp 生成并在 WCF 客户端中正确显示的 PDF 文档。该文档由一页组成,该页面又包含一个图像。代码是:

PdfDocument d = new PdfDocument();
d.Info.Title = "Temporary Passport Document";
d.Info.Title = applicant.ApplID + " " + initials + " - Passport";

PdfPage p = new PdfPage();
d.AddPage(p);
XGraphics gfx = XGraphics.FromPdfPage(p);
XImage img = XImage.FromFile(passport.AttachmentPath);
p.Width = img.Size.Width + 40;
p.Height = img.Size.Height + 40;
gfx.DrawImage(img, 20, 20);

使用以下代码将 PDF 文档保存到 byte[](d 是 PDFSharp 文档,passport.PassportScan 是字节[]):

MemoryStream ms = new MemoryStream();
d.Save(ms, false);
passport.PassportScan = ms.ToArray();  

字节[]被发送到WCF服务保存。因此,我将 byte[] 转换回 PDFsharp 文档并尝试使用以下代码保存它:

PdfDocument d = new PdfDocument(new MemoryStream(pPassport.PassportScan));
 d.Save(RecruitAidPathRoot + "100104 - JPD - John Paul Doe\" + "JPD - file.pdf");

执行最后一行时出现错误"Cannot save a PDF document with no pages"

文档在客户端有1页,所以服务端的文档也应该有1页对吗?

提前致谢。

您调用的方法具有此签名:public PdfDocument(Stream outputStream)

描述如下:

Creates a new PDF document using the specified stream. The stream won't be used until the document is closed, at that time the document is saved automatically. Do not call Save() for documents created with this constructor, just call Close(). To open an existing PDF file, use the PdfReader class.

改用 PdfReader class 的 Open 方法:public static PdfDocument Open(Stream stream)