ITextSharp adding text. Some text not showing up

itextsharppdf

I am adding text to an already created pdf document using this method.
ITextSharp insert text to an existing pdf
Basically it uses the PdfContentByte and then adds the content template to the page.

I am finding that in some areas of the file, the text doesn't show up.
It seems that the text I am adding is showing up behind the content that is already on the page? I flattened the pdf document down to it just being images but I am still having the same issue happen with the flattened file.

Has anyone had any issues adding text being hidden using Itextsharp?

I also tried using DirectContentUnder as was suggested in this link to no avail..
iTextSharp hides text when write

Here is the code I am using…With this I am trying to basically overlay graph paper on top of the PDF. In this example, there is a box in the upper left corner of every page that doesn't get populated. There is an image in the original pdf in this spot. And on the 4th and 5th pages, there are boxes that don't get populated, but they don't seem to be images.

PdfReader reader = new PdfReader(oldFile);
iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);

// open the writer
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();


// the pdf content
PdfContentByte cb = writer.DirectContent;


for (int i = 0; i < reader.NumberOfPages; i++)
{
    document.NewPage();
    // select the font properties
    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

    cb.SetFontAndSize(bf, 4);
    cb.SetColorStroke(BaseColor.GREEN);
    cb.SetLineWidth(1f);
    for (int j = 10; j < 600; j += 10)
    {
        WriteToDoc(ref cb, j.ToString(), j, 10);//Write the line number
        WriteToDoc(ref cb, j.ToString(), j, 780);//Write the line number
        if (j % 20 == 0)
        {
            cb.MoveTo(j, 20);
            cb.LineTo(j, 760);
            cb.Stroke();
        }
    }
    for (int j = 10; j < 800; j += 10)
    {

       WriteToDoc(ref cb, j.ToString(), 5, j);//Write the line number
       WriteToDoc(ref cb, j.ToString(), 590, j);//Write the line number
       if (j % 20 == 0)
       {
             cb.MoveTo(15, j);
             cb.LineTo(575, j);
             cb.Stroke();
       }
    }




 // create the new page and add it to the pdf
 PdfImportedPage page = writer.GetImportedPage(reader, i + 1);
 cb.AddTemplate(page, 0, 0);

 }

 // close the streams and voilá the file should be changed :)
 document.Close();
 fs.Close();
 writer.Close();
 reader.Close();

Thanks for any of the help you can provide…I really appreciate it!
-Greg

Best Answer

First of all: If you are trying to basically overlay graph paper on top of the PDF, why do you first draw the graph paper and stamp the original page onto it? You essentially are underlaying graph paper, not overlaying it.

Depending on the content of the page, your graph paper this way may easily get covered. E.g. if there is a filled rectangle in the page content, in the result there is a box in the upper left corner of every page that doesn't get populated.

Thus, simply first add the old page content, then add overlay changes.

This being said, for the task of applying changes to an existing PDF, using PdfWriter and GetImportedPage is less than optimal. This actually is a task for the PdfStamper class which its made for stamping additional content on existing PDFs.

E.g. have a look at the sample StampText, the pivotal code being:

PdfReader reader = new PdfReader(resource); 
using (var ms = new MemoryStream())
{
  using (PdfStamper stamper = new PdfStamper(reader, ms))
  {
    PdfContentByte canvas = stamper.GetOverContent(1);
    ColumnText.ShowTextAligned( canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0 );
  }
  return ms.ToArray();
}
Related Topic