01: package com.etymon.pj.samples;
02:
03: import java.io.*;
04: import java.util.*;
05: import com.etymon.pj.*;
06: import com.etymon.pj.exception.*;
07: import com.etymon.pj.object.*;
08:
09: /**
10: Demonstrates appending a page to the end of a PDF file.
11: @author Nassib Nassar
12: */
13: public class AppendPage {
14:
15: public static void main(String args[]) {
16: if (args.length < 2) {
17: System.out.println("AppendPage [infile] [outfile]");
18: return;
19: }
20: String infile = args[0];
21: String outfile = args[1];
22: System.out
23: .println("This appends a new page with a short message to the end of");
24: System.out.println("the pdf document.");
25: try {
26: System.out.println("Reading and parsing input file...");
27:
28: // load the infile
29: Pdf pdf = new Pdf(infile);
30:
31: System.out.println("Got it.");
32:
33: // create a font object
34: PjFontType1 font = new PjFontType1();
35: font.setBaseFont(new PjName("Helvetica-Bold"));
36: font.setEncoding(new PjName("PDFDocEncoding"));
37:
38: int fontId = pdf.registerObject(font);
39:
40: // create a resources dictionary
41: PjResources resources = new PjResources();
42: // add ProcSet
43: Vector procsetVector = new Vector();
44: procsetVector.addElement(new PjName("PDF"));
45: procsetVector.addElement(new PjName("Text"));
46: resources.setProcSet(new PjProcSet(procsetVector));
47: // add Font
48: Hashtable fontResHt = new Hashtable();
49: fontResHt.put(new PjName("F1"), new PjReference(
50: new PjNumber(fontId)));
51: resources.setFont(new PjDictionary(fontResHt));
52: int resourcesId = pdf.registerObject(resources);
53:
54: // add text
55: String s = new String(
56: "BT\n/F1 24 Tf\n72 720 Td\n(Hello, world!) Tj\nET\n");
57: byte[] data = s.getBytes();
58: PjStream stream = new PjStream(data);
59: int streamId = pdf.registerObject(stream);
60:
61: // create a new page
62: PjPage page = new PjPage();
63: page.setResources(new PjReference(
64: new PjNumber(resourcesId), PjNumber.ZERO));
65: pdf.addToPage(page, streamId);
66: int pageId = pdf.registerObject(page);
67:
68: pdf.appendPage(pageId);
69:
70: System.out.println("Writing modified output file...");
71:
72: pdf.writeToFile(outfile);
73:
74: System.out.println("Done.");
75: } catch (PjException pje) {
76: System.out.println(pje);
77: } catch (IOException ioe) {
78: System.out.println(ioe);
79: }
80: }
81:
82: }
|