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

📄 rtfwriter.java

📁 java itext java itext java itext
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
   * @param chunk The <code>Chunk</code> item to be written   * @param out The <code>ByteArrayOutputStream</code> to write to   *   * @throws IOException   */    private void writeChunk(Chunk chunk, ByteArrayOutputStream out) throws IOException    {        out.write(escape);        out.write(fontNumber);        if(chunk.font().family() != Font.UNDEFINED) { writeInt(out, addFont(chunk.font())); } else { writeInt(out, 0); }        out.write(escape);        out.write(fontSize);        if(chunk.font().size() > 0) { writeInt(out, (int) (chunk.font().size() * 2)); } else { writeInt(out, 20); }        out.write(escape);        out.write(fontColor);        writeInt(out, addColor(chunk.font().color()));        if(chunk.font().isBold()) { out.write(escape); out.write(bold); }        if(chunk.font().isItalic()) { out.write(escape); out.write(italic); }        if(chunk.font().isUnderlined()) { out.write(escape); out.write(underline); }        if(chunk.font().isStrikethru()) { out.write(escape); out.write(strikethrough); }        out.write(delimiter);        out.write(chunk.content().getBytes());        if(chunk.font().isBold()) { out.write(escape); out.write(bold); writeInt(out, 0); }        if(chunk.font().isItalic()) { out.write(escape); out.write(italic); writeInt(out, 0); }        if(chunk.font().isUnderlined()) { out.write(escape); out.write(underline); writeInt(out, 0); }        if(chunk.font().isStrikethru()) { out.write(escape); out.write(strikethrough); writeInt(out, 0); }    }      /**   * Write a <code>ListItem</code>   *   * @param listItem The <code>ListItem</code> to be written   * @param out The <code>ByteArrayOutputStream</code> to write to   *   * @throws IOException   */    private void writeListElement(ListItem listItem, ByteArrayOutputStream out) throws IOException, DocumentException    {        Iterator chunks = listItem.getChunks().iterator();        while(chunks.hasNext())        {            Chunk ch = (Chunk) chunks.next();            addElement(ch, out);        }        out.write(escape);        out.write(paragraph);    }      /**   * Write a <code>List</code>   *   * @param list The <code>List</code> to be written   * @param out The <code>ByteArrayOutputStream</code> to write to   *   * @throws IOException, DocumentException   */    private void writeList(com.lowagie.text.List list, ByteArrayOutputStream out) throws IOException, DocumentException    {        int type = 0;        int align = 0;        int fontNr = addFont(new Font(Font.SYMBOL, 10, Font.NORMAL, new Color(0, 0, 0)));        if(!list.isNumbered()) type = 23;        if(listLevel == 0)        {            maxListLevel = 0;            listtable.write(openGroup);            listtable.write(escape);            listtable.write(listDefinition);            int i = getRandomInt();            listtable.write(escape);            listtable.write(listTemplateID);            writeInt(listtable, i);            listtable.write(escape);            listtable.write(hybridList);            listtable.write((byte) '\n');        }        if(listLevel >= maxListLevel)        {            maxListLevel++;            listtable.write(openGroup);            listtable.write(escape);            listtable.write(listLevelDefinition);            listtable.write(escape);            listtable.write(listLevelTypeOld);            writeInt(listtable, type);            listtable.write(escape);            listtable.write(listLevelTypeNew);            writeInt(listtable, type);            listtable.write(escape);            listtable.write(listLevelAlignOld);            writeInt(listtable, align);            listtable.write(escape);            listtable.write(listLevelAlignNew);            writeInt(listtable, align);            listtable.write(escape);            listtable.write(listLevelStartAt);            writeInt(listtable, 1);            listtable.write(openGroup);            listtable.write(escape);            listtable.write(listLevelTextDefinition);            listtable.write(escape);            listtable.write(listLevelTextLength);            if(list.isNumbered()) { writeInt(listtable, 2); } else { writeInt(listtable, 1); }            listtable.write(escape);            if(list.isNumbered()) { listtable.write(listLevelTextStyleNumbers); } else { listtable.write(listLevelTextStyleBullet); }            listtable.write(commaDelimiter);            listtable.write(closeGroup);            listtable.write(openGroup);            listtable.write(escape);            listtable.write(listLevelNumbersDefinition);            if(list.isNumbered()) { listtable.write(delimiter); listtable.write(listLevelNumbers); writeInt(listtable, listLevel + 1); }            listtable.write(commaDelimiter);            listtable.write(closeGroup);            if(!list.isNumbered()) { listtable.write(escape); listtable.write(fontNumber); writeInt(listtable, fontNr); }            listtable.write(escape);            listtable.write(firstIndent);            writeInt(listtable, (int) (list.indentationLeft() * twipsFactor * -1));            listtable.write(escape);            listtable.write(listIndent);            writeInt(listtable, (int) ((list.indentationLeft() + list.symbolIndent()) * twipsFactor));            listtable.write(escape);            listtable.write(tabStop);            writeInt(listtable, (int) (list.symbolIndent() * twipsFactor));            listtable.write(closeGroup);            listtable.write((byte) '\n');        }        // Actual List Begin in Content        out.write(escape);        out.write(paragraphDefaults);        out.write(escape);        out.write(alignLeft);        out.write(escape);        out.write(firstIndent);        writeInt(out, (int) (list.indentationLeft() * twipsFactor * -1));        out.write(escape);        out.write(listIndent);        writeInt(out, (int) ((list.indentationLeft() + list.symbolIndent()) * twipsFactor));        out.write(escape);        out.write(fontSize);        writeInt(out, 20);        out.write(escape);        out.write(listBegin);        writeInt(out, currentListID);        if(listLevel > 0)        {            out.write(escape);            out.write(listCurrentLevel);            writeInt(out, listLevel);        }        out.write(openGroup);        ListIterator listItems = list.getItems().listIterator();        Element listElem;        int count = 1;        while(listItems.hasNext())        {            listElem = (Element) listItems.next();            if(listElem.type() == Element.CHUNK) { listElem = new ListItem((Chunk) listElem); }            if(listElem.type() == Element.LISTITEM)            {                out.write(openGroup);                out.write(escape);                out.write(listTextOld);                out.write(escape);                out.write(paragraphDefaults);                out.write(escape);                out.write(fontNumber);                if(list.isNumbered()) { writeInt(out, addFont(new Font(Font.TIMES_NEW_ROMAN, Font.NORMAL, 10, new Color(0, 0, 0)))); } else { writeInt(out, fontNr); }                out.write(escape);                out.write(firstIndent);                writeInt(out, (int) (list.indentationLeft() * twipsFactor * -1));                out.write(escape);                out.write(listIndent);                writeInt(out, (int) ((list.indentationLeft() + list.symbolIndent()) * twipsFactor));                out.write(delimiter);                if(list.isNumbered()) { writeInt(out, count); out.write(".".getBytes()); } else { out.write(escape); out.write( listBulletOld); }                out.write(escape);                out.write(tab);                out.write(closeGroup);                writeListElement((ListItem) listElem, out);                count++;            }            else if(listElem.type() == Element.LIST)            {                listLevel++;                writeList((com.lowagie.text.List) listElem, out);                listLevel--;                out.write(escape);                out.write(paragraphDefaults);                out.write(escape);                out.write(alignLeft);                out.write(escape);                out.write(firstIndent);                writeInt(out, (int) (list.indentationLeft() * twipsFactor * -1));                out.write(escape);                out.write(listIndent);                writeInt(out, (int) ((list.indentationLeft() + list.symbolIndent()) * twipsFactor));                out.write(escape);                out.write(fontSize);                writeInt(out, 20);                out.write(escape);                out.write(listBegin);                writeInt(out, currentListID);                if(listLevel > 0)                {                    out.write(escape);                    out.write(listCurrentLevel);                    writeInt(out, listLevel);                }            }            out.write((byte)'\n');        }        out.write(closeGroup);        if(listLevel == 0)        {            int i = getRandomInt();            listtable.write(escape);            listtable.write(listID);            writeInt(listtable, i);            listtable.write(closeGroup);            listtable.write((byte) '\n');            listoverride.write(openGroup);            listoverride.write(escape);            listoverride.write(listOverride);            listoverride.write(escape);            listoverride.write(listID);            writeInt(listoverride, i);            listoverride.write(escape);            listoverride.write(listOverrideCount);            writeInt(listoverride, 0);            listoverride.write(escape);            listoverride.write(listBegin);            writeInt(listoverride, currentListID);            currentListID++;            listoverride.write(closeGroup);            listoverride.write((byte) '\n');        }    }      /**   * Write a <code>Table</code>.   *   * @param table The <code>table</code> to be written   * @param out The <code>ByteArrayOutputStream</code> to write to   *   * Currently no nesting of tables is supported. If a cell contains anything but a Cell Object it is ignored.   *   * @throws IOException, DocumentException   */    private void writeTable(Table table, ByteArrayOutputStream out) throws IOException, DocumentException    {        RtfTable rtfTable = new RtfTable(this);        rtfTable.importTable(table, 12239);        rtfTable.writeTable(out);    }          /**   * Write an <code>Image</code>.   *   * @param image The <code>image</code> to be written   * @param out The <code>ByteArrayOutputStream</code> to write to   *   * At the moment only PNG and JPEG Images are supported.   *   * @throws IOException, DocumentException   */    private void writeImage(Image image, ByteArrayOutputStream out) throws IOException, DocumentException    {        if(!image.isPng() && !image.isJpeg()) throw new DocumentException("Only PNG and JPEG images are supported by the RTF Writer");        switch(image.alignment())        {            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 : out.write(escape); out.write(alignJustify); break;        }        out.write(openGroup);        out.write(extendedEscape);        out.write(pictureGroup);        out.write(openGroup);        out.write(escape);        out.write(picture);        out.write(escape);        if(image.isPng()) out.write(picturePNG);        if(image.isJpeg())  out.write(pictureJPEG);        out.write(escape);        out.write(pictureWidth);        writeInt(out, (int) (image.width() * twipsFactor));        out.write(escape);        out.write(pictureHeight);        writeInt(out, (int) (image.height() * twipsFactor));        out.write(escape);        out.write(pictureIntendedWidth);        writeInt(out, (int) (image.plainWidth() * twipsFactor));        out.write(escape);        out.write(pictureIntendedHeight);        writeInt(out, (int) (image.plainHeight() * twipsFactor));        if(image.width() > 0)        {            out.write(escape);            out.write(pictureScaleX);            writeInt(out, (int) (100 / image.width() * image.plainWidth()));        }        if(image.height() > 0)        {            out.write(escape);            out.write(pictureScaleY);            writeInt(out, (int) (100 / image.height() * image.plainHeight()));        }        out.write(delimiter);        InputStream imgIn;        if(image.rawData() == null) { imgIn = image.url().openStream(); } else { imgIn = new ByteArrayInputStream(image.rawData()); }        int buffer = -1;        int count = 0;        out.write((byte) '\n');        while((buffer = imgIn.read()) != -1)        {            String helperStr = Integer.toHexString(buffer);            if(helperStr.length() < 2) helperStr = "0" + helperStr;            out.write(helperStr.getBytes());            count++;            if(count == 64) { out.write((byte) '\n'); count = 0; }        }        out.write(closeGroup);        out.write(closeGroup);        out.write((byte) '\n');    }      /**   * Write an <code>Annotation</code>   *   * @param annotationElement The <code>Annotation</code> to be written   * @param out The <code>ByteArrayOutputStream</code> to write to   *   * @throws IOException   */    private void writeAnnotation(Annotation annotationElement, ByteArrayOutputStream out) throws IOException, DocumentException    {        int id = getRandomInt();        out.write(openGroup);        out.write(extendedEscape);        out.write(annotationID);        out.write(delimiter);        writeInt(out, id);        out.write(closeGroup);        out.write(openGroup);        out.write(extendedEscape);        out.write(annotationAuthor);        out.write(delimiter);        out.write(annotationElement.title().getBytes());        out.write(closeGroup);        out.write(openGroup);        out.write(extendedEscape);        out.write(annotation);        out.write(escape);        out.write(paragraphDefaults);        out.write(delimiter);        out.write(annotationElement.content().getBytes());        out.write(closeGroup);    }      /**   * Add a <code>Meta</code> element. It is written to the Inforamtion Group   * and merged with the main <code>ByteArrayOutputStream</code> when the   * Document is closed.   *   * @param metaName The type of <code>Meta</code> element to be added   * @param meta The <code>Meta</code> element to be added   *   * Currently only the Meta Elements Author, Subject, Keywords, Title, Producer and CreationDate are supported.   *   * @throws IOException   */    private void writeMeta(byte[] metaName, Meta meta) throws IOException    {        info.write(openGroup);        try        {            info.write(escape);            info.write(metaName);            info.write(delimiter);            if(meta.type() == Meta.CREATIONDATE) { writeFormatedDateTime(meta.content()); } else { info.write(meta.content().getBytes()); }        }        finally        {            info.write(closeGroup);        }    }      /**   * Writes a date. The date is formated <strong>Year, Month, Day, Hour, Minute, Second</strong>   *   * @param date The date to be written   *   * @throws IOException   */    private void writeFormatedDateTime(String date) throws IOException    {        Calendar cal = Calendar.getInstance();        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");        ParsePosition pp = new ParsePosition(0);        Date d = sdf.parse(date, pp);        if(d == null) { d = new Date(); }        cal.setTime(d);        info.write(escape);        info.write(year);        writeInt(info, cal.get(Calendar.YEAR));        info.write(escape);        info.write(month);        writeInt(info, cal.get(Calendar.MONTH));        info.write(escape);

⌨️ 快捷键说明

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