KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Concatenate


1 import org.faceless.pdf2.*;
2 import java.io.*;
3 import java.util.*;
4
5 // One of the most common requests we get is "How do I join two
6
// documents together". This example shows just that, in the
7
// simplest way possible. The "getPages()" method here is only
8
// being used to add pages to the end, but as it returns a
9
// java.util.List it's easy to manipulate the list as necessary.
10
//
11
// $Id: Concatenate.java,v 1.4 2003/11/03 10:56:14 mike Exp $
12
//
13
public class Concatenate
14 {
15     public static void main(String JavaDoc[] args) throws IOException
16     {
17     if (args.length<2) {
18         System.out.println("Usage: java Concatenate <file1> <file2> ... <filen>");
19         System.out.println(" Creates file \"Concatenate.pdf\"");
20         System.exit(0);
21     }
22
23
24     PDF pdf = new PDF(new PDFReader(new FileInputStream(args[0])));
25
26     for (int i=1;i<args.length;i++)
27     {
28         // Load the PDF to be concatenated
29
//
30
FileInputStream in = new FileInputStream(args[i]);
31         PDF newpdf = new PDF(new PDFReader(in));
32         in.close();
33
34         // Move all the pages from "newpdf" to "pdf"
35
// Move all the form fields from "newpdf" to "pdf"
36
//
37
// In version 1, the form fields were moved with the pages.
38
// Not so in version 2! If you want them you have to move
39
// them yourself.
40
//
41
pdf.getPages().addAll(newpdf.getPages());
42         pdf.getForm().getElements().putAll(newpdf.getForm().getElements());
43     }
44
45     pdf.render(new FileOutputStream("Concatenate.pdf"));
46     }
47 }
48
Popular Tags