⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdfwriter.java

📁 iText可以制作中文PDF文件的JAVA源程序
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * <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     */        public PdfIndirectReference add(PdfPage page, PdfContents contents) throws PdfException {        if (!open) {            throw new PdfException("The document isn't open.");        }        PdfIndirectObject object = body.add(contents);        try {            object.writeTo(os);        }        catch(IOException ioe) {            throw new ExceptionConverter(ioe);        }        page.add(object.getIndirectReference());        page.setParent(ROOTREFERENCE);        PdfIndirectObject pageObject = body.add(page, getPageReference(currentPageNumber++));        try {            pageObject.writeTo(os);        }        catch(IOException ioe) {            throw new ExceptionConverter(ioe);        }        root.add(pageObject.getIndirectReference());        return pageObject.getIndirectReference();    }        /**     * Writes a <CODE>PdfImage</CODE> to the outputstream.     *     * @param pdfImage the image to be added     * @return a <CODE>PdfIndirectReference</CODE> to the encapsulated image     * @throws PdfException when a document isn't open yet, or has been closed     */        public PdfIndirectReference add(PdfImage pdfImage) throws PdfException {        if (! imageDictionary.contains(pdfImage)) {            PdfIndirectObject object = body.add(pdfImage);            try {                object.writeTo(os);            }            catch(IOException ioe) {                throw new ExceptionConverter(ioe);            }            imageDictionary.put(pdfImage.name(), object.getIndirectReference());            return object.getIndirectReference();        }        return (PdfIndirectReference) imageDictionary.get(pdfImage.name());    }        protected PdfIndirectReference add(PdfICCBased icc) throws PdfException {        PdfIndirectObject object = body.add(icc);        try {            object.writeTo(os);        }        catch(IOException ioe) {            throw new ExceptionConverter(ioe);        }        return object.getIndirectReference();    }        /**     * return the <CODE>PdfIndirectReference</CODE> to the image with a given name.     *     * @param name the name of the image     * @return a <CODE>PdfIndirectReference</CODE>     */        public PdfIndirectReference getImageReference(PdfName name) {        return (PdfIndirectReference) imageDictionary.get(name);    }        // methods to open and close the writer        /**     * Signals that the <CODE>Document</CODE> has been opened and that     * <CODE>Elements</CODE> can be added.     * <P>     * When this method is called, the PDF-document header is     * written to the outputstream.     */        public void open() {        try {            os.write(HEADER);        }        catch(IOException ioe) {            throw new ExceptionConverter(ioe);        }    }        protected PdfDictionary getCatalog(PdfIndirectReference rootObj)    {        return ((PdfDocument)document).getCatalog(rootObj);    }    /**     * Signals that the <CODE>Document</CODE> was closed and that no other     * <CODE>Elements</CODE> will be added.     * <P>     * The pages-tree is built and written to the outputstream.     * A Catalog is constructed, as well as an Info-object,     * the referencetable is composed and everything is written     * to the outputstream embedded in a Trailer.     */        public synchronized void close() {        if (open) {            if ((currentPageNumber - 1) != pageReferences.size())                throw new RuntimeException("The page " + pageReferences.size() +                " was requested but the document has only " + (currentPageNumber - 1) + " pages.");            pdf.close();            try {                // add the fonts                for (Iterator it = documentFonts.values().iterator(); it.hasNext();) {                    FontDetails details = (FontDetails)it.next();                    details.writeFont(this);                }                                // add the form XObjects                for (Iterator it = formXObjects.values().iterator(); it.hasNext();) {                    Object objs[] = (Object[])it.next();                    PdfTemplate template = (PdfTemplate)objs[1];                    if (template != null && template.getType() == PdfTemplate.TYPE_TEMPLATE) {                        PdfIndirectObject obj = body.add(template.getFormXObject(), template.getIndirectReference());                        obj.writeTo(os);                    }                }                // add all the dependencies in the imported pages                for (Iterator it = importedPages.values().iterator(); it.hasNext();) {                    currentPdfReaderInstance = (PdfReaderInstance)it.next();                    currentPdfReaderInstance.writeAllPages();                }                // add the color                for (Iterator it = documentColors.values().iterator(); it.hasNext();) {                    ColorDetails color = (ColorDetails)it.next();                    PdfIndirectObject cobj = body.add(color.getSpotColor(this), color.getIndirectReference());                    cobj.writeTo(os);                }                // add the pattern                for (Iterator it = documentPatterns.keySet().iterator(); it.hasNext();) {                    PdfPatternPainter pat = (PdfPatternPainter)it.next();                    PdfIndirectObject pobj = body.add(pat.getPattern(), pat.getIndirectReference());                    pobj.writeTo(os);                }                // add the shading patterns                for (Iterator it = documentShadingPatterns.keySet().iterator(); it.hasNext();) {                    PdfShadingPattern shadingPattern = (PdfShadingPattern)it.next();                    shadingPattern.addToBody();                }                // add the shadings                for (Iterator it = documentShadings.keySet().iterator(); it.hasNext();) {                    PdfShading shading = (PdfShading)it.next();                    shading.addToBody();                }                // add the root to the body                PdfIndirectObject rootObject = body.add(root);                rootObject.writeTo(os);                // make the catalog-object and add it to the body                PdfIndirectObject indirectCatalog = body.add(((PdfDocument)document).getCatalog(rootObject.getIndirectReference()));                indirectCatalog.writeTo(os);                // add the info-object to the body                PdfIndirectObject info = body.add(((PdfDocument)document).getInfo());                info.writeTo(os);                PdfIndirectReference encryption = null;                PdfLiteral fileID = null;                if (crypto != null) {                    PdfIndirectObject encryptionObject = body.add(crypto.getEncryptionDictionary());                    encryptionObject.writeTo(os);                    encryption = encryptionObject.getIndirectReference();                    fileID = crypto.getFileID();                }                                // write the cross-reference table of the body                os.write(body.getCrossReferenceTable());                // make the trailer                PdfTrailer trailer = new PdfTrailer(body.size(),                body.offset(),                indirectCatalog.getIndirectReference(),                info.getIndirectReference(),                encryption,                fileID);                os.write(trailer.toPdf(this));                super.close();            }            catch(IOException ioe) {                throw new ExceptionConverter(ioe);            }        }    }        // methods        /**     * Returns the number of the next object that can be added to the body.     *     * @return	the size of the body-object     */        int size() {        return body.size();    }        /**     * Sometimes it is necessary to know where the just added <CODE>Table</CODE> ends.     *     * For instance to avoid to add another table in a page that is ending up, because     * the new table will be probably splitted just after the header (it is an     * unpleasant effect, isn't it?).     *     * Added on September 8th, 2001     * by Francesco De Milato     * francesco.demilato@tiscalinet.it     * @param table the <CODE>Table</CODE>     * @return the bottom height of the just added table     */        public float getTableBottom(Table table) {        return pdf.bottom(table) - pdf.indentBottom();    }        /**     * Checks if a <CODE>Table</CODE> fits the current page of the <CODE>PdfDocument</CODE>.     *     * @param	table	the table that has to be checked     * @param	margin	a certain margin     * @return	<CODE>true</CODE> if the <CODE>Table</CODE> fits the page, <CODE>false</CODE> otherwise.     */        public boolean fitsPage(Table table, float margin) {        return pdf.bottom(table) > pdf.indentBottom() + margin;    }        /**     * Checks if a <CODE>Table</CODE> fits the current page of the <CODE>PdfDocument</CODE>.     *     * @param	table	the table that has to be checked     * @return	<CODE>true</CODE> if the <CODE>Table</CODE> fits the page, <CODE>false</CODE> otherwise.     */        public boolean fitsPage(Table table) {        return fitsPage(table, 0);    }        /**     * Checks if a <CODE>PdfPTable</CODE> fits the current page of the <CODE>PdfDocument</CODE>.     *     * @param	table	the table that has to be checked     * @param	margin	a certain margin     * @return	<CODE>true</CODE> if the <CODE>PdfPTable</CODE> fits the page, <CODE>false</CODE> otherwise.     */    public boolean fitsPage(PdfPTable table, float margin) {        return pdf.fitsPage(table, margin);    }        /**     * Checks if a <CODE>PdfPTable</CODE> fits the current page of the <CODE>PdfDocument</CODE>.     *     * @param	table	the table that has to be checked     * @return	<CODE>true</CODE> if the <CODE>PdfPTable</CODE> fits the page, <CODE>false</CODE> otherwise.     */    public boolean fitsPage(PdfPTable table) {        return pdf.fitsPage(table, 0);    }        /**     * Checks if writing is paused.     *     * @return		<CODE>true</CODE> if writing temporarely has to be paused, <CODE>false</CODE> otherwise.     */        boolean isPaused() {        return pause;    }        /**     * Gets the direct content for this document. There is only one direct content,     * multiple calls to this method will allways retrieve the same.     * @return the direct content     */        public PdfContentByte getDirectContent() {        return directContent;    }        /**     * Gets the direct content under for this document. There is only one direct content,     * multiple calls to this method will allways retrieve the same.     * @return the direct content     */        public PdfContentByte getDirectContentUnder() {        return directContentUnder;    }        /**     * Resets all the direct contents to empty. This happens when a new page is started.     */        void resetContent() {        directContent.reset();        directContentUnder.reset();    }        /** Gets the AcroForm object.     * @return the <CODE>PdfAcroForm</CODE>     */        public PdfAcroForm getAcroForm() {        return pdf.getAcroForm();    }        /** Gets the root outline.     * @return the root outline     */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -