📄 rtfwriter.java
字号:
private static final byte[] pictureWidth = "picw".getBytes(); /** Picture height */ private static final byte[] pictureHeight = "pich".getBytes(); /** Picture width after scaling */ private static final byte[] pictureIntendedWidth = "picwgoal".getBytes(); /** Picture height after scaling */ private static final byte[] pictureIntendedHeight = "pichgoal".getBytes(); /** Picture scale horizontal percent */ private static final byte[] pictureScaleX = "picscalex".getBytes(); /** Picture scale vertical percent */ private static final byte[] pictureScaleY = "picscaley".getBytes(); /** * Fields (for page numbering) */ /** Begin field tag */ private static final byte[] field = "field".getBytes(); /** Content fo the field */ private static final byte[] fieldContent = "fldinst".getBytes(); /** PAGE numbers */ private static final byte[] fieldPage = "PAGE".getBytes(); /** HYPERLINK field */ private static final byte[] fieldHyperlink = "HYPERLINK".getBytes(); /** Last page number (not used) */ private static final byte[] fieldDisplay = "fldrslt".getBytes(); /** Class variables */ /** * Because of the way RTF works and the way itext works, the text has to be * stored and is only written to the actual OutputStream at the end. */ /** This <code>Vector</code> contains all fonts used in the document. */ private Vector fontList = new Vector(); /** This <code>Vector</code> contains all colours used in the document. */ private Vector colorList = new Vector(); /** This <code>ByteArrayOutputStream</code> contains the main body of the document. */ private ByteArrayOutputStream content = null; /** This <code>ByteArrayOutputStream</code> contains the information group. */ private ByteArrayOutputStream info = null; /** This <code>ByteArrayOutputStream</code> contains the list table. */ private ByteArrayOutputStream listtable = null; /** This <code>ByteArrayOutputStream</code> contains the list override table. */ private ByteArrayOutputStream listoverride = null; /** Which font is currently being used. */ private int listFont = 0; /** Document header. */ private HeaderFooter header = null; /** Document footer. */ private HeaderFooter footer = null; /** Left margin. */ private int marginLeft = 1800; /** Right margin. */ private int marginRight = 1800; /** Top margin. */ private int marginTop = 1440; /** Bottom margin. */ private int marginBottom = 1440; /** Page width. */ private int pageWidth = 12240; /** Page height. */ private int pageHeight = 15840; /** Factor to use when converting. */ protected double twipsFactor = 20.5714; /** Current annotation ID. */ private int currentAnnotationID = 0; /** Current list ID. */ private int currentListID = 1; /** List of current Lists. */ private Vector listIds = null; /** Current List Level. */ private int listLevel = 0; /** Current maximum List Level. */ private int maxListLevel = 0; /** Protected Constructor */ /** * Constructs a <CODE>RtfWriter</CODE>. * * @param document The <CODE>Document</CODE> that is to be written as RTF * @param os The <CODE>OutputStream</CODE> the writer has to write to. */ protected RtfWriter(Document doc, OutputStream os) { super(doc, os); document.addDocListener(this); initDefaults(); } /** Public functions from the DocWriter Interface */ /** * Gets an instance of the <CODE>RtfWriter</CODE>. * * @param document The <CODE>Document</CODE> that has to be written * @param os The <CODE>OutputStream</CODE> the writer has to write to. * @return a new <CODE>RtfWriter</CODE> */ public static RtfWriter getInstance(Document document, OutputStream os) { return(new RtfWriter(document, os)); } /** * Signals that the <CODE>Document</CODE> has been opened and that * <CODE>Elements</CODE> can be added. */ public void open() { super.open(); } /** * Signals that the <CODE>Document</CODE> was closed and that no other * <CODE>Elements</CODE> will be added. * <p> * The content of the font table, color table, information group, content, header, footer are merged into the final * <code>OutputStream</code> */ public void close() { writeDocument(); super.close(); } /** * Adds the footer to the bottom of the <CODE>Document</CODE>. */ public void setFooter(HeaderFooter footer) { this.footer = footer; } /** * Adds the header to the top of the <CODE>Document</CODE>. */ public void setHeader(HeaderFooter header) { this.header = 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() throws DocumentException { try { content.write(escape); content.write(newPage); } catch(IOException e) { return false; } 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) { pageWidth = (int) (pageSize.width() * twipsFactor); pageHeight = (int) (pageSize.height() * twipsFactor); return true; } /** * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>. * * @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 { return addElement(element, content); } /** Private functions */ /** * Adds an <CODE>Element</CODE> to the <CODE>Document</CODE>. * @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 */ private 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 : writeTable((Table) element, out); break; case Element.ANNOTATION : writeAnnotation((Annotation) element, out); break; case Element.PNG : case Element.JPEG : writeImage((Image) element, 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 */ private void writeSection(Section sectionElement, ByteArrayOutputStream out) throws IOException { out.write(escape); out.write(sectionDefaults); if(sectionElement.title() != null) { sectionElement.title().process(this); out.write(escape); out.write(paragraph); } sectionElement.process(this); out.write(escape); out.write(section); } /** * Write the beginning of a new <code>Paragraph</code> * * <<<<<<< RtfWriter.java * @param paragraphElement The <code>Paragraph</code> to be written * @param out The <code>ByteArrayOutputStream</code> to write to * * @throws IOException, DocumentExceptipn */ private void writeParagraph(Paragraph paragraphElement, ByteArrayOutputStream out) throws IOException, DocumentException { out.write(escape); out.write(paragraphDefaults); switch(paragraphElement.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(escape); out.write(listIndent); writeInt(out, (int) (paragraphElement.indentationLeft() * twipsFactor)); Iterator chunks = paragraphElement.getChunks().iterator(); while(chunks.hasNext()) { Chunk ch = (Chunk) chunks.next(); ch.setFont(ch.font().difference(paragraphElement.font())); addElement(ch, out); } out.write(escape); out.write(paragraph); } /** * Write a <code>Phrase</code>. * * @param chunk The <code>Phrase</code> item to be written * @param out The <code>ByteArrayOutputStream</code> to write to * * @throws IOException, DocumentException */ private void writePhrase(Phrase phrase, ByteArrayOutputStream out) throws IOException, DocumentException { out.write(escape); out.write(paragraphDefaults); Iterator chunks = phrase.getChunks().iterator(); while(chunks.hasNext()) { Chunk ch = (Chunk) chunks.next(); addElement(ch, out); } } /** * Write an <code>Anchor</code>. Anchors are treated like Phrases. * * @param chunk 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, DocumentException { if (anchor.url() != 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.url().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. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -