forked from VahidN/iTextSharp.LGPLv2.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPdfReaderTests.cs
150 lines (124 loc) · 4.74 KB
/
PdfReaderTests.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
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
147
148
149
150
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace iTextSharp.LGPLv2.Core.FunctionalTests
{
[TestClass]
public class PdfReaderTests
{
[TestMethod]
public void Detect_Blank_Pages_In_Pdf()
{
// value where we can consider that this is a blank image
// can be much higher or lower depending of what is considered as a blank page
const int blankThreshold = 20;
var pdfFile = createSamplePdfFile();
var reader = new PdfReader(pdfFile);
var blankPages = 0;
for (var pageNum = 1; pageNum <= reader.NumberOfPages; pageNum++)
{
// first check, examine the resource dictionary for /Font or /XObject keys.
// If either are present -> not blank.
var pageDict = reader.GetPageN(pageNum);
var resDict = (PdfDictionary)pageDict.Get(PdfName.Resources);
var hasFont = resDict.Get(PdfName.Font) != null;
if (hasFont)
{
Console.WriteLine($"Page {pageNum} has font(s).");
continue;
}
var hasImage = resDict.Get(PdfName.Xobject) != null;
if (hasImage)
{
Console.WriteLine($"Page {pageNum} has image(s).");
continue;
}
var content = reader.GetPageContent(pageNum);
if (content.Length <= blankThreshold)
{
Console.WriteLine($"Page {pageNum} is blank");
blankPages++;
}
}
reader.Close();
Assert.AreEqual(expected: 1, actual: blankPages, message: $"{reader.NumberOfPages} page(s) with {blankPages} blank page(s).");
}
[TestMethod]
public void Test_Extract_Text()
{
var pdfFile = createSamplePdfFile();
var reader = new PdfReader(pdfFile);
var streamBytes = reader.GetPageContent(1);
var tokenizer = new PrTokeniser(new RandomAccessFileOrArray(streamBytes));
var stringsList = new List<string>();
while (tokenizer.NextToken())
{
if (tokenizer.TokenType == PrTokeniser.TK_STRING)
{
stringsList.Add(tokenizer.StringValue);
}
}
reader.Close();
Assert.IsTrue(stringsList.Contains("Hello DNT!"));
}
private static byte[] createSamplePdfFile()
{
using (var stream = new MemoryStream())
{
var document = new Document();
// step 2
var writer = PdfWriter.GetInstance(document, stream);
// step 3
document.AddAuthor(TestUtils.Author);
document.Open();
// step 4
document.Add(new Paragraph("Hello DNT!"));
document.NewPage();
// we don't add anything to this page: newPage() will be ignored
document.Add(new Phrase(""));
document.NewPage();
writer.PageEmpty = false;
document.Close();
return stream.ToArray();
}
}
[TestMethod]
public void Test_Draw_Text()
{
var pdfFilePath = TestUtils.GetOutputFileName();
var fileStream = new FileStream(pdfFilePath, FileMode.Create);
var pdfDoc = new Document(PageSize.A4);
var pdfWriter = PdfWriter.GetInstance(pdfDoc, fileStream);
pdfDoc.AddAuthor(TestUtils.Author);
pdfDoc.Open();
pdfDoc.Add(new Paragraph("Test"));
PdfContentByte cb = pdfWriter.DirectContent;
BaseFont bf = BaseFont.CreateFont();
cb.BeginText();
cb.SetFontAndSize(bf, 12);
cb.MoveText(88.66f, 367);
cb.ShowText("ld");
cb.MoveText(-22f, 0);
cb.ShowText("Wor");
cb.MoveText(-15.33f, 0);
cb.ShowText("llo");
cb.MoveText(-15.33f, 0);
cb.ShowText("He");
cb.EndText();
PdfTemplate tmp = cb.CreateTemplate(250, 25);
tmp.BeginText();
tmp.SetFontAndSize(bf, 12);
tmp.MoveText(0, 7);
tmp.ShowText("Hello People");
tmp.EndText();
cb.AddTemplate(tmp, 36, 343);
pdfDoc.Close();
fileStream.Dispose();
TestUtils.VerifyPdfFileIsReadable(pdfFilePath);
}
}
}