0

I have a PDF file and I want to add a simple number on each page.

Here is my code:

reader = new PdfReader(fileOut);
Document final = new Document(reader.GetPageSize(1));
PdfWriter w = PdfWriter.GetInstance(final, new FileStream(finalFile, FileMode.Create, FileAccess.Write));
w.SetFullCompression();

final.Open();

for (int i = 1; i <= reader.NumberOfPages; i++)
{
    final.NewPage();
    PdfContentByte cb = w.DirectContent;
    ControlNumberTimes(cb, "C"+i, 560, 725, 270, Element.ALIGN_LEFT);
    cb.AddTemplate(w.GetImportedPage(reader, i), 0, 0);
}

final.Close();
reader.Close();



private static void ControlNumberTimes( PdfContentByte cb1, string control, int x, int y, int rotation, int allign )
{
    cb1.BeginText();

    cb1.SetColorFill(BaseColor.BLACK);
    cb1.SetFontAndSize(BaseFont.CreateFont("C:\\windows\\Fonts\\times.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED), 7.5f);
    cb1.ShowTextAligned(allign, control, x, y, rotation);
    cb1.EndText();
}

Before adding this text, PDF file size is 3.6 Mb, after the size is 11 Mb. What I am doing wrong?

This is my code now:

string finalFile = System.IO.Path.GetDirectoryName(fileOut) + "\\" +
                           System.IO.Path.GetFileNameWithoutExtension(fileOut) + "_num.pdf";

        reader = new PdfReader(fileOut);

        using (FileStream fs = new FileStream(finalFile, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            using (PdfStamper stamper = new PdfStamper(reader, fs))
            {
                int pageCount = reader.NumberOfPages;
                for (int i = 1; i <= pageCount; i++)
                {
                    ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_CENTER, new Phrase(
                        $"C{i}"), 560, 725, 0);
                }
            }
        }

The pdf file I can't share due of the confidential information.

Razvan
  • 80
  • 8
  • Possible duplicate of [put page number when create PDF with iTextSharp](http://stackoverflow.com/questions/22807292/put-page-number-when-create-pdf-with-itextsharp) – Bruno Lowagie Apr 13 '16 at 06:50
  • *The pdf file I can't share due of the confidential information.* - That's a pity because the code at first glance looks ok, so most likely your issue is due to some peculiarity of that file. – mkl Apr 13 '16 at 08:45
  • I've tried another pdf files and the return file size is ok now. I think the problem was the original input pdf file. Thank you very much for the help. – Razvan Apr 13 '16 at 09:04

1 Answers1

3

This is completely wrong:

reader = new PdfReader(fileOut);
Document final = new Document(reader.GetPageSize(1));
PdfWriter w = PdfWriter.GetInstance(final, new FileStream(finalFile, FileMode.Create, FileAccess.Write));
w.SetFullCompression();
final.Open();
for (int i = 1; i <= reader.NumberOfPages; i++)
{
    final.NewPage();
    PdfContentByte cb = w.DirectContent;
    ControlNumberTimes(cb, "C"+i, 560, 725, 270, Element.ALIGN_LEFT);
    cb.AddTemplate(w.GetImportedPage(reader, i), 0, 0);
}
final.Close();

Saying "I am copying a file using Document, PdfWriter, PdfImportedPage and AddTemplate, why does my file size increase?" is like asking "I've stabbed myself in the belly with a sharp knife, why do I bleed?"

If you want to add page numbers to an existing document, you have to use PdfStamper as explained in chapter 6 of my book.

You want to manipulate an existing PDF, more specifically, you want to add page numbers in the footer. That is done like this:

PdfReader reader = new PdfReader(outputFile);
using (FileStream fs = new FileStream(secondFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (PdfStamper stamper = new PdfStamper(reader, fs)) {
        int PageCount = reader.NumberOfPages;
        for (int i = 1; i <= PageCount; i++) {
            ColumnText.ShowTextAligned(stamper.GetOverContent(i), Element.ALIGN_CENTER, new Phrase(String.Format("Page {0} of {1}", i, PageCount)), 560, 725, 270);
        }
    }
}

Some remarks:

  • You are using absolute coordinates (X = 560, Y = 725). It would be better to use coordinates relative to the page size as described in the official documentation: How to position text relative to page?
  • You are using BeginText() ... EndText(), but it might be easier for you to use ColumnText.ShowTextAligned().
  • You think you are using a font that isn't embedded when you create a BaseFont like this BaseFont.CreateFont("C:\\windows\\Fonts\\times.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED). That's not true. As documented, BaseFont.NOT_EMBEDDED is ignored when using BaseFont.IDENTITY_H. See Why is iText embedding a font even when I specify not to embed? If a small file size is desired, I suggest you don't embed the font.

The main problem with your code, is the fact that you aren't manipulating a file the correct way. I think this is caused by the fact that you copy/pasted your code from a badly written tutorial. Please don't copy code from people who don't know what they're doing.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I changed my code based on your example, but the result file size is the same. – Razvan Apr 13 '16 at 07:27
  • @Razvan please share your exact current code and your input PDF to enable us to reproduce your problem. – mkl Apr 13 '16 at 08:17