PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 3:38 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: XGraphics.ScaleTransform
PostPosted: Tue Jul 01, 2008 5:31 pm 
Offline

Joined: Tue Jul 01, 2008 5:14 pm
Posts: 3
Location: Whittier, California
Hi,

I am trying to scale down an PDF document, and I am not invoking any change with the code that I have written. Here is what I have:

Code:
        private void testScaleADocument()
        {
           
            PdfSharp.Drawing.XGraphics gfx;

            //Create Source Document
            File.Copy(this.pdfFileForRead, this.pdfFileForWriteTmp, true);

            //Open the source document
            PdfSharp.Pdf.PdfDocument doc = PdfSharp.Pdf.IO.PdfReader.Open(pdfFileForWriteTmp, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import);

            // Create the output document
            PdfSharp.Pdf.PdfDocument outputDocument = new PdfSharp.Pdf.PdfDocument();
 
            foreach (PdfSharp.Pdf.PdfPage page in doc.Pages)
            {
                //Add the page to the output document, which returns
                //a new page object relative to the output document
                PdfSharp.Pdf.PdfPage newPage = outputDocument.AddPage(page);

                //Transform the page
                gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(newPage);
                gfx.ScaleTransform(0.9);
               
            }

            outputDocument.Save(this.pdfFileForScalingWrite);
            doc.Close();
            outputDocument.Close();

            Process.Start(this.pdfFileForScalingWrite);
        }
    }


Admittedly I am new to PDFSharp, and I am sure that I must be missing something very simple, but I have not yet figured out what that might be. I appreciate any help that you can provide.

Thanks,

Jeff


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Jul 02, 2008 8:32 pm 
Offline

Joined: Tue Jul 01, 2008 5:14 pm
Posts: 3
Location: Whittier, California
I figured out my problem (I got hints from the two pages to one page sample code and from reading the Adobe PDF developers reference). I needed to set the scaling factor first, and then follow it with some sort of draw operation. Merely applying a scaling factor to a graphics object derived from a page does not work. This was definitely a newbie mistake.

Here is some code that utilizes ScaleTransform.

Code:
        private void testScaleADocument()
        {
           
            PdfSharp.Drawing.XGraphics gfx = null;
            PdfSharp.Drawing.XRect rect;
            PdfSharp.Pdf.PdfPage newPage;
 
            //Create Source Document
            File.Copy(this.pdfFileForScaling, this.pdfFileForWriteTmp, true);

            //Open the source document as XPdfForm object
            PdfSharp.Drawing.XPdfForm form =
            PdfSharp.Drawing.XPdfForm.FromFile(this.pdfFileForWriteTmp);

            // Create the output document
            PdfSharp.Pdf.PdfDocument outputDocument
                = new PdfSharp.Pdf.PdfDocument();

            for (int i = 0; i < form.PageCount; i++)
            {
                //Create a new page in the output document, which returns
                //a new page object relative to the output document
                newPage = outputDocument.AddPage();

                //Create a graphics element from the OUTPUT page
                gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(newPage);

                //Set the source page number to indicate which page number to
                //work with.  The page number is one-based.
                form.PageNumber = i + 1;
                rect = new PdfSharp.Drawing.XRect(0, 0, newPage.Width, newPage.Height);

                //Set the scaling, which affects operations that follow.
                //Must be set prior to operations that require scaling.
                gfx.ScaleTransform(0.5);

                // Draw the page identified by the page number (set above)
                // as an image into a XRect
                gfx.DrawImage(form, rect);

            }

            //Write the new output PDF file to disk.
            outputDocument.Save(this.pdfFileForScalingWrite);
           
            //Cleanup
            form.Dispose();
            outputDocument.Close();
            if (gfx != null)
                gfx.Dispose();
               
            //View
            Process.Start(this.pdfFileForScalingWrite);
        }



Best Regards,

Jeff


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 367 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