-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathProgram.Document.cs
More file actions
146 lines (113 loc) · 4.62 KB
/
Program.Document.cs
File metadata and controls
146 lines (113 loc) · 4.62 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
namespace Demo
{
internal partial class Program
{
internal static void TestMoveFile()
{
string filePath = Path.GetFullPath("testmove.pdf");
string movedPath = Path.GetFullPath("moved.pdf");
if (File.Exists(filePath))
File.Delete(filePath);
if (File.Exists(movedPath))
File.Delete(movedPath);
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();
}
}
byte[] pdfBytes;
using (Document d = new Document(filePath))
{
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();
using MemoryStream tmp = new MemoryStream();
d.Save(tmp, garbage: 3, deflateFonts: 1, deflate: 1);
pdfBytes = tmp.ToArray();
}
using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
fs.Write(pdfBytes, 0, pdfBytes.Length);
fs.Flush(true);
}
File.Move(filePath, movedPath, true);
Console.WriteLine($"Moved {filePath} -> {movedPath}");
}
internal static void TestMetadata()
{
Console.WriteLine("\n=== TestMetadata =====================");
string testFilePath = @"../../../../TestDocuments/Demo/Annot.pdf";
Document doc = new Document(testFilePath);
Dictionary<string, string> metaDict = doc.MetaData;
foreach (string key in metaDict.Keys)
{
Console.WriteLine(key + ": " + metaDict[key]);
}
doc.Close();
Console.WriteLine("TestMetadata completed.");
}
internal static void TestMorph()
{
Console.WriteLine("\n=== TestMorph =====================");
string testFilePath = @"../../../../TestDocuments/Demo/Morph.pdf";
Document doc = new Document(testFilePath);
Page page = doc[0];
Rect printrect = new Rect(180, 30, 650, 60);
int pagerot = page.Rotation;
TextWriter pw = new TextWriter(page.TrimBox);
string txt = "Origin 100.100";
pw.Append(new Point(100, 100), txt, new Font("tiro"), fontSize: 24);
pw.WriteText(page);
txt = "rotated 270 - 100.100";
Matrix matrix = new IdentityMatrix();
matrix.Prerotate(270);
Morph mo = new Morph(new Point(100, 100), matrix);
pw = new TextWriter(page.TrimBox);
pw.Append(new Point(100, 100), txt, new Font("tiro"), fontSize: 24);
pw.WriteText(page, morph:mo);
page.SetRotation(270);
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/Demo/你好.pdf";
Document doc = new Document(testFilePath);
doc.Save(@"你好_.pdf");
doc.Close();
Console.WriteLine("TestUnicodeDocument completed.");
}
internal static void TestMemoryLeak()
{
Console.WriteLine("\n=== TestMemoryLeak =======================");
string testFilePath = Path.GetFullPath("../../../../TestDocuments/Demo/Blank.pdf");
for (int i = 0; i < 100; i++)
{
Document doc = new Document(testFilePath);
Page page = doc.NewPage();
page.Dispose();
doc.Close();
}
Console.WriteLine("Memory leak test completed. No leaks should be detected.");
}
}
}