📄 pdf.java
字号:
// $Id: PDF.java,v 1.9 2003/12/02 10:37:53 mike Exp $package org.faceless.pdf;import java.io.*;import java.util.*;/** * <p>A <code>PDF</code> describes a single document in Adobe's Portable * Document Format. It is the highest-level object in the package.</p> * * <p> * The life-cycle of a PDF generally consists of being created, adding * new pages, optionally adding information about the document structure * (like bookmarks), and finally rendering to an <code>OutputStream</code>. * </p><p> * This class only deals with the structure of the document. To actually * create some content see the {@link PDFPage} class. * </p> * * Here's the ubiquitous example: * <pre> * import org.faceless.pdf.*; * * // Create a new PDF * PDF p = new PDF(); * * // Create a new page * PDFPage page = p.newPage(PDF.PAGESIZE_A4); * * // Create a new "style" to write in - Black 24pt Times Roman. * PDFStyle mystyle = new PDFStyle(); * mystyle.setFont(new StandardFont(StandardFont.TIMES), 24); * mystyle.setFillColor(java.awt.Color.black); * * // Put something on the page * page.setStyle(mystyle); * page.drawText("Hello, PDF-viewing World!", 100, 100); * * // Automatically go to this page when the document is opened. * p.setOpenAction(PDFAction.goTo(page)); * * // Add some document info * p.setInfo("Author", "Joe Bloggs"); * p.setInfo("Title", "My Document"); * * // Add a bookmark * java.util.List bookmarks = p.getBookmarks(); * bookmarks.add(new PDFBookmark("Hello World page", PDFAction.goTo(page))); * * // Allow the user to print the document, but nothing else * p.setAccessLevel(PDF.ACCESS_PRINT); * * // Write the document to a file * p.render(new FileOutputStream("test.pdf")); * </pre> * * @see PDFPage * @see PDFReader * @see PDFBookmark * @see PDFStyle * @version $Revision: 1.9 $ * */public class PDF{ private org.faceless.pdf2.PDF pdf; /** * The package version number */ public static final String VERSION="BFOPDF $Revision: 1.9 $"; /** * Argument to <code>setLayout</code> to display each page separately (the default) */ public static final String SINGLEPAGE = "SinglePage"; /** * Argument to <code>setLayout</code> to display the pages in a single column. */ public static final String ONECOLUMN = "OneColumn"; /** * Argument to <code>setLayout</code> to display the pages in two columns, with * odd numbered pages to the left. */ public static final String TWOCOLUMNLEFT = "TwoColumnLeft"; /** * Argument to <code>setLayout</code> to display the pages in two columns, with * odd numbered pages to the right. */ public static final String TWOCOLUMNRIGHT = "TwoColumnRight"; /** * Argument to <code>newPage</code> to create a new A3 portrait page */ public static final String PAGESIZE_A3 = "842x1190"; /** * Argument to <code>newPage</code> to create a new A3 landscape page */ public static final String PAGESIZE_A3_LANDSCAPE = "1190x842"; /** * Argument to <code>newPage</code> to create a new A4 portrait page */ public static final String PAGESIZE_A4 = "595x842"; /** * Argument to <code>newPage</code> to create a new A4 landscape page */ public static final String PAGESIZE_A4_LANDSCAPE = "842x595"; /** * Argument to <code>newPage</code> to create a new A5 portrait page */ public static final String PAGESIZE_A5 = "421x595"; /** * Argument to <code>newPage</code> to create a new A5 landscape page */ public static final String PAGESIZE_A5_LANDSCAPE = "595x421"; /** * Argument to <code>newPage</code> to create a new A6 portrait page */ public static final String PAGESIZE_A6 = "297x421"; /** * Argument to <code>newPage</code> to create a new A6 landscape page */ public static final String PAGESIZE_A6_LANDSCAPE = "421x297"; /** * Argument to <code>newPage</code> to create a new "letter" portrait page */ public static final String PAGESIZE_LETTER = "612x792"; /** * Argument to <code>newPage</code> to create a new "letter" landscape page */ public static final String PAGESIZE_LETTER_LANDSCAPE = "792x612"; /** * Argument to <code>newPage</code> to create a new "legal" portrait page */ public static final String PAGESIZE_LEGAL = "612x1008"; /** * Argument to <code>newPage</code> to create a new "legal" landscape page */ public static final String PAGESIZE_LEGAL_LANDSCAPE = "1008x612"; /** * Argument to <code>setAccessLevel</code> to allow the user full access * to the document. * @since 1.1.12 */ public static final int ACCESS_ALL = 0xFFFFFFFF; /** * Argument to <code>setAccessLevel</code> to allow the user to print * the document. If 128-bit encryption is used, this flag on it's own * will only allow low-quality printing - you should set the * {@link #ACCESS_HQPRINT} flag too for high quality printing. If * 40-bit encryption is used, printing is always high-quality. */ public static final int ACCESS_PRINT = 4; /** * Argument to <code>setAccessLevel</code> to allow the user to edit * the document with software like Adobe Acrobat™ */ public static final int ACCESS_EDIT = 8; /** * Argument to <code>setAccessLevel</code> to allow the user to copy * and paste text and images from the document */ public static final int ACCESS_COPY = 16; /** * Argument to <code>setAccessLevel</code> to allow the user to add * annotations to the document and create or edit interactive form * fields (including signature fields). */ public static final int ACCESS_ANNOTATE = 32; /** * Argument to <code>setAccessLevel</code> to allow the user to fill * in existing interactive form fields (including signature fields), * even if ACCESS_ANNOTATE is not set. (128 bit encryption only) * @since 1.1.12 */ public static final int ACCESS_FORMS = 256; /** * Argument to <code>setAccessLevel</code> to allow the user to * extract text and graphics (in support of accessibility for disabled * users or for other purposes). (128 bit encryption only) * @since 1.1.12 */ public static final int ACCESS_EXTRACT = 512; /** * Argument to <code>setAccessLevel</code> to allow the user to * assemble the document (insert, rotate or delete pages and create * bookmarks or thumbnail images), even if ACCESS_EDIT is not set. * (128 bit encryption only) * @since 1.1.12 */ public static final int ACCESS_ASSEMBLE = 1024; /** * Argument to <code>setAccessLevel</code> to allow the user to * print the document to a representation from which a faithful digital * copy of the PDF content could be generated. When this option is not set * and ACCESS_PRINT is set, printing is limited to a low-level * representation of the appearance, possibly of degraded quality. * (128 bit encryption only) * @since 1.1.12 */ public static final int ACCESS_HQPRINT = 2048; /** * Argument to <code>setEncryptionAlgorithm</code> to * turn off encryption for the document. This is * the default. * @since 1.1.12 */ public static final int ENCRYPT_NONE = 0; /** * Argument to <code>setEncryptionAlgorithm</code> to * set the encryption to the standard 40 bit encryption * algorithm used by Acrobat 3.x and later. * @since 1.1.12 */ public static final int ENCRYPT_40BIT = 40; /** * Argument to <code>setEncryptionAlgorithm</code> to * set the encryption to the 128 bit encryption * algorithm added in Acrobat 5.x. Documents encrypted * with this algorithm cannot be opened by earlier * versions of Acrobat. * @since 1.1.12 */ public static final int ENCRYPT_128BIT = 128; public PDF(org.faceless.pdf2.PDF pdf) { this.pdf=pdf; } /** * Create a new PDF document. */ public PDF() { pdf = new org.faceless.pdf2.PDF(); } /** * Create a PDF from the specified PDFReader. The <tt>PDFReader</tt> class * is available as part of the "Extended Edition" of the PDF library, and is * included with this package. If the document contains multiple revisions, * the latest revision is loaded. * @since 1.1.12 */ public PDF(PDFReader reader) { pdf = new org.faceless.pdf2.PDF(reader.reader); } /** * Create a PDF from the specified PDFReader, using the specified revision of * the document. The <tt>PDFReader</tt> class is available as part of the * "Extended Edition" of the PDF library, and is included with this package. * The revision number must be between 0 and * {@link PDFReader#getNumberOfRevisions}, otherwise an * <code>IllegalArgumentException</code> is thrown. * @param reader the <code>PDFReader</code> to load the document from * @param revision the revision of the document to load - betwween * <code>reader.getNumberOfRevisions()-1</code> to load the latest revision, or * <code>0</code> to load the first. * @since 1.2.1 */ public PDF(PDFReader reader, int revision) { pdf = new org.faceless.pdf2.PDF(reader.reader, revision); } /** * Create a new page with the specified pagesize. This version is * usually called to set the page to a standard size, like * {@link #PAGESIZE_A4} * @param pagesize The size of the page as a String of the format "WxH", * showing width and height in points. */ public PDFPage newPage(String pagesize) { return (PDFPage)PeeredObject.getPeer(pdf.newPage(pagesize)); } /** * Create a new page in the specified size. * @param width The width of the page, in points * @param height The height of the page, in points */ public PDFPage newPage(float width, float height) { return (PDFPage)PeeredObject.getPeer(pdf.newPage((int)width,(int)height)); } /** * Return the page returned from the last call to <code>newPage</code> * @return the last page returned from the {@link #newPage} method, or * <tt>null</tt> if that method hasn't been called yet. */ public PDFPage getLastPage() { return getPage(getNumberOfPages()-1); } /** * Return the specified page, where <code>0 <= page < numpages</code>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -