0
votes

I want to insert given text to existing pdf at X,Y location provided.

I am using iTextSharp(4.1.6.0) for it

I am accepting the location(to insert the text at) and the value to be inserted there, in the datagridview control , The text is getting inserted at the specified position only when the location specified has no image.

The contents are not being inserted at the location which has image in the input pdf.

Is there any different way to add text to existing pdf so that the text will be inserted at the location specified irrespective of the existence of image.

Please find my code below:

for (int i = 0; i < reader.NumberOfPages; i++)
        {
            document.NewPage();

            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                gridColumn = Convert.ToInt32(row.Cells[2].Value);
                if (gridColumn == i + 1)
                {
                    //document.NewPage();
                    BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                    cb.SetColorFill(iTextSharp.text.Color.BLACK);
                    cb.SetFontAndSize(bf, 8);

                    text = "" + row.Cells[3].Value;
                    cb.BeginText();
                    cb.ShowTextAligned(2, text, Convert.ToSingle(row.Cells[0].Value), Convert.ToSingle(row.Cells[1].Value), 0);
                    cb.EndText();

                    page = writer.GetImportedPage(reader, Convert.ToInt32(row.Cells[2].Value));

                    cb.AddTemplate(page, 0, 0);


                }
                else
                {
                    page = writer.GetImportedPage(reader, i + 1);
                    cb.AddTemplate(page, 0, 0);

                }

           }//end foreach

      }//end for i
1
How do you get your PdfContentByte? Did you make sure you get it by using PdfStamper.GetOverContent and not PdfStamper.GetUnderContent?Alexis Pigeon
I got that using writer.DirectContentSupereme

1 Answers

1
votes

I got that problem solved by using SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL) of PdfContentByte.
I used writer.DirectContent while inserting my own text on the PDF otherwise I used writer.DirectContentUnder

Find My modified Code below:

for (int i = 0; i < reader.NumberOfPages; i++)
{
     document.NewPage();
     log.Debug("Creating new page in the document..");
     bool wasPageImported = false;

     foreach (DataGridViewRow row in dataGridView1.Rows)
     {
           gridColumn = Convert.ToInt32(row.Cells[2].Value);
           if (gridColumn == i + 1)
           {
               //DirectContent for writing text to PDF
                PdfContentByte cb = writer.DirectContent;

                if (!wasPageImported)
               {
                    page = writer.GetImportedPage(reader,       Convert.ToInt32(row.Cells[2].Value));
                    cb.AddTemplate(page, 0, 0);
                    wasPageImported = true;
                }
                    BaseFont bf = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
             //The text rendering mode causes entered text to appear above backround
                                 cb.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);

               cb.SetColorFill(iTextSharp.text.Color.BLACK);
               int fontSize = -1;
               if (row.Cells[4].Value.ToString() == "" || row.Cells[4].Value == null)
               {
                   fontSize = 12;
               }
               else
              {
                   fontSize = Convert.ToInt32(row.Cells[4].Value);
               }

               cb.SetFontAndSize(bf, fontSize);

               text = "" + row.Cells[3].Value;
               cb.BeginText();
               cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, Convert.ToSingle(row.Cells[0].Value), Convert.ToSingle(row.Cells[1].Value), 0);
               cb.EndText();

         }
        else
       {
            //DirectContentUnder if not writing any text to PDF
              PdfContentByte cb = writer.DirectContentUnder;
              page = writer.GetImportedPage(reader, i + 1);
              cb.AddTemplate(page, 0, 0);
        }

 }//end foreach

}//end for int i