📄 pdfdocument.java
字号:
private boolean isParagraph = true; // Horizontal line /** The line that is currently being written. */ private PdfLine line = null; /** This represents the current indentation of the PDF Elements on the left side. */ private float indentLeft = 0; /** This represents the current indentation of the PDF Elements on the right side. */ private float indentRight = 0; /** This represents the current indentation of the PDF Elements on the left side. */ private float listIndentLeft = 0; /** This represents the current alignment of the PDF Elements. */ private int alignment = Element.ALIGN_LEFT; // Vertical lines /** This is the PdfContentByte object, containing the text. */ private PdfContentByte text; /** This is the PdfContentByte object, containing the borders and other Graphics. */ private PdfContentByte graphics; /** The lines that are written until now. */ private ArrayList lines = new ArrayList(); /** This represents the leading of the lines. */ private float leading = 0; /** This is the current height of the document. */ private float currentHeight = 0; /** This represents the current indentation of the PDF Elements on the top side. */ private float indentTop = 0; /** This represents the current indentation of the PDF Elements on the bottom side. */ private float indentBottom = 0; /** This checks if the page is empty. */ private boolean pageEmpty = true; // resources /** This is the size of the current Page. */ protected Rectangle thisPageSize = null; /** This is the FontDictionary of the current Page. */ protected PdfFontDictionary fontDictionary; /** This is the XObjectDictionary of the current Page. */ protected PdfXObjectDictionary xObjectDictionary; // images /** This is the list with all the images in the document. */ private HashMap images = new HashMap(); /** This is the image that could not be shown on a previous page. */ private Image imageWait = null; /** This is the position where the image ends. */ private float imageEnd = -1; /** This is the indentation caused by an image on the left. */ private float imageIndentLeft = 0; /** This is the indentation caused by an image on the right. */ private float imageIndentRight = 0; // annotations and outlines /** This is the array containing the references to the annotations. */ private PdfArray annotations; /** This is the <CODE>ArrayList</CODE> with the outlines of the document. */ private ArrayList outlines; /** This is the current <CODE>PdfOutline</CODE> in the hierarchy of outlines. */ private PdfOutline currentOutline; /** The current active <CODE>PdfAction</CODE> when processing an <CODE>Anchor</CODE>. */ private PdfAction currentAction = null; /** * Stores the destinations keyed by name. Value is * <CODE>Object[]{PdfAction,PdfIndirectReference,PdfDestintion}</CODE>. */ private TreeMap localDestinations = new TreeMap(new StringCompare()); /** Stores the destinations for the current page. */ private HashMap localPageDestinations = new HashMap(); /** these are the viewerpreferences of the document */ private int viewerPreferences = 0; private String openActionName; private PdfAction openActionAction; private PdfPageLabels pageLabels; //add by Jin-Hsia Yang private boolean isNewpage = false; private boolean isParagraphE = false; private float paraIndent = 0; //end add by Jin-Hsia Yang // constructors /** * Constructs a new PDF document. * @throws DocumentException on error */ public PdfDocument() throws DocumentException { super(); addProducer(); addCreationDate(); } // listener methods /** * Adds a <CODE>PdfWriter</CODE> to the <CODE>PdfDocument</CODE>. * * @param writer the <CODE>PdfWriter</CODE> that writes everything * what is added to this document to an outputstream. * @throws DocumentException on error */ public final void addWriter(PdfWriter writer) throws DocumentException { if (this.writer == null) { this.writer = writer; return; } throw new DocumentException("You can only add a writer to a PdfDocument once."); } /** * Sets the pagesize. * * @param pageSize the new pagesize * @return <CODE>true</CODE> if the page size was set */ public boolean setPageSize(Rectangle pageSize) { if (writer != null && writer.isPaused()) { return false; } this.pageSize = pageSize; return true; } /** * Changes the header of this document. * * @param header the new header */ public void setHeader(HeaderFooter header) { if (writer != null && writer.isPaused()) { return; } super.setHeader(header); } /** * Resets the header of this document. */ public void resetHeader() { if (writer != null && writer.isPaused()) { return; } super.resetHeader(); } /** * Changes the footer of this document. * * @param footer the new footer */ public void setFooter(HeaderFooter footer) { if (writer != null && writer.isPaused()) { return; } super.setFooter(footer); } /** * Resets the footer of this document. */ public void resetFooter() { if (writer != null && writer.isPaused()) { return; } super.resetFooter(); } /** * Sets the page number to 0. */ public void resetPageCount() { if (writer != null && writer.isPaused()) { return; } super.resetPageCount(); } /** * Sets the page number. * * @param pageN the new page number */ public void setPageCount(int pageN) { if (writer != null && writer.isPaused()) { return; } super.setPageCount(pageN); } /** * Sets the <CODE>Watermark</CODE>. * * @param watermark the watermark to add * @return <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not. */ public boolean add(Watermark watermark) { if (writer != null && writer.isPaused()) { return false; } this.watermark = watermark; return true; } /** * Removes the <CODE>Watermark</CODE>. */ public void removeWatermark() { if (writer != null && writer.isPaused()) { return; } this.watermark = null; } /** * Sets the margins. * * @param marginLeft the margin on the left * @param marginRight the margin on the right * @param marginTop the margin on the top * @param marginBottom the margin on the bottom * @return a <CODE>boolean</CODE> */ public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) { if (writer != null && writer.isPaused()) { return false; } this.marginLeft = marginLeft; this.marginRight = marginRight; this.marginTop = marginTop; this.marginBottom = marginBottom; return true; } /** * Makes a new page and sends it to the <CODE>PdfWriter</CODE>. * * @return a <CODE>boolean</CODE> * @throws DocumentException on error */ public boolean newPage() throws DocumentException { //add by Jin-Hsia Yang isNewpage = true; //end add by Jin-Hsia Yang if (writer.getDirectContent().size() == 0 && writer.getDirectContentUnder().size() == 0 && (pageEmpty || (writer != null && writer.isPaused()))) { return false; } PdfPageEvent pageEvent = writer.getPageEvent(); if (pageEvent != null) pageEvent.onEndPage(writer, this); //Added to inform any listeners that we are moving to a new page (added by David Freels) super.newPage(); // we flush the arraylist with recently written lines flushLines(); // we assemble the resources of this pages PdfResources resources = new PdfResources(); int procset = PdfProcSet.PDF; if (fontDictionary.containsFont()) { resources.add(fontDictionary); procset |= PdfProcSet.TEXT; } if (xObjectDictionary.containsXObject()) { resources.add(xObjectDictionary); procset |= PdfProcSet.IMAGEC; } resources.add(new PdfProcSet(procset)); // we make a new page and add it to the document PdfPage page = new PdfPage(new PdfRectangle(thisPageSize), resources); // we add the annotations if (annotations.size() > 0) { page.put(PdfName.ANNOTS, annotations); } if (!open || close) { throw new PdfException("The document isn't open."); } text.endText(); PdfIndirectReference pageReference = writer.add(page, new PdfContents(writer.getDirectContentUnder(), graphics, text, writer.getDirectContent())); // we update the outlines for (Iterator i = outlines.iterator(); i.hasNext(); ) { PdfOutline outline = (PdfOutline) i.next(); outline.setDestinationPage(pageReference); } // we update the local destinations for (Iterator i = localPageDestinations.keySet().iterator(); i.hasNext();) { PdfDestination destination = (PdfDestination)i.next(); destination.addPage(pageReference); } localPageDestinations.clear(); // we initialize the new page initPage(); //add by Jin-Hsia Yang isNewpage = false; //end add by Jin-Hsia Yang return true; } // methods to open and close a document
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -