PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 10:15 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Thu Aug 27, 2020 10:21 pm 
Offline

Joined: Thu Aug 27, 2020 3:10 pm
Posts: 2
Hello,

I'm new to PDF processing and have used PDFSharp successfully (great product) but have ran into an issue:

My goal is to resize a PDF and scale all the content to the new page size including the page annotations (markup, stamps, etc...). There are some examples of this in the forums and other places.
e.g. https://forum.pdfsharp.net/viewtopic.php?f=2&t=3662&p=11141&hilit=scale+page#p11141

Unfortunately this does not render out the PdfAnnotations in the final scaled document. Can anyone recommend a way to do this? Is it possible?
I've attached a test project (very small) with an example PDF document i am trying to scale. The code is as follows:
Code:
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.Annotations;
using PdfSharp.Pdf.IO;
using System.IO;

namespace PDFSharp1
{
   class Program
   {
      static void Main(string[] args)
      {
         const string inputFile = "input.pdf";
         // Create the new output document
         using (PdfDocument outputDocument = new PdfDocument())
         using (PdfDocument inputDoc = PdfReader.Open(inputFile, PdfDocumentOpenMode.Modify))
         {
            if (inputDoc.AcroForm != null)
               inputDoc.Flatten();
            using (MemoryStream ms = new MemoryStream())
            {

               inputDoc.Save(ms);
               ms.Position = 0;
               // create XPdfForm to resize page and scale internal content
               using (XPdfForm origForm = XPdfForm.FromStream(ms))
               {

                  // manipulate pages
                  for (int idx = 0; idx < origForm.PageCount; idx++)
                  {

                     // Set page number (which is one-based)
                     // that the XPdfForm is currently referencing
                     origForm.PageNumber = idx + 1;

                     PdfPage origPage = origForm.Page;
                     PdfPage newPage = outputDocument.AddPage();

                     // change size
                     double scaleX = 1.5;
                     double scaleY = 1.5;
                     double newWidth = origPage.Width * scaleX;
                     double newHeight = origPage.Height * scaleY;
                     newPage.Width = newWidth;
                     newPage.Height = newHeight;


                     using (XGraphics gfx = XGraphics.FromPdfPage(newPage))
                     {
                        // Draw the page with scaled dimentions
                        XRect box = new XRect(0, 0, newWidth, newHeight);
                        gfx.DrawImage(origForm, box);
                        XPen pen = new XPen(XColors.Green);

                        // my attempt to copy and scale the original page annotations
                        // - I dont know how to apply the PdfAnnotation to the new page
                        // - I also dont know how to scale them properly
                        foreach (PdfAnnotation annon in origPage.Annotations)
                        {
                           XRect anonXRect = annon.Rectangle.ToXRect();
                           XRect pdfRect = gfx.Transformer.WorldToDefaultPage(anonXRect);
                           XRect scaledRect = pdfRect;
                           scaledRect.Scale(scaleX, scaleY);
                           double newX = pdfRect.X + (pdfRect.Width / 2) - (scaledRect.Width / 2);
                           double newY = pdfRect.Y + (pdfRect.Height / 2) - (scaledRect.Height / 2);
                           scaledRect.X = newX;
                           scaledRect.Y = newY;
                           gfx.DrawRectangle(pen, scaledRect);
                        }

                     }

                  }
               }
            }

            // Resulting ouput file has the main document scaled properly but none of the PdfAnnotation data shows up
            // - i cant seem to clone the Annontations and add them to the new document (get an exception as the new doc is not the owner)
            // - i cant seem to scale them properly
            // I have drawn rectangles where my scaling math should put the annontations (which are off)
            // Changing the scale factor from (1.5 to 1) renders my Annontation rectangles in in the correct position... but i still
            // dont know how to render the Annontation to the new page.
            outputDocument.Save("output.pdf");
            outputDocument.Close();
            inputDoc.Close();
         }
      }
   }
}


Any help on this would be greatly appreciated.
Thanks so much in advance!


Attachments:
File comment: Zip file of complete project with example PDF
PDFSharp1.zip [144.47 KiB]
Downloaded 366 times
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 46 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group