【发布时间】:2017-08-01 14:26:18
【问题描述】:
我正在为我的问题寻找解决方案。
我正在尝试使用 itextsharp 修改出现在 pdf 文档特定位置的文本。
有人可以帮忙吗?
解决方案:
我已经决定写这个了:
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;
}
【问题讨论】:
-
你试过什么?请与我们分享您的代码,我们将为您提供帮助:)
-
(A) 如何“删除特定位置的文本”---你看过
PdfCleanUpProcessor,例如讨论过here? (B) 如何覆盖特定位置的文本 --- 使用PdfCleanUpProcessor清除该区域后,您现在可以绘制适合该区域的任何文本。 (C) 如何“修改出现在特定位置的文本” --- modify 一词可能暗示您希望周围的文本根据某些内容重排所需的格式。如果你真的希望,你很可能会失望。 -
如果您想发布适合您的问题的解决方案,请不要将其编辑到问题中,而是将其作为答案。另一方面,如果您对那个“解决方案”还不满意,那么只要您指出您不满意哪些方面,就可以将其添加到问题中。话虽如此,您的解决方案确实有一个缺点:旧文本可能不再可见,但它仍然存在。因此,如果您从文档中复制和粘贴(尝试在页面上使用 CTRL-A CTRL-C 并在某些编辑器中使用 CTRL-V),旧文本将在那里!
-
对于您提出的问题有更好的解决方案吗?
标签: pdf text itext pdf-generation