| 1 |
package org.freehep.graphicsio.pdf.test; |
| 2 |
|
| 3 |
import java.io.*; |
| 4 |
import java.util.*; |
| 5 |
|
| 6 |
import org.freehep.graphicsio.pdf.*; |
| 7 |
|
| 8 |
/** |
| 9 |
* This class tests the lower-level PDF Writer interfaces. |
| 10 |
* It simply writes a document with several pages, including |
| 11 |
* some text and graphics. |
| 12 |
* <p> |
| 13 |
* @author Mark Donszelmann |
| 14 |
* @version $Id: freehep-graphicsio-pdf/PDFWriterLowLevelTest.java 967bf3619090 2005/12/01 05:41:40 duns $ |
| 15 |
*/ |
| 16 |
public class PDFWriterLowLevelTest { |
| 17 |
|
| 18 |
public static void main(String[] args) throws Exception { |
| 19 |
FileOutputStream out = new FileOutputStream("PDFWriterLowLevelTest.pdf"); |
| 20 |
|
| 21 |
// PDF file |
| 22 |
PDFWriter pdf = new PDFWriter(out, "1.3"); |
| 23 |
pdf.comment("PDFGraphicsTest file generated by PDFWriterLowLevelTest, Freehep lib"); |
| 24 |
|
| 25 |
// info |
| 26 |
PDFDictionary info = pdf.openDictionary("DocInfo"); |
| 27 |
info.entry("Title", "PDFWriter LowLevel Test Output"); |
| 28 |
info.entry("Author", "Mark Donszelmann"); |
| 29 |
info.entry("Subject", "LowLevel Test File of the PDFWriter of the FreeHEP library"); |
| 30 |
info.entry("Keywords", "PDFWriter; FreeHEP"); |
| 31 |
info.entry("Creator", "org.freehep.graphicsio-pdf.PDFWriter"); |
| 32 |
info.entry("CreationDate", Calendar.getInstance()); |
| 33 |
pdf.close(info); |
| 34 |
|
| 35 |
// catalog |
| 36 |
PDFDictionary catalog = pdf.openDictionary("Catalog"); |
| 37 |
catalog.entry("Type", pdf.name("Catalog")); |
| 38 |
catalog.entry("Outlines", pdf.ref("Outlines")); |
| 39 |
catalog.entry("Pages", pdf.ref("RootPage")); |
| 40 |
catalog.entry("PageMode", pdf.name("UseOutlines")); |
| 41 |
catalog.entry("ViewerPreferences", pdf.ref("Preferences")); |
| 42 |
pdf.close(catalog); |
| 43 |
|
| 44 |
// preferences |
| 45 |
PDFDictionary prefs = pdf.openDictionary("Preferences"); |
| 46 |
prefs.entry("FitWindow", false); |
| 47 |
prefs.entry("CenterWindow", false); |
| 48 |
pdf.close(prefs); |
| 49 |
|
| 50 |
// outlines |
| 51 |
PDFDictionary outlines = pdf.openDictionary("Outlines"); |
| 52 |
outlines.entry("Type", pdf.name("Outlines")); |
| 53 |
outlines.entry("Count", 0); |
| 54 |
pdf.close(outlines); |
| 55 |
|
| 56 |
// pages |
| 57 |
PDFDictionary pages = pdf.openDictionary("RootPage"); |
| 58 |
pages.entry("Type", pdf.name("Pages")); |
| 59 |
pages.entry("Kids", new Object[] {pdf.ref("FirstPage"), pdf.ref("SecondPage")}); |
| 60 |
pages.entry("Count", 2); |
| 61 |
pages.entry("MediaBox", new int[] {0, 0, 612, 792}); |
| 62 |
PDFDictionary resources = pages.openDictionary("Resources"); |
| 63 |
resources.entry("ProcSet", pdf.ref("OurPageProcSet")); |
| 64 |
PDFDictionary fontList = resources.openDictionary("Font"); |
| 65 |
fontList.entry("F1", pdf.ref("Helvetica")); |
| 66 |
resources.close(fontList); |
| 67 |
pages.close(resources); |
| 68 |
pdf.close(pages); |
| 69 |
|
| 70 |
// first page |
| 71 |
PDFDictionary firstPage = pdf.openDictionary("FirstPage"); |
| 72 |
firstPage.entry("Type", pdf.name("Page")); |
| 73 |
firstPage.entry("Parent", pdf.ref("RootPage")); |
| 74 |
firstPage.entry("Contents", pdf.ref("FirstPageContent")); |
| 75 |
pdf.close(firstPage); |
| 76 |
|
| 77 |
// first page content |
| 78 |
PDFStream first = pdf.openStream("FirstPageContent"); |
| 79 |
first.beginText(); |
| 80 |
first.font(pdf.name("F1"), 24); |
| 81 |
first.text(100, 100); |
| 82 |
first.show("Hello"); |
| 83 |
first.show(" World"); |
| 84 |
first.endText(); |
| 85 |
pdf.close(first); |
| 86 |
|
| 87 |
// second page |
| 88 |
PDFDictionary secondPage = pdf.openDictionary("SecondPage"); |
| 89 |
secondPage.entry("Type", pdf.name("Page")); |
| 90 |
secondPage.entry("Parent", pdf.ref("RootPage")); |
| 91 |
secondPage.entry("Contents", pdf.ref("SecondPageContent")); |
| 92 |
pdf.close(secondPage); |
| 93 |
|
| 94 |
// second page content |
| 95 |
PDFStream second = pdf.openStream("SecondPageContent"); |
| 96 |
second.comment("Draw a black line segment, using the default line width."); |
| 97 |
second.move(150, 250); |
| 98 |
second.line(150, 350); |
| 99 |
second.stroke(); |
| 100 |
second.comment("Draw a thicker, dashed line segment."); |
| 101 |
second.width(4); |
| 102 |
second.comment("Set line width to 4 points"); |
| 103 |
second.dash(new int[] {4,6}, 0); |
| 104 |
second.comment("Set dash pattern to 4 units on, 6 units off"); |
| 105 |
second.move(150, 250); |
| 106 |
second.line(400, 250); |
| 107 |
second.stroke(); |
| 108 |
second.dash(new int[0], 0); |
| 109 |
second.comment("Reset dash pattern to a solid line"); |
| 110 |
second.width(1); |
| 111 |
second.comment("Reset line width to 1 unit"); |
| 112 |
second.comment("Draw a rectangle with a 1 unit red border, filled with light blue."); |
| 113 |
second.colorSpaceStroke(1.0, 0.0, 0.0); |
| 114 |
second.comment("Red for stroke color"); |
| 115 |
second.colorSpace(0.5, 0.75, 1.0); |
| 116 |
second.comment("Light blue for fill color"); |
| 117 |
second.rectangle(200, 300, 50, 75); |
| 118 |
second.fillAndStroke(); |
| 119 |
second.comment("Draw a curve filled with gray and with a colored border."); |
| 120 |
second.colorSpaceStroke(0.5, 0.1, 0.2); |
| 121 |
second.colorSpace(0.7); |
| 122 |
second.move(300, 300); |
| 123 |
second.cubic(300, 400, 400, 400, 400, 300); |
| 124 |
second.closeFillAndStroke(); |
| 125 |
pdf.close(second); |
| 126 |
|
| 127 |
// our page proc set |
| 128 |
pdf.object("OurPageProcSet", new Object[] {pdf.name("PDF"), pdf.name("Text")}); |
| 129 |
|
| 130 |
// font |
| 131 |
PDFDictionary font = pdf.openDictionary("Helvetica"); |
| 132 |
font.entry("Type", pdf.name("Font")); |
| 133 |
font.entry("Subtype", pdf.name("Type1")); |
| 134 |
font.entry("Name", pdf.name("F1")); |
| 135 |
font.entry("BaseFont", pdf.name("Helvetica")); |
| 136 |
pdf.close(font); |
| 137 |
|
| 138 |
// write xref table and trailer |
| 139 |
pdf.close("Catalog", "DocInfo"); |
| 140 |
out.close(); |
| 141 |
} |
| 142 |
} |