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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 162 additions & 53 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MuPDF.NET;
using mupdf;
using MuPDF.NET;
using SkiaSharp;
using System;
using System.Collections.Generic;
Expand All @@ -7,6 +8,7 @@
using System.Linq;
using System.Text;
using System.Threading;
using static ICSharpCode.SharpZipLib.Zip.ExtendedUnixData;
using static System.Net.Mime.MediaTypeNames;
using static System.Net.WebRequestMethods;
using File = System.IO.File;
Expand All @@ -33,53 +35,177 @@ class Program
{
static void Main(string[] args)
{
//TestInsertHtmlbox();
//TestLineAnnot();
//AnnotationsFreeText1.Run(args);
//AnnotationsFreeText2.Run(args);
//NewAnnots.Run(args);
//TestHelloWorldToNewDocument(args);
//TestHelloWorldToExistingDocument(args);
//TestReadBarcode(args);
//TestReadDataMatrix();
//TestWriteBarcode(args);
//TestExtractTextWithLayout(args);
//TestWidget(args);
//TestColor(args);
TestInsertHtmlbox();
TestLineAnnot();
AnnotationsFreeText1.Run(args);
AnnotationsFreeText2.Run(args);
NewAnnots.Run(args);
TestHelloWorldToNewDocument(args);
TestHelloWorldToExistingDocument(args);
TestReadBarcode(args);
TestReadDataMatrix();
TestWriteBarcode(args);
TestExtractTextWithLayout(args);
TestWidget(args);
TestColor(args);
TestCMYKRecolor(args);
TestSVGRecolor(args);
//TestReplaceImage(args);
//TestInsertImage(args);
//TestGetImageInfo(args);
//TestGetTextPageOcr(args);
//TestCreateImagePage(args);
//TestJoinPdfPages(args);
//TestFreeTextAnnot(args);
//TestTextFont(args);
TestReplaceImage(args);
TestInsertImage(args);
TestGetImageInfo(args);
TestGetTextPageOcr(args);
TestCreateImagePage(args);
TestJoinPdfPages(args);
TestFreeTextAnnot(args);
TestTextFont(args);
TestMemoryLeak();
//TestDrawLine();
//TestWriteBarcode1();
//TestCMYKRecolor1(args);
//TestUnicodeDocument();
//TestMorph();
TestWidget();
TestDrawLine();
TestWriteBarcode1();
TestUnicodeDocument();
TestMorph();
TestMetadata();
TestTable();
TestMoveFile();
TestOpenErrorAtAcrobat();

return;
}
static void TestWidget()

static void TestOpenErrorAtAcrobat()
{
Console.WriteLine("\n=== TestWidget =====================");
Console.WriteLine("\n=== TestOpenErrorAtAcrobat =====================");

string testFilePath = @"E:\MuPDF.NET\Tmp\Peter\1101\test.pdf";
string outputFilePath = @"E:\MuPDF.NET\Tmp\Peter\1101\output.pdf";

Document doc = new Document(testFilePath);

Page page = doc[0];

page.DrawLine(new Point(45, 50), new Point(80, 50), width: 0.5f, dashes: "[5] 0");
page.DrawLine(new Point(90, 50), new Point(150, 50), width: 0.5f, dashes: "[5] 0");
page.DrawLine(new Point(45, 80), new Point(180, 80), width: 0.5f, dashes: "[5] 0");
page.DrawLine(new Point(45, 100), new Point(180, 100), width: 0.5f, dashes: "[5] 0");

Document doc = new Document("../../../TestDocuments/test_widget_parse.pdf");
Color lineColor = new Color(); // Default to black
lineColor.Stroke = new float[] { 0, 0, 0 }; // RGB black

Shape img = page.NewShape();
Point startPoint = new Point(100, 100);
Point endPoint = new Point(200, 200);

String dashString = "[2] 0"; // Example dash pattern

img.DrawLine(startPoint, endPoint);
img.Finish(width: 2, color: lineColor.Stroke, dashes: dashString);
img.Commit();

page.Dispose();

doc.Save(outputFilePath);
doc.Close();

Console.WriteLine("TestOpenErrorAtAcrobat completed.");
}

var currentPage = 0;
while (currentPage < doc.PageCount)
static void TestMoveFile()
{
string origfilename = @"E:\test.pdf";

Document d = new Document(origfilename);

Page page = d[0];

Point tl = new Point(100, 120);
Point br = new Point(300, 150);

Rect rect = new Rect(tl, br);

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

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();

MemoryStream tmp = new MemoryStream();

d.Save(tmp, garbage: 3, deflateFonts: 1, deflate: 1);

d.Close();

File.WriteAllBytes(origfilename, tmp.ToArray());

tmp.Dispose();

File.Move(origfilename, @"e:\new.pdf", true);
}

public static float MmToPs(float mm)
{
return mm * 2.8346456693f;
}

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

string testFilePath = @"E:\MuPDF.NET\Tmp\Peter\Table\test.pdf";

Rect rect = new Rect(X0:MmToPs(110), X1:MmToPs(198), Y0: MmToPs(42), Y1: MmToPs(76));

Document doc = new Document(testFilePath);

FzStextOptions options = new FzStextOptions(0);
options.Dispose();

string text = "";

text = doc[0].GetText(clip: rect, flags: (int)TextFlags.TEXT_MEDIABOX_CLIP, sort: true);
Console.WriteLine(text);

/*
text = "";
List<TextBlock> textBlocks = doc[0].GetTextBlocks(rect);
foreach (TextBlock block in textBlocks)
{
text += block.Text;
}

Console.WriteLine(text);

text = doc[0].GetTextWithLayout(rect);

Console.WriteLine(text);
*/
doc.Close();

Console.WriteLine("TestTable completed.");
}

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

