PDFsharp & MigraDoc Foundation

Annotations from existing PDF to new PDF using XPdfForm
Page 1 of 1

Author:  JimboBaggins [ Fri Aug 31, 2018 1:23 pm ]
Post subject:  Annotations from existing PDF to new PDF using XPdfForm


I am loading a PDF document into a PDFSharp Form which contains text comments (or annotations, I mean the same thing). Using PDFSharp GDI 1.50.4845-RC2a I am creating a new PDF from the existing one, changing the page size, adding a margin and saving the new PDF. The saved PDF loses the 2 comments that were in the original PDF.

I found this comment in PDFSharp.PdfPages from GitHub source control:
/// Important: In contrast to PdfFormXObject adding an external page always make a deep copy
/// of their transitive closure.

I found the following thread that didn't seem to answer/close this off for me, as the issue referred to is fixed in 1.50.4845-RC2a, and I don't think comes into play when using XPdfForm

When I load the Form, form.Page.HasAnnotations = true, and there are (correctly) two annotations in the page elements

Is there a way to keep the annotations in original PDF when creating the new one? Any guidance appreciated, many thanks

           form = XPdfForm.FromFile(Path.Combine(sourceDirectory, sourceFileName));

// Create the output document

            PdfSharp.Pdf.PdfDocument outputDocument = new PdfSharp.Pdf.PdfDocument();
            XGraphics gfx;
            XRect box;

                for (int idx = 0; idx < form.PageCount; idx++)
                    // Add a new page to the output document
                    PdfSharp.Pdf.PdfPage page = outputDocument.AddPage();
                    XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.Letter);

                    if (form.PixelWidth > form.PixelHeight)
                        page.Width = size.Height;
                        page.Height = size.Width;
                        page.Width = size.Width;
                        page.Height = size.Height;

                    int rotate = page.Elements.GetInteger("/Rotate");

                    gfx = XGraphics.FromPdfPage(page);

                    box = new XRect(0, 25, page.Width, (page.Height - 25));
                    // Draw the page identified by the page number like an image
                    gfx.DrawImage(form, box);

                form = null;

                pdfFileName = sourceFileName;
                outputDocument.Save(Path.Combine(sourceDirectory, pdfFileName));

                isSuccess = true;
            catch (Exception ex)
                errorMessage = string.Format("GeneratePDFFromPDF: There was an exception when adding existing PDF <{0}> to Header-formatted final document, ex {1}", sourceFileName, ex);


Author:  ChristopherO [ Thu Aug 27, 2020 3:52 pm ]
Post subject:  Re: Annotations from existing PDF to new PDF using XPdfForm

I had this exact issue using version 1.50.5147.0 (Nuget package). When i copy over annotations they do not scale and actually duplicate the content they are annotating.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group