c# - 如何使用 iTextSharper 检测 PDF 中的隐藏文本
问题描述
我们在下面有一个 PDF 文档,其中隐藏了一些文本(用白框覆盖)。是否可以使用 iTextSharp 技术来检测 PDF 是否有隐藏文本?
我确实附上了带有隐藏文本的 PDF 图像以及我们从 PDF 中提取文本的两种方法
预先感谢大家的帮助
使用 iTextSharp 我可以通过两种方式提取文本(两种方法,ReadFile 和 ExtractText)
public string ReadFile(string Filename)
{
PdfReader reader = new PdfReader(Filename);
string pdfText = string.Empty;
string OCRErrorPages = string.Empty;
for (int i = 1; i <= reader.NumberOfPages; i++)
{
iTextSharp.text.pdf.parser.ITextExtractionStrategy its = new iTextSharp.text.pdf.parser.SimpleTextExtractionStrategy();
String extractText = iTextSharp.text.pdf.parser.PdfTextExtractor.GetTextFromPage(reader, i, its);
extractText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(extractText)));
if (extractText != "")
{
pdfText = pdfText + extractText;
}
else
{
OCRErrorPages = OCRErrorPages + i + extractText + "<br/>";
}
}
reader.Close();
if (OCRErrorPages != "")
{
return OCRErrorPages + " This page contains no text";
}
else
{
return pdfText;
}
}
public string ExtractText(string inFileName)
{
string line = string.Empty;
// Create a reader for the given PDF file
PdfReader reader = new PdfReader(inFileName);
int totalLen = 68;
float charUnit = ((float)totalLen) / (float)reader.NumberOfPages;
int totalWritten = 0;
float curUnit = 0;
for (int page = 1; page <= reader.NumberOfPages; page++)
{
line += ExtractTextFromPDFBytes(reader.GetPageContent(page)) + " ";
var thing = reader.GetPageContent(page);
// Write the progress.
if (charUnit >= 1.0f)
{
for (int i = 0; i < (int)charUnit; i++)
{
Console.Write("#");
totalWritten++;
}
}
else
{
curUnit += charUnit;
if (curUnit >= 1.0f)
{
for (int i = 0; i < (int)curUnit; i++)
{
Console.Write("#");
totalWritten++;
}
curUnit = 0;
}
}
}
if (totalWritten < totalLen)
{
for (int i = 0; i < (totalLen - totalWritten); i++)
{
Console.Write("#");
}
}
return line;
}
解决方案
推荐阅读
- php - 其他格式的数组
- javascript - 无法更新 AngularJS 输入值
- html - 如何在 HTML5 中正确使用图像 srcset
- inputstream - 从 InputStream 开始,使用 PDFBox 将文本图像添加到 PDF 并以原始 InputStream 变量结束
- python - 我可以使用 .py 文件而不是 .yml 创建 Conda 环境吗?
- llvm - GEP 分段错误 LLVM C++ API
- mysql - 从容器内连接到远程 mysql 数据库
- ios - iOS 使用 node-apn 给通知一个标题
- google-chrome - Chrome 67 破坏了我的 WebComponent
- apache-spark - Spark 读取 avro