File: PDFWriterLowLevelTest.java

package info (click to toggle)
freehep-graphicsio-pdf 2.1.1+dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 360 kB
  • sloc: java: 3,555; xml: 75; sh: 10; makefile: 3
file content (142 lines) | stat: -rw-r--r-- 5,663 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package org.freehep.graphicsio.pdf.test;

import java.io.*;
import java.util.*;

import org.freehep.graphicsio.pdf.*;

/**
 * This class tests the lower-level PDF Writer interfaces.
 * It simply writes a document with several pages, including
 * some text and graphics.
 * <p>
 * @author Mark Donszelmann
 * @version $Id: PDFWriterLowLevelTest.java 8584 2006-08-10 23:06:37Z duns $
 */
public class PDFWriterLowLevelTest {

    public static void main(String[] args) throws Exception {
        FileOutputStream out = new FileOutputStream("PDFWriterLowLevelTest.pdf");

        // PDF file
        PDFWriter pdf = new PDFWriter(out, "1.3");
        pdf.comment("PDFGraphicsTest file generated by PDFWriterLowLevelTest, Freehep lib");
        
        // info
        PDFDictionary info = pdf.openDictionary("DocInfo");
        info.entry("Title", "PDFWriter LowLevel Test Output");
        info.entry("Author", "Mark Donszelmann");
        info.entry("Subject", "LowLevel Test File of the PDFWriter of the FreeHEP library");
        info.entry("Keywords", "PDFWriter; FreeHEP");
        info.entry("Creator", "org.freehep.util.pdf.PDFWriter");
        info.entry("CreationDate", Calendar.getInstance());
        pdf.close(info);
        
        // catalog
        PDFDictionary catalog = pdf.openDictionary("Catalog");
        catalog.entry("Type", pdf.name("Catalog"));
        catalog.entry("Outlines", pdf.ref("Outlines"));
        catalog.entry("Pages", pdf.ref("RootPage"));
        catalog.entry("PageMode", pdf.name("UseOutlines"));
        catalog.entry("ViewerPreferences", pdf.ref("Preferences"));
        pdf.close(catalog);
        
        // preferences
        PDFDictionary prefs = pdf.openDictionary("Preferences");
        prefs.entry("FitWindow", false);
        prefs.entry("CenterWindow", false);
        pdf.close(prefs);
        
        // outlines
        PDFDictionary outlines = pdf.openDictionary("Outlines");
        outlines.entry("Type", pdf.name("Outlines"));
        outlines.entry("Count", 0);
        pdf.close(outlines);
        
        // pages
        PDFDictionary pages = pdf.openDictionary("RootPage");
        pages.entry("Type", pdf.name("Pages"));
        pages.entry("Kids", new Object[] {pdf.ref("FirstPage"), pdf.ref("SecondPage")});
        pages.entry("Count", 2);
        pages.entry("MediaBox", new int[] {0, 0, 612, 792});
        PDFDictionary resources = pages.openDictionary("Resources");
        resources.entry("ProcSet", pdf.ref("OurPageProcSet"));
        PDFDictionary fontList = resources.openDictionary("Font");
        fontList.entry("F1", pdf.ref("Helvetica"));
        resources.close(fontList);
        pages.close(resources);
        pdf.close(pages);
        
        // first page
        PDFDictionary firstPage = pdf.openDictionary("FirstPage");
        firstPage.entry("Type", pdf.name("Page"));
        firstPage.entry("Parent", pdf.ref("RootPage"));
        firstPage.entry("Contents", pdf.ref("FirstPageContent"));
        pdf.close(firstPage);

        // first page content
        PDFStream first = pdf.openStream("FirstPageContent");
        first.beginText();
        first.font(pdf.name("F1"), 24);
        first.text(100, 100);
        first.show("Hello");
        first.show(" World");
        first.endText();
        pdf.close(first);
                
        // second page
        PDFDictionary secondPage = pdf.openDictionary("SecondPage");
        secondPage.entry("Type", pdf.name("Page"));
        secondPage.entry("Parent", pdf.ref("RootPage"));
        secondPage.entry("Contents", pdf.ref("SecondPageContent"));
        pdf.close(secondPage);

        // second page content
        PDFStream second = pdf.openStream("SecondPageContent");
        second.comment("Draw a black line segment, using the default line width.");
        second.move(150, 250);
        second.line(150, 350);
        second.stroke();
        second.comment("Draw a thicker, dashed line segment.");
        second.width(4);
        second.comment("Set line width to 4 points");
        second.dash(new int[] {4,6}, 0);
        second.comment("Set dash pattern to 4 units on, 6 units off");
        second.move(150, 250);
        second.line(400, 250);
        second.stroke();
        second.dash(new int[0], 0);
        second.comment("Reset dash pattern to a solid line");
        second.width(1); 
        second.comment("Reset line width to 1 unit");
        second.comment("Draw a rectangle with a 1 unit red border, filled with light blue.");
        second.colorSpaceStroke(1.0, 0.0, 0.0);
        second.comment("Red for stroke color");
        second.colorSpace(0.5, 0.75, 1.0);
        second.comment("Light blue for fill color");
        second.rectangle(200, 300, 50, 75);
        second.fillAndStroke();
        second.comment("Draw a curve filled with gray and with a colored border.");
        second.colorSpaceStroke(0.5, 0.1, 0.2);
        second.colorSpace(0.7);
        second.move(300, 300);
        second.cubic(300, 400, 400, 400, 400, 300);
        second.closeFillAndStroke();
        pdf.close(second);
                
        // our page proc set
        pdf.object("OurPageProcSet", new Object[] {pdf.name("PDF"), pdf.name("Text")});
        
        // font
        PDFDictionary font = pdf.openDictionary("Helvetica");
        font.entry("Type", pdf.name("Font"));
        font.entry("Subtype", pdf.name("Type1"));
        font.entry("Name", pdf.name("F1"));
        font.entry("BaseFont", pdf.name("Helvetica"));
        pdf.close(font);
        
        // write xref table and trailer      
        pdf.close("Catalog", "DocInfo");
        out.close();
    }
}