Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mono_crash.*

# Build results
docs/build/
TestDocuments/_Output/
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
Expand Down
1 change: 1 addition & 0 deletions Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Platforms>AnyCPU;x64;x86</Platforms>
<NoWarn>$(NoWarn);0618;CA1416</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Demo/SampleMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ private sealed record Sample(string Category, string Name, string Description, A
new("Image filters (Skia)", "image-filter-ocr", "Pixmap OCR with filter pipeline", _ => Program.TestImageFilterOcr()),

// —— Barcodes —— Samples/Barcodes
new("Barcodes", "read-barcode", "Read barcodes from image and PDF", a => Program.TestReadBarcode(a)),
new("Barcodes", "read-datamatrix", "Read Data Matrix from PDF", _ => Program.TestReadDataMatrix()),
//new("Barcodes", "read-barcode", "Read barcodes from image and PDF", a => Program.TestReadBarcode(a)),
//new("Barcodes", "read-datamatrix", "Read Data Matrix from PDF", _ => Program.TestReadDataMatrix()),
//new("Barcodes", "read-qrcode", "Render PDF page and read QR from PNG", a => Program.TestReadQrCode(a)),
new("Barcodes", "write-barcode", "Write many barcode types to PDF and PNG", a => Program.TestWriteBarcode(a)),
new("Barcodes", "write-barcode1", "Write CODE39/CODE128/DM with Units rects", _ => Program.TestWriteBarcode1()),
Expand Down
14 changes: 7 additions & 7 deletions Demo/Samples/Barcodes/Program.Barcodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ internal static void TestWriteBarcode1()
{
Console.WriteLine("\n=== TestWriteBarcode1 =====================");

string testFilePath = Path.GetFullPath("../../../TestDocuments/Blank.pdf");
string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/Blank.pdf");
Document doc = new Document(testFilePath);

Page page = doc[0];
Expand Down Expand Up @@ -82,7 +82,7 @@ internal static void TestReadDataMatrix()

Console.WriteLine("\n=== TestReadDataMatrix =======================");

string testFilePath = Path.GetFullPath("../../../TestDocuments/Barcodes/datamatrix.pdf");
string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/Barcodes/datamatrix.pdf");
Document doc = new Document(testFilePath);

Page page = doc[0];
Expand Down Expand Up @@ -144,7 +144,7 @@ internal static void TestReadBarcode(string[] args)
Console.WriteLine("\n=== TestReadBarcode =======================");

Console.WriteLine("--- Read from image file ----------");
string testFilePath1 = Path.GetFullPath("../../../TestDocuments/Barcodes/rendered.bmp");
string testFilePath1 = Path.GetFullPath("../../../../TestDocuments/Demo/Barcodes/rendered.bmp");

Rect rect1 = new Rect(1260, 390, 1720, 580);
List<Barcode> barcodes2 = Utils.ReadBarcodes(testFilePath1, clip:rect1);
Expand All @@ -158,7 +158,7 @@ internal static void TestReadBarcode(string[] args)

Console.WriteLine("--- Read from pdf file ----------");

string testFilePath = Path.GetFullPath("../../../TestDocuments/Barcodes/Samples.pdf");
string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/Barcodes/Samples.pdf");
Document doc = new Document(testFilePath);

Page page = doc[0];
Expand All @@ -179,7 +179,7 @@ internal static void TestReadQrCode(string[] args)
int i = 0;
/*
Console.WriteLine("=== Read from image file =====================");
string testFilePath1 = Path.GetFullPath("../../../TestDocuments/Barcodes/2.png");
string testFilePath1 = Path.GetFullPath("../../../../TestDocuments/Demo/Barcodes/2.png");

List<Barcode> barcodes2 = Utils.ReadBarcodes(testFilePath1, autoRotate:true);

Expand All @@ -194,7 +194,7 @@ internal static void TestReadQrCode(string[] args)
Console.WriteLine("--- Read from pdf file ----------");

string testImagePath = @"test.png";
string testFilePath = Path.GetFullPath("../../../TestDocuments/Barcodes/input.pdf");
string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/Barcodes/input.pdf");
Document doc = new Document(testFilePath);

Page page = doc[0];
Expand Down Expand Up @@ -239,7 +239,7 @@ internal static void TestWriteBarcode(string[] args)
{
Console.WriteLine("\n=== TestWriteBarcode =======================");
Console.WriteLine("--- Write to pdf file ----------");
string testFilePath = Path.GetFullPath("../../../TestDocuments/Blank.pdf");
string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/Blank.pdf");
Document doc = new Document(testFilePath);
Page page = doc[0];

Expand Down
81 changes: 51 additions & 30 deletions Demo/Samples/Document/Program.Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,68 @@ internal partial class Program
{
internal static void TestMoveFile()
{
string origfilename = @"../../../TestDocuments/Blank.pdf";
string filePath = Path.GetFullPath("testmove.pdf");
string movedPath = Path.GetFullPath("moved.pdf");

string filePath = @"testmove.pdf";
if (File.Exists(filePath))
File.Delete(filePath);
if (File.Exists(movedPath))
File.Delete(movedPath);

File.Copy(origfilename, filePath, true);

Document d = new Document(filePath);
try
{
File.Copy(Path.GetFullPath("../../../../TestDocuments/Demo/Blank.pdf"), filePath, true);
}
catch (FileNotFoundException)
{
using (Document seed = new Document())
{
seed.NewPage();
seed.Save(filePath);
seed.Close();
}
}

Page page = d[0];

Point tl = new Point(100, 120);
Point br = new Point(300, 150);
byte[] pdfBytes;
using (Document d = new Document(filePath))
{
Page page = d[0];

Rect rect = new Rect(tl, br);

TextWriter pw = new TextWriter(page.TrimBox);
/*
Font font = new Font(fontName: "tiro");
Point tl = new Point(100, 120);
Point br = new Point(300, 150);

List<(string, float)> ret = pw.FillTextbox(rect, "This is a test to overwrite the original file and move it", font, fontSize: 24);
*/
pw.WriteText(page);

page.Dispose();
Rect rect = new Rect(tl, br);

MemoryStream tmp = new MemoryStream();
TextWriter pw = new TextWriter(page.TrimBox);
/*
Font font = new Font(fontName: "tiro");

d.Save(tmp, garbage: 3, deflateFonts: 1, deflate: 1);
List<(string, float)> ret = pw.FillTextbox(rect, "This is a test to overwrite the original file and move it", font, fontSize: 24);
*/
pw.WriteText(page);

d.Close();
page.Dispose();

File.WriteAllBytes(filePath, tmp.ToArray());
using MemoryStream tmp = new MemoryStream();
d.Save(tmp, garbage: 3, deflateFonts: 1, deflate: 1);
pdfBytes = tmp.ToArray();
}

tmp.Dispose();
using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
fs.Write(pdfBytes, 0, pdfBytes.Length);
fs.Flush(true);
}

