KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PageStitch


1 import org.faceless.pdf2.*;
2 import java.io.*;
3 import java.util.*;
4
5 // An example which "stitches" pages together - displaying two pages
6
// from the original documents on a single page in "2-up" format.
7
//
8
// Takes a list of filenames on the command line.
9
//
10
// $Id: PageStitch.java,v 1.2 2003/11/03 10:56:14 mike Exp $
11
//
12
public class PageStitch
13 {
14     public static void main(String JavaDoc[] args)
15         throws IOException
16     {
17     if (args.length==0) {
18         System.err.println("Usage: java PageStitch <file1.pdf> [<file2.pdf> ...]");
19         System.err.println(" Creates the file \"PageStitch.pdf\"\n");
20         System.exit(0);
21     }
22
23     PDF dest = new PDF();
24     PDFPage destpage=null;
25     List holdlist = new ArrayList();
26
27     // For each filename, add all the pages to the list "holdlist"
28
//
29
for (int i=0;i<args.length;i++) {
30         PDFReader rd = new PDFReader(new File(args[i]));
31         PDF src = new PDF(rd);
32         holdlist.addAll(src.getPages());
33     }
34
35
36     // Now add the pages from "holdlist" to the new PDF document
37
//
38
// Note that this takes no account of aspect ratio of the pages.
39
// For ISO page sizes this is OK - because the ratio of width
40
// to height is sqrt(2), no distortion is introduced. American
41
// and Canadian documents with their archaic paper formats may
42
// have some distortion introduced.
43
//
44
for (int i=0;i<holdlist.size();i++)
45     {
46         PDFPage srcpage = (PDFPage)holdlist.get(i);
47
48             // In order for the source page to be drawn to a target page it must be made into
49
// a Canvas first.
50

51             PDFCanvas canvas = new PDFCanvas(srcpage);
52
53         // This next block of code takes care of merging two pages into a
54
// single page, by taking the contents of one page and pasting it
55
// onto another.
56
//
57
if (i%2==0)
58         {
59         // If it's an even number, make a new page and measure
60
// it in percentage points from the top-left.
61
//
62
destpage = dest.newPage(PDF.PAGESIZE_A4_LANDSCAPE);
63         destpage.setUnits(PDFPage.UNITS_PERCENT, PDFPage.ORIGIN_PAGETOP);
64
65         // Place the contents of the input page on the output page
66
// in the rectangle from (0%,0%) to (50%,100%) of the page.
67
// Create a canvas of the source page first...
68
destpage.drawCanvas(canvas, 0, 0, 50, 100);
69         } else {
70         //
71
// Place the contents of the input page on the output page
72
// in the rectangle from (50%,0%) to (100%,100%) of the page.
73
//
74
destpage.drawCanvas(canvas, 50, 0, 100, 100);
75         }
76     }
77         dest.render(new FileOutputStream("PageStitch.pdf"));
78     }
79 }
80
Popular Tags