2015-12-07 27 views
0

我有一個pdf文件。每頁都有一個頁腳。現在我想用一些其他文本替換頁腳上存在的靜態文本。請幫我..... 我試圖與下面的情況,但沒有成功替換合併的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)); 
} 
+0

哪裏'PdfEncodings'來自PDF文件?你確切的問題是什麼?你有什麼例外嗎? pdf保存後無效嗎? – BendEg

+0

是的,文檔無效。它給我錯誤打開「頁面上有錯誤」PdfEncodings是IText庫中的內置枚舉。 –

+0

我想知道這個方法是什麼:'ConvertToString',因爲你不能將PDF轉換爲字符串,而不是將其轉換回來。 – BendEg

回答

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」