替换合并的pdf文件中的文本

时间:2015-12-07 09:02:32

标签: c# pdf itextsharp

我有一个pdf文件。每页都有一个页脚。现在我想用一些其他文本替换页脚上存在的静态文本。请帮我..... 我试过以下案例但没有成功

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

1 个答案:

答案 0 :(得分:0)

假设您有一个PDF数据或任何PDF文件的字节数组。首先将此文件转换为字节数组..之后我们必须应用下面的代码部分。它对我来说很好......

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




        }
    }
  

它将生成一个名为“MergedOutputt.pdf”

的pdf文件