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

📄 rtfwriter.java

📁 iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     */
    public void setFooter(HeaderFooter footer) {
        this.footer = footer;
        processHeaderFooter(this.footer);
    }

    /**
     * Adds the header to the top of the <CODE>Document</CODE>.
     * @param header
     */
    public void setHeader(HeaderFooter header) {
        this.header = header;
        processHeaderFooter(this.header);
    }

    /**
     * Resets the footer.
     */
    public void resetFooter() {
        setFooter(null);
    }

    /**
     * Resets the header.
     */
    public void resetHeader() {
        setHeader(null);
    }

    /**
     * Tells the <code>RtfWriter</code> that a new page is to be begun.
     *
     * @return <code>true</code> if a new Page was begun.
     * @throws DocumentException if the Document was not open or had been closed.
     */
    public boolean newPage() {
        try {
            content.write(escape);
            content.write(newPage);
            content.write(escape);
            content.write(paragraph);
        } catch (IOException e) {
            throw new ExceptionConverter(e);
        }
        return true;
    }

    /**
     * Sets the page margins
     *
     * @param marginLeft The left margin
     * @param marginRight The right margin
     * @param marginTop The top margin
     * @param marginBottom The bottom margin
     *
     * @return <code>true</code> if the page margins were set.
     */
    public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) {
        this.marginLeft = (int) (marginLeft * TWIPSFACTOR);
        this.marginRight = (int) (marginRight * TWIPSFACTOR);
        this.marginTop = (int) (marginTop * TWIPSFACTOR);
        this.marginBottom = (int) (marginBottom * TWIPSFACTOR);
        return true;
    }

    /**
     * Sets the page size
     *
     * @param pageSize A <code>Rectangle</code> specifying the page size
     *
     * @return <code>true</code> if the page size was set
     */
    public boolean setPageSize(Rectangle pageSize) {
        if (!parseFormat(pageSize, false)) {
            pageWidth = (int) (pageSize.getWidth() * TWIPSFACTOR);
            pageHeight = (int) (pageSize.getHeight() * TWIPSFACTOR);
            landscape = pageWidth > pageHeight;
        }
        return true;
    }

    /**
     * Write the table of contents.
     *
     * @param tocTitle The title that will be displayed above the TOC
     * @param titleFont The <code>Font</code> that will be used for the tocTitle
     * @param showTOCasEntry Set this to true if you want the TOC to appear as an entry in the TOC
     * @param showTOCEntryFont Use this <code>Font</code> to specify what Font to use when showTOCasEntry is true
     *
     * @return <code>true</code> if the TOC was added.
     */
    public boolean writeTOC(String tocTitle, Font titleFont, boolean showTOCasEntry, Font showTOCEntryFont) {
        try {
            RtfTOC toc = new RtfTOC(tocTitle, titleFont);
            if (showTOCasEntry) {
                toc.addTOCAsTOCEntry(tocTitle, showTOCEntryFont);
            }
            add(new Paragraph(toc));
        } catch (DocumentException de) {
            return false;
        }
        return true;
    }

    /**
     * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
     * 
     * @param element A high level object to add
     * @return    <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
     * @throws    DocumentException   if a document isn't open yet, or has been closed
     */
    public boolean add(Element element) throws DocumentException {
        if (pause) {
            return false;
        }
        return addElement(element, content);
    }


    /** Private functions */

    /**
     * Adds an <CODE>Element</CODE> to the <CODE>Document</CODE>.
     * @param element the high level element to add
     * @param out the outputstream to which the RTF data is sent
     * @return    <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
     * @throws    DocumentException   if a document isn't open yet, or has been closed
     */
    protected boolean addElement(Element element, ByteArrayOutputStream out) throws DocumentException {
        try {
            switch (element.type()) {
                case Element.CHUNK:
                    writeChunk((Chunk) element, out);
                    break;
                case Element.PARAGRAPH:
                    writeParagraph((Paragraph) element, out);
                    break;
                case Element.ANCHOR:
                    writeAnchor((Anchor) element, out);
                    break;
                case Element.PHRASE:
                    writePhrase((Phrase) element, out);
                    break;
                case Element.CHAPTER:
                case Element.SECTION:
                    writeSection((Section) element, out);
                    break;
                case Element.LIST:
                    writeList((com.lowagie.text.List) element, out);
                    break;
                case Element.TABLE:
                	try {
                		writeTable((Table) element, out);
                	}
                	catch(ClassCastException cce) {
                		writeTable(((SimpleTable)element).createTable(), out);
                	}
                    break;
                case Element.ANNOTATION:
                    writeAnnotation((Annotation) element, out);
                    break;
                case Element.IMGRAW:
                case Element.IMGTEMPLATE:
                case Element.JPEG:
                    Image img = (Image)element;
                    writeImage(img, out);
                    break;

                case Element.AUTHOR:
                    writeMeta(metaAuthor, (Meta) element);
                    break;
                case Element.SUBJECT:
                    writeMeta(metaSubject, (Meta) element);
                    break;
                case Element.KEYWORDS:
                    writeMeta(metaKeywords, (Meta) element);
                    break;
                case Element.TITLE:
                    writeMeta(metaTitle, (Meta) element);
                    break;
                case Element.PRODUCER:
                    writeMeta(metaProducer, (Meta) element);
                    break;
                case Element.CREATIONDATE:
                    writeMeta(metaCreationDate, (Meta) element);
                    break;
            }
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    /**
     * Write the beginning of a new <code>Section</code>
     *
     * @param sectionElement The <code>Section</code> be written
     * @param out The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     * @throws DocumentException
     */
    private void writeSection(Section sectionElement, ByteArrayOutputStream out) throws IOException, DocumentException {
        if (sectionElement.type() == Element.CHAPTER) {
            out.write(escape);
            out.write(sectionDefaults);
            writeSectionDefaults(out);
        }
        if (sectionElement.getTitle() != null) {
            if (writeTOC) {
                StringBuffer title = new StringBuffer("");
                for (ListIterator li = sectionElement.getTitle().getChunks().listIterator(); li.hasNext();) {
                    title.append(((Chunk) li.next()).getContent());
                }
                add(new RtfTOCEntry(title.toString(), sectionElement.getTitle().getFont()));
            } else {
                add(sectionElement.getTitle());
            }
            out.write(escape);
            out.write(paragraph);
        }
        sectionElement.process(this);
        if (sectionElement.type() == Element.CHAPTER) {
            out.write(escape);
            out.write(section);
        }
        if (sectionElement.type() == Element.SECTION) {
            out.write(escape);
            out.write(paragraph);
        }
    }

    /**
     * Write the beginning of a new <code>Paragraph</code>
     *
     * @param paragraphElement The <code>Paragraph</code> to be written
     * @param out The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     */
    private void writeParagraph(Paragraph paragraphElement, ByteArrayOutputStream out) throws IOException {
        out.write(escape);
        out.write(paragraphDefaults);
        if (inTable) {
            out.write(escape);
            out.write(RtfCell.cellInTable);
        }
        switch (paragraphElement.getAlignment()) {
            case Element.ALIGN_LEFT:
                out.write(escape);
                out.write(alignLeft);
                break;
            case Element.ALIGN_RIGHT:
                out.write(escape);
                out.write(alignRight);
                break;
            case Element.ALIGN_CENTER:
                out.write(escape);
                out.write(alignCenter);
                break;
            case Element.ALIGN_JUSTIFIED:
            case Element.ALIGN_JUSTIFIED_ALL:
                out.write(escape);
                out.write(alignJustify);
                break;
        }
        out.write(escape);
        out.write(listIndent);
        writeInt(out, (int) (paragraphElement.getIndentationLeft() * TWIPSFACTOR));
        out.write(escape);
        out.write(rightIndent);
        writeInt(out, (int) (paragraphElement.getIndentationRight() * TWIPSFACTOR));
        Iterator chunks = paragraphElement.getChunks().iterator();
        while (chunks.hasNext()) {
            Chunk ch = (Chunk) chunks.next();
            ch.setFont(paragraphElement.getFont().difference(ch.getFont()));
        }
        ByteArrayOutputStream save = content;
        content = out;
        paragraphElement.process(this);
        content = save;
        if (!inTable) {
            out.write(escape);
            out.write(paragraph);
        }
    }

    /**
     * Write a <code>Phrase</code>.
     *
     * @param phrase  The <code>Phrase</code> item to be written
     * @param out     The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     */
    private void writePhrase(Phrase phrase, ByteArrayOutputStream out) throws IOException {
        out.write(escape);
        out.write(paragraphDefaults);
        if (inTable) {
            out.write(escape);
            out.write(RtfCell.cellInTable);
        }
        Iterator chunks = phrase.getChunks().iterator();
        while (chunks.hasNext()) {
            Chunk ch = (Chunk) chunks.next();
            ch.setFont(phrase.getFont().difference(ch.getFont()));
        }
        ByteArrayOutputStream save = content;
        content = out;
        phrase.process(this);
        content = save;
    }

    /**
     * Write an <code>Anchor</code>. Anchors are treated like Phrases.
     *
     * @param anchor  The <code>Chunk</code> item to be written
     * @param out     The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     */
    private void writeAnchor(Anchor anchor, ByteArrayOutputStream out) throws IOException {
        if (anchor.getUrl() != null) {
            out.write(openGroup);
            out.write(escape);
            out.write(field);
            out.write(openGroup);
            out.write(extendedEscape);
            out.write(fieldContent);
            out.write(openGroup);
            out.write(fieldHyperlink);
            out.write(delimiter);
            out.write(anchor.getUrl().toString().getBytes());
            out.write(closeGroup);
            out.write(closeGroup);
            out.write(openGroup);
            out.write(escape);
            out.write(fieldDisplay);
            out.write(delimiter);
            writePhrase(anchor, out);
            out.write(closeGroup);
            out.write(closeGroup);
        } else {
            writePhrase(anchor, out);
        }
    }

    /**
     * Write a <code>Chunk</code> and all its font properties.
     *
     * @param chunk The <code>Chunk</code> item to be written
     * @param out The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     * @throws DocumentException
     */
    private void writeChunk(Chunk chunk, ByteArrayOutputStream out) throws IOException, DocumentException {
        if (chunk instanceof RtfField) {
            ((RtfField) chunk).write(this, out);
        } else {
            if (chunk.getImage() != null) {
                writeImage(chunk.getImage(), out);
            } else {
                writeInitialFontSignature(out, chunk);
                out.write(filterSpecialChar(chunk.getContent(), false).getBytes());
                writeFinishingFontSignature(out, chunk);
            }
        }
    }


    protected void writeInitialFontSignature(OutputStream out, Chunk chunk) throws IOException {
        Font font = chunk.getFont();

        out.write(escape);
        out.write(fontNumber);
        if (!font.getFamilyname().equalsIgnoreCase("unknown")) {

⌨️ 快捷键说明

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