我有3个pdf,我想将页面从它们中添加到输出PDF文件中。我要做的是:导入第一个PDF->创建新的PDF文档 ->添加页面 ->在某些页面中绘制,最后我想将页面从该文档添加到将要导出的主PDF文档。如果需要,继续使用第二个PDF文件进行操作。
ERROR: A PDF document must be opened with PdfDocumentOpenMode.Import to import pages from it.
从主类中我称之为处理PDF的方法:
Pdftest pdftest = new Pdftest();
PdfDocument pdf = PdfReader.Open(@"C:Userspdf_file.pdf", PdfDocumentOpenMode.Import);
pdftest.CreatePages(pdf_file);
pdftest类:
public class Pdftest
{
PdfDocument PDFNewDoc = new PdfDocument();
XFont fontChico = new XFont("Verdana", 8, XFontStyle.Bold);
XFont fontGrande = new XFont("Verdana", 12, XFontStyle.Bold);
XBrush fontBrush = XBrushes.Black;
public void CreatePages(PdfDocument archivoPdf)
{
PdfDocument NuevoDoc = new PdfDocument();
for (int Pg = 0; Pg < archivoPdf.Pages.Count; Pg++)
{
PdfPage pp = NuevoDoc.AddPage(archivoPdf.Pages[Pg]);
}
XGraphics Graficador = XGraphics.FromPdfPage(NuevoDoc.Pages[0]);
XPoint coordinate = new XPoint();
coordinate.X = XUnit.FromInch(1.4);
coordinate.Y = XUnit.FromInch(1.8);
graficador.DrawString("TEST", fontChico, fontBrush, coordinates);
for (int Pg = 0; Pg < NuevoDoc.Pages.Count; Pg++)
{
PdfPage pp = PDFNewDoc.AddPage(NuevoDoc.Pages[Pg]); //Error mentioned.
}
}
}
错误消息与 NuevoDoc
有关。您必须将NuevoDoc
保存在存储器中,并在导入模式下重新打开以使您的代码运行。
我不明白为什么您尝试将页面从NuevoDoc
复制到PDFNewDoc
-因此,在优化代码时,很可能可以避免记忆流。