PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Annotations from existing PDF to new PDF using XPdfForm
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3842
Page 1 of 1

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

Hi,

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:
Code:
/// 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
viewtopic.php?f=3&t=3659

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

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

// Create the output document
 

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

            try
            {
                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;
                    }
                    else
                    {
                        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.PageNumber++;
                }

                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
https://www.phpbb.com/