📄 pdfwriter.java
字号:
* There is only one direct content, multiple calls to this method * will always retrieve the same object. * @return the direct content */ public PdfContentByte getDirectContentUnder() { if (!open) throw new RuntimeException("The document is not open."); return directContentUnder; } /** * Resets all the direct contents to empty. * This happens when a new page is started. */ void resetContent() { directContent.reset(); directContentUnder.reset(); } // PDF body /* * A PDF file has 4 parts: a header, a body, a cross-reference table, and a trailer. * The body contains all the PDF objects that make up the PDF document. * Each element gets a reference (a set of numbers) and the byte position of * every object is stored in the cross-reference table. * Use these methods only if you know what you're doing. */ /** body of the PDF document */ protected PdfBody body; /** * Adds the local destinations to the body of the document. * @param dest the <CODE>HashMap</CODE> containing the destinations * @throws IOException on error */ void addLocalDestinations(TreeMap dest) throws IOException { for (Iterator i = dest.entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); String name = (String) entry.getKey(); Object obj[] = (Object[]) entry.getValue(); PdfDestination destination = (PdfDestination)obj[2]; if (obj[1] == null) obj[1] = getPdfIndirectReference(); if (destination == null) addToBody(new PdfString("invalid_" + name), (PdfIndirectReference)obj[1]); else addToBody(destination, (PdfIndirectReference)obj[1]); } } /** * Use this method to add a PDF object to the PDF body. * Use this method only if you know what you're doing! * @param object * @return a PdfIndirectObject * @throws IOException */ public PdfIndirectObject addToBody(PdfObject object) throws IOException { PdfIndirectObject iobj = body.add(object); return iobj; } /** * Use this method to add a PDF object to the PDF body. * Use this method only if you know what you're doing! * @param object * @param inObjStm * @return a PdfIndirectObject * @throws IOException */ public PdfIndirectObject addToBody(PdfObject object, boolean inObjStm) throws IOException { PdfIndirectObject iobj = body.add(object, inObjStm); return iobj; } /** * Use this method to add a PDF object to the PDF body. * Use this method only if you know what you're doing! * @param object * @param ref * @return a PdfIndirectObject * @throws IOException */ public PdfIndirectObject addToBody(PdfObject object, PdfIndirectReference ref) throws IOException { PdfIndirectObject iobj = body.add(object, ref); return iobj; } /** * Use this method to add a PDF object to the PDF body. * Use this method only if you know what you're doing! * @param object * @param ref * @param inObjStm * @return a PdfIndirectObject * @throws IOException */ public PdfIndirectObject addToBody(PdfObject object, PdfIndirectReference ref, boolean inObjStm) throws IOException { PdfIndirectObject iobj = body.add(object, ref, inObjStm); return iobj; } /** * Use this method to add a PDF object to the PDF body. * Use this method only if you know what you're doing! * @param object * @param refNumber * @return a PdfIndirectObject * @throws IOException */ public PdfIndirectObject addToBody(PdfObject object, int refNumber) throws IOException { PdfIndirectObject iobj = body.add(object, refNumber); return iobj; } /** * Use this method to add a PDF object to the PDF body. * Use this method only if you know what you're doing! * @param object * @param refNumber * @param inObjStm * @return a PdfIndirectObject * @throws IOException */ public PdfIndirectObject addToBody(PdfObject object, int refNumber, boolean inObjStm) throws IOException { PdfIndirectObject iobj = body.add(object, refNumber, inObjStm); return iobj; } /** * Use this to get an <CODE>PdfIndirectReference</CODE> for an object that * will be created in the future. * Use this method only if you know what you're doing! * @return the <CODE>PdfIndirectReference</CODE> */ public PdfIndirectReference getPdfIndirectReference() { return body.getPdfIndirectReference(); } int getIndirectReferenceNumber() { return body.getIndirectReferenceNumber(); } /** * Returns the outputStreamCounter. * @return the outputStreamCounter */ OutputStreamCounter getOs() { return os; }// PDF Catalog /* * The Catalog is also called the root object of the document. * Whereas the Cross-Reference maps the objects number with the * byte offset so that the viewer can find the objects, the * Catalog tells the viewer the numbers of the objects needed * to render the document. */ protected PdfDictionary getCatalog(PdfIndirectReference rootObj) { PdfDictionary catalog = pdf.getCatalog(rootObj); // [F12] tagged PDF if (tagged) { try { getStructureTreeRoot().buildTree(); } catch (Exception e) { throw new ExceptionConverter(e); } catalog.put(PdfName.STRUCTTREEROOT, structureTreeRoot.getReference()); PdfDictionary mi = new PdfDictionary(); mi.put(PdfName.MARKED, PdfBoolean.PDFTRUE); if (userProperties) mi.put(PdfName.USERPROPERTIES, PdfBoolean.PDFTRUE); catalog.put(PdfName.MARKINFO, mi); } // [F13] OCG if (!documentOCG.isEmpty()) { fillOCProperties(false); catalog.put(PdfName.OCPROPERTIES, OCProperties); } return catalog; } /** Holds value of property extraCatalog this is used for Output Intents. */ protected PdfDictionary extraCatalog; /** * Sets extra keys to the catalog. * @return the catalog to change */ public PdfDictionary getExtraCatalog() { if (extraCatalog == null) extraCatalog = new PdfDictionary(); return this.extraCatalog; } // PdfPages /* * The page root keeps the complete page tree of the document. * There's an entry in the Catalog that refers to the root * of the page tree, the page tree contains the references * to pages and other page trees. */ /** The root of the page tree. */ protected PdfPages root = new PdfPages(this); /** The PdfIndirectReference to the pages. */ protected ArrayList pageReferences = new ArrayList(); /** The current page number. */ protected int currentPageNumber = 1; /** * Use this method to make sure the page tree has a linear structure * (every leave is attached directly to the root). * Use this method to allow page reordering with method reorderPages. */ public void setLinearPageMode() { root.setLinearMode(null); } /** * Use this method to reorder the pages in the document. * A <CODE>null</CODE> argument value only returns the number of pages to process. * It is advisable to issue a <CODE>Document.newPage()</CODE> before using this method. * @return the total number of pages * @param order an array with the new page sequence. It must have the * same size as the number of pages. * @throws DocumentException if all the pages are not present in the array */ public int reorderPages(int order[]) throws DocumentException { return root.reorderPages(order); } /** * Use this method to get a reference to a page existing or not. * If the page does not exist yet the reference will be created * in advance. If on closing the document, a page number greater * than the total number of pages was requested, an exception * is thrown. * @param page the page number. The first page is 1 * @return the reference to the page */ public PdfIndirectReference getPageReference(int page) { --page; if (page < 0) throw new IndexOutOfBoundsException("The page numbers start at 1."); PdfIndirectReference ref; if (page < pageReferences.size()) { ref = (PdfIndirectReference)pageReferences.get(page); if (ref == null) { ref = body.getPdfIndirectReference(); pageReferences.set(page, ref); } } else { int empty = page - pageReferences.size(); for (int k = 0; k < empty; ++k) pageReferences.add(null); ref = body.getPdfIndirectReference(); pageReferences.add(ref); } return ref; } /** * Gets the pagenumber of this document. * This number can be different from the real pagenumber, * if you have (re)set the page number previously. * @return a page number */ public int getPageNumber() { return pdf.getPageNumber(); } PdfIndirectReference getCurrentPage() { return getPageReference(currentPageNumber); } public int getCurrentPageNumber() { return currentPageNumber; } /** * Adds some <CODE>PdfContents</CODE> to this Writer. * <P> * The document has to be open before you can begin to add content * to the body of the document. * * @return a <CODE>PdfIndirectReference</CODE> * @param page the <CODE>PdfPage</CODE> to add * @param contents the <CODE>PdfContents</CODE> of the page * @throws PdfException on error */ PdfIndirectReference add(PdfPage page, PdfContents contents) throws PdfException { if (!open) { throw new PdfException("The document isn't open."); } PdfIndirectObject object; try { object = addToBody(contents); } catch(IOException ioe) { throw new ExceptionConverter(ioe); } page.add(object.getIndirectReference()); // [U5] if (group != null) { page.put(PdfName.GROUP, group); group = null; } else if (rgbTransparencyBlending) { PdfDictionary pp = new PdfDictionary(); pp.put(PdfName.TYPE, PdfName.GROUP); pp.put(PdfName.S, PdfName.TRANSPARENCY); pp.put(PdfName.CS, PdfName.DEVICERGB); page.put(PdfName.GROUP, pp); } root.addPage(page); currentPageNumber++; return null; } // page events /* * Page events are specific for iText, not for PDF. * Upon specific events (for instance when a page starts * or ends), the corresponding method in the page event * implementation that is added to the writer is invoked. */ /** The <CODE>PdfPageEvent</CODE> for this document. */ private PdfPageEvent pageEvent; /** * Sets the <CODE>PdfPageEvent</CODE> for this document. * @param event the <CODE>PdfPageEvent</CODE> for this document
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -