How do I scale a pdf page, while maintaining rotation in itextsharp? I have the following, but I lose rotation:
public static void ScaleToLetter(string inPDF, string outPDF)
{
PdfReader reader = new PdfReader(inPDF);
Document doc = new Document(PageSize.LETTER);
Document.Compress = true;
PdfWriter writer = PdfWriter.GetInstance(doc,
new FileStream(outPDF, FileMode.Create));
doc.Open();
PdfContentByte cb = writer.DirectContent;
int rotation;
PdfImportedPage page;
for (int pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
{
page = writer.GetImportedPage(reader, pageNumber);
rotation = reader.GetPageRotation(pageNumber);
cb.AddTemplate(page,
PageSize.LETTER.Width / reader.GetPageSize(pageNumber).Width,
0, 0,
PageSize.LETTER.Height / reader.GetPageSize(pageNumber).Height,
0, 0);
doc.NewPage();
}
doc.Close();
}
I had a similar issue, and got it working by changing the height and width of each page, from LETTER to LETTER rotated. In other words, if the current page is thin and tall then it is vertical. But if it is wide and short, then it is horizontal and I should rotate the page.
Here's your code changed:
public static void ScaleToLetter(string inPDF, string outPDF)
{
using (var reader = new PdfReader(inPDF))
using (var doc = new Document(PageSize.LETTER))
{
Document.Compress = true;
using (
var writer = PdfWriter.GetInstance(doc,
new FileStream(outPDF, FileMode.Create)))
{
doc.Open();
var cb = writer.DirectContent;
PdfImportedPage page;
for (var pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
{
page = writer.GetImportedPage(reader, pageNumber);
if (page.Width <= page.Height)
doc.SetPageSize(PageSize.LETTER);
else
doc.SetPageSize(PageSize.LETTER.Rotate());
doc.NewPage();
cb.AddTemplate(page,
doc.PageSize.Width / reader.GetPageSize(pageNumber).Width,
0, 0,
doc.PageSize.Height / reader.GetPageSize(pageNumber).Height,
0, 0);
}
doc.Close();
}
}
}
I also needed to constrain proportions, so I ended up with this:
public static void ScaleToA4(string inPDF, string outPDF)
{
using (var reader = new PdfReader(new MemoryStream(File.ReadAllBytes(inPDF))))
using (var document = new Document(PageSize.A4))
using (var ms = new MemoryStream())
using (var writer = PdfWriter.GetInstance(document, ms))
{
document.Open();
var cb = writer.DirectContent;
for (var pageNumber = 1; pageNumber <= reader.NumberOfPages; pageNumber++)
{
var page = writer.GetImportedPage(reader, pageNumber);
document.SetPageSize(page.Width <= page.Height ?
PageSize.A4 : PageSize.A4.Rotate());
document.NewPage();
var widthFactor = document.PageSize.Width / page.Width;
var heightFactor = document.PageSize.Height / page.Height;
var factor = Math.Min(widthFactor, heightFactor);
var offsetX = (document.PageSize.Width - (page.Width * factor)) / 2;
var offsetY = (document.PageSize.Height - (page.Height * factor)) / 2;
cb.AddTemplate(page, factor, 0, 0, factor, offsetX, offsetY);
}
document.Close();
File.WriteAllBytes(outPDF, ms.GetBuffer());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With