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

📄 rtfwriter.java

📁 java itext java itext java itext
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        info.write(day);        writeInt(info, cal.get(Calendar.DAY_OF_MONTH));        info.write(escape);        info.write(hour);        writeInt(info, cal.get(Calendar.HOUR_OF_DAY));        info.write(escape);        info.write(minute);        writeInt(info, cal.get(Calendar.MINUTE));        info.write(escape);        info.write(second);        writeInt(info, cal.get(Calendar.SECOND));    }      /**   * Add a new <code>Font</code> to the list of fonts. If the <code>Font</code>   * already exists in the list of fonts, then it is not added again.   *   * @param newFont The <code>Font</code> to be added   *   * @return The index of the <code>Font</code> in the font list   */    private int addFont(Font newFont)    {        int fn = -1;                for(int i = 0; i < fontList.size(); i++)        {            if(newFont.family() == ((Font)fontList.get(i)).family()) { fn = i; }        }        if(fn == -1)        {            fontList.add(newFont);            return fontList.size()-1;        }        return fn;    }      /**   * Add a new <code>Color</code> to the list of colours. If the <code>Color</code>   * already exists in the list of colours, then it is not added again.   *   * @param newColor The <code>Color</code> to be added   *   * @return The index of the <code>color</code> in the colour list   */    protected int addColor(Color newColor)    {        int cn = 0;        if(newColor == null) { return cn; }        cn = colorList.indexOf(newColor);        if(cn == -1)        {            colorList.add(newColor);            return colorList.size()-1;        }        return cn;    }      /**   * Merge all the different <code>Vector</code>s and <code>ByteArrayOutputStream</code>s   * to the final <code>ByteArrayOutputStream</code>   *   * @return <code>true</code> if all information was sucessfully written to the <code>ByteArrayOutputStream</code>   */    private boolean writeDocument()    {        try        {            writeDocumentIntro();            os.write((byte)'\n');            writeFontList();            os.write((byte)'\n');            writeColorList();            os.write((byte)'\n');            writeList();            os.write((byte)'\n');            writeInfoGroup();            os.write((byte)'\n');            writeDocumentFormat();            os.write((byte)'\n');            writeHeaderFooter(this.footer, footerBegin, os);            writeHeaderFooter(this.header, headerBegin, os);            content.writeTo(os);            os.write(closeGroup);            return true;        }        catch(IOException e)        {            System.err.println(e.getMessage());            return false;        }            }      /** Write the Rich Text file settings   *   * @return <code>true</code if the writing operation succeded   */    private void writeDocumentIntro() throws IOException    {        os.write(openGroup);        os.write(escape);        os.write(docBegin);        os.write(escape);        os.write(ansi);        os.write(escape);        os.write(ansiCodepage);        writeInt(os, 1252);        os.write(escape);        os.write(defaultFont);        writeInt(os, 0);    }      /**   * Write the font list to the final <code>ByteArrayOutputStream</code>   */    private void writeFontList() throws IOException    {        Font fnt;                os.write(openGroup);        os.write(escape);        os.write(fontTable);        for(int i = 0; i < fontList.size(); i++)        {            fnt = (Font) fontList.get(i);            os.write(openGroup);            os.write(escape);            os.write(fontNumber);            writeInt(os, i);            os.write(escape);            switch(fnt.family())            {                case Font.COURIER:                    os.write(fontModern);                    os.write(escape);                    os.write(fontCharset);                    writeInt(os, 0);                    os.write(delimiter);                    os.write(fontCourier);                    break;                case Font.HELVETICA:                    os.write(fontSwiss);                    os.write(escape);                    os.write(fontCharset);                    writeInt(os, 0);                    os.write(delimiter);                    os.write(fontArial);                    break;                case Font.SYMBOL:                    os.write(fontRoman);                    os.write(escape);                    os.write(fontCharset);                    writeInt(os, 2);                    os.write(delimiter);                    os.write(fontSymbol);                    break;                case Font.TIMES_NEW_ROMAN:                    os.write(fontRoman);                    os.write(escape);                    os.write(fontCharset);                    writeInt(os, 0);                    os.write(delimiter);                    os.write(fontTimesNewRoman);                    break;                case Font.ZAPFDINGBATS:                    os.write(fontTech);                    os.write(escape);                    os.write(fontCharset);                    writeInt(os, 0);                    os.write(delimiter);                    os.write(fontWindings);                    break;            }            os.write(commaDelimiter);            os.write(closeGroup);        }        os.write(closeGroup);    }      /**   * Write the colour list to the final <code>ByteArrayOutputStream</code>   */    private void writeColorList() throws IOException    {        Color color = null;                os.write(openGroup);        os.write(escape);        os.write(colorTable);        for(int i = 0; i < colorList.size(); i++)        {            color = (Color) colorList.get(i);            os.write(escape);            os.write(colorRed);            writeInt(os, color.getRed());            os.write(escape);            os.write(colorGreen);            writeInt(os, color.getGreen());            os.write(escape);            os.write(colorBlue);            writeInt(os, color.getBlue());            os.write(commaDelimiter);        }        os.write(closeGroup);    }      /**   * Write the Information Group to the final <code>ByteArrayOutputStream</code>   */    private void writeInfoGroup() throws IOException    {        os.write(openGroup);        os.write(escape);        os.write(infoBegin);        info.writeTo(os);        os.write(closeGroup);    }      /**   * Write the listtable and listoverridetable to the final <code>ByteArrayOutputStream</code>   */    private void writeList() throws IOException    {        listtable.write(closeGroup);        listoverride.write(closeGroup);        listtable.writeTo(os);        os.write((byte)'\n');        listoverride.writeTo(os);    }      /**   * Write an integer   *   * @param out The <code>OuputStream</code> to which the <code>int</code> value is to be written   * @param i The <code>int</code> value to be written   */    private void writeInt(OutputStream out, int i) throws IOException    {        out.write(Integer.toString(i).getBytes());    }      /**   * Get a random integer.   * This returns a <b>unique</b> random integer to be used with listids.   *   * @return Random <code>int</code> value.   */    private int getRandomInt()    {        boolean ok = false;        Integer newInt = null;        Integer oldInt = null;        while(!ok)        {            newInt = new Integer((int) (Math.random() * Integer.MAX_VALUE));            ok = true;            for(int i = 0; i < listIds.size(); i++)            {                oldInt = (Integer) listIds.get(i);                if(oldInt.equals(newInt)) { ok = true; }            }        }        listIds.add(newInt);        return newInt.intValue();    }      /**   * Write the header to the final <code>ByteArrayOutputStream</code>   */    private void writeHeaderFooter(HeaderFooter headerFooter, byte[] hfType, BufferedOutputStream target) throws IOException    {        try        {            ByteArrayOutputStream out = new ByteArrayOutputStream();            if(headerFooter == null)            {                out.write(openGroup);                out.write(escape);                out.write(hfType);                out.write(delimiter);                out.write(closeGroup);            }            else            {                Element[] headerElements = new Element[3];                int headerCount = headerFooter.paragraph().getChunks().size();                if(headerCount >= 1) { headerElements[0] = (Element) headerFooter.paragraph().getChunks().get(0); }                if(headerCount >= 2) { headerElements[1] = (Element) headerFooter.paragraph().getChunks().get(1); }                if(headerCount >= 3) { headerElements[2] = (Element) headerFooter.paragraph().getChunks().get(2); }                out.write(openGroup);                out.write(escape);                out.write(hfType);                if(headerCount >= 1)                {                    if(headerElements[0].type() == Element.CHUNK) { writeChunk((Chunk) headerElements[0], out); }                    if(headerElements[0].type() == Element.PHRASE) { writePhrase((Phrase) headerElements[0], out); }                }                if(headerCount >= 2)                {                    if(headerElements[1].type() == Element.CHUNK)                    {                        try                        {                            Integer.parseInt(((Chunk) headerElements[1]).content());                            out.write(openGroup);                            out.write(escape);                            out.write(field);                            out.write(openGroup);                            out.write(extendedEscape);                            out.write(fieldContent);                            out.write(openGroup);                            out.write(delimiter);                            out.write(fieldPage);                            out.write(delimiter);                            out.write(closeGroup);                            out.write(closeGroup);                            out.write(openGroup);                            out.write(escape);                            out.write(fieldDisplay);                            out.write(openGroup);                            out.write(closeGroup);                            out.write(closeGroup);                            out.write(closeGroup);                        }                        catch(NumberFormatException nfe)                        {                            writeChunk((Chunk) headerElements[1], out);                        }                    }                    if(headerElements[1].type() == Element.PHRASE) { writePhrase((Phrase) headerElements[1], out); }                }                if(headerCount >= 3)                {                    if(headerElements[2].type() == Element.CHUNK) { writeChunk((Chunk) headerElements[2], out); }                    if(headerElements[2].type() == Element.PHRASE) { writePhrase((Phrase) headerElements[2], out); }                }                out.write(closeGroup);            }            out.writeTo(target);        }        catch(DocumentException e)        {            throw new IOException("DocumentException - "+e.getMessage());        }    }      /**   *  Write the <code>Document</code>'s Paper and Margin Size   *  to the final <code>ByteArrayOutputStream</code>   */    private void writeDocumentFormat() throws IOException    {        os.write(openGroup);        os.write(escape);        os.write(rtfPaperWidth);        writeInt(os, pageWidth);        os.write(escape);        os.write(rtfPaperHeight);        writeInt(os, pageHeight);        os.write(escape);        os.write(rtfMarginLeft);        writeInt(os, marginLeft);        os.write(escape);        os.write(rtfMarginRight);        writeInt(os, marginRight);        os.write(escape);        os.write(rtfMarginTop);        writeInt(os, marginTop);        os.write(escape);        os.write(rtfMarginBottom);        writeInt(os, marginBottom);        os.write(closeGroup);    }      /**   * Initialise all helper classes.   * Clears alls lists, creates new <code>ByteArrayOutputStream</code>'s   */    private void initDefaults()    {        fontList.clear();        colorList.clear();        info = new ByteArrayOutputStream();        content = new ByteArrayOutputStream();        listtable = new ByteArrayOutputStream();        listoverride = new ByteArrayOutputStream();        document.addProducer();        document.addCreationDate();        addFont(new Font(Font.TIMES_NEW_ROMAN, 10, Font.NORMAL));        addColor(new Color(0, 0, 0));        addColor(new Color(255, 255, 255));        listIds = new Vector();        try        {            listtable.write(openGroup);            listtable.write(extendedEscape);            listtable.write(listtableGroup);            listtable.write((byte) '\n');            listoverride.write(openGroup);            listoverride.write(extendedEscape);            listoverride.write(listoverridetableGroup);            listoverride.write((byte) '\n');        }        catch(IOException e)        {            System.out.println("InitDefaultsError" + e);        }    }}

⌨️ 快捷键说明

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