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