string testFilePath = @"e:\Annot.pdf";

Document doc = new Document(testFilePath);

Dictionary<string, string> metaDict = doc.MetaData;

foreach (string key in metaDict.Keys)
{
var page = doc[currentPage];
var widgets = page.GetWidgets().ToList();
currentPage++;
Console.WriteLine(key + ": " + metaDict[key]);
}

doc.Close();

Console.WriteLine("TestDocument completed.");
}

static void TestMorph()
{
Console.WriteLine("\n=== TestMorph =====================");
Expand Down Expand Up @@ -123,23 +249,6 @@ static void TestUnicodeDocument()
Console.WriteLine("TestDocument completed.");
}

static void TestCMYKRecolor1(string[] args)
{
Console.WriteLine("\n=== TestCMYKRecolor =====================");

string testFilePath = "../../../TestDocuments/CMYK_Recolor1.pdf";
Document doc = new Document(testFilePath);
//List<Entry> images = doc.GetPageImages(0);
//Console.WriteLine($"CaName: {images[0].CsName}");
doc.Recolor(0, "CMYK");
//images = doc.GetPageImages(0);
//Console.WriteLine($"CaName: {images[0].AltCsName}");
doc.Save(@"CMYKRecolor.pdf");
doc.Close();

Console.WriteLine("CMYK Recolor test completed.");
}

static void TestWriteBarcode1()
{
string testFilePath = Path.GetFullPath("../../../TestDocuments/Blank.pdf");
Expand Down
39 changes: 39 additions & 0 deletions MuPDF.NET.Test/DocumentTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,44 @@ public void TestJoinPdfPages()
doc2.Close();
doc1.Close();
}

[Test]
public void TestMoveFile()
{
string testFilePath1 = Path.GetFullPath(@"../../../resources/DocumentTest/Widget.pdf");
string testFilePath2 = Path.GetFullPath(@"TestMoveOrig.pdf");
string testFilePath3 = Path.GetFullPath(@"TestMoveNew.pdf");

File.Copy(testFilePath1, testFilePath2, true);

Document doc = new Document(testFilePath2);
Page page = doc[0];

Point tl = new Point(100, 120);
Point br = new Point(300, 150);

Rect rect = new Rect(tl, br);
TextWriter pw = new TextWriter(page.TrimBox);
Font font = new Font(fontName: "tiro");
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();

MemoryStream tmp = new MemoryStream();

doc.Save(tmp, garbage: 3, deflateFonts: 1, deflate: 1);
doc.Close();

File.WriteAllBytes(testFilePath2, tmp.ToArray());

tmp.Dispose();

File.Move(testFilePath2, testFilePath3, true);

Document newDoc = new Document(testFilePath3);
Assert.IsTrue(newDoc.PageCount == 6);
newDoc.Close();
}
}
}
Loading