All pastes #901968 Raw Edit

Mine

public text v1 · immutable
·published 2008-02-13 01:49 UTC
rendered paste body
package com.plexq.pdf.writer;

import com.plexq.pdf.*;
import java.io.*;
import java.util.*;

public class PDFWriter extends FileOutputStream {
	protected int bytesWritten=0;
	
	public PDFWriter(String fileName) throws IOException {
		super(fileName);
	}
	
	public void write(PDF p) throws IOException {
		write("%PDF-1.4\n");
		write("%âãÏÓ");
		Iterator<PDFInteger> it=p.getXRef().keySet().iterator();
		PDFXRef xref=new PDFXRef();
		int mark;
		int maxObject=0;
		while (it.hasNext()) {
			maxObject++;
			PDFInteger i=it.next();
			PDFObject o=p.getXRef().getObject(i);
			mark=getBytesWritten();
			xref.putEntry(new PDFInteger(mark),new PDFInteger(maxObject),new PDFInteger(0));
			write(""+maxObject);
			write(" ");
			write("0 obj ");
			write(o);
		}
		
		mark=getBytesWritten();
		write(xref);
		PDFTrailer trailer=p.getTrailer();
		trailer.put(new PDFName("Size"),new PDFInteger(maxObject+1));
		trailer.remove(new PDFName("Prev"));
		write(trailer);
		write("startxref\n");
		write(""+mark);
		write("\n%%EOF\n");
	}
	
	public void write(PDFArray a) throws IOException {
		write("[");
		for (int t=0;t<a.size();t++) {
			write(a.get(t));
			write(" ");
		}
		write("]");
	}
	public void write(PDFBoolean po) throws IOException {
		boolean b=po.getValue();
		if (b) {
			write("true");
		}
		else {
			write("false");
		}
	}
	public void write(PDFComment c) throws IOException {
		write("%"+c.getComment()+"\r");
	}

	public void write(PDFDictionary d) throws IOException {
		writeDictionary(d);
		if (d.getStream()!=null) {
			write(d.getStream());
		}
	}
	
	public void writeDictionary(PDFDictionary d) throws IOException {
		Iterator<PDFName> it=d.keySet().iterator();
		write("<<\n");
		while (it.hasNext()) {
			write("\t");
			PDFName n=it.next();
			PDFObject o=d.get(n);
			write(n);
			write(" ");
			write(o);
			write("\n");
		}
		write(">>\n");
	}
	public void write(PDFHexString phs) throws IOException {
		StringBuffer sb=new StringBuffer();
		String value=phs.getValue();
		
		sb.append("<");
		for (int t=0;t<value.length();t++) {
			sb.append(Integer.toString((int)value.charAt(t),16));
		}
		sb.append(">");
		write(sb.toString());
	}
	public void write(PDFIndirectReference pir) throws IOException {		
		StringBuffer sb=new StringBuffer();
		sb.append(pir.getRefId());
		sb.append(" ");
		sb.append(pir.getRefGeneration());
		sb.append(" ");
		sb.append("R");
		
		write(sb.toString());
	}
	public void write(PDFInteger i) throws IOException {
		write(""+i.getValue());
	}
	public void write(PDFName n) throws IOException {
		write("/"+n.getName());
	}

	public void write(PDFReal r) throws IOException {
		write(""+r.getValue());
	}
	public void write(PDFString ps) throws IOException {
		StringBuffer sb=new StringBuffer();
		
		sb.append("(");
		sb.append(ps.getValue());
		sb.append(")");
		write(sb.toString());
	}
	public void write(PDFTrailer t) throws IOException {
		write("trailer\n");
		writeDictionary(t);
	}
	public void write(PDFObject o){
		throw new RuntimeException("Can't call write with a bare object");
	}
	public void write(PDFStream s) throws IOException {
		write("stream\n");
		write(s.getData());
		write("\nendstream");
	}
	public void write(String s) throws IOException {
		write(s.getBytes());
	}
	
	public void write(byte[] b) throws IOException {
		for (int t=0;t<b.length;t++) {
			write(b[t]);
		}
	}
	public void write(byte[] b,int offset, int length) throws IOException {
		for (int t=0;t<length;t++) {
			write(b[offset+t]);
		}
	}
	
	public void write(byte b) throws IOException {
		bytesWritten++;
		super.write(b);
	}

	/**
	 * getBytesWritten() returns the number of bytes that have been written to this stream
	 * @return returns the number of bytes that have been written to this stream
	 */
	public int getBytesWritten() {
		return bytesWritten;
	}
}