javajs.export.PDFCreator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmol Show documentation
Show all versions of jmol Show documentation
Jmol: an open-source Java viewer for chemical structures in 3D
/* $RCSfile$
* $Author: hansonr $
* $Date: 2009-06-30 18:58:33 -0500 (Tue, 30 Jun 2009) $
* $Revision: 11158 $
*
* Copyright (C) 2002-2005 The Jmol Development Team
*
* Contact: [email protected]
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package javajs.export;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Map;
import java.util.Map.Entry;
import javajs.util.Lst;
import javajs.util.SB;
public class PDFCreator {
private OutputStream os;
private Lst indirectObjects;
private PDFObject root;
private PDFObject graphics;
// private PDFObject pageResources;
// private PDFObject graphicsResources;
private int pt;
private int xrefPt;
private int count;
private int height;
private int width;
private Mapfonts;
public PDFCreator() {
// for Class.forName
}
public void setOutputStream(OutputStream os) {
this.os = os;
}
public void newDocument(int paperWidth, int paperHeight, boolean isLandscape) {
width = (isLandscape ? paperHeight : paperWidth);
height = (isLandscape ? paperWidth : paperHeight);
System.out.println("Creating PDF with width=" + width + " and height=" + height);
fonts = new Hashtable();
indirectObjects = new Lst();
//graphicsResources = newObject(null);
//pageResources = newObject(null); // will set this to compressed stream later
root = newObject("Catalog");
PDFObject pages = newObject("Pages");
PDFObject page = newObject("Page");
PDFObject pageContents = newObject(null);
graphics = newObject("XObject");
root.addDef("Pages", pages.getRef());
pages.addDef("Count", "1");
pages.addDef("Kids", "[ " + page.getRef() +" ]");
page.addDef("Parent", pages.getRef());
page.addDef("MediaBox", "[ 0 0 " + paperWidth + " " + paperHeight + " ]");
if (isLandscape)
page.addDef("Rotate", "90");
pageContents.addDef("Length", "?");
pageContents.append((isLandscape ? "q 0 1 1 0 0 0 " : "q 1 0 0 -1 0 "+(paperHeight))+" cm /" + graphics.getID() + " Do Q");
page.addDef("Contents", pageContents.getRef());
addProcSet(page);
addProcSet(graphics);
// will add fonts as well as they are needed
graphics.addDef("Subtype", "/Form");
graphics.addDef("FormType", "1");
graphics.addDef("BBox", "[0 0 " + width + " " + height + "]");
graphics.addDef("Matrix", "[1 0 0 1 0 0]");
graphics.addDef("Length", "?");
page.addResource("XObject", graphics.getID(), graphics.getRef());
g("q 1 w 1 J 1 j 10 M []0 d q "); // line width 1, line cap circle, line join circle, miter limit 10, solid
clip(0, 0, width, height);
}
private void addProcSet(PDFObject o) {
o.addResource(null, "ProcSet", "[/PDF /Text /ImageB /ImageC /ImageI]");
}
private void clip(int x1, int y1, int x2, int y2) {
moveto(x1, y1);
lineto(x2, y1);
lineto(x2, y2);
lineto(x1, y2);
g("h W n");
}
public void moveto(int x, int y) {
g(x + " " + y + " m");
}
public void lineto(int x, int y) {
g(x + " " + y + " l");
}
private PDFObject newObject(String type) {
PDFObject o = new PDFObject(++count);
if (type != null)
o.addDef("Type", "/" + type);
indirectObjects.addLast(o);
return o;
}
public void addInfo(Map data) {
Hashtable info = new Hashtable();
for (Entry e: data.entrySet()) {
String value = "(" + e.getValue().replace(')','_').replace('(','_')+ ")";
info.put(e.getKey(), value);
}
root.addDef("Info", info);
}
private PDFObject addFontResource(String fname) {
PDFObject f = newObject("Font");
fonts.put(fname, f);
f.addDef("BaseFont", fname);
f.addDef("Encoding", "/WinAnsiEncoding");
f.addDef("Subtype", "/Type1");
graphics.addResource("Font", f.getID(), f.getRef());
return f;
}
private Map