|
PDFsharp - moved to http://forum.pdfsharp.net/ Please visit the new PDFsharp forum at http://forum.pdfsharp.net/
|
Important Notice:
We regret to inform you that our free phpBB forum hosting service will be discontinued by the end of June 30, 2024.
If you wish to migrate to our paid hosting service, please contact billing@hostonnet.com.
View previous topic :: View next topic |
Author |
Message |
jeff2008
Joined: 01 Jul 2008 Posts: 3 Location: Whittier, California
|
Posted: Tue Jul 01, 2008 5:31 pm Post subject: XGraphics.ScaleTransform |
|
|
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 |
|
Back to top |
|
|
jeff2008
Joined: 01 Jul 2008 Posts: 3 Location: Whittier, California
|
Posted: Wed Jul 02, 2008 8:32 pm Post subject: |
|
|
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 |
|
Back to top |
|
|
|
|
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 vote in polls in this forum
|
|