Czytaj PDF linijka po linijce za pomocą iText7 i wypełnij pola tekstowe Winforms

0

Pytanie

Pracuję nad aplikacją WinForms. Używam plik pdf do resetowania hasła i wartości w pdf są przechowywane w postaci par klucz-wartość(e-mail: [email protected] pomiń: 11111).

Co chcę zrobić:

Przeczytaj plik PDF wiersz po wierszu, a następnie wypełnij odpowiednie pola tekstowe.

Co zrobiłem:

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;
    }
}

Z góry dziękuję wam, chłopaki!. Wszelkie sugestie dotyczące CreatePDF również mile widziane.

c# pdf winforms
2021-11-22 11:36:14
1

Najlepsza odpowiedź

0

Oto co wymyśliłem,

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();
2021-11-23 02:40:57

W innych językach

Ta strona jest w innych językach

Русский
..................................................................................................................
Italiano
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................