FileDocCategorySizeDatePackage
PDF.javaAPI DocExample5530Wed May 17 18:25:20 BST 2000com.darwinsys.spdf

PDF

public class PDF extends Object
The main class for the Darwin Open Systems {Simple,Stupid,Simplistic} PDF API. PDF is Adobe's Portable Document Format, and is probably a trademark of Adobe Systems Inc, Mountain View, California. The Adobe PDF Specification which they publish grants everyone permission to write code to generate and/or process PDF files. A PDF Object represents one PDF file.
author
Ian F. Darwin, ian@darwinsys.com
version
$Id: PDF.java,v 1.5 2000/05/17 21:25:21 ian Exp $

Fields Summary
protected PrintWriter
out
The output writer
protected ArrayList
pages
The list of pages
protected ArrayList
xrefs
The list of object xrefs
PDFObject
rootObj
The root object
InfoObject
infoObj
The Info object
OutlinesObject
outlinesObj
The outlines (outline font) object
PagesObject
pagesObj
The Pages object
FontDict
fontDict
The Font Dictionary
protected int
currObj
The object number of the current object
protected boolean
startedWriting
A flag to avoid writing twice
protected static final String
PDF_MAGIC
A magic number that identifies the output as a PDF file
long
offset
DecimalFormat
nf10
DecimalFormat
nf5
long
xrefStart
Constructors Summary
public PDF(PrintWriter o)
Constructor


	  
	   
		out = o;

		pages = new ArrayList();
		xrefs = new ArrayList();

	
Methods Summary
public voidadd(Page p)

		pages.add(p);
	
protected voidaddXref()
Add an entry into the offset table

		xrefs.add(new Long(offset));
	
public voidinsertPage(int where, Page p)

		pages.add(where, p);
	
protected voidprint(java.lang.String s)
Print a String


	    
	   
		out.print(s);
		offset += s.length();
	
protected voidprint(java.lang.Object o)
Print an Object

		print(o.toString());
	
protected voidprint(int i)
Print an int

		String s = Integer.toString(i);
		print(s);
	
protected voidprintXref(long n, int where, char inUse)
Write one Xref, in the format 0000000000 65535 f


	          
	        
		println(nf10.format(n) + " " +  nf5.format(where) + " " + inUse);
	
protected voidprintln()
Println with no args

		print("\n");
	
protected voidprintln(java.lang.String s)
Println a String

		print(s);
		print("\n");
	
protected voidprintln(java.lang.Object o)
Println an Object

		println(o.toString());
	
protected voidprintln(int i)
Println an int

		String s = Integer.toString(i);
		print(s);
	
public voidsetAuthor(java.lang.String au)

		infoObj.dict.put("Author", "(" + au + ")");
	
public voidwritePDF()
Write the entire output

		if (startedWriting) {
			throw new IllegalStateException(
				"writePDF() can only be called once.");
		}
		startedWriting = true;

		writePDFHeader();
		writePDFbody();
		writeXrefs();
		writePDFTrailer();
		out.flush();
		out.close();
	
protected voidwritePDFHeader()

		println(PDF_MAGIC);

		rootObj.print();	// 1

		infoObj.print();	// 2

		outlinesObj.print(); // 3

		pagesObj.print();	// 4
	
protected voidwritePDFTrailer()

		println("trailer");
		println("<<");
		println("/Size " + (xrefs.size() + 1));
		println("/Root 1 0 R");
		println("/Info 2 0 R");
		println(">>");
		println("% startxref");
		println("% " + xrefStart);
		println("%%EOF");
	
protected voidwritePDFbody()


		for (int i=0; i<pages.size(); i++) {
			 ((Page)pages.get(i)).print();		// 5, 6
		}

		addXref();
		print(currObj++); println(" 0 obj");
		println("[/PDF /Text]");
		println("endobj");

		fontDict.print();		// 8
	
protected voidwriteXrefs()
Write all the xrefs, using the format above

		xrefStart = offset;
		println("xref");
		print(0);
		print(" ");
		print(xrefs.size() + 1);
		println();
		// "fake" entry at 0, always 0, -1, f(free).
		printXref(0, 65535, 'f");
		// Remaining xref entries are for real objects.
		for (int i=0; i<xrefs.size(); i++) {
			Long lo = (Long)xrefs.get(i);
			long l = lo.longValue();
			printXref(l, 0, 'n");
		}