KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > etymon > pj > samples > AppendPage


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