I have a program that takes a pdf and prints text onto the first page using Itextsharp and PdfWriter. Currently this has been working as intended for each pdf that that I had to input text on. However, when the source pdf's layout is landscape, the writer rotates the layout to portrait after inputting the text onto the first page of the pdf. I cant find documentation on why the default layout is changed to portrait after text has been input on the pdf. This rotation causes the information to end up being cut off on the right hand side since the original layout was landscape.
I've looked at other answers involving the PdfStamper, but am having troubles manipulating existing code to work with what I am doing. Im fairly new to programming in C#, pdf manipulation, and iTextSharp. The end goal of the text on the pdf that is highlightable.
//Adds white invisible text to the pdf document that is highlightable
public static void stamp(string pdfName, string filePath, string textToStamp)
{
//Make a Temporary copy of the original to manipulate
string tempPath = @"C:\TemporaryFilePath\" + pdfName + "";
File.Copy(filePath, tempPath);
//Make a new reader using the copied source file
PdfReader reader = new PdfReader(tempPath);
using (Document document = new Document())
{
using (PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create)))
{
document.Open();
int numofpages = reader.NumberOfPages;
for (int p = 1; p <= numofpages; p++)
{
//create the ContentByte to give the text a position on the first page
PdfContentByte cb = writer.DirectContent;
//Get the page to work with
PdfImportedPage page = writer.GetImportedPage(reader, p);
document.NewPage();
cb.AddTemplate(page, 0, 20);
var FontColour = new BaseColor(255, 255, 255);
var MyFont = FontFactory.GetFont("Times New Roman", 12, FontColour);
//Gets the first page and sends the text to the upper left corner
if (p == 1)
{
if (TextToStamp!= null)
{
document.Add(new Paragraph("Hello World", MyFont));
}
}
document.Close();
}
}
reader.Close();
File.Delete(tempPath);
}
Any comments or suggests that you would like to add, feel free! Thank