I am trying to preserve an existing PDF documents in there entirety and add text to these PDF documents. This includes keeping PDF markup such as comments and Pop Ups.
Version: 1.50.4000-beta3b
I have tried using both PdfDocumentOpenMode.Modify and PdfDocumentOpenMode.Import as you can see from my example code. Both have issues.
-
Modify preserves PDF markup such as comments and Pop Ups, however I lose page size and page orientation with the saved PDF
outputPDF1.
-
Import strips the copied pages of markup information (comments, popups)but outputs the pdf with the original page orientation and size
outputPDF2.
Code:
PdfDocument pdfModify = PdfReader.Open(myOriginalPDF, PdfDocumentOpenMode.Modify);
PdfDocument pdfImport = PdfReader.Open(myOriginalPDF, PdfDocumentOpenMode.Import);
PdfDocument pdfNewFromImport = new PdfDocument();
for (int pageNumber = 0; pageNumber < pdfModify.Pages.Count; pageNumber++)
{
PdfPage pageModify = pdfModify.Pages[pageNumber];
XGraphics xg1 = XGraphics.FromPdfPage(pageModify);
XFont xfont1 = new XFont("Arial", 14, XFontStyle.Regular);
xg1.DrawString("My Added Text", xfont1, XBrushes.Red, new XRect(0, 0, pageModify.Width, pageModify.Height), XStringFormats.TopLeft);
PdfPage pageNew = pdfNewFromImport.AddPage(pdfImport.Pages[pageNumber]);
XGraphics xg2 = XGraphics.FromPdfPage(pageNew);
XFont xfont2 = new XFont("Arial", 14, XFontStyle.Regular);
xg2.DrawString("My Added Text", xfont2, XBrushes.Red, new XRect(0, 0, pageNew.Width, pageNew.Height), XStringFormats.TopLeft);
}
pdfModify.Save(outputPDF1);
pdfNewFromImport.Save(outputPDF2);