【问题标题】:Read PDF Line By Line using iText7 and Fill on Textboxes Winforms使用 iText7 逐行阅读 PDF 并填充文本框 Winforms
【发布时间】:2022-01-01 00:24:58
【问题描述】:

我正在开发一个 WinForms 应用程序。我使用pdf文件重置密码,pdf上的值存储为键值对(email:xxxx@mail.com,pass:11111)。

我想做的事

逐行阅读PDF文件并填写相应的文本框。

我做了什么

public bool CreatePDF(string location, string email, string key)
    {
        if(location != "" && email != "" && key != "")
        {
            PdfWriter pdfwriter = new PdfWriter(location);
            PdfDocument pdf = new PdfDocument(pdfwriter);
            Document document = new Document(pdf);
            Paragraph fields = new Paragraph("Email: "+email + "\n" + "Secret Key: "+key);
            document.Add(fields);
            document.Close();
            return true;
        }            
        else
        {
            return false;
        }
    }

    public string ReadPDF(string location)
    {
        var pdfDocument = new PdfDocument(new PdfReader(location));
        StringBuilder processed = new StringBuilder();
        var strategy = new LocationTextExtractionStrategy();
        string text = "";
        for (int i = 1; i <= pdfDocument.GetNumberOfPages(); ++i)
        {
            var page = pdfDocument.GetPage(i);
            text += PdfTextExtractor.GetTextFromPage(page, strategy);
            processed.Append(text);
        }
        return text;
    }
}

提前谢谢你们!也欢迎对 CreatePDF 提出任何建议。

【问题讨论】:

  • 考虑将ReadPDF的结果拆分为'\n'。

标签: c# winforms pdf


【解决方案1】:

这是我想出来的,

var pdfDocument = new PdfDocument(new PdfReader("G:\\Encryption_File.pdf"));
        StringBuilder processed = new StringBuilder();
        var strategy = new LocationTextExtractionStrategy();
        string text = "";
        for (int i = 1; i <= pdfDocument.GetNumberOfPages(); ++i)
        {
            var page = pdfDocument.GetPage(i);
            text += PdfTextExtractor.GetTextFromPage(page, strategy);
            processed.Append(text);                
        }
        text.Split('\n');
        string line = "";                        
        line = text + "&";            
        string[] newLines = line.Split('&'); 
        textBox1.Text = newLines[0].Split(':')[1].ToString(); 
        textBox2.Text = newLines[0].Split(':')[2].ToString();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-18
    • 2013-11-24
    • 1970-01-01
    • 2014-12-21
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多