I work on a utility that transforms vendor datasheets for inclusion in a larger PDF "bundle" (containing suppliers, etc.). As part of this, I scale pages to about 90% of the input size to leave room for markings around the edges.
Due to environment factors in our systems, I am using the latest PDFSharp/MigraDoc 1.5 from Github.
The compiled (by me) DLLs are version 1.51.5185.0.Some of the input datasheets, though, simply don't scale properly. I've looked at multiple code samples and feel I'm not making any headway.
I stripped some code down to the bare minimum and include it below. It takes a file to scale, a target page size (usually PageSize.Letter in my case), and a scale factor, typically 0.9. It should scale the input PDF and output it, preserving the page orientation and page size.
Code:
static public int ScalePagesSimple(string fn, PageSize pageSize, double scale)
{
if (scale >= 1.0) // 1.0 means nothing to do, larger isn't supported here
{
return 0;
}
int scaledPagesCount = 0;
var dir = System.IO.Path.GetDirectoryName(fn);
var originalFilenameNoPath = System.IO.Path.GetFileName(fn);
var backupFilename = System.IO.Path.GetFileNameWithoutExtension(fn) + "_beforescale.PDF";
var backupFilePath = System.IO.Path.Combine(new string[] { dir, backupFilename });
// backup the original file then delete it.
if (File.Exists(backupFilePath)) File.Delete(backupFilePath);
System.IO.File.Copy(fn, backupFilePath);
var o = PdfReader.Open(fn, PdfDocumentOpenMode.Modify);
// load the original document
var inpPdfForm = XPdfForm.FromFile(fn);
// scale each page into the output document
for (int thisPageNumber = 0; thisPageNumber < o.PageCount; thisPageNumber++)
{
scaledPagesCount++;
var inpPage = o.Pages[thisPageNumber];
// calculate the center point on the page; unsure if this is necessary
var newXcenter = inpPage.Width.Point / 2;
var newYcenter = inpPage.Height.Point / 2;
// Associate a graphics context with the designated output page, indicating that the graphics
// context will overwrite existing content (Replace) taken from the inpPage.
// Then specify scaling to apply to everything drawn in the context.
XGraphics gfx = XGraphics.FromPdfPage(inpPage, XGraphicsPdfPageOptions.Replace);
gfx.ScaleAtTransform(scale, scale, newXcenter, newYcenter);
// Construct a rectangle with dimensions of the page
XRect pageRect = new XRect(0, 0, inpPage.Width.Point, inpPage.Height.Point);
// convert the input page to a graphic and then draw it on the page.
inpPdfForm.PageNumber = thisPageNumber + 1; // pdf forms are 1-based
XImage inpImg = inpPdfForm;
// Render the image of the current page in the PDF into the scaled rectange and draw it into
// the graphics context.
gfx.DrawImage(inpImg, 0, 0);
}
o.Save(fn);
return scaledPagesCount;
}
I am attaching two files showing what happens. The one with "beforescale" is the input PDF. The other is the resulting PDF after scaling to 0.9 (90%).
I'm thinking I'm not understanding something correctly about the combination of orientation/width/height/rotation, but maybe there's something more.
Any tips here are appreciated.