0

i have a pdf file. There is a footer on each page. now i want to replace a static text exist on footer with some other text. Please Help me..... I have tried with following case but not success

PdfReader readere = new PdfReader(@"D:\MergedOutput.pdf");
for (int i = 1; i < readere.NumberOfPages; i++)
{
   byte[] contentBytes = PdfEncodings.ConvertToBytes(PdfTextExtractor.GetTextFromPage(readere, i), PdfObject.TEXT_PDFDOCENCODING);
   byte[] searchStringArray = PdfEncodings.ConvertToBytes("Side", PdfObject.TEXT_PDFDOCENCODING);
   byte[] replacedByString = PdfEncodings.ConvertToBytes("Hello", PdfObject.TEXT_PDFDOCENCODING);
   string searchString = PdfEncodings.ConvertToString(searchStringArray, PdfObject.TEXT_PDFDOCENCODING);
   string contentString = PdfEncodings.ConvertToString(contentBytes, PdfObject.TEXT_PDFDOCENCODING);
   string replaceString = PdfEncodings.ConvertToString(replacedByString, PdfObject.TEXT_PDFDOCENCODING);

   if (contentString.Contains(searchString))
   {
      contentString = contentString.Replace(searchString, replaceString);
   }

   readere.SetPageContent(i, PdfEncodings.ConvertToBytes(contentString, PdfObject.TEXT_PDFDOCENCODING));
}
croxy
  • 4,082
  • 9
  • 28
  • 46
Dalip Choudhary
  • 546
  • 5
  • 18
  • Where does `PdfEncodings` comes from? What is your exact problem? Do you get any exceptions? Is the pdf not valid after saving? – BendEg Dec 07 '15 at 09:05
  • Yes, Document is not valid. Its give me error on opening "There is a error on page" PdfEncodings is the inbuilt enum in IText library. – Dalip Choudhary Dec 07 '15 at 09:31
  • Im am wondering what this method is for: `ConvertToString` because you can not convert a pdf to a string and than back.? – BendEg Dec 07 '15 at 09:33
  • Take a look at this: http://stackoverflow.com/a/4084104/2630261 – BendEg Dec 07 '15 at 09:47
  • Before addressing your question I can tell you that you absolutely should not mess around with encodings like that, that will at best be a no-op but more than likely it will be things up. See [Once you have a string, you have a string, and it is Unicode, always](http://stackoverflow.com/a/10191879/231316) – Chris Haas Dec 07 '15 at 13:58

1 Answers1

0

Suppose you have a byte array of PDF data or any PDF files. First convert this file to byte array.. after that we have to apply below code section. its working fine for me...

 iTextSharp.text.Font blackFont = FontFactory.GetFont("Seoge UI", 10, iTextSharp.text.Font.NORMAL, new BaseColor(Color.FromArgb(97, 102, 116)));
    //Path to where you want the file to output
    string outputFilePath = "MergedOutputt.pdf";
    //Path to where the pdf you want to modify is
    //string inputFilePath = "D:\\MergedOutput.pdf";
    try
    {

        using (Stream outputPdfStream = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
        using (Stream outputPdfStream2 = new FileStream(outputFilePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
        {
            //Opens the unmodified PDF for reading
            var reader = new PdfReader(MergedOutputStream.ToArray());
            //Creates a stamper to put an image on the original pdf
            var stamper = new PdfStamper(reader, outputPdfStream) { FormFlattening = true, FreeTextFlattening = true };
            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                //Creates an image that is the size i need to hide the text i'm interested in removing
                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(new Bitmap(120, 20), BaseColor.WHITE);
                //Sets the position that the image needs to be placed (ie the location of the text to be removed)
                image.SetAbsolutePosition(reader.GetPageSize(i).Width - 120, 42);
                //Adds the image to the output pdf
                stamper.GetOverContent(i).AddImage(image, true);
                //Creates the first copy of the outputted pdf
                PdfPTable table = new PdfPTable(1);
                float[] width = new float[] { 38 };

                table.SetTotalWidth(width);
                PdfContentByte pb;

                //Get PdfContentByte object for first page of pdf file
                pb = stamper.GetOverContent(i);
                cellSequenceNumber = new PdfPCell(new Phrase(new Chunk("Side " + i.ToString(), blackFont)));
                cellSequenceNumber.Border = 0;
                table.AddCell(cellSequenceNumber);
                table.WriteSelectedRows(0, table.Rows.Count, reader.GetPageSize(i).Width - 82, 54, pb);

            }
            stamper.Close();
            //Opens our outputted file for reading
           var reader2 = new PdfReader(outputPdfStream2);
           reader2.Close();               




        }
    }

It will generate a pdf file named as "MergedOutputt.pdf"

Dalip Choudhary
  • 546
  • 5
  • 18