📄 pdfwriter.java
字号:
/** * Sets the encryption options for this document. The userPassword and the * ownerPassword can be null or have zero length. In this case the ownerPassword * is replaced by a random string. The open permissions for the document can be * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations, * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting. * The permissions can be combined by ORing them. * @param strength true for 128 bit key length. false for 40 bit key length * @param userPassword the user password. Can be null or empty * @param ownerPassword the owner password. Can be null or empty * @param permissions the user permissions * @throws DocumentException if the document is already open */ public void setEncryption(boolean strength, String userPassword, String ownerPassword, int permissions) throws DocumentException { setEncryption(getISOBytes(userPassword), getISOBytes(ownerPassword), permissions, strength); } PdfIndirectObject addToBody(PdfObject object) throws IOException { PdfIndirectObject iobj = body.add(object); iobj.writeTo(os); return iobj; } PdfIndirectObject addToBody(PdfObject object, PdfIndirectReference ref) throws IOException { PdfIndirectObject iobj = body.add(object, ref); iobj.writeTo(os); return iobj; } PdfIndirectObject addToBody(PdfObject object, int refNumber) throws IOException { PdfIndirectObject iobj = body.add(object, refNumber); iobj.writeTo(os); return iobj; } /** When the document opens it will jump to the destination with * this name. * @param name the name of the destination to jump to */ public void setOpenAction(String name) { pdf.setOpenAction(name); } /** Additional-actions defining the actions to be taken in * response to various trigger events affecting the document * as a whole. The actions types allowed are: <CODE>DOCUMENT_CLOSE</CODE>, * <CODE>WILL_SAVE</CODE>, <CODE>DID_SAVE</CODE>, <CODE>WILL_PRINT</CODE> * and <CODE>DID_PRINT</CODE>. * * @param actionType the action type * @param action the action to execute in response to the trigger * @throws PdfException on invalid action type */ public void setAdditionalAction(PdfName actionType, PdfAction action) throws PdfException { if (!(actionType.equals(DOCUMENT_CLOSE) || actionType.equals(WILL_SAVE) || actionType.equals(DID_SAVE) || actionType.equals(WILL_PRINT) || actionType.equals(DID_PRINT))) { throw new PdfException("Invalid additional action type: " + actionType.toString()); } pdf.addAdditionalAction(actionType, action); } /** When the document opens this <CODE>action</CODE> will be * invoked. * @param action the action to be invoked */ public void setOpenAction(PdfAction action) { pdf.setOpenAction(action); } /** Sets the page labels * @param pageLabels the page labels */ public void setPageLabels(PdfPageLabels pageLabels) { pdf.setPageLabels(pageLabels); } PdfEncryption getEncryption() { return crypto; } RandomAccessFileOrArray getReaderFile(PdfReader reader) { return currentPdfReaderInstance.getReaderFile(); } int getNewObjectNumber(PdfReader reader, int number, int generation) { return currentPdfReaderInstance.getNewObjectNumber(number, generation); } /** Gets a page from other PDF document. The page can be used as * any other PdfTemplate. Note that calling this method more than * once with the same parameters will retrieve the same object. * @param reader the PDF document where the page is * @param pageNumber the page number. The first page is 1 * @return the template representing the imported page */ public PdfImportedPage getImportedPage(PdfReader reader, int pageNumber) { PdfReaderInstance inst = (PdfReaderInstance)importedPages.get(reader); if (inst == null) { inst = reader.getPdfReaderInstance(this); importedPages.put(reader, inst); } return inst.getImportedPage(pageNumber); } /** Adds a JavaScript action at the document level. When the document * opens all this JavaScript runs. * @param js The JavaScrip action */ public void addJavaScript(PdfAction js) { pdf.addJavaScript(js); } /** Adds a JavaScript action at the document level. When the document * opens all this JavaScript runs. * @param code the JavaScript code * @param unicode select JavaScript unicode. Note that the internal * Acrobat JavaScript engine does not support unicode, * so this may or may not work for you */ public void addJavaScript(String code, boolean unicode) { addJavaScript(PdfAction.javaScript(code, this, unicode)); } /** Adds a JavaScript action at the document level. When the document * opens all this JavaScript runs. * @param code the JavaScript code */ public void addJavaScript(String code) { addJavaScript(code, false); } /** Sets the crop box. The crop box should not be rotated even if the * page is rotated. This change only takes effect in the next * page. * @param crop the crop box */ public void setCropBoxSize(Rectangle crop) { pdf.setCropBoxSize(crop); } /** Gets 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 */ 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; } PdfIndirectReference getCurrentPage() { return getPageReference(currentPageNumber); } int getCurrentPageNumber() { return currentPageNumber; } /** Adds the <CODE>PdfAnnotation</CODE> to the calculation order * array. * @param annot the <CODE>PdfAnnotation</CODE> to be added */ public void addCalculationOrder(PdfFormField annot) { pdf.addCalculationOrder(annot); } /** Set the signature flags. * @param f the flags. This flags are ORed with current ones */ public void setSigFlags(int f) { pdf.setSigFlags(f); } /** Adds a <CODE>PdfAnnotation</CODE> or a <CODE>PdfFormField</CODE> * to the document. Only the top parent of a <CODE>PdfFormField</CODE> * needs to be added. * @param annot the <CODE>PdfAnnotation</CODE> or the <CODE>PdfFormField</CODE> to add */ public void addAnnotation(PdfAnnotation annot) { pdf.addAnnotation(annot); } /** Sets the PDF version. Must be used right before the document * is opened. Valid options are VERSION_1_2, VERSION_1_3 and * VERSION_1_4. VERSION_1_4 is the default. * @param version the version number */ public void setPdfVersion(char version) { HEADER[VPOINT] = (byte)version; } /** 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); } /** Gets the space/character extra spacing ratio for * fully justified text. * @return the space/character extra spacing ratio */ public float getSpaceCharRatio() { return spaceCharRatio; } /** Sets the ratio between the extra word spacing and the extra character spacing * when the text is fully justified. * Extra word spacing will grow <CODE>spaceCharRatio</CODE> times more than extra character spacing. * If the ratio is <CODE>PdfWriter.NO_SPACE_CHAR_RATIO</CODE> then the extra character spacing * will be zero. * @param spaceCharRatio the ratio between the extra word spacing and the extra character spacing */ public void setSpaceCharRatio(float spaceCharRatio) { if (spaceCharRatio < 0.001f) this.spaceCharRatio = 0.001f; else this.spaceCharRatio = spaceCharRatio; } /** Sets the run direction. This is only used as a placeholder * as it does not affect anything. * @param runDirection the run direction */ public void setRunDirection(int runDirection) { if (runDirection < RUN_DIRECTION_NO_BIDI || runDirection > RUN_DIRECTION_RTL) throw new RuntimeException("Invalid run direction: " + runDirection); this.runDirection = runDirection; } /** Gets the run direction. * @return the run direction */ public int getRunDirection() { return runDirection; } /** * Sets the display duration for the page (for presentations) * @param seconds the number of seconds to display the page */ public void setDuration(int seconds) { pdf.setDuration(seconds); } /** * Sets the transition for the page * @param transition the Transition object */ public void setTransition(PdfTransition transition) { pdf.setTransition(transition); } /** Writes the reader to the document and frees the memory used by it. * The main use is when concatenating multiple documents to keep the * memory usage restricted to the current appending document. * @param reader the <CODE>PdfReader</CODE> to free * @throws IOException on error */ public void freeReader(PdfReader reader) throws IOException { currentPdfReaderInstance = (PdfReaderInstance)importedPages.get(reader); if (currentPdfReaderInstance == null) return; currentPdfReaderInstance.writeAllPages(); currentPdfReaderInstance = null; importedPages.remove(reader); } /** Sets the open and close page additional action. * @param actionType the action type. It can be <CODE>PdfWriter.PAGE_OPEN</CODE> * or <CODE>PdfWriter.PAGE_CLOSE</CODE> * @param action the action to perform * @throws PdfException if the action type is invalid */ public void setPageAction(PdfName actionType, PdfAction action) throws PdfException { if (!actionType.equals(PAGE_OPEN) && !actionType.equals(PAGE_CLOSE)) throw new PdfException("Invalid page additional action type: " + actionType.toString()); pdf.setPageAction(actionType, action); } /** Gets the current document size. This size only includes * the data already writen to the output stream, it does not * include templates or fonts. It is usefull if used with * <CODE>freeReader()</CODE> when concatenating many documents * and an idea of the current size is needed. * @return the approximate size without fonts or templates */ public int getCurrentDocumentSize() { return body.offset() + body.size() * 20 + 0x48; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -