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 --- .../java/com/lowagie/text/rtf/table/RtfBorder.java | 577 +++++++++++++++++++++ .../com/lowagie/text/rtf/table/RtfBorderGroup.java | 232 +++++++++ .../java/com/lowagie/text/rtf/table/RtfCell.java | 534 +++++++++++++++++++ .../java/com/lowagie/text/rtf/table/RtfRow.java | 377 ++++++++++++++ .../java/com/lowagie/text/rtf/table/RtfTable.java | 271 ++++++++++ 5 files changed, 1991 insertions(+) create mode 100644 src/main/java/com/lowagie/text/rtf/table/RtfBorder.java create mode 100644 src/main/java/com/lowagie/text/rtf/table/RtfBorderGroup.java create mode 100644 src/main/java/com/lowagie/text/rtf/table/RtfCell.java create mode 100644 src/main/java/com/lowagie/text/rtf/table/RtfRow.java create mode 100644 src/main/java/com/lowagie/text/rtf/table/RtfTable.java (limited to 'src/main/java/com/lowagie/text/rtf/table') diff --git a/src/main/java/com/lowagie/text/rtf/table/RtfBorder.java b/src/main/java/com/lowagie/text/rtf/table/RtfBorder.java new file mode 100644 index 0000000..8a35867 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/table/RtfBorder.java @@ -0,0 +1,577 @@ +/* + * $Id: RtfBorder.java,v 1.16 2005/05/04 14:33:54 blowagie Exp $ + * $Name: $ + * + * Copyright 2001, 2002, 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.table; + +import java.awt.Color; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.document.RtfDocument; +import com.lowagie.text.rtf.style.RtfColor; + + +/** + * The RtfBorder handle one row or cell border. + * INTERNAL USE ONLY + * + * @version $Version:$ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + * @author Unknown + */ +public class RtfBorder extends RtfElement { + + /** + * Constant for the left row border + */ + protected static final byte[] ROW_BORDER_LEFT = "\\trbrdrl".getBytes(); + /** + * Constant for the top row border + */ + protected static final byte[] ROW_BORDER_TOP = "\\trbrdrt".getBytes(); + /** + * Constant for the right row border + */ + protected static final byte[] ROW_BORDER_RIGHT = "\\trbrdrr".getBytes(); + /** + * Constant for the bottom row border + */ + protected static final byte[] ROW_BORDER_BOTTOM = "\\trbrdrb".getBytes(); + /** + * Constant for the horizontal line + */ + protected static final byte[] ROW_BORDER_HORIZONTAL = "\\trbrdrh".getBytes(); + /** + * Constant for the vertical line + */ + protected static final byte[] ROW_BORDER_VERTICAL = "\\trbrdrv".getBytes(); + /** + * Constant for the left cell border + */ + protected static final byte[] CELL_BORDER_LEFT = "\\clbrdrl".getBytes(); + /** + * Constant for the top cell border + */ + protected static final byte[] CELL_BORDER_TOP = "\\clbrdrt".getBytes(); + /** + * Constant for the right cell border + */ + protected static final byte[] CELL_BORDER_RIGHT = "\\clbrdrr".getBytes(); + /** + * Constant for the bottom cell border + */ + protected static final byte[] CELL_BORDER_BOTTOM = "\\clbrdrb".getBytes(); + /** + * Constant for the border width + */ + protected static final byte[] BORDER_WIDTH = "\\brdrw".getBytes(); + /** + * Constant for the border colour number + */ + protected static final byte[] BORDER_COLOR_NUMBER = "\\brdrcf".getBytes(); + /** + * Constant for the single border style + */ + protected static final byte[] BORDER_STYLE_SINGLE = "\\brdrs".getBytes(); + /** + * Constant for the double thick border style + */ + protected static final byte[] BORDER_STYLE_DOUBLE_THICK = "\\brdrth".getBytes(); + /** + * Constant for the shadowed border style + */ + protected static final byte[] BORDER_STYLE_SHADOWED = "\\brdrsh".getBytes(); + /** + * Constant for the dotted border style + */ + protected static final byte[] BORDER_STYLE_DOTTED = "\\brdrdot".getBytes(); + /** + * Constant for the dashed border style + */ + protected static final byte[] BORDER_STYLE_DASHED = "\\brdrdash".getBytes(); + /** + * Constant for the hairline border style + */ + protected static final byte[] BORDER_STYLE_HAIRLINE = "\\brdrhair".getBytes(); + /** + * Constant for the double border style + */ + protected static final byte[] BORDER_STYLE_DOUBLE = "\\brdrdb".getBytes(); + /** + * Constant for the dot dash border style + */ + protected static final byte[] BORDER_STYLE_DOT_DASH = "\\brdrdashd".getBytes(); + /** + * Constant for the dot dot dash border style + */ + protected static final byte[] BORDER_STYLE_DOT_DOT_DASH = "\\brdrdashdd".getBytes(); + /** + * Constant for the triple border style + */ + protected static final byte[] BORDER_STYLE_TRIPLE = "\\brdrtriple".getBytes(); + /** + * Constant for the thick thin border style + */ + protected static final byte[] BORDER_STYLE_THICK_THIN = "\\brdrtnthsg".getBytes(); + /** + * Constant for the thin thick border style + */ + protected static final byte[] BORDER_STYLE_THIN_THICK = "\\brdrthtnsg".getBytes(); + /** + * Constant for the thin thick thin border style + */ + protected static final byte[] BORDER_STYLE_THIN_THICK_THIN = "\\brdrtnthtnsg".getBytes(); + /** + * Constant for the thick thin medium border style + */ + protected static final byte[] BORDER_STYLE_THICK_THIN_MED = "\\brdrtnthmg".getBytes(); + /** + * Constant for the thin thick medium border style + */ + protected static final byte[] BORDER_STYLE_THIN_THICK_MED = "\\brdrthtnmg".getBytes(); + /** + * Constant for the thin thick thin medium border style + */ + protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_MED = "\\brdrtnthtnmg".getBytes(); + /** + * Constant for the thick thin large border style + */ + protected static final byte[] BORDER_STYLE_THICK_THIN_LARGE = "\\brdrtnthlg".getBytes(); + /** + * Constant for the thin thick large border style + */ + protected static final byte[] BORDER_STYLE_THIN_THICK_LARGE = "\\brdrthtnlg".getBytes(); + /** + * Constant for the thin thick thin large border style + */ + protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_LARGE = "\\brdrtnthtnlg".getBytes(); + /** + * Constant for the wavy border style + */ + protected static final byte[] BORDER_STYLE_WAVY = "\\brdrwavy".getBytes(); + /** + * Constant for the double wavy border style + */ + protected static final byte[] BORDER_STYLE_DOUBLE_WAVY = "\\brdrwavydb".getBytes(); + /** + * Constant for the striped border style + */ + protected static final byte[] BORDER_STYLE_STRIPED = "\\brdrdashdotstr".getBytes(); + /** + * Constant for the embossed border style + */ + protected static final byte[] BORDER_STYLE_EMBOSS = "\\brdremboss".getBytes(); + /** + * Constant for the engraved border style + */ + protected static final byte[] BORDER_STYLE_ENGRAVE = "\\brdrengrave".getBytes(); + + /** + * Constant for a row border + */ + protected static final int ROW_BORDER = 1; + /** + * Constant for a cell border + */ + protected static final int CELL_BORDER = 2; + + /** + * This border is no border :-) + */ + protected static final int NO_BORDER = 0; + /** + * Constant for a left border + */ + protected static final int LEFT_BORDER = 1; + /** + * Constant for a top border + */ + protected static final int TOP_BORDER = 2; + /** + * Constant for a right border + */ + protected static final int RIGHT_BORDER = 4; + /** + * Constant for a bottom border + */ + protected static final int BOTTOM_BORDER = 8; + /** + * Constant for a box (left, top, right, bottom) border + */ + protected static final int BOX_BORDER = 15; + /** + * Constant for a vertical line + */ + protected static final int VERTICAL_BORDER = 16; + /** + * Constant for a horizontal line + */ + protected static final int HORIZONTAL_BORDER = 32; + + /** + * Constant for a border with no border + */ + public static final int BORDER_NONE = 0; + /** + * Constant for a single border + */ + public static final int BORDER_SINGLE = 1; + /** + * Constant for a double thick border + */ + public static final int BORDER_DOUBLE_THICK = 2; + /** + * Constant for a shadowed border + */ + public static final int BORDER_SHADOWED = 3; + /** + * Constant for a dotted border + */ + public static final int BORDER_DOTTED = 4; + /** + * Constant for a dashed border + */ + public static final int BORDER_DASHED = 5; + /** + * Constant for a hairline border + */ + public static final int BORDER_HAIRLINE = 6; + /** + * Constant for a double border + */ + public static final int BORDER_DOUBLE = 7; + /** + * Constant for a dot dash border + */ + public static final int BORDER_DOT_DASH = 8; + /** + * Constant for a dot dot dash border + */ + public static final int BORDER_DOT_DOT_DASH = 9; + /** + * Constant for a triple border + */ + public static final int BORDER_TRIPLE = 10; + /** + * Constant for a thick thin border + */ + public static final int BORDER_THICK_THIN = 11; + /** + * Constant for a thin thick border + */ + public static final int BORDER_THIN_THICK = 12; + /** + * Constant for a thin thick thin border + */ + public static final int BORDER_THIN_THICK_THIN = 13; + /** + * Constant for a thick thin medium border + */ + public static final int BORDER_THICK_THIN_MED = 14; + /** + * Constant for a thin thick medium border + */ + public static final int BORDER_THIN_THICK_MED = 15; + /** + * Constant for a thin thick thin medium border + */ + public static final int BORDER_THIN_THICK_THIN_MED = 16; + /** + * Constant for a thick thin large border + */ + public static final int BORDER_THICK_THIN_LARGE = 17; + /** + * Constant for a thin thick large border + */ + public static final int BORDER_THIN_THICK_LARGE = 18; + /** + * Constant for a thin thick thin large border + */ + public static final int BORDER_THIN_THICK_THIN_LARGE = 19; + /** + * Constant for a wavy border + */ + public static final int BORDER_WAVY = 20; + /** + * Constant for a double wavy border + */ + public static final int BORDER_DOUBLE_WAVY = 21; + /** + * Constant for a striped border + */ + public static final int BORDER_STRIPED = 22; + /** + * Constant for an embossed border + */ + public static final int BORDER_EMBOSS = 23; + /** + * Constant for an engraved border + */ + public static final int BORDER_ENGRAVE = 24; + + /** + * The type of this RtfBorder + */ + private int borderType = ROW_BORDER; + /** + * The position of this RtfBorder + */ + private int borderPosition = NO_BORDER; + /** + * The style of this RtfBorder + */ + private int borderStyle = BORDER_NONE; + /** + * The width of this RtfBorder + */ + private int borderWidth = 20; + /** + * The colour of this RtfBorder + */ + private RtfColor borderColor = null; + + /** + * Makes a copy of the given RtfBorder + * + * @param doc The RtfDocument this RtfBorder belongs to + * @param borderType The border type of this RtfBorder + * @param border The RtfBorder to copy + */ + protected RtfBorder(RtfDocument doc, int borderType, RtfBorder border) { + super(doc); + this.borderType = borderType; + this.borderPosition = border.getBorderPosition(); + this.borderStyle = border.getBorderStyle(); + this.borderWidth = border.getBorderWidth(); + this.borderColor = new RtfColor(this.document, border.getBorderColor()); + } + + /** + * Constructs a RtfBorder + * + * @param doc The RtfDocument this RtfBorder belongs to + * @param borderType The type of border this RtfBorder is + * @param borderPosition The position of this RtfBorder + * @param borderStyle The style of this RtfBorder + * @param borderWidth The width of this RtfBorder + * @param borderColor The colour of this RtfBorder + */ + protected RtfBorder(RtfDocument doc, int borderType, int borderPosition, int borderStyle, float borderWidth, Color borderColor) { + super(doc); + this.borderType = borderType; + this.borderPosition = borderPosition; + this.borderStyle = borderStyle; + if(borderWidth > 2) { + borderWidth = 2; + } + this.borderWidth = (int) (borderWidth * TWIPS_FACTOR); + if(this.borderWidth == 0) { + this.borderStyle = BORDER_NONE; + } + if(borderColor == null) { + this.borderColor = new RtfColor(this.document, new Color(0, 0, 0)); + } else { + this.borderColor = new RtfColor(this.document, borderColor); + } + } + + /** + * Writes the RtfBorder settings + * + * @return A byte array with the RtfBorder settings + */ + public byte[] write() { + if(this.borderStyle == BORDER_NONE || this.borderPosition == NO_BORDER || this.borderWidth == 0) { + return new byte[0]; + } + + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + if(this.borderType == ROW_BORDER) { + switch(this.borderPosition) { + case LEFT_BORDER: + result.write(ROW_BORDER_LEFT); + break; + case TOP_BORDER: + result.write(ROW_BORDER_TOP); + break; + case RIGHT_BORDER: + result.write(ROW_BORDER_RIGHT); + break; + case BOTTOM_BORDER: + result.write(ROW_BORDER_BOTTOM); + break; + case HORIZONTAL_BORDER: + result.write(ROW_BORDER_HORIZONTAL); + break; + case VERTICAL_BORDER: + result.write(ROW_BORDER_VERTICAL); + break; + default: + return new byte[0]; + } + result.write(writeBorderStyle()); + result.write(BORDER_WIDTH); + result.write(intToByteArray(this.borderWidth)); + result.write(BORDER_COLOR_NUMBER); + result.write(intToByteArray(this.borderColor.getColorNumber())); + result.write('\n'); + } else if(this.borderType == CELL_BORDER) { + switch(this.borderPosition) { + case LEFT_BORDER: + result.write(CELL_BORDER_LEFT); + break; + case TOP_BORDER: + result.write(CELL_BORDER_TOP); + break; + case RIGHT_BORDER: + result.write(CELL_BORDER_RIGHT); + break; + case BOTTOM_BORDER: + result.write(CELL_BORDER_BOTTOM); + break; + default: + return new byte[0]; + } + result.write(writeBorderStyle()); + result.write(BORDER_WIDTH); + result.write(intToByteArray(this.borderWidth)); + result.write(BORDER_COLOR_NUMBER); + result.write(intToByteArray(this.borderColor.getColorNumber())); + result.write('\n'); + } + } catch(IOException ioe) { + ioe.printStackTrace(); + } + + return result.toByteArray(); + } + + /** + * Writes the style of this RtfBorder + * + * @return A byte array containing the style of this RtfBorder + */ + private byte[] writeBorderStyle() { + switch(this.borderStyle) { + case BORDER_NONE : return new byte[0]; + case BORDER_SINGLE : return BORDER_STYLE_SINGLE; + case BORDER_DOUBLE_THICK : return BORDER_STYLE_DOUBLE_THICK; + case BORDER_SHADOWED : return BORDER_STYLE_SHADOWED; + case BORDER_DOTTED : return BORDER_STYLE_DOTTED; + case BORDER_DASHED : return BORDER_STYLE_DASHED; + case BORDER_HAIRLINE : return BORDER_STYLE_HAIRLINE; + case BORDER_DOUBLE : return BORDER_STYLE_DOUBLE; + case BORDER_DOT_DASH : return BORDER_STYLE_DOT_DASH; + case BORDER_DOT_DOT_DASH : return BORDER_STYLE_DOT_DOT_DASH; + case BORDER_TRIPLE : return BORDER_STYLE_TRIPLE; + case BORDER_THICK_THIN : return BORDER_STYLE_THICK_THIN; + case BORDER_THIN_THICK : return BORDER_STYLE_THIN_THICK; + case BORDER_THIN_THICK_THIN : return BORDER_STYLE_THIN_THICK_THIN; + case BORDER_THICK_THIN_MED : return BORDER_STYLE_THICK_THIN_MED; + case BORDER_THIN_THICK_MED : return BORDER_STYLE_THIN_THICK_MED; + case BORDER_THIN_THICK_THIN_MED : return BORDER_STYLE_THIN_THICK_THIN_MED; + case BORDER_THICK_THIN_LARGE : return BORDER_STYLE_THICK_THIN_LARGE; + case BORDER_THIN_THICK_LARGE : return BORDER_STYLE_THIN_THICK_LARGE; + case BORDER_THIN_THICK_THIN_LARGE : return BORDER_STYLE_THIN_THICK_THIN_LARGE; + case BORDER_WAVY : return BORDER_STYLE_WAVY; + case BORDER_DOUBLE_WAVY : return BORDER_STYLE_DOUBLE_WAVY; + case BORDER_STRIPED : return BORDER_STYLE_STRIPED; + case BORDER_EMBOSS : return BORDER_STYLE_EMBOSS; + case BORDER_ENGRAVE : return BORDER_STYLE_ENGRAVE; + default : return BORDER_STYLE_SINGLE; + } + } + + /** + * Gets the colour of this RtfBorder + * + * @return Returns RtfColor of this RtfBorder + */ + protected RtfColor getBorderColor() { + return borderColor; + } + + /** + * Gets the position of this RtfBorder + * @return Returns the position of this RtfBorder + */ + protected int getBorderPosition() { + return borderPosition; + } + + /** + * Gets the style of this RtfBorder + * + * @return Returns the style of this RtfBorder + */ + protected int getBorderStyle() { + return borderStyle; + } + + /** + * Gets the type of this RtfBorder + * + * @return Returns the type of this RtfBorder + */ + protected int getBorderType() { + return borderType; + } + + /** + * Gets the width of this RtfBorder + * + * @return Returns the width of this RtfBorder + */ + protected int getBorderWidth() { + return borderWidth; + } +} diff --git a/src/main/java/com/lowagie/text/rtf/table/RtfBorderGroup.java b/src/main/java/com/lowagie/text/rtf/table/RtfBorderGroup.java new file mode 100644 index 0000000..5b8904f --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/table/RtfBorderGroup.java @@ -0,0 +1,232 @@ +/* + * $Id: RtfBorderGroup.java,v 1.8 2004/12/14 12:51:39 blowagie Exp $ + * $Name: $ + * + * Copyright 2001, 2002, 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.table; + +import java.awt.Color; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Enumeration; +import java.util.Hashtable; + +import com.lowagie.text.Rectangle; +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.document.RtfDocument; + + +/** + * The RtfBorderGroup represents a collection of RtfBorders to use in a RtfCell + * or RtfTable. + * + * @version $Version:$ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + */ +public class RtfBorderGroup extends RtfElement { + /** + * The type of borders this RtfBorderGroup contains. + * RtfBorder.ROW_BORDER or RtfBorder.CELL_BORDER + */ + private int borderType = RtfBorder.ROW_BORDER; + /** + * The borders in this RtfBorderGroup + */ + private Hashtable borders = null; + + /** + * Constructs an empty RtfBorderGroup. + */ + public RtfBorderGroup() { + super(null); + this.borders = new Hashtable(); + } + + /** + * Constructs a RtfBorderGroup with on border style for multiple borders. + * + * @param bordersToAdd The borders to add (Rectangle.LEFT, Rectangle.RIGHT, Rectangle.TOP, Rectangle.BOTTOM, Rectangle.BOX) + * @param borderStyle The style of border to add (from RtfBorder) + * @param borderWidth The border width to use + * @param borderColor The border color to use + */ + public RtfBorderGroup(int bordersToAdd, int borderStyle, float borderWidth, Color borderColor) { + super(null); + this.borders = new Hashtable(); + addBorder(bordersToAdd, borderStyle, borderWidth, borderColor); + } + + /** + * Constructs a RtfBorderGroup based on another RtfBorderGroup. + * + * @param doc The RtfDocument this RtfBorderGroup belongs to + * @param borderType The type of borders this RtfBorderGroup contains + * @param borderGroup The RtfBorderGroup to use as a base + */ + protected RtfBorderGroup(RtfDocument doc, int borderType, RtfBorderGroup borderGroup) { + super(doc); + this.borders = new Hashtable(); + this.borderType = borderType; + if(borderGroup != null) { + Enumeration borderEnum = borderGroup.getBorders().keys(); + while(borderEnum.hasMoreElements()) { + Integer borderPos = (Integer) borderEnum.nextElement(); + RtfBorder border = (RtfBorder) borderGroup.getBorders().get(borderPos); + this.borders.put(borderPos, new RtfBorder(this.document, this.borderType, border)); + } + } + } + + /** + * Constructs a RtfBorderGroup with certain borders + * + * @param doc The RtfDocument this RtfBorderGroup belongs to + * @param borderType The type of borders this RtfBorderGroup contains + * @param bordersToUse The borders to add (Rectangle.LEFT, Rectangle.RIGHT, Rectangle.TOP, Rectangle.BOTTOM, Rectangle.BOX) + * @param borderWidth The border width to use + * @param borderColor The border color to use + */ + protected RtfBorderGroup(RtfDocument doc, int borderType, int bordersToUse, float borderWidth, Color borderColor) { + super(doc); + this.borderType = borderType; + this.borders = new Hashtable(); + addBorder(bordersToUse, RtfBorder.BORDER_SINGLE, borderWidth, borderColor); + } + + /** + * Sets a border in the Hashtable of borders + * + * @param borderPosition The position of this RtfBorder + * @param borderStyle The type of borders this RtfBorderGroup contains + * @param borderWidth The border width to use + * @param borderColor The border color to use + */ + private void setBorder(int borderPosition, int borderStyle, float borderWidth, Color borderColor) { + RtfBorder border = new RtfBorder(this.document, this.borderType, borderPosition, borderStyle, borderWidth, borderColor); + this.borders.put(new Integer(borderPosition), border); + } + + /** + * Adds borders to the RtfBorderGroup + * + * @param bordersToAdd The borders to add (Rectangle.LEFT, Rectangle.RIGHT, Rectangle.TOP, Rectangle.BOTTOM, Rectangle.BOX) + * @param borderStyle The style of border to add (from RtfBorder) + * @param borderWidth The border width to use + * @param borderColor The border color to use + */ + public void addBorder(int bordersToAdd, int borderStyle, float borderWidth, Color borderColor) { + if((bordersToAdd & Rectangle.LEFT) == Rectangle.LEFT) { + setBorder(RtfBorder.LEFT_BORDER, borderStyle, borderWidth, borderColor); + } + if((bordersToAdd & Rectangle.TOP) == Rectangle.TOP) { + setBorder(RtfBorder.TOP_BORDER, borderStyle, borderWidth, borderColor); + } + if((bordersToAdd & Rectangle.RIGHT) == Rectangle.RIGHT) { + setBorder(RtfBorder.RIGHT_BORDER, borderStyle, borderWidth, borderColor); + } + if((bordersToAdd & Rectangle.BOTTOM) == Rectangle.BOTTOM) { + setBorder(RtfBorder.BOTTOM_BORDER, borderStyle, borderWidth, borderColor); + } + if((bordersToAdd & Rectangle.BOX) == Rectangle.BOX && this.borderType == RtfBorder.ROW_BORDER) { + setBorder(RtfBorder.VERTICAL_BORDER, borderStyle, borderWidth, borderColor); + setBorder(RtfBorder.HORIZONTAL_BORDER, borderStyle, borderWidth, borderColor); + } + } + + /** + * Removes borders from the list of borders + * + * @param bordersToRemove The borders to remove (from Rectangle) + */ + public void removeBorder(int bordersToRemove) { + if((bordersToRemove & Rectangle.LEFT) == Rectangle.LEFT) { + this.borders.remove(new Integer(RtfBorder.LEFT_BORDER)); + } + if((bordersToRemove & Rectangle.TOP) == Rectangle.TOP) { + this.borders.remove(new Integer(RtfBorder.TOP_BORDER)); + } + if((bordersToRemove & Rectangle.RIGHT) == Rectangle.RIGHT) { + this.borders.remove(new Integer(RtfBorder.RIGHT_BORDER)); + } + if((bordersToRemove & Rectangle.BOTTOM) == Rectangle.BOTTOM) { + this.borders.remove(new Integer(RtfBorder.BOTTOM_BORDER)); + } + if((bordersToRemove & Rectangle.BOX) == Rectangle.BOX && this.borderType == RtfBorder.ROW_BORDER) { + this.borders.remove(new Integer(RtfBorder.VERTICAL_BORDER)); + this.borders.remove(new Integer(RtfBorder.HORIZONTAL_BORDER)); + } + } + + /** + * Writes the borders of this RtfBorderGroup + * + * @return A byte array with the borders of this RtfBorderGroup + */ + public byte[] write() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + Enumeration borderEnum = this.borders.keys(); + while(borderEnum.hasMoreElements()) { + result.write(((RtfBorder) this.borders.get(borderEnum.nextElement())).write()); + } + } catch(IOException ioe) { + ioe.printStackTrace(); + } + + return result.toByteArray(); + } + + /** + * Gets the RtfBorders of this RtfBorderGroup + * + * @return The RtfBorders of this RtfBorderGroup + */ + protected Hashtable getBorders() { + return this.borders; + } +} diff --git a/src/main/java/com/lowagie/text/rtf/table/RtfCell.java b/src/main/java/com/lowagie/text/rtf/table/RtfCell.java new file mode 100644 index 0000000..abfee02 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/table/RtfCell.java @@ -0,0 +1,534 @@ +/* + * $Id: RtfCell.java,v 1.15 2006/02/09 17:13:11 hallm Exp $ + * $Name: $ + * + * Copyright 2001, 2002, 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.table; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Iterator; +import java.util.Properties; +import java.util.ArrayList; + +import com.lowagie.text.BadElementException; +import com.lowagie.text.Cell; +import com.lowagie.text.DocumentException; +import com.lowagie.text.Element; +import com.lowagie.text.List; +import com.lowagie.text.Paragraph; +import com.lowagie.text.rtf.RtfBasicElement; +import com.lowagie.text.rtf.RtfExtendedElement; +import com.lowagie.text.rtf.document.RtfDocument; +import com.lowagie.text.rtf.style.RtfColor; +import com.lowagie.text.rtf.style.RtfParagraphStyle; +import com.lowagie.text.rtf.text.RtfParagraph; + + +/** + * The RtfCell wraps a Cell, but can also be added directly to a Table. + * The RtfCell is an extension of Cell, that supports a multitude of different + * borderstyles. + * + * @version $Id: RtfCell.java,v 1.15 2006/02/09 17:13:11 hallm Exp $ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + * @author Steffen Stundzig + * @author Benoit WIART + * @see com.lowagie.text.rtf.table.RtfBorder + */ +public class RtfCell extends Cell implements RtfExtendedElement { + + /** + * This cell is not merged + */ + private static final int MERGE_NONE = 0; + /** + * This cell is the parent cell of a vertical merge operation + */ + private static final int MERGE_VERT_PARENT = 1; + /** + * This cell is a child cell of a vertical merge operation + */ + private static final int MERGE_VERT_CHILD = 2; + + /** + * The parent RtfRow of this RtfCell + */ + private RtfRow parentRow = null; + /** + * The content of this RtfCell + */ + private ArrayList content = null; + /** + * The right margin of this RtfCell + */ + private int cellRight = 0; + /** + * The width of this RtfCell + */ + private int cellWidth = 0; + /** + * The borders of this RtfCell + */ + private RtfBorderGroup borders = null; + /** + * The vertical alignment of this RtfCell + */ + private int verticalAlignment = Cell.ALIGN_MIDDLE; + /** + * The background color of this RtfCell + */ + private RtfColor backgroundColor = null; + /** + * The padding of this RtfCell + */ + private int cellPadding = 0; + /** + * The merge type of this RtfCell + */ + private int mergeType = MERGE_NONE; + /** + * The number of columns spanned by this RtfCell + */ + private int colspan = 1; + /** + * The number of rows spanned by this RtfCell + */ + private int rowspan = 1; + /** + * The RtfDocument this RtfCell belongs to + */ + private RtfDocument document = null; + /** + * Whether this RtfCell is in a header + */ + private boolean inHeader = false; + + /** + * Constructs an empty RtfCell + */ + public RtfCell() { + super(); + this.borders = new RtfBorderGroup(); + } + + /** + * Constructs a RtfCell based upon a String + * + * @param content The String to base the RtfCell on + */ + public RtfCell(String content) { + super(content); + this.borders = new RtfBorderGroup(); + } + + /** + * Constructs a RtfCell based upon an Element + * + * @param element The Element to base the RtfCell on + * @throws BadElementException If the Element is not valid + */ + public RtfCell(Element element) throws BadElementException { + super(element); + this.borders = new RtfBorderGroup(); + } + + /** + * Constructs a RtfCell based upon certain Properties + * + * @param properties The Properties for this RtfCell + */ + public RtfCell(Properties properties) { + super(properties); + this.borders = new RtfBorderGroup(); + } + + /** + * Constructs a RtfCell based on a Cell. + * + * @param doc The RtfDocument this RtfCell belongs to + * @param row The RtfRow this RtfCell lies in + * @param cell The Cell to base this RtfCell on + */ + protected RtfCell(RtfDocument doc, RtfRow row, Cell cell) { + this.document = doc; + this.parentRow = row; + importCell(cell); + } + + /** + * Imports the Cell properties into the RtfCell + * + * @param cell The Cell to import + */ + private void importCell(Cell cell) { + this.content = new ArrayList(); + + if(cell == null) { + this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, this.parentRow.getParentTable().getBorders()); + return; + } + + this.colspan = cell.colspan(); + this.rowspan = cell.rowspan(); + if(cell.rowspan() > 1) { + this.mergeType = MERGE_VERT_PARENT; + } + if(cell instanceof RtfCell) { + this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, ((RtfCell) cell).getBorders()); + } else { + this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, cell.border(), cell.borderWidth(), cell.borderColor()); + } + this.verticalAlignment = cell.verticalAlignment(); + if(cell.backgroundColor() == null) { + this.backgroundColor = new RtfColor(this.document, 255, 255, 255); + } else { + this.backgroundColor = new RtfColor(this.document, cell.backgroundColor()); + } + + this.cellPadding = (int) this.parentRow.getParentTable().getCellPadding(); + + if(cell != null) { + Iterator cellIterator = cell.getElements(); + Paragraph container = null; + while(cellIterator.hasNext()) { + try { + Element element = (Element) cellIterator.next(); + // should we wrap it in a paragraph + if(!(element instanceof Paragraph) && !(element instanceof List)) { + if(container != null) { + container.add(element); + } else { + container = new Paragraph(); + container.setAlignment(cell.horizontalAlignment()); + container.add(element); + } + } else { + if(container != null) { + RtfBasicElement rtfElement = this.document.getMapper().mapElement(container); + rtfElement.setInTable(true); + this.content.add(rtfElement); + container = null; + } + // if horizontal alignment is undefined overwrite + // with that of enclosing cell + if (element instanceof Paragraph && ((Paragraph) element).alignment() == Element.ALIGN_UNDEFINED) { + ((Paragraph) element).setAlignment(cell.horizontalAlignment()); + } + + RtfBasicElement rtfElement = this.document.getMapper().mapElement(element); + rtfElement.setInTable(true); + this.content.add(rtfElement); + } + } catch(DocumentException de) { + de.printStackTrace(); + } + } + if(container != null) { + try { + RtfBasicElement rtfElement = this.document.getMapper().mapElement(container); + rtfElement.setInTable(true); + this.content.add(rtfElement); + } catch(DocumentException de) { + de.printStackTrace(); + } + } + } + } + + /** + * Write the cell definition part of this RtfCell + * + * @return A byte array with the cell definition + */ + public byte[] writeDefinition() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + if(this.mergeType == MERGE_VERT_PARENT) { + result.write("\\clvmgf".getBytes()); + } else if(this.mergeType == MERGE_VERT_CHILD) { + result.write("\\clvmrg".getBytes()); + } + switch (verticalAlignment) { + case Element.ALIGN_BOTTOM: + result.write("\\clvertalb".getBytes()); + break; + case Element.ALIGN_CENTER: + case Element.ALIGN_MIDDLE: + result.write("\\clvertalc".getBytes()); + break; + case Element.ALIGN_TOP: + result.write("\\clvertalt".getBytes()); + break; + } + result.write(this.borders.write()); + + if(this.backgroundColor != null) { + result.write("\\clcbpat".getBytes()); + result.write(intToByteArray(this.backgroundColor.getColorNumber())); + } + result.write('\n'); + + result.write("\\clftsWidth3".getBytes()); + result.write('\n'); + + result.write("\\clwWidth".getBytes()); + result.write(intToByteArray(this.cellWidth)); + result.write('\n'); + + if(this.cellPadding > 0) { + result.write("\\clpadl".getBytes()); + result.write(intToByteArray(this.cellPadding / 2)); + result.write("\\clpadt".getBytes()); + result.write(intToByteArray(this.cellPadding / 2)); + result.write("\\clpadr".getBytes()); + result.write(intToByteArray(this.cellPadding / 2)); + result.write("\\clpadb".getBytes()); + result.write(intToByteArray(this.cellPadding / 2)); + result.write("\\clpadfl3".getBytes()); + result.write("\\clpadft3".getBytes()); + result.write("\\clpadfr3".getBytes()); + result.write("\\clpadfb3".getBytes()); + } + result.write("\\cellx".getBytes()); + result.write(intToByteArray(this.cellRight)); + + } catch(IOException ioe) { + ioe.printStackTrace(); + } + + return result.toByteArray(); + } + + /** + * Write the content of this RtfCell + * + * @return A byte array with the content of this RtfCell + */ + public byte[] write() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + if(this.content.size() == 0) { + result.write(RtfParagraph.PARAGRAPH_DEFAULTS); + if(this.parentRow.getParentTable().getTableFitToPage()) { + result.write(RtfParagraphStyle.KEEP_TOGETHER_WITH_NEXT); + } + result.write(RtfParagraph.IN_TABLE); + } else { + for(int i = 0; i < this.content.size(); i++) { + RtfBasicElement rtfElement = (RtfBasicElement) this.content.get(i); + if(rtfElement instanceof RtfParagraph) { + ((RtfParagraph) rtfElement).setKeepTogetherWithNext(this.parentRow.getParentTable().getTableFitToPage()); + } + result.write(rtfElement.write()); + if(rtfElement instanceof RtfParagraph && i < (this.content.size() - 1)) { + result.write(RtfParagraph.PARAGRAPH); + } + } + } + result.write("\\cell".getBytes()); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + + return result.toByteArray(); + } + + /** + * Sets the right margin of this cell. Used in merge operations + * + * @param cellRight The right margin to use + */ + protected void setCellRight(int cellRight) { + this.cellRight = cellRight; + } + + /** + * Gets the right margin of this RtfCell + * + * @return The right margin of this RtfCell. + */ + protected int getCellRight() { + return this.cellRight; + } + + /** + * Sets the cell width of this RtfCell. Used in merge operations. + * + * @param cellWidth The cell width to use + */ + protected void setCellWidth(int cellWidth) { + this.cellWidth = cellWidth; + } + + /** + * Gets the cell width of this RtfCell + * + * @return The cell width of this RtfCell + */ + protected int getCellWidth() { + return this.cellWidth; + } + + /** + * Gets the number of columns this RtfCell spans + * + * @return The number of columns this RtfCell spans + */ + protected int getColspan() { + return this.colspan; + } + + /** + * Gets the number of rows this RtfCell spans + * + * @return The number of rows this RtfCell spans + */ + protected int getRowspan() { + return this.rowspan; + } + + /** + * Gets the cell padding of this RtfCell + * + * @return The cell padding of this RtfCell + */ + protected int getCellpadding() { + return this.cellPadding; + } + + /** + * Gets the borders of this RtfCell + * + * @return The borders of this RtfCell + */ + protected RtfBorderGroup getBorders() { + return this.borders; + } + + /** + * Set the borders of this RtfCell + * + * @param borderGroup The RtfBorderGroup to use as borders + */ + public void setBorders(RtfBorderGroup borderGroup) { + this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, borderGroup); + } + + /** + * Get the vertical alignment of this RtfCell + * + * @return The vertical alignment of this RtfCell + */ + protected int getVerticalAlignment() { + return this.verticalAlignment; + } + + /** + * Get the background color of this RtfCell + * + * @return The background color of this RtfCell + */ + protected RtfColor getBackgroundColor() { + return this.backgroundColor; + } + + /** + * Merge this cell into the parent cell. + * + * @param mergeParent The RtfCell to merge with + */ + protected void setCellMergeChild(RtfCell mergeParent) { + this.mergeType = MERGE_VERT_CHILD; + this.cellWidth = mergeParent.getCellWidth(); + this.cellRight = mergeParent.getCellRight(); + this.cellPadding = mergeParent.getCellpadding(); + this.borders = mergeParent.getBorders(); + this.verticalAlignment = mergeParent.getVerticalAlignment(); + this.backgroundColor = mergeParent.getBackgroundColor(); + } + + /** + * Sets the RtfDocument this RtfCell belongs to + * + * @param doc The RtfDocument to use + */ + public void setRtfDocument(RtfDocument doc) { + this.document = doc; + } + + /** + * Unused + * @param inTable + */ + public void setInTable(boolean inTable) { + } + + /** + * Sets whether this RtfCell is in a header + * + * @param inHeader True if this RtfCell is in a header, false otherwise + */ + public void setInHeader(boolean inHeader) { + this.inHeader = inHeader; + for(int i = 0; i < this.content.size(); i++) { + ((RtfBasicElement) this.content.get(i)).setInHeader(inHeader); + } + } + + /** + * Transforms an integer into its String representation and then returns the bytes + * of that string. + * + * @param i The integer to convert + * @return A byte array representing the integer + */ + private byte[] intToByteArray(int i) { + return Integer.toString(i).getBytes(); + } +} diff --git a/src/main/java/com/lowagie/text/rtf/table/RtfRow.java b/src/main/java/com/lowagie/text/rtf/table/RtfRow.java new file mode 100644 index 0000000..4075ed2 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/table/RtfRow.java @@ -0,0 +1,377 @@ +/* + * $Id: RtfRow.java,v 1.16 2005/05/24 16:30:53 hallm Exp $ + * $Name: $ + * + * Copyright 2001, 2002, 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.table; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.ArrayList; + +import com.lowagie.text.Cell; +import com.lowagie.text.Element; +import com.lowagie.text.Row; +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.document.RtfDocument; + + +/** + * The RtfRow wraps one Row for a RtfTable. + * INTERNAL USE ONLY + * + * @version $Version:$ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + * @author Steffen Stundzig + * @author Lorenz Maierhofer + */ +public class RtfRow extends RtfElement { + + /** + * Constant for the RtfRow beginning + */ + private static final byte[] ROW_BEGIN = "\\trowd".getBytes(); + /** + * Constant for the RtfRow width style + */ + private static final byte[] ROW_WIDTH_STYLE = "\\trftsWidth3".getBytes(); + /** + * Constant for the RtfRow width + */ + private static final byte[] ROW_WIDTH = "\\trwWidth".getBytes(); + /** + * Constant to specify that this RtfRow are not to be broken across pages + */ + private static final byte[] ROW_KEEP_TOGETHER = "\\trkeep".getBytes(); + /** + * Constant to specify that this is a header RtfRow + */ + private static final byte[] ROW_HEADER_ROW = "\\trhdr".getBytes(); + /** + * Constant for left alignment of this RtfRow + */ + private static final byte[] ROW_ALIGN_LEFT = "\\trql".getBytes(); + /** + * Constant for right alignment of this RtfRow + */ + private static final byte[] ROW_ALIGN_RIGHT = "\\trqr".getBytes(); + /** + * Constant for center alignment of this RtfRow + */ + private static final byte[] ROW_ALIGN_CENTER = "\\trqc".getBytes(); + /** + * Constant for justified alignment of this RtfRow + */ + private static final byte[] ROW_ALIGN_JUSTIFIED = "\\trqj".getBytes(); + /** + * Constant for the graph style of this RtfRow + */ + private static final byte[] ROW_GRAPH = "\\trgaph10".getBytes(); + /** + * Constant for the cell left spacing + */ + private static final byte[] ROW_CELL_SPACING_LEFT = "\\trspdl".getBytes(); + /** + * Constant for the cell top spacing + */ + private static final byte[] ROW_CELL_SPACING_TOP = "\\trspdt".getBytes(); + /** + * Constant for the cell right spacing + */ + private static final byte[] ROW_CELL_SPACING_RIGHT = "\\trspdr".getBytes(); + /** + * Constant for the cell bottom spacing + */ + private static final byte[] ROW_CELL_SPACING_BOTTOM = "\\trspdb".getBytes(); + /** + * Constant for the cell left spacing style + */ + private static final byte[] ROW_CELL_SPACING_LEFT_STYLE = "\\trspdfl3".getBytes(); + /** + * Constant for the cell top spacing style + */ + private static final byte[] ROW_CELL_SPACING_TOP_STYLE = "\\trspdft3".getBytes(); + /** + * Constant for the cell right spacing style + */ + private static final byte[] ROW_CELL_SPACING_RIGHT_STYLE = "\\trspdfr3".getBytes(); + /** + * Constant for the cell bottom spacing style + */ + private static final byte[] ROW_CELL_SPACING_BOTTOM_STYLE = "\\trspdfb3".getBytes(); + /** + * Constant for the cell left padding + */ + private static final byte[] ROW_CELL_PADDING_LEFT = "\\trpaddl".getBytes(); + /** + * Constant for the cell right padding + */ + private static final byte[] ROW_CELL_PADDING_RIGHT = "\\trpaddr".getBytes(); + /** + * Constant for the cell left padding style + */ + private static final byte[] ROW_CELL_PADDING_LEFT_STYLE = "\\trpaddfl3".getBytes(); + /** + * Constant for the cell right padding style + */ + private static final byte[] ROW_CELL_PADDING_RIGHT_STYLE = "\\trpaddfr3".getBytes(); + /** + * Constant for the end of a row + */ + private static final byte[] ROW_END = "\\row".getBytes(); + + /** + * The RtfTable this RtfRow belongs to + */ + private RtfTable parentTable = null; + /** + * The cells of this RtfRow + */ + private ArrayList cells = null; + /** + * The width of this row + */ + private int width = 0; + /** + * The row number + */ + private int rowNumber = 0; + + /** + * Constructs a RtfRow for a Row. + * + * @param doc The RtfDocument this RtfRow belongs to + * @param rtfTable The RtfTable this RtfRow belongs to + * @param row The Row this RtfRow is based on + * @param rowNumber The number of this row + */ + protected RtfRow(RtfDocument doc, RtfTable rtfTable, Row row, int rowNumber) { + super(doc); + this.parentTable = rtfTable; + this.rowNumber = rowNumber; + importRow(row); + } + + /** + * Imports a Row and copies all settings + * + * @param row The Row to import + */ + private void importRow(Row row) { + this.cells = new ArrayList(); + this.width = this.document.getDocumentHeader().getPageSetting().getPageWidth() - this.document.getDocumentHeader().getPageSetting().getMarginLeft() - this.document.getDocumentHeader().getPageSetting().getMarginRight(); + this.width = (int) (this.width / 100 * this.parentTable.getTableWidthPercent()); + + int cellRight = 0; + int cellWidth = 0; + for(int i = 0; i < row.columns(); i++) { + cellWidth = (int) (this.width / 100 * this.parentTable.getProportionalWidths()[i]); + cellRight = cellRight + cellWidth; + + Cell cell = (Cell) row.getCell(i); + RtfCell rtfCell = new RtfCell(this.document, this, cell); + rtfCell.setCellRight(cellRight); + rtfCell.setCellWidth(cellWidth); + this.cells.add(rtfCell); + } + } + + /** + * Performs a second pass over all cells to handle cell row/column spanning. + */ + protected void handleCellSpanning() { + int realCellIndex = 0; + for(int i = 0; i < this.cells.size(); i++) { + RtfCell rtfCell = (RtfCell) this.cells.get(i); + if(rtfCell.getColspan() > 1) { + int j = rtfCell.getColspan(); + while(j > 1) { + if(i + 1 < this.cells.size()) { + RtfCell rtfCellMerge = (RtfCell) this.cells.get(i + 1); + rtfCell.setCellRight(rtfCell.getCellRight() + rtfCellMerge.getCellWidth()); + rtfCell.setCellWidth(rtfCell.getCellWidth() + rtfCellMerge.getCellWidth()); + this.cells.remove(i + 1); + j--; + } + } + } + if(rtfCell.getRowspan() > 1) { + ArrayList rows = this.parentTable.getRows(); + for(int j = 1; j < rtfCell.getRowspan(); j++) { + RtfRow mergeRow = (RtfRow) rows.get(this.rowNumber + j); + if(this.rowNumber + j < rows.size()) { + RtfCell rtfCellMerge = (RtfCell) mergeRow.getCells().get(realCellIndex); + rtfCellMerge.setCellMergeChild(rtfCell); + } + if(rtfCell.getColspan() > 1) { + int k = rtfCell.getColspan(); + while(k > 1 && (realCellIndex + 1 < mergeRow.getCells().size())) { + mergeRow.getCells().remove(realCellIndex + 1); + k--; + } + } + } + } + realCellIndex = realCellIndex + rtfCell.getColspan(); + } + } + + private byte[] writeRowDefinitions() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(ROW_BEGIN); + result.write('\n'); + result.write(ROW_WIDTH_STYLE); + result.write(ROW_WIDTH); + result.write(intToByteArray(this.width)); + if(this.parentTable.getCellsFitToPage()) { + result.write(ROW_KEEP_TOGETHER); + } + if(this.rowNumber <= this.parentTable.getHeaderRows()) { + result.write(ROW_HEADER_ROW); + } + switch (this.parentTable.getAlignment()) { + case Element.ALIGN_LEFT: + result.write(ROW_ALIGN_LEFT); + break; + case Element.ALIGN_RIGHT: + result.write(ROW_ALIGN_RIGHT); + break; + case Element.ALIGN_CENTER: + result.write(ROW_ALIGN_CENTER); + break; + case Element.ALIGN_JUSTIFIED: + case Element.ALIGN_JUSTIFIED_ALL: + result.write(ROW_ALIGN_JUSTIFIED); + break; + } + result.write(ROW_GRAPH); + + result.write(this.parentTable.getBorders().write()); + + if(this.parentTable.getCellSpacing() > 0) { + result.write(ROW_CELL_SPACING_LEFT); + result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2))); + result.write(ROW_CELL_SPACING_LEFT_STYLE); + result.write(ROW_CELL_SPACING_TOP); + result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2))); + result.write(ROW_CELL_SPACING_TOP_STYLE); + result.write(ROW_CELL_SPACING_RIGHT); + result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2))); + result.write(ROW_CELL_SPACING_RIGHT_STYLE); + result.write(ROW_CELL_SPACING_BOTTOM); + result.write(intToByteArray((int) (this.parentTable.getCellSpacing() / 2))); + result.write(ROW_CELL_SPACING_BOTTOM_STYLE); + } + + result.write(ROW_CELL_PADDING_LEFT); + result.write(intToByteArray((int) (this.parentTable.getCellPadding() / 2))); + result.write(ROW_CELL_PADDING_RIGHT); + result.write(intToByteArray((int) (this.parentTable.getCellPadding() / 2))); + result.write(ROW_CELL_PADDING_LEFT_STYLE); + result.write(ROW_CELL_PADDING_RIGHT_STYLE); + + result.write('\n'); + + for(int i = 0; i < this.cells.size(); i++) { + RtfCell rtfCell = (RtfCell) this.cells.get(i); + result.write(rtfCell.writeDefinition()); + } + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Writes the content of this RtfRow + * + * @return A byte array with the content of this RtfRow + */ + public byte[] write() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + result.write(writeRowDefinitions()); + + for(int i = 0; i < this.cells.size(); i++) { + RtfCell rtfCell = (RtfCell) this.cells.get(i); + result.write(rtfCell.write()); + } + + result.write(DELIMITER); + + if(this.document.getDocumentSettings().isOutputTableRowDefinitionAfter()) { + result.write(writeRowDefinitions()); + } + + result.write(ROW_END); + result.write("\n".getBytes()); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Gets the parent RtfTable of this RtfRow + * + * @return The parent RtfTable of this RtfRow + */ + protected RtfTable getParentTable() { + return this.parentTable; + } + + /** + * Gets the cells of this RtfRow + * + * @return The cells of this RtfRow + */ + protected ArrayList getCells() { + return this.cells; + } +} diff --git a/src/main/java/com/lowagie/text/rtf/table/RtfTable.java b/src/main/java/com/lowagie/text/rtf/table/RtfTable.java new file mode 100644 index 0000000..285d9e0 --- /dev/null +++ b/src/main/java/com/lowagie/text/rtf/table/RtfTable.java @@ -0,0 +1,271 @@ +/* + * $Id: RtfTable.java,v 1.11 2005/07/16 11:39:30 hallm Exp $ + * $Name: $ + * + * Copyright 2001, 2002, 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.table; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Iterator; +import java.util.ArrayList; + +import com.lowagie.text.Element; +import com.lowagie.text.Row; +import com.lowagie.text.Table; +import com.lowagie.text.rtf.RtfElement; +import com.lowagie.text.rtf.document.RtfDocument; +import com.lowagie.text.rtf.text.RtfParagraph; + + +/** + * The RtfTable wraps a Table. + * INTERNAL USE ONLY + * + * @version $Version:$ + * @author Mark Hall (mhall@edu.uni-klu.ac.at) + * @author Steffen Stundzig + * @author Benoit WIART + */ +public class RtfTable extends RtfElement { + + /** + * The rows of this RtfTable + */ + private ArrayList rows = null; + /** + * The percentage of the page width that this RtfTable covers + */ + private float tableWidthPercent = 80; + /** + * An array with the proportional widths of the cells in each row + */ + private float[] proportionalWidths = null; + /** + * The cell padding + */ + private float cellPadding = 0; + /** + * The cell spacing + */ + private float cellSpacing = 0; + /** + * The border style of this RtfTable + */ + private RtfBorderGroup borders = null; + /** + * The alignment of this RtfTable + */ + private int alignment = Element.ALIGN_CENTER; + /** + * Whether the cells in this RtfTable must fit in a page + */ + private boolean cellsFitToPage = false; + /** + * Whether the whole RtfTable must fit in a page + */ + private boolean tableFitToPage = false; + /** + * The number of header rows in this RtfTable + */ + private int headerRows = 0; + + /** + * Constructs a RtfTable based on a Table for a RtfDocument. + * + * @param doc The RtfDocument this RtfTable belongs to + * @param table The Table that this RtfTable wraps + */ + public RtfTable(RtfDocument doc, Table table) { + super(doc); + table.complete(); + importTable(table); + } + + /** + * Imports the rows and settings from the Table into this + * RtfTable. + * + * @param table The source Table + */ + private void importTable(Table table) { + this.rows = new ArrayList(); + this.tableWidthPercent = table.widthPercentage(); + this.proportionalWidths = table.getProportionalWidths(); + this.cellPadding = (float) (table.cellpadding() * TWIPS_FACTOR); + this.cellSpacing = (float) (table.cellspacing() * TWIPS_FACTOR); + this.borders = new RtfBorderGroup(this.document, RtfBorder.ROW_BORDER, table.border(), table.borderWidth(), table.borderColor()); + this.alignment = table.alignment(); + + int i = 0; + Iterator rowIterator = table.iterator(); + while(rowIterator.hasNext()) { + this.rows.add(new RtfRow(this.document, this, (Row) rowIterator.next(), i)); + i++; + } + for(i = 0; i < this.rows.size(); i++) { + ((RtfRow) this.rows.get(i)).handleCellSpanning(); + } + this.headerRows = table.lastHeaderRow(); + this.cellsFitToPage = table.hasToFitPageCells(); + this.tableFitToPage = table.hasToFitPageTable(); + } + + /** + * Writes the content of this RtfTable + * + * @return A byte array with the content of this RtfTable + */ + public byte[] write() { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + try { + if(!inHeader) { + result.write(RtfParagraph.PARAGRAPH); + } + + for(int i = 0; i < this.rows.size(); i++) { + result.write(((RtfElement) this.rows.get(i)).write()); + } + + result.write(RtfParagraph.PARAGRAPH_DEFAULTS); + } catch(IOException ioe) { + ioe.printStackTrace(); + } + return result.toByteArray(); + } + + /** + * Gets the alignment of this RtfTable + * + * @return The alignment of this RtfTable. + */ + protected int getAlignment() { + return alignment; + } + + /** + * Gets the borders of this RtfTable + * + * @return The borders of this RtfTable. + */ + protected RtfBorderGroup getBorders() { + return this.borders; + } + + /** + * Gets the cell padding of this RtfTable + * + * @return The cell padding of this RtfTable. + */ + protected float getCellPadding() { + return cellPadding; + } + + /** + * Gets the cell spacing of this RtfTable + * + * @return The cell spacing of this RtfTable. + */ + protected float getCellSpacing() { + return cellSpacing; + } + + /** + * Gets the proportional cell widths of this RtfTable + * + * @return The proportional widths of this RtfTable. + */ + protected float[] getProportionalWidths() { + return (float[]) proportionalWidths.clone(); + } + + /** + * Gets the percentage of the page width this RtfTable covers + * + * @return The percentage of the page width. + */ + protected float getTableWidthPercent() { + return tableWidthPercent; + } + + /** + * Gets the rows of this RtfTable + * + * @return The rows of this RtfTable + */ + protected ArrayList getRows() { + return this.rows; + } + + /** + * Gets the cellsFitToPage setting of this RtfTable. + * + * @return The cellsFitToPage setting of this RtfTable. + */ + protected boolean getCellsFitToPage() { + return this.cellsFitToPage; + } + + /** + * Gets the tableFitToPage setting of this RtfTable. + * + * @return The tableFitToPage setting of this RtfTable. + */ + protected boolean getTableFitToPage() { + return this.tableFitToPage; + } + + /** + * Gets the number of header rows of this RtfTable + * + * @return The number of header rows + */ + protected int getHeaderRows() { + return this.headerRows; + } +} -- cgit v1.2.3