⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdfpage.java

📁 Java生成PDF Java生成PDF Java生成PDF
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
// $Id: PDFPage.java,v 1.11 2005/08/23 14:31:09 mike Exp $package org.faceless.pdf;import java.util.*;import java.io.*;/** * <p> * This class represents a single page in a PDF document. This class * takes care of drawing shapes, text and images to the document. * </p> * * <b>1. Geometry</b> * <p> * By default, the geometry of a PDF page is measured in points (defined in * PostScript as 1/72 of an inch), from the bottom-left hand corner of the page. * This can be altered by calling the <code>setCanvas</code> method as shown * here.</p> * <p>All methods which specify a rectangle on the page take both corners * of the rectangle as parameters, rather than one corner plus the width and * height. This is less ambiguous when a page can be measured in different * directions. * </p> * <pre> *    // Create a canvas measured from the top-left of the page *    // in points, with a 100 point margin around it to the edge *    // of the page. *    // *    PDFPage page = pdf.newPage(800,500); *    page.setCanvas(100, 100, 600, 300, POINTS, PAGETOP+PAGELEFT); *    page.drawText("This is 100 points from the left and top of the page", 0, 0); * </pre> * * <b>2. Drawing shapes</b> * <p> * Geometric shapes are drawn using either the simple "draw" methods or * the more powerful "path" methods. Whether the shape is filled or just * drawn as an outline depends on the <code>FillColor</code> * and <code>LineColor</code> of the current style. * <ul> * <li><b><code>drawLine, drawRectangle, drawPolygon, drawEllipse,</code></b> * and (since 1.1) <b><code>drawCircle, drawCircleArc, drawEllipseArc, * drawRoundedRectangle</code></b>: These methods draw simple shapes onto the * page with a single method call. * <pre> *    PDFPage page = pdf.newPage(PAGESIZE_A4); *    PDFStyle linestyle = new PDFStyle(); *    linestyle.setLineColor(java.awt.Color.red); * *    // Draw a rectangle with two diagonal lines inside it. *    page.setStyle(linestyle); *    page.drawRectangle(100,100, 300, 200);	// Box *    page.drawLine(100,100, 400, 300);		// Diagonal 1 *    page.drawLine(100,300, 400, 100);		// Diagonal 2 * </pre> </li> * <li><b><code>pathMove, pathLine, pathBezier, pathClose</code></b>, and (since 1.1) * <code><b>pathArc</code></b>: These more primitive methods allow greater * control over the creation of geometric shapes, by creating a "path" which * can then be drawn with the <code>pathPaint</code> method. * <pre> *    PDFPage page = pdf.newPage(PAGESIZE_A4); *    PDFStyle linestyle = new PDFStyle(); *    linestyle.setLineColor(java.awt.Color.red); * *    // Draw the same rectangle with two diagonal lines inside it. *    page.setStyle(linestyle); *    page.pathMove(100,100);	// Start Box *    page.pathLine(100,300); *    page.pathLine(400,300); *    page.pathLine(400,100); *    page.pathLine(100,300);	// Diagonal 1 *    page.pathPaint();		// Paint the box and the first diagonal *    page.pathMove(100,100);	// Start Diagonal 2 *    page.pathLine(400,300); *    page.pathPaint();		// Paint the second diagonal * </pre> * </ul> * </p> * * <b>3. Drawing Text</b> * <p> * <ul> * <li>A single lines of text can be drawn at a specified location by using the * <b><code>drawText</code></b> method. * <pre> *    PDFPage page = pdf.newPage(PAGESIZE_A4); *    PDFStyle textstyle = new PDFStyle(); *    textstyle.setFillColor(java.awt.Color.black); *    textstyle.setFont(new StandardFont(StandardFont.COURIER), 12); * *    // Draw some text at the specified location *    page.setStyle(textstyle); *    page.drawText("This is some text", 100, 100); * </pre> * <li>Larger blocks of text can be drawn by calling <b><code>beginText</code></b>, * followed by one or more calls to <b><code>drawText</code></b>, and closing with * a call to <b><code>endText</code></b>. * This method can be used to mix several styles, even different fonts, in a single * paragraph, and since 1.1 the methods <code>beginTextLink</code> and * <code>endTextLink</code> can turn portions of the text into HTML-like hyperlinks. * <pre> *    PDFPage page = pdf.newPage(PAGESIZE_A4);	// 595 x 842 points * *    // Create first style - 12pt black Helvetica *    PDFStyle style1 = new PDFStyle(); *    style1.setFillColor(java.awt.Color.black); *    style1.setFont(new StandardFont(StandardFont.HELVETICA), 12); * *    // Create second style - 12pt black Verdana (TrueType font) *    PDFStyle style2 = (PDFStyle)style1.clone(); *    PDFFont ver = new TrueTypeFont(new FileInputStream("verdana.ttf"), true, true); *    style2.setFont(ver, 12); * *    // Create an action to perform when the user clicks on the word "hyperlink". *    PDFAction action = PDFAction.goToURL(new java.net.URL("http://big.faceless.org")); * *    // Draw some text. Use the whole page, less a 100 point margin. *    page.beginText(100,100, page.getWidth()-100, page.getHeight()-100); * *    page.setStyle(style1); *    page.drawText("This text is in "); *    page.setStyle(style2); *    page.drawText("Verdana.\n"); * *    page.setStyle(style1); *    page.drawText("And this text is a "); *    page.beginTextLink(action, null); *    page.drawText("hyperlink"); *    page.endTextLink(); * *    page.endText(false); * </pre> * </p> * * <b>4. Drawing Images</b> * <p> * Bitmap images are drawn using the <b><code>drawImage</code></b> method. * <pre> *   PDFImage img = new PDFImage(new FileInputStream("mypicture.jpg")); *   page.drawImage(img, 100, 100, 200, 200); * </pre> * </p> * * <b>5. Transformations</b> * <p> * As well as adding graphic elements to the page, adjustments can be made to the * page itself. A page can be rotated, offset by an amount or scaled in the X and Y * directions by calling the <b><code>rotate</code></b>, * <b><code>translate</code></b> and <b><code>scale</code></b> methods. * These methods affect all operations on the page, like drawing lines or text, and * also cause any future transformations to be transformed. This can be confusing. * For example: * <pre> *   page.rotate(0,0,90); *   page.translate(0,100); * </pre> * This section of code first rotates the page 90 degrees clockwise around (0,0), then * translates the page by 100 points in the Y axis. Because the page has been rotated * 90 degrees, the <code>translate</code> actually has the effect of moving all future * operations 100 points <i>to the right</i>, rather than 100 points up the page. The * order that transformations are made in is consequently very important. * </p> * * <b>6. Save, Restore and Undo</b> * <p> * Three further operations simplify page layout, especially when using transformations. * The <b><code>save</code></b> and <b><code>restore</code></b> methods allow you to * backup and restore the state of a page, and since 1.1 the <b><code>undo</code></b> * method allows you to restore the page to the state before the last call to * <code>save</code>. It's often a good idea to save the page stage before applying any * transformations, so you can quickly get back to exactly the way it was before. * <pre> *   page.save();		// Save the page before we mess it up. *   page.translate(100,200); *   page.rotate(300,100,45); *   page.setStyle(weirdstyle); *     . *     . *   page.restore();		// Everything is now as it was. * </pre> * </p> * * <b>7. Clipping</b> * <p> * Similar to the <tt>drawRectangle</tt>, <tt>drawCircle</tt> etc. methods above, the * <tt>clipRectangle</tt>, <tt>clipRoundedRectangle</tt>, <tt>clipCircle</tt>, * <tt>clipEllipse</tt> and <tt>clipPolygon</tt> methods can be used to set the current * <i>clipping area</i> on the page. Any future graphics or text operations will only * take place inside that clipping area, which defaults to the entire page. For finer * control, a path can be drawn using the <tt>path</tt> methods demonstrated above, * and the <tt>pathClip</tt> method used to set the clipping area. * </p><p> * There is no way to enlarge the current clipping area, or to set a new clipping area * without reference to the current one. However, as the current clipping area is part * of the graphics state, it can (and should) be nested inside calls to <tt>save</tt> * and <tt>restore</tt> to limit its effect. * </p><p>Here's an example which draws an image on the page, clipped to a circle.</p> * <pre> *   page.save();		// Save the current clipping path - the whole page * *   PDFImage img = new PDFImage(new FileInputStream("mypicture.jpg")); *   page.clipEllipse(100,100,300,300); *   page.drawImage(img, 100, 100, 300, 300); * *   page.restore();		// Restore the previous clipping path * </pre> * * @see PDFStyle * @see PDFFont * @see PDFImage * @see PDF * @version $Revision: 1.11 $ * */public final class PDFPage extends PeeredObject{    final org.faceless.pdf2.PDFPage page;    private PDFStyle tempstyle;    private State state;    private Stack statestack;    private float translatex, translatey, scalex, scaley, canvaswidth, canvasheight;    private class State    {        float translatex, translatey, scalex, scaley;	public State()	{	    scalex=scaley=1;	}    }    /**     * Argument to <code>setFilter</code> to compress the page using the     * <code>java.util.zip.Deflater</code> filter (the default).     */    public static final int FILTER_FLATE = 0;    /**     * Argument to <code>setFilter</code> to not compress the page.     */    public static final int FILTER_NONE = 0;    /**     * Argument to <code>setCanvas</code> to measure the page in inches     */    public static final int INCHES=4;    /**     * Argument to <code>setCanvas</code> to measure the page in centimeters     */    public static final int CM=8;    /**     * Argument to <code>setCanvas</code> to measure the page in millimeters     */    public static final int MM=12;    /**     * Argument to <code>setCanvas</code> to measure the page in picas (1 pica=12 points)     */    public static final int PICAS=16;    /**     * Argument to <code>setCanvas</code> to measure the page in percent. Unlike     * the other measurements, this can result in changes to the aspect ratio.     * (10% of the page width is usually less than 10% of the page height).     */    public static final int PERCENT=20;    /**     * Argument to <code>setCanvas</code> to measure the page in points (the default)     */    public static final int POINTS=24;    /**     * Argument to <code>setCanvas</code> to measure the page from the bottom     */    public static final int PAGEBOTTOM=0;    /**     * Argument to <code>setCanvas</code> to measure the page from the top     */    public static final int PAGETOP=1;    /**     * Argument to <code>setCanvas</code> to measure the page from the left     */    public static final int PAGELEFT=0;    /**     * Argument to <code>setCanvas</code> to measure the page from the right     */    public static final int PAGERIGHT=2;    /**     * Barcode type for <code>drawBarCode</code> representing a "Code 39" barcode.     * This barcode can display digits, the 26 upper-case letters, the space character     * and the symbols '-', '+', '/', '.', '$' and '%'.     */    public static final int BARCODE39=0;    /**     * Barcode type for <code>drawBarCode</code> representing a "Code 39" barcode, with     * checksum. This barcode can display digits, the 26 capital letters, the space character     * and the symbols '-', '+', '/', '.', '$' and '%'. The checksum algorithm is described     * on <a target=_new href="http://www.adams1.com/pub/russadam/39code.html">this page</a>.     */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -