Question

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

Was it helpful?

Solution 2

Using the information posted by Bruno, I came up with this solution. This allows stamping information on the page no matter what the layout is and gives it a small amount of customization.

public static void AddText(string pdfName, string filePath, string textToStamp, float? x = null, float? y = null)
{
    //x and y are used to position the text and allow multiple different templates to use the same method
    //Designate the Temporary source to be used
    string tempPath = @"C:\TemporaryFilePath\" + pdfName + "";
    //Copy to file to the source path
    File.Copy(filePath, tempPath);
    PdfReader reader = new PdfReader(tempPath);
    iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(1);
    //Convert the pageHeight into a float
    int pageHeight = Convert.ToInt32(pageSize.Height);
    PdfStamper stamper = new PdfStamper(reader, new FileStream(filePath, FileMode.Create));

    PdfContentByte canvas = stamper.GetOverContent(1);
    //Set a default value if x and y have no value 
    if (x.HasValue == false)
    {
        x = 35;
    }
    if (y.HasValue == false)
    {
        y = 30;
    }
    //choose the font type 
    var FontColour = new BaseColor(255, 255, 255);
    var MyFont = FontFactory.GetFont("Times New Roman", 10, FontColour);
    ColumnText.ShowTextAligned
                (canvas, Element.ALIGN_LEFT, new Phrase("Hello World", MyFont), (float)x, pageHeight - (float)y, 0);

    stamper.Close();
    reader.Close();
    File.Delete(tempPath);
}

OTHER TIPS

When reading http://manning.com/lowagie2/samplechapter6.pdf that Bruno pointed to, specifically pay attention to 6.3.1, which explains how to use PdfStamper to add text at a certain position on a certain page. It also shows the difference between two types of landscape pages: rotated pages and pages that have width > height.

The full code example can be found here: http://www.itextpdf.com/examples/iia.php?id=117

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top