📄 pdfdocument.java
字号:
/* * $Id: PdfDocument.java,v 1.77 2001/12/10 15:37:44 blowagie Exp $ * $Name: $ * * Copyright 1999, 2000, 2001 by Bruno Lowagie. * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published * by the Free Software Foundation; either version 2 of the License, or any * later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * You should have received a copy of the GNU Library General Public License along * with this library; if not, write to the Free Foundation, Inc., 59 Temple Place, * Suite 330, Boston, MA 02111-1307 USA. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ * * ir-arch Bruno Lowagie, * Adolf Baeyensstraat 121 * 9040 Sint-Amandsberg * BELGIUM * tel. +32 (0)9 228.10.97 * bruno@lowagie.com * */package com.lowagie.text.pdf;import com.lowagie.text.StringCompare;import com.lowagie.text.Anchor;import com.lowagie.text.Annotation;import java.util.Comparator;import com.lowagie.text.DocListener;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.DocWriter;import com.lowagie.text.Element;import com.lowagie.text.Chunk;import com.lowagie.text.Graphic;import com.lowagie.text.Header;import com.lowagie.text.HeaderFooter;import com.lowagie.text.Image;import com.lowagie.text.ImgWMF;import com.lowagie.text.List;import com.lowagie.text.ListItem;import com.lowagie.text.Meta;import com.lowagie.text.Phrase;import com.lowagie.text.Paragraph;import com.lowagie.text.Rectangle;import com.lowagie.text.Section;import com.lowagie.text.Table;import com.lowagie.text.Watermark;import java.awt.Color;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;import java.util.TreeMap;import java.util.Iterator;/** * <CODE>PdfDocument</CODE> is the class that is used by <CODE>PdfWriter</CODE> * to translate a <CODE>Document</CODE> into a PDF with different pages. * <P> * A <CODE>PdfDocument</CODE> always listens to a <CODE>Document</CODE> * and adds the Pdf representation of every <CODE>Element</CODE> that is * added to the <CODE>Document</CODE>. * * @see com.lowagie.text.Document * @see com.lowagie.text.DocListener * @see PdfWriter * * @author bruno@lowagie.com */class PdfDocument extends Document implements DocListener { /** * <CODE>PdfInfo</CODE> is the PDF InfoDictionary. * <P> * A document's trailer may contain a reference to an Info dictionary that provides information * about the document. This optional dictionary may contain one or more keys, whose values * should be strings.<BR> * This object is described in the 'Portable Document Format Reference Manual version 1.3' * section 6.10 (page 120-121) * * @author bruno@lowagie.com */ public class PdfInfo extends PdfDictionary { // constructors /** * Construct a <CODE>PdfInfo</CODE>-object. */ PdfInfo() { super(); addProducer(); addCreationDate(); } /** * Constructs a <CODE>PdfInfo</CODE>-object. * * @param author name of the author of the document * @param title title of the document * @param subject subject of the document */ PdfInfo(String author, String title, String subject) { this(); addTitle(title); addSubject(subject); addAuthor(author); } /** * Adds the title of the document. * * @param title the title of the document */ void addTitle(String title) { put(PdfName.TITLE, new PdfString(title, PdfObject.TEXT_UNICODE)); } /** * Adds the subject to the document. * * @param subject the subject of the document */ void addSubject(String subject) { put(PdfName.SUBJECT, new PdfString(subject, PdfObject.TEXT_UNICODE)); } /** * Adds some keywords to the document. * * @param keywords the keywords of the document */ void addKeywords(String keywords) { put(PdfName.KEYWORDS, new PdfString(keywords, PdfObject.TEXT_UNICODE)); } /** * Adds the name of the author to the document. * * @param author the name of the author */ void addAuthor(String author) { put(PdfName.AUTHOR, new PdfString(author, PdfObject.TEXT_UNICODE)); } /** * Adds the name of the creator to the document. * * @param creator the name of the creator */ void addCreator(String creator) { put(PdfName.CREATOR, new PdfString(creator, PdfObject.TEXT_UNICODE)); } /** * Adds the name of the producer to the document. */ void addProducer() { // This line may only be changed by Bruno Lowagie or Paulo Soares put(PdfName.PRODUCER, new PdfString("iText by lowagie.com (r0.73)", PdfObject.TEXT_UNICODE)); // Do not edit the line above! } /** * Adds the date of creation to the document. */ void addCreationDate() { put(PdfName.CREATIONDATE, new PdfDate()); } } /** * <CODE>PdfCatalog</CODE> is the PDF Catalog-object. * <P> * The Catalog is a dictionary that is the root node of the document. It contains a reference * to the tree of pages contained in the document, a reference to the tree of objects representing * the document's outline, a reference to the document's article threads, and the list of named * destinations. In addition, the Catalog indicates whether the document's outline or thumbnail * page images should be displayed automatically when the document is viewed and wether some location * other than the first page should be shown when the document is opened.<BR> * In this class however, only the reference to the tree of pages is implemented.<BR> * This object is described in the 'Portable Document Format Reference Manual version 1.3' * section 6.2 (page 67-71) * * @author bruno@lowagie.com */ class PdfCatalog extends PdfDictionary { // constructors /** * Constructs a <CODE>PdfCatalog</CODE>. * * @param pages an indirect reference to the root of the document's Pages tree. */ PdfCatalog(PdfIndirectReference pages) { super(CATALOG); put(PdfName.PAGES, pages); } /** * Constructs a <CODE>PdfCatalog</CODE>. * * @param pages an indirect reference to the root of the document's Pages tree. * @param outlines an indirect reference to the outline tree. */ PdfCatalog(PdfIndirectReference pages, PdfIndirectReference outlines) { super(CATALOG); put(PdfName.PAGES, pages); put(PdfName.PAGEMODE, PdfName.USEOUTLINES); put(PdfName.OUTLINES, outlines); } /** * Adds the names of the named destinations to the catalog. * @param localDestinations the local destinations */ void addNames(TreeMap localDestinations) { if (localDestinations.size() == 0) return; PdfArray ar = new PdfArray(); for (Iterator i = localDestinations.keySet().iterator(); i.hasNext();) { String name = (String)i.next(); Object obj[] = (Object[])localDestinations.get(name); PdfIndirectReference ref = (PdfIndirectReference)obj[1]; ar.add(new PdfString(name)); ar.add(ref); } PdfArray limits = new PdfArray(); limits.add((PdfString)ar.getArrayList().get(0)); limits.add((PdfString)ar.getArrayList().get(ar.size() - 2)); PdfDictionary dests = new PdfDictionary(); PdfDictionary names = new PdfDictionary(); dests.put(PdfName.LIMITS, limits); dests.put(PdfName.NAMES, ar); names.put(PdfName.DESTS, dests); put(PdfName.NAMES, names); } /** Sets the viewer preferences as the sum of several constants. * @param preferences the viewer preferences * @see PdfWriter#setViewerPreferences */ void setViewerPreferences(int preferences) { if ((preferences & PdfWriter.PageLayoutSinglePage) != 0) put(PdfName.PAGELAYOUT, PdfName.SINGLEPAGE); else if ((preferences & PdfWriter.PageLayoutOneColumn) != 0) put(PdfName.PAGELAYOUT, PdfName.ONECOLUMN); else if ((preferences & PdfWriter.PageLayoutTwoColumnLeft) != 0) put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNLEFT); else if ((preferences & PdfWriter.PageLayoutTwoColumnRight) != 0) put(PdfName.PAGELAYOUT, PdfName.TWOCOLUMNRIGHT); if ((preferences & PdfWriter.PageModeUseNone) != 0) put(PdfName.PAGEMODE, PdfName.USENONE); else if ((preferences & PdfWriter.PageModeUseOutlines) != 0) put(PdfName.PAGEMODE, PdfName.USEOUTLINES); else if ((preferences & PdfWriter.PageModeUseThumbs) != 0) put(PdfName.PAGEMODE, PdfName.USETHUMBS); else if ((preferences & PdfWriter.PageModeFullScreen) != 0) put(PdfName.PAGEMODE, PdfName.FULLSCREEN); if ((preferences & PdfWriter.ViewerPreferencesMask) == 0) return; PdfDictionary vp = new PdfDictionary(); if ((preferences & PdfWriter.HideToolbar) != 0) vp.put(PdfName.HIDETOOLBAR, PdfBoolean.PDFTRUE); if ((preferences & PdfWriter.HideMenubar) != 0) vp.put(PdfName.HIDEMENUBAR, PdfBoolean.PDFTRUE); if ((preferences & PdfWriter.HideWindowUI) != 0) vp.put(PdfName.HIDEWINDOWUI, PdfBoolean.PDFTRUE); if ((preferences & PdfWriter.FitWindow) != 0) vp.put(PdfName.FITWINDOW, PdfBoolean.PDFTRUE); if ((preferences & PdfWriter.CenterWindow) != 0) vp.put(PdfName.CENTERWINDOW, PdfBoolean.PDFTRUE); if ((preferences & PdfWriter.NonFullScreenPageModeUseNone) != 0) vp.put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USENONE); else if ((preferences & PdfWriter.NonFullScreenPageModeUseOutlines) != 0) vp.put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USEOUTLINES); else if ((preferences & PdfWriter.NonFullScreenPageModeUseThumbs) != 0) vp.put(PdfName.NONFULLSCREENPAGEMODE, PdfName.USETHUMBS); if ((preferences & PdfWriter.DirectionL2R) != 0) vp.put(PdfName.DIRECTION, PdfName.L2R); else if ((preferences & PdfWriter.DirectionR2L) != 0) vp.put(PdfName.DIRECTION, PdfName.R2L); put(PdfName.VIEWERPREFERENCES, vp); } void setOpenAction(PdfAction action) { put(PdfName.OPENACTION, action); } void setPageLabels(PdfPageLabels pageLabels) { put(PdfName.PAGELABELS, pageLabels.getDictionary()); } } // membervariables /** * The ratio between the extra word spacing and the extra character spacing.<BR> * Extra word spacing will grow <CODE>ratio</CODE> times more than extra character spacing. */ static final float ratio = 2.5f; /** The characters to be applied the hanging ponctuation. */ static final String hangingPunctuation = ".,;:'"; /** The <CODE>PdfWriter</CODE>. */ private PdfWriter writer; /** some meta information about the Document. */ private PdfInfo info = new PdfInfo(); /** Signals that OnOpenDocument should be called. */ private boolean firstPageEvent = true; /** Signals that onParagraph is valid. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -