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