Click here to Skip to main content
15,892,222 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I have written a C# console program to replace a specific word with new word in pdf.My program doing this but my new pdf does does not look like orginal pdf. My basic purpose to replace some words without changing the look and feel of the orginal pdf.Can u help me out regard this, your help is heartily appreciated.Here is my code:


C#
static void Main(string[] args)
{
   var editedText = ExtractTextFromPdf("@"C:\temp\MyPdf_Orginal.pdf"");
   using (var fileStream = new FileStream(@"C:\temp\MyPdf_New.pdf", FileMode.Create,    FileAccess.Write)
   {
      Document document = new Document(PageSize.A4, 25, 25, 30, 30);
      PdfWriter writer = PdfWriter.GetInstance(document, fileStream);
      document.Open();  
      document.Open();  
      document.Add(new Paragraph(editedText));
      //Close the document
      document.Close();  
      writer.Close();
      fileStream .Close();
   }
}

public static string ExtractTextFromPdf(string path)
{
   using (PdfReader reader = new PdfReader(path))
   {
      StringBuilder text = new StringBuilder();
      for (int i = 1; i <= reader.NumberOfPages; i++)
      {
         text.Append(PdfTextExtractor.GetTextFromPage(reader, i));
         text.Replace("Delhi", "Mumbai");
      }
      return text.ToString();
   }
}


Any new approach is most welcome.
Posted
Updated 23-Nov-14 23:45pm
v2
Comments
Member 11246198 24-Nov-14 5:21am    
You can optimize the code.It is bit scrambbeled.

//Call below function to create a white image over the text
//you want to delete
RemoveText();

//Call below function to create a new text over the deleted one
string strSource = "D:\\test.pdf";
CreatePDFTemplateMSnew(strSource);


public void RemoveText()
{
//Path to where you want the file to output
string outputFilePath = "D:\\test.pdf";
//Path to where the pdf you want to modify is
string inputFilePath = "D:\\input.pdf";
try
{
using (Stream inputPdfStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
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
PdfReader reader = new PdfReader(inputPdfStream);
//Creates a stamper to put an image on the original pdf
PdfStamper stamper = new PdfStamper(reader, outputPdfStream); //{ FormFlattening = true, FreeTextFlattening = true };

//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(130, 20), BaseColor.WHITE);
//Sets the position that the image needs to be placed (ie the location of the text to be removed)
//txtX.Text = 33,txtY.Text = 708
image.SetAbsolutePosition(Convert.ToInt16(txtX.Text), Convert.ToInt16(txtY.Text));
//Adds the image to the output pdf
stamper.GetOverContent(1).AddImage(image, true);
//Creates the first copy of the outputted pdf
stamper.Close();
}
}
catch (Exception ex)
{
}
}



//Create a PDF from existing and with a template
private void CreatePDFTemplateMSnew(string strSource)
{
string oldFile = strSource;

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

// open the writer
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, ms);
document.Open();

// the pdf content
PdfContentByte cb = writer.DirectContent;
// select the font properties
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetColorFill(BaseColor.DARK_GRAY);
cb.SetFontAndSize(bf, 12);
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);

// write the text in the pdf content
cb.BeginText();
string text = txtText.Text; //"Put Text"
int intAlign = Convert.ToInt16(txtAlign.Text); //0
int intX = Convert.ToInt16(txtX.Text); //35
int intY = Convert.ToInt16(txtY.Text); //714
int intRotation = Convert.ToInt16(txtRotation.Text); //0
// put the alignment and coordinates here
cb.ShowTextAligned(intAlign, text, intX, intY, intRotation);
cb.EndText();

//document.newPage();
//PdfImportedPage page2 = writer.GetImportedPage(reader, 2);
//cb.AddTemplate(page2, 0, 0);
writer.CloseStream = false;
if (document.IsOpen()) document.Close();
ms.Position = 0;
DownloadAsPDF(ms);

// close the streams and voilá the file should be changed :)
if (writer != null) writer.Close();
if (ms != null) ms.Close();
if (reader != null) reader.Close();
}
private void DownloadAsPDF(MemoryStream ms)
{
string attachment = "att

This example talks about manipulating text - Manipulating PDF files with iTextSharp and VB.NET 2012[^]
This example removes text but can be used for replacing text as well - iTextSharp remove text from static PDF document C#[^]
 
Share this answer
 
You can optimize the code.It is bit scrambbeled.
C#
//Call below function to create a white image over the text
//you want to delete
RemoveText();

//Call below function to create a new text over the deleted one
string strSource = "D:\\test.pdf";
CreatePDFTemplateMSnew(strSource);


public void RemoveText()
{
   //Path to where you want the file to output
   string outputFilePath = "D:\\test.pdf";
   //Path to where the pdf you want to modify is
   string inputFilePath = "D:\\input.pdf";
   try
   {
      using (Stream inputPdfStream = new FileStream(inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
      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
         PdfReader reader = new PdfReader(inputPdfStream);
         //Creates a stamper to put an image on the original pdf
         PdfStamper stamper = new PdfStamper(reader, outputPdfStream); //{ FormFlattening = true, FreeTextFlattening = true };
        		
         //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(130, 20), BaseColor.WHITE);
         //Sets the position that the image needs to be placed (ie the location of the text to be removed)
         //txtX.Text = 33,txtY.Text = 708
         image.SetAbsolutePosition(Convert.ToInt16(txtX.Text), Convert.ToInt16(txtY.Text));
         //Adds the image to the output pdf
         stamper.GetOverContent(1).AddImage(image, true);
         //Creates the first copy of the outputted pdf
         stamper.Close();
      }
   }
   catch (Exception ex)
   {
   }
}

//Create a PDF from existing and with a template
private void CreatePDFTemplateMSnew(string strSource)
{
   string oldFile = strSource;

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

   // open the writer
   MemoryStream ms = new MemoryStream();
   PdfWriter writer = PdfWriter.GetInstance(document, ms);
   document.Open();

   // the pdf content
   PdfContentByte cb = writer.DirectContent;
   // select the font properties
   BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
   cb.SetColorFill(BaseColor.DARK_GRAY);
   cb.SetFontAndSize(bf, 12);
   // create the new page and add it to the pdf
   PdfImportedPage page = writer.GetImportedPage(reader, 1);
   cb.AddTemplate(page, 0, 0);
   // write the text in the pdf content
   cb.BeginText();

   /*
   Paragraph paragraph = new Paragraph();
   paragraph.Alignment = Element.ALIGN_JUSTIFIED;
   Phrase pharse = new Phrase();
   Chunk chunk = new Chunk(txtPdf.Text);
   pharse.Add(chunk);
   paragraph.Add(pharse);
   document.Add(paragraph);
   */
   string text = txtText.Text; //"Put Text"
   int intAlign = Convert.ToInt16(txtAlign.Text); //0
   int intX = Convert.ToInt16(txtX.Text); //35
   int intY = Convert.ToInt16(txtY.Text); //714
   int intRotation = Convert.ToInt16(txtRotation.Text); //0
   // put the alignment and coordinates here
   cb.ShowTextAligned(intAlign, text, intX, intY, intRotation);
   cb.EndText();
   //cb.BeginText();
   //text = "Other random blabla...";
   // put the alignment and coordinates here
   //cb.ShowTextAligned(2, text, 100, 200, 0);
   //cb.EndText();

   //document.newPage(); 
   //PdfImportedPage page2 = writer.GetImportedPage(reader, 2); 
   //cb.AddTemplate(page2, 0, 0);
   writer.CloseStream = false;
   if (document.IsOpen()) document.Close();
   ms.Position = 0;
   DownloadAsPDF(ms);

   // close the streams and voilá the file should be changed :)
   if (writer != null) writer.Close();
   if (ms != null) ms.Close();
   if (reader != null) reader.Close();
}

private void DownloadAsPDF(MemoryStream ms)
{
   string attachment = "attachment; filename=Report_" + DateTime.Now.ToString("ddMMyyyyhhmmss") + ".pdf";
   Response.Clear();
   Response.ClearContent();
   Response.ClearHeaders();

   Response.AppendHeader("Content-Disposition", attachment);
   Response.ContentType = "application/pdf";
   Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
   Response.OutputStream.Flush();
   Response.OutputStream.Close();
   Response.End();
   ms.Close();
}
 
Share this answer
 
v3
Comments
kapil koli 1-Sep-16 10:26am    
How to get the location of the text to be removed and how to pass correct parameters to SetAbsolutePosition? Any suggestions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900