From 6025b6016517c6d898d8957d1d7e03ba71431912 Mon Sep 17 00:00:00 2001 From: tknall Date: Fri, 1 Dec 2006 12:20:24 +0000 Subject: Initial import of release 2.2. git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@4 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../com/lowagie/text/rtf/document/RtfCodePage.java | 102 +++++ .../com/lowagie/text/rtf/document/RtfDocument.java | 301 ++++++++++++++ .../text/rtf/document/RtfDocumentHeader.java | 310 +++++++++++++++ .../text/rtf/document/RtfDocumentSettings.java | 182 +++++++++ .../lowagie/text/rtf/document/RtfInfoElement.java | 181 +++++++++ .../lowagie/text/rtf/document/RtfInfoGroup.java | 117 ++++++ .../lowagie/text/rtf/document/RtfPageSetting.java | 435 +++++++++++++++++++++ .../text/rtf/document/output/RtfDataCache.java | 86 ++++ .../text/rtf/document/output/RtfDiskCache.java | 113 ++++++ .../text/rtf/document/output/RtfMemoryCache.java | 93 +++++ 10 files changed, 1920 insertions(+) create mode 100644 src/main/java/com/lowagie/text/rtf/document/RtfCodePage.java create mode 100644 src/main/java/com/lowagie/text/rtf/document/RtfDocument.java create mode 100644 src/main/java/com/lowagie/text/rtf/document/RtfDocumentHeader.java create mode 100644 src/main/java/com/lowagie/text/rtf/document/RtfDocumentSettings.java create mode 100644 src/main/java/com/lowagie/text/rtf/document/RtfInfoElement.java create mode 100644 src/main/java/com/lowagie/text/rtf/document/RtfInfoGroup.java create mode 100644 src/main/java/com/lowagie/text/rtf/document/RtfPageSetting.java create mode 100644 src/main/java/com/lowagie/text/rtf/document/output/RtfDataCache.java create mode 100644 src/main/java/com/lowagie/text/rtf/document/output/RtfDiskCache.java create mode 100644 src/main/java/com/lowagie/text/rtf/document/output/RtfMemoryCache.java (limited to 'src/main/java/com/lowagie/text/rtf/document') diff --git a/src/main/java/com/lowagie/text/rtf/document/RtfCodePage.java b/src/main/java/com/lowagie/text/rtf/document/RtfCodePage.java new file mode 100644 index 0000000..e88a942 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/document/RtfCodePage.java @@ -0,0 +1,102 @@ +/* + * $Id: RtfCodePage.java,v 1.16 2005/05/04 14:33:53 blowagie Exp $ + * $Name: $ + * + * Copyright 2003, 2004 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.document; + +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.RtfExtendedElement; + +import java.io.IOException; +import java.io.ByteArrayOutputStream; + +/** + * The RtfCodePage class allows different code pages to be used in the rtf document. + * Currently always ansi / ansicpg1252 + * + * Version: $Id: RtfCodePage.java,v 1.16 2005/05/04 14:33:53 blowagie Exp $ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfCodePage extends RtfElement implements RtfExtendedElement { + /** + * Constant for ansi encoded rtf documents + */ + private static final byte[] ANSI = "\\ansi".getBytes(); + /** + * Constant for the ansi codepage + */ + private static final byte[] ANSI_CODEPAGE = "\\ansicpg".getBytes(); + + /** + * Construct an RtfCodePage + * + * @param doc The RtfDocument this RtfCodePage belongs to + */ + public RtfCodePage(RtfDocument doc) { + super(doc); + } + + /** + * Writes the selected codepage to a byte array + * + * @return Byte array with the current codepage + */ + public byte[] writeDefinition() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(ANSI); + result.write(ANSI_CODEPAGE); + result.write(intToByteArray(1252)); + result.write((byte)'\n'); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } +} diff --git a/src/main/java/com/lowagie/text/rtf/document/RtfDocument.java b/src/main/java/com/lowagie/text/rtf/document/RtfDocument.java new file mode 100644 index 0000000..63f0321 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/document/RtfDocument.java @@ -0,0 +1,301 @@ +/* + * $Id: RtfDocument.java,v 1.16 2005/12/24 13:14:59 hallm Exp $ + * $Name: $ + * + * Copyright 2003, 2004, 2005 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.document; + +import com.lowagie.text.DocumentException; +import com.lowagie.text.rtf.RtfBasicElement; +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.RtfMapper; +import com.lowagie.text.rtf.document.output.RtfDataCache; +import com.lowagie.text.rtf.document.output.RtfDiskCache; +import com.lowagie.text.rtf.document.output.RtfMemoryCache; +import com.lowagie.text.rtf.graphic.RtfImage; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; + +/** + * The RtfDocument stores all document related data and also the main data stream. + * INTERNAL CLASS - NOT TO BE USED DIRECTLY + * + * Version: $Id: RtfDocument.java,v 1.16 2005/12/24 13:14:59 hallm Exp $ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + * @author Todd Bush (Todd.Bush@canopysystems.com) [Tab support] + */ +public class RtfDocument extends RtfElement { + /** + * Stores the actual document data + */ + private RtfDataCache data = null; + /** + * The RtfMapper to use in this RtfDocument + */ + private RtfMapper mapper = null; + /** + * The RtfDocumentHeader that handles all document header methods + */ + private RtfDocumentHeader documentHeader = null; + /** + * Stores integers that have been generated as unique random numbers + */ + private ArrayList previousRandomInts = null; + /** + * Whether to automatically generate TOC entries for Chapters and Sections. Defaults to false + */ + private boolean autogenerateTOCEntries = false; + /** + * Whether data has been written to the RtfDataCache. + */ + private boolean dataWritten = false; + /** + * The RtfDocumentSettings for this RtfDocument. + */ + private RtfDocumentSettings documentSettings = null; + /** + * The last RtfBasicElement that was added directly to the RtfDocument. + */ + private RtfBasicElement lastElementWritten = null; + + /** + * Constant for the Rtf document start + */ + private static final byte[] RTF_DOCUMENT = "\\rtf1".getBytes(); + + /** + * The default constructor for a RtfDocument + */ + public RtfDocument() { + super(null); + data = new RtfMemoryCache(); + mapper = new RtfMapper(this); + documentHeader = new RtfDocumentHeader(this); + documentHeader.init(); + previousRandomInts = new ArrayList(); + this.documentSettings = new RtfDocumentSettings(this); + } + + /** + * Writes the document + * + * @return A byte array containing the complete rtf document + */ + public byte[] writeDocument() { + ByteArrayOutputStream docStream = new ByteArrayOutputStream(); + try { + docStream.write(OPEN_GROUP); + docStream.write(RtfDocument.RTF_DOCUMENT); + docStream.write(documentHeader.write()); + data.writeTo(docStream); + docStream.write(CLOSE_GROUP); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return docStream.toByteArray(); + } + + /** + * Adds an element to the rtf document + * + * @param element The element to add + */ + public void add(RtfBasicElement element) { + try { + if(element instanceof RtfInfoElement) { + this.documentHeader.addInfoElement((RtfInfoElement) element); + } else { + this.dataWritten = true; + if(element instanceof RtfImage) { + ((RtfImage) element).setTopLevelElement(true); + } + data.getOutputStream().write(element.write()); + this.lastElementWritten = element; + } + } catch(IOException ioe) { + ioe.printStackTrace(); + } + } + + /** + * Gets the RtfMapper object of this RtfDocument + * + * @return The RtfMapper + */ + public RtfMapper getMapper() { + return mapper; + } + + /** + * Generates a random integer that is unique with respect to the document. + * + * @return A random int + */ + public int getRandomInt() { + Integer newInt = null; + do { + newInt = new Integer((int) (Math.random() * Integer.MAX_VALUE)); + } while(previousRandomInts.contains(newInt)); + previousRandomInts.add(newInt); + return newInt.intValue(); + } + + /** + * Gets the RtfDocumentHeader of this RtfDocument + * + * @return The RtfDocumentHeader of this RtfDocument + */ + public RtfDocumentHeader getDocumentHeader() { + return this.documentHeader; + } + + /** + * Replaces special characters with their unicode values + * @param str The original String + * @param useHex indicated if the hexadecimal value has to be used + * @param softLineBreaks whether to use soft line breaks instead of default hard ones. + * + * @return The converted String + */ + public String filterSpecialChar(String str, boolean useHex, boolean softLineBreaks) { + int length = str.length(); + int z = (int) 'z'; + StringBuffer ret = new StringBuffer(length); + for (int i = 0; i < length; i++) { + char ch = str.charAt(i); + + if (ch == '\\') { + ret.append("\\\\"); + } else if (ch == '\n') { + if(softLineBreaks) { + ret.append("\\line "); + } else { + ret.append("\\par "); + } + } else if (ch == '\t') { + ret.append("\\tab "); + } else if (((int) ch) > z && this.documentSettings.isAlwaysUseUnicode()) { + if(useHex) { + ret.append("\\\'").append(Long.toHexString((long) ch)); + } else { + ret.append("\\u").append((long) ch).append('?'); + } + } else { + ret.append(ch); + } + } + String s = ret.toString(); + if(s.indexOf("$newpage$") >= 0) { + String before = s.substring(0, s.indexOf("$newpage$")); + String after = s.substring(s.indexOf("$newpage$") + 9); + ret = new StringBuffer(before); + ret.append("\\page\\par "); + ret.append(after); + return ret.toString(); + } + return s; + } + + /** + * Whether to automagically generate table of contents entries when + * adding Chapters or Sections. + * + * @param autogenerate Whether to automatically generate TOC entries + */ + public void setAutogenerateTOCEntries(boolean autogenerate) { + this.autogenerateTOCEntries = autogenerate; + } + + /** + * Get whether to autmatically generate table of contents entries + * + * @return Wheter to automatically generate TOC entries + */ + public boolean getAutogenerateTOCEntries() { + return this.autogenerateTOCEntries; + } + + /** + * Sets the rtf data cache style to use. Valid values are given in the + * RtfDataCache class. + * + * @param dataCacheStyle The style to use. + * @throws DocumentException If data has already been written into the data cache. + * @throws IOException If the disk cache could not be initialised. + */ + public void setDataCacheStyle(int dataCacheStyle) throws DocumentException, IOException { + if(dataWritten) { + throw new DocumentException("Data has already been written into the data cache. You can not change the cache style anymore."); + } + switch(dataCacheStyle) { + case RtfDataCache.CACHE_MEMORY : this.data = new RtfMemoryCache(); break; + case RtfDataCache.CACHE_DISK : this.data = new RtfDiskCache(); break; + default : this.data = new RtfMemoryCache(); break; + } + } + + /** + * Gets the RtfDocumentSettings that specify how the rtf document is generated. + * + * @return The current RtfDocumentSettings. + */ + public RtfDocumentSettings getDocumentSettings() { + return this.documentSettings; + } + + /** + * Gets the last RtfBasicElement that was directly added to the RtfDocument. + * + * @return The last RtfBasicElement that was directly added to the RtfDocument. + */ + public RtfBasicElement getLastElementWritten() { + return this.lastElementWritten; + } +} diff --git a/src/main/java/com/lowagie/text/rtf/document/RtfDocumentHeader.java b/src/main/java/com/lowagie/text/rtf/document/RtfDocumentHeader.java new file mode 100644 index 0000000..1ae8027 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/document/RtfDocumentHeader.java @@ -0,0 +1,310 @@ +/* + * $Id: RtfDocumentHeader.java,v 1.16 2005/12/24 13:14:59 hallm Exp $ + * $Name: $ + * + * Copyright 2003, 2004 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.document; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import com.lowagie.text.HeaderFooter; +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter; +import com.lowagie.text.rtf.headerfooter.RtfHeaderFooterGroup; +import com.lowagie.text.rtf.list.RtfList; +import com.lowagie.text.rtf.list.RtfListTable; +import com.lowagie.text.rtf.style.RtfColor; +import com.lowagie.text.rtf.style.RtfColorList; +import com.lowagie.text.rtf.style.RtfFont; +import com.lowagie.text.rtf.style.RtfFontList; +import com.lowagie.text.rtf.style.RtfParagraphStyle; +import com.lowagie.text.rtf.style.RtfStylesheetList; + + +/** + * The RtfDocumentHeader contains all classes required for the generation of + * the document header area. + * + * @version $Version:$ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfDocumentHeader extends RtfElement { + /** + * Constant for the title page + */ + private static final byte[] TITLE_PAGE = "\\titlepg".getBytes(); + /** + * Constant for facing pages + */ + private static final byte[] FACING_PAGES = "\\facingp".getBytes(); + + /** + * The code page to use + */ + private RtfCodePage codePage = null; + /** + * Stores all the colors used in the document + */ + private RtfColorList colorList = null; + /** + * Stores all the fonts used in the document + */ + private RtfFontList fontList = null; + /** + * Manages List tables + */ + private RtfListTable listTable = null; + /** + * Stores all paragraph styles used in the document. + */ + private RtfStylesheetList stylesheetList = null; + /** + * The information group with author/subject/keywords/title/producer/creationdate data + */ + private RtfInfoGroup infoGroup = null; + /** + * The page settings + */ + private RtfPageSetting pageSetting = null; + /** + * The current RtfHeaderFooterGroup for the header + */ + private RtfHeaderFooterGroup header = null; + /** + * The current RtfHeaderFooterGroup for the footer + */ + private RtfHeaderFooterGroup footer = null; + + /** + * Constructs a RtfDocumentHeader for a RtfDocument + * + * @param doc The RtfDocument this RtfDocumentHeader belongs to + */ + protected RtfDocumentHeader(RtfDocument doc) { + super(doc); + } + + /** + * Initialises the RtfDocumentHeader. + */ + protected void init() { + this.codePage = new RtfCodePage(this.document); + this.colorList = new RtfColorList(this.document); + this.fontList = new RtfFontList(this.document); + this.listTable = new RtfListTable(this.document); + this.stylesheetList = new RtfStylesheetList(this.document); + this.infoGroup = new RtfInfoGroup(this.document); + this.pageSetting = new RtfPageSetting(this.document); + this.header = new RtfHeaderFooterGroup(this.document, RtfHeaderFooter.TYPE_HEADER); + this.footer = new RtfHeaderFooterGroup(this.document, RtfHeaderFooter.TYPE_FOOTER); + } + + /** + * Write the contents of the document header area. + * + * @return A byte array with the contents of the document header area + */ + public byte[] write() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(this.codePage.writeDefinition()); + result.write(this.fontList.writeDefinition()); + result.write(this.colorList.writeDefinition()); + result.write(this.stylesheetList.writeDefinition()); + result.write(this.listTable.writeDefinition()); + result.write(this.infoGroup.write()); + result.write(this.pageSetting.writeDefinition()); + result.write(writeSectionDefinition()); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Writes the section definition data + * + * @return A byte array with the section definition data + */ + public byte[] writeSectionDefinition() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + if(header.hasTitlePage() || footer.hasTitlePage()) { + result.write(TITLE_PAGE); + header.setHasTitlePage(); + footer.setHasTitlePage(); + } + if(header.hasFacingPages() || footer.hasFacingPages()) { + result.write(FACING_PAGES); + header.setHasFacingPages(); + footer.setHasFacingPages(); + } + result.write(footer.write()); + result.write(header.write()); + result.write(pageSetting.writeSectionDefinition()); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Gets the number of the specified RtfFont + * + * @param font The RtfFont for which to get the number + * @return The number of the font + */ + public int getFontNumber(RtfFont font) { + return this.fontList.getFontNumber(font); + } + + /** + * Gets the number of the specified RtfColor + * + * @param color The RtfColor for which to get the number + * @return The number of the color + */ + public int getColorNumber(RtfColor color) { + return this.colorList.getColorNumber(color); + } + + /** + * Gets the number of the specified RtfList + * + * @param list The RtfList for which to get the number + * @return The number of the list + */ + public int getListNumber(RtfList list) { + return this.listTable.getListNumber(list); + } + + /** + * Gets the RtfParagraphStyle with the given style name. + * + * @param styleName The style name of the RtfParagraphStyle to get. + * @return The RtfParagraphStyle with the given style name or null. + */ + public RtfParagraphStyle getRtfParagraphStyle(String styleName) { + return this.stylesheetList.getRtfParagraphStyle(styleName); + } + + /** + * Removes a RtfList from the list table + * + * @param list The RtfList to remove + */ + public void freeListNumber(RtfList list) { + this.listTable.freeListNumber(list); + } + + /** + * Gets the RtfPageSetting object of this RtfDocument + * + * @return The RtfPageSetting object + */ + public RtfPageSetting getPageSetting() { + return this.pageSetting; + } + + /** + * Adds an RtfInfoElement to the list of RtfInfoElements + * + * @param rtfInfoElement The RtfInfoElement to add + */ + public void addInfoElement(RtfInfoElement rtfInfoElement) { + this.infoGroup.add(rtfInfoElement); + } + + /** + * Sets the current header to use + * + * @param header The HeaderFooter to use as header + */ + public void setHeader(HeaderFooter header) { + if(header != null) { + if(header instanceof RtfHeaderFooterGroup) { + this.header = new RtfHeaderFooterGroup(this.document, (RtfHeaderFooterGroup) header, RtfHeaderFooter.TYPE_HEADER); + } else if(header instanceof RtfHeaderFooter) { + this.header = new RtfHeaderFooterGroup(this.document, (RtfHeaderFooter) header, RtfHeaderFooter.TYPE_HEADER); + } else { + this.header = new RtfHeaderFooterGroup(this.document, header, RtfHeaderFooter.TYPE_HEADER); + } + } else { + this.header = new RtfHeaderFooterGroup(this.document, RtfHeaderFooter.TYPE_HEADER); + } + } + + /** + * Sets the current footer to use + * + * @param footer The HeaderFooter to use as footer + */ + public void setFooter(HeaderFooter footer) { + if(footer != null) { + if(footer instanceof RtfHeaderFooterGroup) { + this.footer = new RtfHeaderFooterGroup(this.document, (RtfHeaderFooterGroup) footer, RtfHeaderFooter.TYPE_FOOTER); + } else if(footer instanceof RtfHeaderFooter) { + this.footer = new RtfHeaderFooterGroup(this.document, (RtfHeaderFooter) footer, RtfHeaderFooter.TYPE_FOOTER); + } else { + this.footer = new RtfHeaderFooterGroup(this.document, footer, RtfHeaderFooter.TYPE_FOOTER); + } + } else { + this.footer = new RtfHeaderFooterGroup(this.document, RtfHeaderFooter.TYPE_FOOTER); + } + } + + /** + * Registers the RtfParagraphStyle for further use in the document. + * + * @param rtfParagraphStyle The RtfParagraphStyle to register. + */ + public void registerParagraphStyle(RtfParagraphStyle rtfParagraphStyle) { + this.stylesheetList.registerParagraphStyle(rtfParagraphStyle); + } +} diff --git a/src/main/java/com/lowagie/text/rtf/document/RtfDocumentSettings.java b/src/main/java/com/lowagie/text/rtf/document/RtfDocumentSettings.java new file mode 100644 index 0000000..15b5b92 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/document/RtfDocumentSettings.java @@ -0,0 +1,182 @@ +/* + * $Id: RtfDocumentSettings.java,v 1.4 2005/12/24 13:14:59 hallm Exp $ + * $Name: $ + * + * Copyright 2003, 2004, 2005 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.document; + +import com.lowagie.text.rtf.style.RtfParagraphStyle; + + +/** + * The RtfDocumentSettings contains output specific settings. These settings modify + * how the actual document is then generated and some settings may mean that some + * RTF readers can't read the document or render it wrongly. + * + * @version $Revision: 1.4 $ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfDocumentSettings { + + /** + * The RtfDocument this RtfDocumentSettings belongs to. + */ + private RtfDocument document = null; + /** + * Whether to also output the table row definition after the cell content. + */ + private boolean outputTableRowDefinitionAfter = true; + /** + * Whether to output the line breaks that make the rtf document source more readable. + */ + private boolean outputDebugLineBreaks = true; + /** + * Whether to always generate soft linebreaks for \n in Chunks. + */ + private boolean alwaysGenerateSoftLinebreaks = false; + /** + * Whether to always translate characters past 'z' into unicode representations. + */ + private boolean alwaysUseUnicode = true; + + + /** + * Constructs a new RtfDocumentSettings object. + * + * @param document The RtfDocument this RtfDocumentSettings belong to. + */ + public RtfDocumentSettings(RtfDocument document) { + this.document = document; + } + + /** + * Gets whether to output the line breaks for increased rtf document readability. + * + * @return Whether to output line breaks. + */ + public boolean isOutputDebugLineBreaks() { + return outputDebugLineBreaks; + } + + /** + * Sets whether to output the line breaks for increased rtf document readability. + * Some line breaks may be added where the rtf specification demands it. + * + * @param outputDebugLineBreaks The outputDebugLineBreaks to set. + */ + public void setOutputDebugLineBreaks(boolean outputDebugLineBreaks) { + this.outputDebugLineBreaks = outputDebugLineBreaks; + } + + /** + * Gets whether the table row definition should also be written after the cell content. + * + * @return Returns the outputTableRowDefinitionAfter. + */ + public boolean isOutputTableRowDefinitionAfter() { + return outputTableRowDefinitionAfter; + } + + /** + * Sets whether the table row definition should also be written after the cell content. + * This is recommended to be set to true if you need Word2000 compatiblity and + * false if the document should be opened in OpenOffice.org Writer. + * + * @param outputTableRowDefinitionAfter The outputTableRowDefinitionAfter to set. + */ + public void setOutputTableRowDefinitionAfter( + boolean outputTableRowDefinitionAfter) { + this.outputTableRowDefinitionAfter = outputTableRowDefinitionAfter; + } + + /** + * Gets whether all linebreaks inside Chunks are generated as soft linebreaks. + * + * @return True if soft linebreaks are generated, false for hard linebreaks. + */ + public boolean isAlwaysGenerateSoftLinebreaks() { + return this.alwaysGenerateSoftLinebreaks; + } + + /** + * Sets whether to always generate soft linebreaks. + * + * @param alwaysGenerateSoftLinebreaks Whether to always generate soft linebreaks. + */ + public void setAlwaysGenerateSoftLinebreaks(boolean alwaysGenerateSoftLinebreaks) { + this.alwaysGenerateSoftLinebreaks = alwaysGenerateSoftLinebreaks; + } + + /** + * Gets whether all characters bigger than 'z' are represented as unicode. + * + * @return True if unicode representation is used, false otherwise. + */ + public boolean isAlwaysUseUnicode() { + return this.alwaysUseUnicode; + } + + /** + * Sets whether to represent all characters bigger than 'z' as unicode. + * + * @param alwaysUseUnicode True to use unicode representation, false otherwise. + */ + public void setAlwaysUseUnicode(boolean alwaysUseUnicode) { + this.alwaysUseUnicode = alwaysUseUnicode; + } + + /** + * Registers the RtfParagraphStyle for further use in the document. This does not need to be + * done for the default styles in the RtfParagraphStyle object. Those are added automatically. + * + * @param rtfParagraphStyle The RtfParagraphStyle to register. + */ + public void registerParagraphStyle(RtfParagraphStyle rtfParagraphStyle) { + this.document.getDocumentHeader().registerParagraphStyle(rtfParagraphStyle); + } +} diff --git a/src/main/java/com/lowagie/text/rtf/document/RtfInfoElement.java b/src/main/java/com/lowagie/text/rtf/document/RtfInfoElement.java new file mode 100644 index 0000000..5549684 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/document/RtfInfoElement.java @@ -0,0 +1,181 @@ +/* + * $Id: RtfInfoElement.java,v 1.16 2005/05/04 14:33:53 blowagie Exp $ + * $Name: $ + * + * Copyright 2003, 2004 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.document; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import com.lowagie.text.Meta; +import com.lowagie.text.rtf.RtfElement; + + +/** + * Stores one information group element. Valid elements are + * author, title, subject, keywords, producer and creationdate. + * + * @version $Version:$ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfInfoElement extends RtfElement { + + /** + * Constant for the author element + */ + private static final byte[] INFO_AUTHOR = "\\author".getBytes(); + /** + * Constant for the subject element + */ + private static final byte[] INFO_SUBJECT = "\\subject".getBytes(); + /** + * Constant for the keywords element + */ + private static final byte[] INFO_KEYWORDS = "\\keywords".getBytes(); + /** + * Constant for the title element + */ + private static final byte[] INFO_TITLE = "\\title".getBytes(); + /** + * Constant for the producer element + */ + private static final byte[] INFO_PRODUCER = "\\operator".getBytes(); + /** + * Constant for the creationdate element + */ + private static final byte[] INFO_CREATION_DATE = "\\creationdate".getBytes(); + + /** + * The type of this RtfInfoElement. The values from Element.INFO_ELEMENT_NAME are used. + */ + private int infoType = -1; + /** + * The content of this RtfInfoElement + */ + private String content = ""; + + /** + * Constructs a RtfInfoElement based on the given Meta object + * + * @param doc The RtfDocument this RtfInfoElement belongs to + * @param meta The Meta object this RtfInfoElement is based on + */ + public RtfInfoElement(RtfDocument doc, Meta meta) { + super(doc); + infoType = meta.type(); + content = meta.content(); + } + + /** + * Writes this RtfInfoElement + * + * @return A byte array containing the RtfInfoElement data + */ + public byte[] write() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(OPEN_GROUP); + switch(infoType) { + case Meta.AUTHOR: + result.write(INFO_AUTHOR); + break; + case Meta.SUBJECT: + result.write(INFO_SUBJECT); + break; + case Meta.KEYWORDS: + result.write(INFO_KEYWORDS); + break; + case Meta.TITLE: + result.write(INFO_TITLE); + break; + case Meta.PRODUCER: + result.write(INFO_PRODUCER); + break; + case Meta.CREATIONDATE: + result.write(INFO_CREATION_DATE); + break; + default: + result.write(INFO_AUTHOR); + break; + } + result.write(DELIMITER); + if(infoType == Meta.CREATIONDATE) { + result.write(convertDate(content).getBytes()); + } else { + result.write(content.getBytes()); + } + result.write(CLOSE_GROUP); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Converts a date from the format used by iText to the format required by + * rtf.
iText: EEE MMM dd HH:mm:ss zzz yyyy - rtf: \\'yr'yyyy\\'mo'MM\\'dy'dd\\'hr'HH\\'min'mm\\'sec'ss + * + * @param date The date formated by iText + * @return The date formated for rtf + */ + private String convertDate(String date) { + SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"); + try { + Date creationDate = sdf.parse(date); + sdf = new SimpleDateFormat("\\'yr'yyyy\\'mo'MM\\'dy'dd\\'hr'HH\\'min'mm\\'sec'ss"); + return sdf.format(creationDate); + } catch(ParseException pe) { + pe.printStackTrace(); + return ""; + } + } +} diff --git a/src/main/java/com/lowagie/text/rtf/document/RtfInfoGroup.java b/src/main/java/com/lowagie/text/rtf/document/RtfInfoGroup.java new file mode 100644 index 0000000..fc6c46a --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/document/RtfInfoGroup.java @@ -0,0 +1,117 @@ +/* + * $Id: RtfInfoGroup.java,v 1.16 2005/05/04 14:33:52 blowagie Exp $ + * $Name: $ + * + * Copyright 2003, 2004 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.document; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; + +import com.lowagie.text.rtf.RtfElement; + + +/** + * The RtfInfoGroup stores information group elements. + * + * @version $Version:$ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfInfoGroup extends RtfElement { + /** + * Information group starting tag + */ + private static final byte[] INFO_GROUP = "\\info".getBytes(); + + /** + * The RtfInfoElements that belong to this RtfInfoGroup + */ + ArrayList infoElements = null; + + /** + * Constructs a RtfInfoGroup belonging to a RtfDocument + * + * @param doc The RtfDocument this RtfInfoGroup belongs to + */ + public RtfInfoGroup(RtfDocument doc) { + super(doc); + infoElements = new ArrayList(); + } + + /** + * Adds an RtfInfoElement to the RtfInfoGroup + * + * @param infoElement The RtfInfoElement to add + */ + public void add(RtfInfoElement infoElement) { + this.infoElements.add(infoElement); + } + + /** + * Writes the RtfInfoGroup and its RtfInfoElement elements. + * + * @return A byte array containing the group and its elements + */ + public byte[] write() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(OPEN_GROUP); + result.write(INFO_GROUP); + for(int i = 0; i < infoElements.size(); i++) { + RtfInfoElement infoElement = (RtfInfoElement) infoElements.get(i); + result.write(infoElement.write()); + } + result.write(CLOSE_GROUP); + result.write((byte)'\n'); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } +} diff --git a/src/main/java/com/lowagie/text/rtf/document/RtfPageSetting.java b/src/main/java/com/lowagie/text/rtf/document/RtfPageSetting.java new file mode 100644 index 0000000..fd456e4 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/document/RtfPageSetting.java @@ -0,0 +1,435 @@ +/* + * $Id: RtfPageSetting.java,v 1.15 2005/02/23 16:57:45 hallm Exp $ + * $Name: $ + * + * Copyright 2003, 2004 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.document; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import com.lowagie.text.PageSize; +import com.lowagie.text.Rectangle; +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.RtfExtendedElement; + + +/** + * The RtfPageSetting stores the page size / page margins for a RtfDocument. + * INTERNAL CLASS - NOT TO BE USED DIRECTLY + * + * @version $Version:$ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfPageSetting extends RtfElement implements RtfExtendedElement { + + /** + * Constant for the page height + */ + private static final byte[] PAGE_WIDTH = "\\paperw".getBytes(); + /** + * Constant for the page width + */ + private static final byte[] PAGE_HEIGHT = "\\paperh".getBytes(); + /** + * Constant for the left margin + */ + private static final byte[] MARGIN_LEFT = "\\margl".getBytes(); + /** + * Constant for the right margin + */ + private static final byte[] MARGIN_RIGHT = "\\margr".getBytes(); + /** + * Constant for the top margin + */ + private static final byte[] MARGIN_TOP = "\\margt".getBytes(); + /** + * Constant for the bottom margin + */ + private static final byte[] MARGIN_BOTTOM = "\\margb".getBytes(); + /** + * Constant for landscape + */ + private static final byte[] LANDSCAPE = "\\lndscpsxn".getBytes(); + /** + * Constant for the section page width + */ + private static final byte[] SECTION_PAGE_WIDTH = "\\pgwsxn".getBytes(); + /** + * Constant for the section page height + */ + private static final byte[] SECTION_PAGE_HEIGHT = "\\pghsxn".getBytes(); + /** + * Constant for the section left margin + */ + private static final byte[] SECTION_MARGIN_LEFT = "\\marglsxn".getBytes(); + /** + * Constant for the section right margin + */ + private static final byte[] SECTION_MARGIN_RIGHT = "\\margrsxn".getBytes(); + /** + * Constant for the section top margin + */ + private static final byte[] SECTION_MARGIN_TOP = "\\margtsxn".getBytes(); + /** + * Constant for the section bottom margin + */ + private static final byte[] SECTION_MARGIN_BOTTOM = "\\margbsxn".getBytes(); + + /** + * The page width to use + */ + private int pageWidth = 11906; + /** + * The page height to use + */ + private int pageHeight = 16840; + /** + * The left margin to use + */ + private int marginLeft = 1800; + /** + * The right margin to use + */ + private int marginRight = 1800; + /** + * The top margin to use + */ + private int marginTop = 1440; + /** + * The bottom margin to use + */ + private int marginBottom = 1440; + /** + * Whether the page is portrait or landscape + */ + private boolean landscape = false; + + /** + * Constructs a new RtfPageSetting object belonging to a RtfDocument. + * + * @param doc The RtfDocument this RtfPageSetting belongs to + */ + public RtfPageSetting(RtfDocument doc) { + super(doc); + } + + /** + * Writes the page size / page margin definition + * + * @return A byte array with the page size / page margin definition + */ + public byte[] writeDefinition() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(PAGE_WIDTH); + result.write(intToByteArray(pageWidth)); + result.write(PAGE_HEIGHT); + result.write(intToByteArray(pageHeight)); + result.write(MARGIN_LEFT); + result.write(intToByteArray(marginLeft)); + result.write(MARGIN_RIGHT); + result.write(intToByteArray(marginRight)); + result.write(MARGIN_TOP); + result.write(intToByteArray(marginTop)); + result.write(MARGIN_BOTTOM); + result.write(intToByteArray(marginBottom)); + result.write((byte)'\n'); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Writes the definition part for a new section + * + * @return A byte array containing the definition for a new section + */ + public byte[] writeSectionDefinition() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + if(landscape) { + result.write(LANDSCAPE); + result.write(SECTION_PAGE_WIDTH); + result.write(intToByteArray(pageWidth)); + result.write(SECTION_PAGE_HEIGHT); + result.write(intToByteArray(pageHeight)); + result.write((byte)'\n'); + } else { + result.write(SECTION_PAGE_WIDTH); + result.write(intToByteArray(pageWidth)); + result.write(SECTION_PAGE_HEIGHT); + result.write(intToByteArray(pageHeight)); + result.write((byte)'\n'); + } + result.write(SECTION_MARGIN_LEFT); + result.write(intToByteArray(marginLeft)); + result.write(SECTION_MARGIN_RIGHT); + result.write(intToByteArray(marginRight)); + result.write(SECTION_MARGIN_TOP); + result.write(intToByteArray(marginTop)); + result.write(SECTION_MARGIN_BOTTOM); + result.write(intToByteArray(marginBottom)); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Gets the bottom margin + * + * @return Returns the bottom margin + */ + public int getMarginBottom() { + return marginBottom; + } + + /** + * Sets the bottom margin + * + * @param marginBottom The bottom margin to use + */ + public void setMarginBottom(int marginBottom) { + this.marginBottom = marginBottom; + } + + /** + * Gets the left margin + * + * @return Returns the left margin + */ + public int getMarginLeft() { + return marginLeft; + } + + /** + * Sets the left margin to use + * + * @param marginLeft The left margin to use + */ + public void setMarginLeft(int marginLeft) { + this.marginLeft = marginLeft; + } + + /** + * Gets the right margin + * + * @return Returns the right margin + */ + public int getMarginRight() { + return marginRight; + } + + /** + * Sets the right margin to use + * + * @param marginRight The right margin to use + */ + public void setMarginRight(int marginRight) { + this.marginRight = marginRight; + } + + /** + * Gets the top margin + * + * @return Returns the top margin + */ + public int getMarginTop() { + return marginTop; + } + + /** + * Sets the top margin to use + * + * @param marginTop The top margin to use + */ + public void setMarginTop(int marginTop) { + this.marginTop = marginTop; + } + + /** + * Gets the page height + * + * @return Returns the page height + */ + public int getPageHeight() { + return pageHeight; + } + + /** + * Sets the page height to use + * + * @param pageHeight The page height to use + */ + public void setPageHeight(int pageHeight) { + this.pageHeight = pageHeight; + } + + /** + * Gets the page width + * + * @return Returns the page width + */ + public int getPageWidth() { + return pageWidth; + } + + /** + * Sets the page width to use + * + * @param pageWidth The page width to use + */ + public void setPageWidth(int pageWidth) { + this.pageWidth = pageWidth; + } + + /** + * Set the page size to use. This method will use guessFormat to try to guess the correct + * page format. If no format could be guessed, the sizes from the pageSize are used and + * the landscape setting is determined by comparing width and height; + * + * @param pageSize The pageSize to use + */ + public void setPageSize(Rectangle pageSize) { + if(!guessFormat(pageSize, false)) { + this.pageWidth = (int) (pageSize.width() * RtfElement.TWIPS_FACTOR); + this.pageHeight = (int) (pageSize.height() * RtfElement.TWIPS_FACTOR); + this.landscape = pageWidth > pageHeight; + } + } + + /** + * This method tries to fit the Rectangle pageSize to one of the predefined PageSize rectangles. + * If a match is found the pageWidth and pageHeight will be set according to values determined from files + * generated by MS Word2000 and OpenOffice 641. If no match is found the method will try to match the rotated + * Rectangle by calling itself with the parameter rotate set to true. + * + * @param pageSize the page size for which to guess the correct format + * @param rotate Whether we should try to rotate the size befor guessing the format + * @return True if the format was guessed, false/ otherwise + */ + private boolean guessFormat(Rectangle pageSize, boolean rotate) { + if (rotate) { + pageSize = pageSize.rotate(); + } + if (rectEquals(pageSize, PageSize.A3)) { + pageWidth = 16837; + pageHeight = 23811; + landscape = rotate; + return true; + } + if (rectEquals(pageSize, PageSize.A4)) { + pageWidth = 11907; + pageHeight = 16840; + landscape = rotate; + return true; + } + if (rectEquals(pageSize, PageSize.A5)) { + pageWidth = 8391; + pageHeight = 11907; + landscape = rotate; + return true; + } + if (rectEquals(pageSize, PageSize.A6)) { + pageWidth = 5959; + pageHeight = 8420; + landscape = rotate; + return true; + } + if (rectEquals(pageSize, PageSize.B4)) { + pageWidth = 14570; + pageHeight = 20636; + landscape = rotate; + return true; + } + if (rectEquals(pageSize, PageSize.B5)) { + pageWidth = 10319; + pageHeight = 14572; + landscape = rotate; + return true; + } + if (rectEquals(pageSize, PageSize.HALFLETTER)) { + pageWidth = 7927; + pageHeight = 12247; + landscape = rotate; + return true; + } + if (rectEquals(pageSize, PageSize.LETTER)) { + pageWidth = 12242; + pageHeight = 15842; + landscape = rotate; + return true; + } + if (rectEquals(pageSize, PageSize.LEGAL)) { + pageWidth = 12252; + pageHeight = 20163; + landscape = rotate; + return true; + } + if (!rotate && guessFormat(pageSize, true)) { + int x = pageWidth; + pageWidth = pageHeight; + pageHeight = x; + return true; + } + return false; + } + + /** + * This method compares to Rectangles. They are considered equal if width and height are the same + * + * @param rect1 The first Rectangle to compare + * @param rect2 The second Rectangle to compare + * @return True if the Rectangles equal, false otherwise + */ + private boolean rectEquals(Rectangle rect1, Rectangle rect2) { + return (rect1.width() == rect2.width()) && (rect1.height() == rect2.height()); + } +} diff --git a/src/main/java/com/lowagie/text/rtf/document/output/RtfDataCache.java b/src/main/java/com/lowagie/text/rtf/document/output/RtfDataCache.java new file mode 100644 index 0000000..f9b3e9d --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/document/output/RtfDataCache.java @@ -0,0 +1,86 @@ +/* + * $Id: RtfDataCache.java,v 1.1 2005/02/02 18:09:32 hallm Exp $ + * $Name: $ + * + * Copyright 2005 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.document.output; + +import java.io.IOException; +import java.io.OutputStream; + + +/** + * The RtfDataCache interface must be implemented by classes wishing to + * act as caches for the rtf document data. + * + * @version $Revision: 1.1 $ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public interface RtfDataCache { + /** + * Constant for caching into memory. + */ + public static final int CACHE_MEMORY = 2; + /** + * Constant for caching to the disk. + */ + public static final int CACHE_DISK = 1; + /** + * Get the OutputStream that the RtfDocument can write to. + * + * @return The OutputStream the RtfDocument can use. + */ + public OutputStream getOutputStream(); + /** + * Write the content of the cache into the OutputStream. + * + * @param target The OutputStream to write the content into. + * @throws IOException If an error occurs reading/writing. + */ + public void writeTo(OutputStream target) throws IOException; +} diff --git a/src/main/java/com/lowagie/text/rtf/document/output/RtfDiskCache.java b/src/main/java/com/lowagie/text/rtf/document/output/RtfDiskCache.java new file mode 100644 index 0000000..77053ac --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/document/output/RtfDiskCache.java @@ -0,0 +1,113 @@ +/* + * $Id: RtfDiskCache.java,v 1.1 2005/02/02 18:09:32 hallm Exp $ + * $Name: $ + * + * Copyright 2005 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.document.output; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + + +/** + * The RtfFileCache is a RtfDataCache that uses a temporary file + * to store the rtf document data. Not so fast, but doesn't use any + * memory (just disk space). + * + * @version $Revision: 1.1 $ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfDiskCache implements RtfDataCache { + + /** + * The BufferedOutputStream that stores the cache data. + */ + private BufferedOutputStream data = null; + /** + * The temporary file to store the data in. + */ + private File tempFile = null; + + /** + * Constructs a RtfFileCache. Creates the temp file. + * + * @throws IOException If the temporary file could not be created. + */ + public RtfDiskCache() throws IOException { + this.tempFile = File.createTempFile("iText", null); + this.data = new BufferedOutputStream(new FileOutputStream(tempFile)); + } + + /** + * Gets the BufferedOutputStream to write to. + */ + public OutputStream getOutputStream() { + return this.data; + } + + /** + * Writes the content of the temporary file into the OutputStream. + */ + public void writeTo(OutputStream target) throws IOException { + this.data.close(); + BufferedInputStream tempIn = new BufferedInputStream(new FileInputStream(this.tempFile)); + byte[] buffer = new byte[8192]; + int bytesRead = -1; + while((bytesRead = tempIn.read(buffer)) >= 0) { + target.write(buffer, 0, bytesRead); + } + tempIn.close(); + this.tempFile.delete(); + } + +} diff --git a/src/main/java/com/lowagie/text/rtf/document/output/RtfMemoryCache.java b/src/main/java/com/lowagie/text/rtf/document/output/RtfMemoryCache.java new file mode 100644 index 0000000..002355f --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/document/output/RtfMemoryCache.java @@ -0,0 +1,93 @@ +/* + * $Id: RtfMemoryCache.java,v 1.1 2005/02/02 18:09:31 hallm Exp $ + * $Name: $ + * + * Copyright 2005 by Mark Hall + * + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the License. + * + * The Original Code is 'iText, a free JAVA-PDF library'. + * + * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by + * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. + * All Rights Reserved. + * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer + * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. + * + * Contributor(s): all the names of the contributors are added in the source code + * where applicable. + * + * Alternatively, the contents of this file may be used under the terms of the + * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the + * provisions of LGPL are applicable instead of those above. If you wish to + * allow use of your version of this file only under the terms of the LGPL + * License and not to allow others to use your version of this file under + * the MPL, indicate your decision by deleting the provisions above and + * replace them with the notice and other provisions required by the LGPL. + * If you do not delete the provisions above, a recipient may use your version + * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MPL as stated above or under the terms of the GNU + * Library General Public License as published by the Free Software Foundation; + * either version 2 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more + * details. + * + * If you didn't download this code from the following link, you should check if + * you aren't using an obsolete version: + * http://www.lowagie.com/iText/ + */ + +package com.lowagie.text.rtf.document.output; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; + + +/** + * The RtfMemoryCache is an RtfDataCache that keeps the whole rtf document + * data in memory. Fast but memory intensive. + * + * @version $Revision: 1.1 $ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfMemoryCache implements RtfDataCache { + + /** + * The buffer for the rtf document data. + */ + private ByteArrayOutputStream data = null; + + /** + * Constructs a RtfMemoryCache. + */ + public RtfMemoryCache() { + this.data = new ByteArrayOutputStream(); + } + + /** + * Gets the ByteArrayOutputStream. + */ + public OutputStream getOutputStream() { + return this.data; + } + + /** + * Writes the content of the ByteArrayOutputStream into the OutputStream. + */ + public void writeTo(OutputStream target) throws IOException { + this.data.writeTo(target); + } + +} -- cgit v1.2.3