首页 > 解决方案 > 如何使用 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;
    }

标签: c#pdfitext

解决方案


推荐阅读