I have pdf file in which are lot of post labels pages that have Portrait orientation. I need to shrink the existing content of every page and then add additional information into every page. For this reason I use PDFStamper class which in the code below successfully shrinks the content. Then the next requirement is to rotate content of the page 90 or 270 degrees without rotating the page. Page must leave in Portrait orientation, but the shrunk content must be rotated. I tried to use PdfDictionary class and tested a lot of code variations but the content wasn't rotated as required. I need proposals what code block must be after comment in code - //Code to rotate only content of pdf page 90 or 270 degrees leaving page in Portrait orientation??? My code is below:
using iTextSharp.text.pdf;
using Microsoft.Win32;
using System;
using System.IO;
namespace RotatePDFContent
{
internal class Program
{
[STAThread]
private static void Main(string[] args)
{
string directory = AppDomain.CurrentDomain.BaseDirectory;
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = directory;
ofd.DefaultExt = ".pdf";
ofd.Filter = "PDF Files(.*PDF)|*.PDF|All Files(*.*)|*.*";
string inputPdfFilePath;
if (ofd.ShowDialog() == true)
{
inputPdfFilePath = ofd.FileName.ToString();
using (Stream inputPdfStream = new FileStream(inputPdfFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
PdfReader pdfReader = new PdfReader(inputPdfStream);
int pagesAmount = pdfReader.NumberOfPages;
string pdfInputsDirectoryPath = System.IO.Path.GetDirectoryName(inputPdfFilePath);
string resultFileName = "Output_" + Path.GetFileNameWithoutExtension(inputPdfFilePath) +
Path.GetExtension(inputPdfFilePath);
string resultFilePath = System.IO.Path.Combine(pdfInputsDirectoryPath, resultFileName);
using (Stream outputPdfStream =
new FileStream(resultFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
PdfStamper stamper = new PdfStamper(pdfReader, outputPdfStream);
for (int pageNumber = 1; pageNumber <= pagesAmount; pageNumber++)
{
//Shrink original page contents for the purpose to fit into required area
stamper.GetUnderContent(pageNumber).SetLiteral("\nq 0.53 0 0 0.53 10 25 cm\nq\n");
//Code to rotate only content of pdf page 90 or 270 degrees leaving page
//in Portrait orientation???
//????????????
}
stamper.Close();
}
}
}
}
}
}
I look at the question iText - Rotate page content while creating PDF, but didn't find solution to my code from this question until now.
In the attached images are initial and required result.
