| 1 |
package org.freehep.graphicsio.pdf.test; |
| 2 |
|
| 3 |
import java.awt.*; |
| 4 |
import java.io.*; |
| 5 |
import java.util.*; |
| 6 |
|
| 7 |
import org.freehep.graphicsio.pdf.*; |
| 8 |
import org.freehep.graphicsio.ImageConstants; |
| 9 |
import org.freehep.graphicsbase.util.images.*; |
| 10 |
|
| 11 |
/** |
| 12 |
* This class tests the user-level PDF Writer interfaces. |
| 13 |
* It simply writes a document with several pages, including |
| 14 |
* some text and graphics. |
| 15 |
* <p> |
| 16 |
* @author Mark Donszelmann |
| 17 |
* @version $Id: freehep-graphicsio-pdf/PDFWriterTest.java 2fa79ac3a135 2007/01/09 18:18:57 duns $ |
| 18 |
*/ |
| 19 |
public class PDFWriterTest { |
| 20 |
|
| 21 |
public static void main(String[] args) throws Exception { |
| 22 |
FileOutputStream out = new FileOutputStream("PDFWriterUserTest.pdf"); |
| 23 |
|
| 24 |
// PDF file |
| 25 |
PDFWriter pdf = new PDFWriter(out, "1.3"); |
| 26 |
pdf.comment("PDFGraphicsTest file generated by PDFWriterTest, Freehep lib"); |
| 27 |
|
| 28 |
// info |
| 29 |
PDFDocInfo info = pdf.openDocInfo("DocInfo"); |
| 30 |
info.setTitle("PDFWriter Test Output"); |
| 31 |
info.setAuthor("Mark Donszelmann"); |
| 32 |
info.setSubject("Test File of the PDFWriter of the FreeHEP library"); |
| 33 |
info.setKeywords("PDFWriter; FreeHEP"); |
| 34 |
info.setCreator("org.freehep.graphicsio-pdf.PDFWriter"); |
| 35 |
info.setCreationDate(Calendar.getInstance()); |
| 36 |
pdf.close(info); |
| 37 |
|
| 38 |
// catalog |
| 39 |
PDFCatalog catalog = pdf.openCatalog("Catalog", "RootPage"); |
| 40 |
catalog.setOutlines("Outlines"); |
| 41 |
catalog.setPageMode("UseOutlines"); |
| 42 |
catalog.setViewerPreferences("Preferences"); |
| 43 |
catalog.setOpenAction(new Object[] { pdf.ref("FirstPage"), pdf.name("Fit")}); |
| 44 |
pdf.close(catalog); |
| 45 |
|
| 46 |
// preferences |
| 47 |
PDFViewerPreferences prefs = pdf.openViewerPreferences("Preferences"); |
| 48 |
prefs.setFitWindow(true); |
| 49 |
prefs.setCenterWindow(false); |
| 50 |
pdf.close(prefs); |
| 51 |
|
| 52 |
// outlines |
| 53 |
PDFOutlineList outlines = pdf.openOutlineList("Outlines", "FirstOutline", "LastOutline"); |
| 54 |
pdf.close(outlines); |
| 55 |
|
| 56 |
// outline FIXME: need to connect still |
| 57 |
PDFOutline firstOutline = pdf.openOutline("FirstOutline", "Text Page", |
| 58 |
"Outlines", null, "SecondOutline"); |
| 59 |
firstOutline.setDest(new Object[] { pdf.ref("FirstPage"), pdf.name("Fit")}); |
| 60 |
pdf.close(firstOutline); |
| 61 |
|
| 62 |
PDFOutline secondOutline = pdf.openOutline("SecondOutline", "Graphics Page", |
| 63 |
"Outlines", "FirstOutline", "LastOutline"); |
| 64 |
secondOutline.setDest(new Object[] { pdf.ref("SecondPage"), pdf.name("Fit")}); |
| 65 |
pdf.close(secondOutline); |
| 66 |
|
| 67 |
PDFOutline lastOutline = pdf.openOutline("LastOutline", "Image Page", |
| 68 |
"Outlines", "SecondOutline", null); |
| 69 |
lastOutline.setDest(new Object[] { pdf.ref("ThirdPage"), pdf.name("Fit")}); |
| 70 |
pdf.close(lastOutline); |
| 71 |
|
| 72 |
// pages |
| 73 |
PDFPageTree pages = pdf.openPageTree("RootPage", null); |
| 74 |
pages.addPage("FirstPage"); |
| 75 |
pages.addPage("SecondPage"); |
| 76 |
pages.addPage("ThirdPage"); |
| 77 |
pages.setMediaBox(0, 0, 612, 792); |
| 78 |
pages.setResources("Resources"); |
| 79 |
pdf.close(pages); |
| 80 |
|
| 81 |
// resources |
| 82 |
PDFDictionary resources = pdf.openDictionary("Resources"); |
| 83 |
resources.entry("ProcSet", pdf.ref("OurPageProcSet")); |
| 84 |
PDFDictionary fontList = resources.openDictionary("Font"); |
| 85 |
fontList.entry("F1", pdf.ref("Helvetica")); |
| 86 |
resources.close(fontList); |
| 87 |
pdf.close(resources); |
| 88 |
|
| 89 |
// first page |
| 90 |
PDFPage firstPage = pdf.openPage("FirstPage", "RootPage"); |
| 91 |
firstPage.setContents("FirstPageContent"); |
| 92 |
pdf.close(firstPage); |
| 93 |
|
| 94 |
// first page content |
| 95 |
pdf.comment("hello word"); |
| 96 |
PDFStream first = pdf.openStream("FirstPageContent", new String[] { "Flate", "ASCIIHex" }); |
| 97 |
first.beginText(); |
| 98 |
first.font(pdf.name("F1"), 24); |
| 99 |
first.text(100, 100); |
| 100 |
first.show("Hello"); |
| 101 |
first.show(" World"); |
| 102 |
first.endText(); |
| 103 |
pdf.close(first); |
| 104 |
|
| 105 |
// second page |
| 106 |
PDFPage secondPage = pdf.openPage("SecondPage", "RootPage"); |
| 107 |
secondPage.setContents("SecondPageContent"); |
| 108 |
secondPage.setThumb("SecondPageThumbnail"); |
| 109 |
pdf.close(secondPage); |
| 110 |
|
| 111 |
// second page thumbnail |
| 112 |
PDFStream secondThumb = pdf.openStream("SecondPageThumbnail"); |
| 113 |
MediaTracker tracker = new MediaTracker(new Panel()); |
| 114 |
Image image1 = ImageHandler.getImage("../../test/images/BrokenCursor.gif", PDFWriterTest.class); |
| 115 |
Image image2 = ImageHandler.getImage("../../test/images/transparent-image.gif", PDFWriterTest.class); |
| 116 |
tracker.addImage(image1,0); |
| 117 |
tracker.addImage(image2,1); |
| 118 |
try { |
| 119 |
tracker.waitForAll(); |
| 120 |
} catch (Exception e) { |
| 121 |
e.printStackTrace(); |
| 122 |
} |
| 123 |
secondThumb.image( |
| 124 |
ImageUtilities.createRenderedImage( |
| 125 |
image2, null, Color.BLACK), |
| 126 |
Color.BLACK, |
| 127 |
ImageConstants.ZLIB); |
| 128 |
pdf.close(secondThumb); |
| 129 |
|
| 130 |
// second page content |
| 131 |
PDFStream second = pdf.openStream( |
| 132 |
"SecondPageContent", |
| 133 |
new String[] { |
| 134 |
ImageConstants.ENCODING_FLATE, |
| 135 |
ImageConstants.ENCODING_ASCII85 }); |
| 136 |
|
| 137 |
second.comment("Draw a black line segment, using the default line width."); |
| 138 |
second.move(150, 250); |
| 139 |
second.line(150, 350); |
| 140 |
second.stroke(); |
| 141 |
second.comment("Draw a thicker, dashed line segment."); |
| 142 |
second.width(4); |
| 143 |
second.comment("Set line width to 4 points"); |
| 144 |
second.dash(new int[] {4,6}, 0); |
| 145 |
second.comment("Set dash pattern to 4 units on, 6 units off"); |
| 146 |
second.move(150, 250); |
| 147 |
second.line(400, 250); |
| 148 |
second.stroke(); |
| 149 |
second.dash(new int[0], 0); |
| 150 |
second.comment("Reset dash pattern to a solid line"); |
| 151 |
second.width(1); |
| 152 |
second.comment("Reset line width to 1 unit"); |
| 153 |
second.comment("Draw a rectangle with a 1 unit red border, filled with light blue."); |
| 154 |
second.colorSpaceStroke(1.0, 0.0, 0.0); |
| 155 |
second.comment("Red for stroke color"); |
| 156 |
second.colorSpace(0.5, 0.75, 1.0); |
| 157 |
second.comment("Light blue for fill color"); |
| 158 |
second.rectangle(200, 300, 50, 75); |
| 159 |
second.fillAndStroke(); |
| 160 |
second.comment("Draw a curve filled with gray and with a colored border."); |
| 161 |
second.colorSpaceStroke(0.5, 0.1, 0.2); |
| 162 |
second.colorSpace(0.7); |
| 163 |
second.move(300, 300); |
| 164 |
second.cubic(300, 400, 400, 400, 400, 300); |
| 165 |
second.closeFillAndStroke(); |
| 166 |
pdf.close(second); |
| 167 |
|
| 168 |
// third page |
| 169 |
PDFPage thirdPage = pdf.openPage("ThirdPage", "RootPage"); |
| 170 |
thirdPage.setContents("ThirdPageContent"); |
| 171 |
pdf.close(thirdPage); |
| 172 |
|
| 173 |
// third page content |
| 174 |
PDFStream third = pdf.openStream("ThirdPageContent"); |
| 175 |
third.save(); |
| 176 |
third.matrix(32, 0, 0, 32, 298, 388); |
| 177 |
third.inlineImage( |
| 178 |
ImageUtilities.createRenderedImage(image1, null, Color.BLACK), |
| 179 |
Color.BLACK, |
| 180 |
ImageConstants.ZLIB); |
| 181 |
third.restore(); |
| 182 |
pdf.close(third); |
| 183 |
|
| 184 |
// our page proc set |
| 185 |
pdf.object("OurPageProcSet", new Object[] {pdf.name("PDF"), |
| 186 |
pdf.name("Text"), |
| 187 |
pdf.name("ImageC") }); |
| 188 |
|
| 189 |
// font |
| 190 |
PDFDictionary font = pdf.openDictionary("Helvetica"); |
| 191 |
font.entry("Type", pdf.name("Font")); |
| 192 |
font.entry("Subtype", pdf.name("Type1")); |
| 193 |
font.entry("Name", pdf.name("F1")); |
| 194 |
font.entry("BaseFont", pdf.name("Helvetica")); |
| 195 |
pdf.close(font); |
| 196 |
|
| 197 |
// close pdf |
| 198 |
pdf.close(); |
| 199 |
out.close(); |
| 200 |
|
| 201 |
System.exit(0); |
| 202 |
} |
| 203 |
} |