Last active
July 15, 2023 10:05
-
-
Save sq5gvm/574e918706778a37d2783a3c55dd632d to your computer and use it in GitHub Desktop.
PdfSharp - Tile pages rotating horizontal pages and preserving aspect ratio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region PdfSharpHelpers | |
internal static bool isPdfPageHorizontal(ref XPdfForm form) | |
{ | |
return (form.PointHeight < form.PointWidth); | |
} | |
static XRect scalePDFPageKeepAspectRatio(XRect maxdim, ref XPdfForm form) | |
{ | |
XRect outDim = maxdim; | |
double aspectRatioOrig = form.PointHeight / form.PointWidth; | |
// Figure out the ratio | |
double ratioX = maxdim.Width / form.PointWidth; | |
double ratioY = maxdim.Height / form.PointHeight; | |
// use whichever multiplier is smaller | |
double ratio = ratioX < ratioY ? ratioX : ratioY; | |
if (isPdfPageHorizontal(ref form)) | |
{ | |
outDim.Height = form.PointHeight * ratio; | |
outDim.Width = form.PointWidth * ratio; | |
} | |
else | |
{ | |
outDim.Height = form.PointHeight * ratio; | |
outDim.Width = form.PointWidth * ratio; | |
} | |
return outDim; | |
} | |
internal static bool PdfTileOnA4Horizontal(string sInputFile, string sOutputFile, double margin = 0.0) | |
{ | |
try | |
{ | |
// Create the output document | |
PdfDocument outputDocument = new PdfDocument(); | |
// Show single pages | |
// (Note: one page contains two pages from the source document) | |
outputDocument.PageLayout = PdfPageLayout.SinglePage; | |
// Open the external document as XPdfForm object | |
XPdfForm form = XPdfForm.FromFile(sInputFile); | |
for (int idx = 0; idx < form.PageCount; idx += 2) | |
{ | |
// Add a new page to the output document | |
PdfPage page = outputDocument.AddPage(); | |
page.Size = PageSize.A4; | |
page.Orientation = PageOrientation.Landscape; | |
double width = page.Width; | |
double height = page.Height; | |
int rotate = page.Elements.GetInteger("/Rotate"); | |
XGraphics gfx = XGraphics.FromPdfPage(page); | |
form.PageNumber = idx + 1; | |
// TODO: center resized pages | |
XRect box = new XRect(0, 0, width / 2, height); // 0,0 i polowa szerokosci i cala wysokosc | |
gfx.Save(); | |
if (isPdfPageHorizontal(ref form)) | |
{ | |
gfx.RotateTransform(-90); | |
box = new XRect(0, 0, box.Height - (2 * margin), box.Width - (2 * margin)); | |
box = scalePDFPageKeepAspectRatio(box, ref form); | |
gfx.TranslateTransform(-page.Height.Point + margin, 0.0 + margin); | |
} | |
else | |
{ | |
box = new XRect(0, 0, box.Width - (2 * margin), box.Height - (2 * margin)); | |
box = scalePDFPageKeepAspectRatio(box, ref form); | |
gfx.TranslateTransform(margin, margin); | |
} | |
gfx.DrawImage(form, box); | |
gfx.Restore(); | |
if (idx + 1 < form.PageCount) | |
{ | |
// Set page number (which is one-based) | |
form.PageNumber = idx + 2; | |
box = new XRect(width / 2, 0, width / 2, | |
height); // polowa szerokosci docelowej kartki, na dole kartki | |
gfx.Save(); | |
if (isPdfPageHorizontal(ref form)) | |
{ | |
gfx.RotateTransform(-90); | |
box = new XRect(0, 0, box.Height - (2 * margin), box.Width - (2 * margin)); | |
box = scalePDFPageKeepAspectRatio(box, ref form); | |
gfx.TranslateTransform(-page.Height.Point + margin, (page.Width.Point / 2.0) + margin); | |
} | |
else | |
{ | |
box = new XRect(0, 0, box.Width - (2 * margin), box.Height - (2 * margin)); | |
box = scalePDFPageKeepAspectRatio(box, ref form); | |
gfx.TranslateTransform((width / 2.0) + margin, margin); | |
} | |
gfx.DrawImage(form, box); | |
gfx.Restore(); | |
} | |
} | |
// Save the document... | |
outputDocument.Save(sOutputFile); | |
return true; | |
} | |
catch (Exception exc) // FIXME: detailed catch would be better... | |
{ | |
logger.Error(exc); | |
} | |
return false; | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment