PDFsharp & MigraDoc Foundation https://forum.pdfsharp.net/ |
|
Need help scaling a PDF page with Annotations intact. https://forum.pdfsharp.net/viewtopic.php?f=2&t=4171 |
Page 1 of 1 |
Author: | ChristopherO [ Thu Aug 27, 2020 10:21 pm ] | ||
Post subject: | Need help scaling a PDF page with Annotations intact. | ||
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!
|
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |