如何使用itextsharp C#.net替换pdf中的特定单词 [英] How to replace specific word in pdf using itextsharp C#.net

查看:68
本文介绍了如何使用itextsharp C#.net替换pdf中的特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我编写了一个C#控制台程序,用pdf中的新单词替换特定单词。我的程序执行此操作但我的新pdf确实做了看起来不像原始的pdf。我的基本目的是在不改变原始pdf的外观和感觉的情况下替换一些单词。可以帮助我看看这一点,我非常感谢你的帮助。这是我的代码:





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:


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.

推荐答案

这个例子讲述了操纵文本 - 使用iTextSharp和VB.NET 2012处理PDF文件 [ ^ ]

此示例删除文本,但也可用于替换文本 - iTextSharp从静态PDF文档中删除文本C# [ ^ ]
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#[^]


你可以优化代码。它是有点符号标记的。

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();

   /*
   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();
}


这篇关于如何使用itextsharp C#.net替换pdf中的特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