1
我正在爲我的問題尋找解決方案。在特定位置刪除或覆蓋PDF上的文本
我試圖修改一個文本,使用itextsharp出現在PDF文檔的特定位置。
有人可以幫忙嗎?
SOLUTION:
我已經解決了寫這篇:
public bool StampOnPDF(string _PathPDF, string _text, string _Total)
{
string _fileName = Path.GetFileName(_PathPDF);
string oldFile = _PathPDF;
string BackupPDF = _PathPDF.Replace(".pdf", "_old.pdf");
File.Copy(oldFile, BackupPDF);
iTextSharp.text.Rectangle Zone1 = new iTextSharp.text.Rectangle(495, 157, 540, 148);
iTextSharp.text.Rectangle Zone2 = new iTextSharp.text.Rectangle(495, 130, 540, 105);
using (PdfReader reader = new PdfReader(BackupPDF))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(oldFile, FileMode.Create)))
{
PdfContentByte pbover = stamper.GetOverContent(1);
Zone1.BackgroundColor = BaseColor.WHITE;
pbover.Rectangle(Zone1);
Zone2.BackgroundColor = BaseColor.WHITE;
pbover.Rectangle(Zone2);
// select the font properties
var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 12);
var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);
normalFont.Size = 8;
boldFont.Size = 8;
string text = _testo;
ColumnText.ShowTextAligned(pbover, Element.ALIGN_CENTER, new Phrase(text, normalFont), 300, 180, 0);
text = _Total;
ColumnText.ShowTextAligned(pbover, Element.ALIGN_CENTER, new Phrase(text, boldFont), 523, 115, 0);
ColumnText.ShowTextAligned(pbover, Element.ALIGN_CENTER, new Phrase(text, normalFont), 523, 150, 0);
}
return true;
}
你有什麼嘗試?請與我們分享您的代碼,我們將爲您提供幫助:) – Ben
**(A)**如何「刪除特定位置的文本」---您是否看過「PdfCleanUpProcessor」,例如討論[這裏](http://developers.itextpdf.com/question/how-remove-text-pdf)? **(B)**如何*在特定位置覆蓋文本* ---使用PdfCleanUpProcessor清除區域後,您現在可以繪製任何適合該區域的文本。 **(C)**如何「修改出現在特定位置的文本」 - 「修改」這個詞可能意味着您希望周圍的文本按照某種期望的格式重新排列。如果你確實希望,你很可能會失望。 – mkl
如果您想針對您的問題發佈解決方案,請不要將其編輯爲問題,而是將其作爲答案。另一方面,如果您還不滿足於「解決方案」,只要您指出您不滿足哪些方面,就可以將其添加到問題中。有人說,你的解決方案確實有一個缺點:舊文本可能不再可見,但它仍然存在。因此,如果您從文檔中複製並粘貼(在頁面上嘗試CTRL-A CTRL-C,在某個編輯器中嘗試CTRL-V),舊文本將會在那裏! – mkl