/
PdfDenseMerger.java
200 lines (179 loc) · 6.03 KB
/
PdfDenseMerger.java
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// $Id$
package mkl.testarea.itext7.merge;
import java.io.IOException;
import java.util.List;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.parser.PdfDocumentContentParser;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.kernel.utils.PdfMerger;
/**
* <p>
* This class is a tool for merging PDF <em>page contents</em> in a condensed manner,
* i.e. if the source pages are only partly filled, the contents of multiple pages
* are drawn on a single target page. If on the other hand the contents of a source
* page do not completely fit onto a target page, the source page contents are split
* and as much as possible of it is added onto the current target page before a new
* page is started. This actually allows to even copy content from larger pages onto
* smaller ones, e.g. from A4 to A5 landscape.
* </p>
* <p>
* In contrast to {@link PdfMerger}, though, this class does not copy information
* beyond page content, in particular it completely ignores annotations and the
* structure hierarchy of tagged PDFs.
* </p>
* <p>
* Beware: Simple PDF is not particularly designed for this kind of operations. Thus,
* there are numerous situations in which this tool may not create the desired result.
* It e.g. should not be used to copy documents with header or footer information as
* they will be considered part of the content and so copied and probably moved to
* the middle of the page. Furthermore, water marks, text box backgrounds, etc. will
* also be considered part of the content and, therefore, also will prevent condensed
* merging.
* </p>
*
* @author mkl
*/
public class PdfDenseMerger
{
/**
* This class is used to merge a number of existing documents into one.
*
* @param pdfDocument - the document into which source documents will be merged.
*/
public PdfDenseMerger(PdfDocument pdfDocument)
{
this.pdfDocument = pdfDocument;
}
//
// getters and setters for layout values
//
public PageSize getPageSize()
{
return pageSize;
}
public PdfDenseMerger setPageSize(PageSize pageSize)
{
this.pageSize = pageSize;
return this;
}
public float getTop()
{
return top;
}
public PdfDenseMerger setTop(float top)
{
this.top = top;
return this;
}
public float getBottom()
{
return bottom;
}
public PdfDenseMerger setBottom(float bottom)
{
this.bottom = bottom;
return this;
}
public float getGap()
{
return gap;
}
public PdfDenseMerger setGap(float gap)
{
this.gap = gap;
return this;
}
public PdfDocument getPdfDocument()
{
return pdfDocument;
}
//
// methods controling the actual merger
//
/**
* This method adds pages from the source document to the target document.
*
* @param from - document, from which pages will be copied.
* @param fromPage - start page in the range of pages to be copied.
* @param toPage - end page in the range to be copied.
*/
public void addPages(PdfDocument from, int fromPage, int toPage) throws IOException
{
for (int pageNum = fromPage; pageNum <= toPage; pageNum++)
{
merge(from, pageNum);
}
}
/**
* This method adds pages from the source document to the target document.
*
* @param from - document, from which pages will be copied.
* @param pages - List of numbers of pages which will be copied.
*/
public void addPages(PdfDocument from, List<Integer> pages) throws IOException
{
for (Integer pageNum : pages)
{
merge(from, pageNum);
}
}
//
// helper methods
//
void merge(PdfDocument from, int pageNum) throws IOException
{
PdfPage page = from.getPage(pageNum);
PdfFormXObject formXObject = page.copyAsFormXObject(pdfDocument);
PdfDocumentContentParser contentParser = new PdfDocumentContentParser(from);
PageVerticalAnalyzer finder = contentParser.processContent(pageNum, new PageVerticalAnalyzer());
if (finder.verticalFlips.size() < 2)
return;
Rectangle pageSizeToImport = page.getPageSize();
int startFlip = finder.verticalFlips.size() - 1;
boolean first = true;
while (startFlip > 0)
{
if (!first)
newPage();
float freeSpace = yPosition - (pageSize.getBottom() + bottom);
int endFlip = startFlip + 1;
while ((endFlip > 1) && (finder.verticalFlips.get(startFlip) - finder.verticalFlips.get(endFlip - 2) < freeSpace))
endFlip -=2;
if (endFlip < startFlip)
{
float height = finder.verticalFlips.get(startFlip) - finder.verticalFlips.get(endFlip);
canvas.saveState();
canvas.rectangle(0, yPosition - height, pageSizeToImport.getWidth(), height);
canvas.clip();
canvas.endPath();
canvas.addXObject(formXObject, 0, yPosition - (finder.verticalFlips.get(startFlip) - pageSizeToImport.getBottom()));
canvas.restoreState();
yPosition -= height + gap;
startFlip = endFlip - 1;
}
else if (!first)
throw new IllegalArgumentException(String.format("Page %s content sections too large.", page));
first = false;
}
}
void newPage()
{
PdfPage page = pdfDocument.addNewPage(pageSize);
canvas = new PdfCanvas(page);
yPosition = pageSize.getTop() - top;
}
//
// hidden members
//
final PdfDocument pdfDocument;
PageSize pageSize = PageSize.A4;
float top;
float bottom;
float gap;
PdfCanvas canvas;
float yPosition = 0;
}