[英]Edit PDF text using C#
How can I find and then hide (or delete) specific text phrase?如何找到然后隐藏(或删除)特定的文本短语?
For example, I have created a PDF file containing all sorts of data such as images, tables, text etc.例如,我创建了一个 PDF 文件,其中包含各种数据,例如图像、表格、文本等。
Now, I want to find a specific phrase like "Hello World" wherever it is mentioned in the file and somehow hide it, or -better even- delete it from the PDF.现在,我想在文件中提到的任何地方找到一个像“Hello World”这样的特定短语,并以某种方式隐藏它,或者 - 更好的是 - 从 PDF 中删除它。
And finally get the PDF after deleting this phrase.删除这句话后终于得到了PDF。
I have tried iTextSharp
and Spire
, but couldn't find anything that worked.我试过
iTextSharp
和Spire
,但找不到任何iTextSharp
东西。
Try the following code snippets to hide the specifc text phrase on PDF using Spire.PDF.尝试使用以下代码片段使用 Spire.PDF 隐藏 PDF 上的特定文本短语。
using Spire.Pdf;
using Spire.Pdf.General.Find;
using System.Drawing;
namespace HideText
{
class Program
{
static void Main(string[] args)
{
//load PDF file
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Example.pdf");
//find all results where "Hello World" appears
PdfTextFind[] finds = null;
foreach (PdfPageBase page in doc.Pages)
{
finds = page.FindText("Hello World").Finds;
}
//cover the specific result with white background color
finds[0].ApplyRecoverString("", Color.White, false);
//save to file
doc.SaveToFile("output.pdf");
}
}
}
The following snippetfrom here let you find and black-out the text in pdf document: 此处的以下代码段可让您找到并涂黑 pdf 文档中的文本:
PdfDocument pdf = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
ICleanupStrategy cleanupStrategy = new RegexBasedCleanupStrategy(new Regex(@"Alice", RegexOptions.IgnoreCase)).SetRedactionColor(ColorConstants.PINK);
PdfAutoSweep autoSweep = new PdfAutoSweep(cleanupStrategy);
autoSweep.CleanUp(pdf);
pdf.Close();
Pay attention to the license.注意许可证。 It is AGPL, if you don't buy license.
如果您不购买许可证,则它是 AGPL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.