35 lines
1.2 KiB
C#
35 lines
1.2 KiB
C#
using Elwig.Documents;
|
|
using Elwig.Helpers;
|
|
using NReco.PdfRenderer;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Tests.UnitTests.DocumentTests {
|
|
public static partial class Utils {
|
|
|
|
private static readonly string FileName = Path.Combine(Path.GetTempPath(), "test_document.pdf");
|
|
|
|
[GeneratedRegex(@"\s{2,}")]
|
|
private static partial Regex WideSpaces();
|
|
|
|
public static async Task<string> GeneratePdfText(Document doc, bool preserveLayout = false) {
|
|
using (var ctx = new AppDbContext()) {
|
|
await doc.Generate(ctx);
|
|
}
|
|
try {
|
|
doc.SaveTo(FileName);
|
|
var conv = new PdfToTextConverter { CustomArgs = preserveLayout ? "-layout " : "-raw " };
|
|
return conv.GenerateText(FileName);
|
|
} finally {
|
|
File.Delete(FileName);
|
|
}
|
|
}
|
|
|
|
public static string[][] ExtractTable(string text) {
|
|
return [.. text.Split('\n')
|
|
.Select(row => WideSpaces().Split(row).Select(c => c.Trim()).Where(c => c.Length > 0).ToArray())
|
|
.Where(row => row.Length >= 3)
|
|
.Skip(1)];
|
|
}
|
|
}
|
|
}
|