File.Move(filePath, @"moved.pdf", true);
File.Move(filePath, movedPath, true);
Console.WriteLine($"Moved {filePath} -> {movedPath}");
}

internal static void TestMetadata()
{
Console.WriteLine("\n=== TestMetadata =====================");

string testFilePath = @"../../../TestDocuments/Annot.pdf";
string testFilePath = @"../../../../TestDocuments/Demo/Annot.pdf";

Document doc = new Document(testFilePath);

Expand All @@ -66,7 +85,7 @@ internal static void TestMorph()
{
Console.WriteLine("\n=== TestMorph =====================");

string testFilePath = @"../../../TestDocuments/Morph.pdf";
string testFilePath = @"../../../../TestDocuments/Demo/Morph.pdf";

Document doc = new Document(testFilePath);
Page page = doc[0];
Expand All @@ -89,17 +108,19 @@ internal static void TestMorph()
page.Dispose();
doc.Save(@"morph.pdf");
doc.Close();

Console.WriteLine("Write to morph.pdf");
}

internal static void TestUnicodeDocument()
{
Console.WriteLine("\n=== TestUnicodeDocument =====================");

string testFilePath = @"../../../TestDocuments/你好.pdf";
string testFilePath = @"../../../../TestDocuments/Demo/你好.pdf";

Document doc = new Document(testFilePath);

doc.Save(@"你好_.pdf");
doc.Save(@"你好_.pdf");
doc.Close();

Console.WriteLine("TestUnicodeDocument completed.");
Expand All @@ -108,7 +129,7 @@ internal static void TestUnicodeDocument()
internal static void TestMemoryLeak()
{
Console.WriteLine("\n=== TestMemoryLeak =======================");
string testFilePath = Path.GetFullPath("../../../TestDocuments/Blank.pdf");
string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/Blank.pdf");

for (int i = 0; i < 100; i++)
{
Expand Down
4 changes: 2 additions & 2 deletions Demo/Samples/ImageFilters/Program.ImageFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal partial class Program
{
internal static void TestImageFilter()
{
const string inputPath = @"../../../TestDocuments/Image/table.jpg";
const string inputPath = @"../../../../TestDocuments/Demo/Image/table.jpg";
const string outputPath = @"output.png";

// Load the image file into SKBitmap
Expand Down Expand Up @@ -54,7 +54,7 @@ internal static void TestImageFilter()

internal static void TestImageFilterOcr()
{
const string inputPath = @"../../../TestDocuments/Image/boxedpage.jpg";
const string inputPath = @"../../../../TestDocuments/Demo/Image/boxedpage.jpg";

using (Pixmap pxmp = new Pixmap(inputPath))
{
Expand Down
2 changes: 2 additions & 0 deletions Demo/Samples/Llm/Program.Llm.TableExtract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ internal static void TestTableExtract2()
}

File.WriteAllLines("tables.csv", csvLines, Encoding.UTF8);

Console.WriteLine("Write to tables.csv");
}

internal static void TestTableExtract3()
Expand Down
10 changes: 5 additions & 5 deletions Demo/Samples/Llm/Program.Llm.ToMarkdown.Fixtures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ internal static void Test4LlmToMarkdownCompareExpected370(string[] args)
Console.WriteLine("\n=== Test4LlmToMarkdownCompareExpected370 (PDF4LLM) =======================");

string testsDir = LlmRepositoryTestsDirectory();
string pdfPath = Path.Combine(testsDir, "test_370.pdf");
string expectedPath = Path.Combine(testsDir, "test_370_expected.md");
string pdfPath = @"../../../../TestDocuments/Demo/Llm/test_370.pdf";
string expectedPath = @"../../../../TestDocuments/Demo/Llm/test_370_expected.md";
if (!File.Exists(pdfPath) || !File.Exists(expectedPath))
{
Console.WriteLine($"Skip: need test_370.pdf and test_370_expected.md in: {testsDir}");
Expand Down Expand Up @@ -71,7 +71,7 @@ internal static void Test4LlmToMarkdownOcrFixture1(string[] args)
_ = args;
Console.WriteLine("\n=== Test4LlmToMarkdownOcrFixture1 =======================");

string pdfPath = Path.Combine(LlmRepositoryTestsDirectory(), "test_ocr_loremipsum_FFFD.pdf");
string pdfPath = @"../../../../TestDocuments/Demo/Llm/test_ocr_loremipsum_FFFD.pdf";
if (!File.Exists(pdfPath))
{
Console.WriteLine($"Skip: missing {pdfPath}");
Expand Down Expand Up @@ -109,7 +109,7 @@ internal static void Test4LlmToMarkdownOcrFixture2(string[] args)
_ = args;
Console.WriteLine("\n=== Test4LlmToMarkdownOcrFixture2 =======================");

string pdfPath = Path.Combine(LlmRepositoryTestsDirectory(), "test_ocr_loremipsum_FFFD.pdf");
string pdfPath = @"../../../../TestDocuments/Demo/Llm/test_ocr_loremipsum_FFFD.pdf";
if (!File.Exists(pdfPath))
{
Console.WriteLine($"Skip: missing {pdfPath}");
Expand Down Expand Up @@ -140,7 +140,7 @@ internal static void Test4LlmToMarkdownOcrFixture3(string[] args)
_ = args;
Console.WriteLine("\n=== Test4LlmToMarkdownOcrFixture3 =======================");

string pdfPath = Path.Combine(LlmRepositoryTestsDirectory(), "test_ocr_loremipsum_svg.pdf");
string pdfPath = @"../../../../TestDocuments/Demo/Llm/test_ocr_loremipsum_svg.pdf";
if (!File.Exists(pdfPath))
{
Console.WriteLine($"Skip: missing {pdfPath}");
Expand Down
10 changes: 5 additions & 5 deletions Demo/Samples/Llm/Program.Llm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static void TestMarkdownReader()
Console.WriteLine("\n=== TestMarkdownReader =======================");

var reader = new PDFMarkdownReader();
string testFilePath = Path.GetFullPath("../../../TestDocuments/columns.pdf");
string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/columns.pdf");

var docs = reader.LoadData(testFilePath);

Expand All @@ -22,7 +22,7 @@ internal static void TestGetText()
Console.WriteLine("\n=== TestGetText =======================");

var reader = new PDFMarkdownReader();
string testFilePath = Path.GetFullPath("../../../TestDocuments/columns.pdf");
string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/columns.pdf");

Document doc = new Document(testFilePath);

Expand All @@ -46,7 +46,7 @@ internal static void TestTable()

try
{
string testFilePath = Path.GetFullPath("../../../TestDocuments/err_table.pdf");
string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/err_table.pdf");

if (!File.Exists(testFilePath))
{
Expand Down Expand Up @@ -209,8 +209,8 @@ internal static void TestMuPdfRagToMarkdown()
try
{
// Find a test PDF file
//string testFilePath = Path.GetFullPath("../../../TestDocuments/national-capitals.pdf");
string testFilePath = Path.GetFullPath("../../../TestDocuments/Magazine.pdf");
//string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/national-capitals.pdf");
string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/Magazine.pdf");

Document doc = new Document(testFilePath);
Console.WriteLine($"Document loaded: {doc.PageCount} page(s)");
Expand Down
Loading