forked from GamaGustavo/PDF_Table_Extrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
40 lines (33 loc) · 1.23 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas.Parser;
using iText.Kernel.Pdf.Canvas.Parser.Listener;
using System.IO;
using System.Text;
namespace PDF_Table_Extrator
{
public class Utils
{
public static string ConvertPdfToText(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
{
throw new System.ArgumentException($"'{nameof(filePath)}' cannot be null or whitespace.", nameof(filePath));
}
if (!File.Exists(filePath))
{
throw new System.ArgumentException($"File '{filePath}' not found.", nameof(filePath));
}
var resultado = new StringBuilder();
using PdfReader leitorDePdf = new PdfReader(filePath);
using PdfDocument pdfDoc = new PdfDocument(leitorDePdf);
for (int page = 1; page <= pdfDoc.GetNumberOfPages(); page++)
{
ITextExtractionStrategy strategy = new LocationTextExtractionStrategy();
resultado.Append(PdfTextExtractor.GetTextFromPage(pdfDoc.GetPage(page), strategy));
}
pdfDoc.Close();
leitorDePdf.Close();
return resultado.ToString();
}
}
}