Repository navigation
Expand file tree
/
Copy pathPdfReaderTests.cs
More file actions
109 lines (90 loc) · 3.44 KB
/
Copy pathPdfReaderTests.cs
File metadata and controls
109 lines (90 loc) · 3.44 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
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();
}
}
}
}