-1

I have used iTextSharp to add footer note to existing pdf. Issue is when I access the pdf file and creates another file from its content, the orientation of pages gets changed and the contents get cut off. The code I have used is:

//using itextsharp
string oldFile = dtroldp + strfn;
if (File.Exists(oldFile))
{
    string newFile = strpath + "sys_" + docid + strext;

    PdfReader reader = new PdfReader(oldFile);
    int numberOfPages = reader.NumberOfPages;
    FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);

     Document document = new Document();

    // open the writer
    PdfWriter writer = PdfWriter.GetInstance(document, fs);
    writer.PageEvent = new PDFFooter();

    document.Open();

    for (int i = 1; i <= numberOfPages; i++)
    {
        document.SetPageSize(reader.GetPageSizeWithRotation(1));

        document.NewPage();
        PdfContentByte cb = writer.DirectContent;

        //// select the font properties
        BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.SetColorFill(BaseColor.BLACK);
        cb.SetFontAndSize(bf, 32);

        // write the text in the pdf content
        cb.BeginText();

        cb.EndText();

        PdfImportedPage page = writer.GetImportedPage(reader, i);

        cb.AddTemplate(page, 0, 0);
    }

    document.Close();

    fs.Close();
    writer.Close();
    reader.Close();
}

Can anyone suggest me what wrong I have done? Why the orientation automatically gets changed?

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
Priya
  • 1,375
  • 8
  • 21
  • 45
  • please attach screen shot of your generated pdf – sangram parmar Jun 23 '15 at 11:04
  • Throw away your code. Read the [documentation](http://pages.itextpdf.com/ebook-stackoverflow-questions.html). Read all the answers about `PdfStamper`. Feel ashamed because you didn't read the documentation first, because you are doing it wrong. – Bruno Lowagie Jun 23 '15 at 11:04

1 Answers1

0

Your approach is completely wrong. Please throw away your code.

Adding content to an existing PDF is done using PdfReader and PdfStamper, NOT using Document and PdfWriter. Please start by reading the answer to this question: Watermark in PDF file is hiding behind images

In this example, we add some watermarks at absolute positions. In your case, you have pages with different size, so you should not use absolute positions. You should calculate the x and y value as is done in the answer to this question: How to watermark PDFs using text or images?

You will need to know the answer to the following questions:

Combining all this wisdom, you should be able to add a footer at the appropriate place using the appropriate classes and methods.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165