如何找到然后隐藏(或删除)特定文本短语?
例如,我创建了一个 PDF 文件,其中包含图像、表格、文本等各种数据。
现在,我想在文件中提到的任何地方找到一个特定的短语,如“Hello World”,并以某种方式隐藏它,或者 - 甚至 - 从 PDF 中删除它。
最后在删除这个短语后得到PDF。
我试过iTextSharp
and Spire
,但找不到任何有效的方法。
尝试以下代码片段以使用 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");
}
}
}
此处的以下代码段可让您在 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();
注意许可证。如果您不购买许可证,它是 AGPL。