-
Notifications
You must be signed in to change notification settings - Fork 8
/
MergePdf.java
97 lines (84 loc) · 2.79 KB
/
MergePdf.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
import java.io.OutputStream;
import org.apache.pdfbox.cos.*;
import org.apache.pdfbox.pdfparser.PDFStreamParser;
import org.apache.pdfbox.pdfwriter.ContentStreamWriter;
import org.apache.pdfbox.contentstream.operator.Operator;
import org.apache.pdfbox.text.*;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode;
import org.apache.pdfbox.pdmodel.font.*;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineNode;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.IntegerValidator;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
/**
* This is an example on how to merge PDF documents.
*
* @author Christian H <[email protected]>
*/
public final class MergePdf
{
/**
* Default constructor.
*/
private MergePdf()
{
//example class should not be instantiated
}
/**
*
* @param args The command line arguments.
*
* @throws IOException If there is an error parsing the document.
*/
public static void main( String[] args ) throws IOException
{
if( args.length != 2 )
{
usage();
}
else
{
try
{
String[] infiles = args[0].split(";");
if (infiles.length < 2) {
System.err.println( "Error: Need more than 1 file to combine." );
System.exit( 1 );
} else {
_Merge(infiles, args[1]);
}
}
finally { }
}
}
private static void _Merge(String[] infiles, String outfile) throws IOException
{
PDDocument[] document = new PDDocument[infiles.length];
PDDocument documentOut = document[0];
PDFMergerUtility ut = new PDFMergerUtility();
for (int i=1; i < infiles.length; i++) {
ut.appendDocument(documentOut, document[i]);
}
documentOut.save(outfile);
}
/**
* This will print the usage for this document.
*/
private static void usage()
{
System.err.println( "Usage: java " + MergePdf.class.getName() + " <input-pdf[;input-pdf...]> <output-pdf>" );
}
}