File: examples.html

package info (click to toggle)
libpdfrenderer-java 0.9.0-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,820 kB
  • sloc: java: 19,236; xml: 1,343; makefile: 10
file content (194 lines) | stat: -rw-r--r-- 5,957 bytes parent folder | download | duplicates (4)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!-- 
    Document   : examples
    Created on : Jan 4, 2008, 12:55:19 PM
    Author     : joshy
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="common.css"/>
  </head>
  <body>
      
      
      <h2>Examples</h2>
      
      <h3>How do I show a PDF in my Swing Application</h3>
  
    
    <p>Here is some simple code adapted from the viewer demo which will 
    show a PDF in a JFrame. This uses the PagePanel class from the viewer
    demo, which handles all of the details of repainting the pdf in a
    JPanel.</p>
    
    <pre class="code-sample"><code>package pdfpaneltest;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PagePanel;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.*;

/**
 * An example of using the PagePanel class to show PDFs. For more advanced
 * usage including navigation and zooming, look ad the 
 * com.sun.pdfview.PDFViewer class.
 *
 * @author joshua.marinacci@sun.com
 */
public class Main {

    public static void setup() throws IOException {
    
        //set up the frame and panel
        JFrame frame = new JFrame("PDF Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        <span class="code-highlight">PagePanel panel = new PagePanel();</span>
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

        //load a pdf from a byte buffer
        <span class="code-highlight">File file = new File("test.pdf");
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
            0, channel.size());
        PDFFile pdffile = new PDFFile(buf);</span>

        // show the first page
        <span class="code-highlight">PDFPage page = pdffile.getPage(0);
        panel.showPage(page);</span>
        
    }

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main.setup();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}
    
    </code></pre>
    
    
    <h3>How do I draw a PDF into an Image?</h3>
    
  <p>Here is some sample code to draw a PDF into an image.</p>
  
    <pre class="code-sample"><code>package pdfpaneltest;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.*;

/**
 * An example of drawing a PDF to an image.
 *
 * @author joshua.marinacci@sun.com
 */
public class ImageMain {

    public static void setup() throws IOException {

        //load a pdf from a byte buffer
        File file = new File("test.pdf");
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
        PDFFile pdffile = new PDFFile(buf);

        // draw the first page to an image
        PDFPage page = pdffile.getPage(0);
        
        //get the width and height for the doc at the default zoom 
        <span class="code-highlight">Rectangle rect = new Rectangle(0,0,
                (int)page.getBBox().getWidth(),
                (int)page.getBBox().getHeight());</span>
        
        //generate the image
        <span class="code-highlight">Image img = page.getImage(
                rect.width, rect.height, //width & height
                rect, // clip rect
                null, // null for the ImageObserver
                true, // fill background with white
                true  // block until drawing is done
                );</span>
        
        //show the image in a frame
        JFrame frame = new JFrame("PDF Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JLabel(new ImageIcon(img)));
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    ImageMain.setup();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}

  </code></pre>
  
  
  <h3>How do I draw a PDF directly to my own Graphics2D object?</h3>
  
  <p>Sometimes you may need to draw directly to some other Graphics2D object
  instead of directly to an image. A common example is printing.  The PDFRenderer
lets you draw directly to a Graphics2D object rather than returning an image.</p>

<p>The following code draws a pdf into the Graphics2D from a BufferedImage</p>

<pre class="code-sample"><code>
File file = new File("/Users/joshy/splats.pdf");

// set up the PDF reading
RandomAccessFile raf = new RandomAccessFile(file, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdffile = new PDFFile(buf);

// get the first page
PDFPage page = pdffile.getPage(0);


// create and configure a graphics object
BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = img.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

<span class="code-highlight">// do the actual drawing
PDFRenderer renderer = new PDFRenderer(page, g2, 
    new Rectangle(0, 0, 500, 500), null, Color.RED);
page.waitForFinish();
renderer.run();
</span></code></pre>


 
  
  </body>
</html>