public void drawString(String str, int x, int y) { if (str == null || str.equals("")) return; Font excelFont = font; if (font.getName().equals("SansSerif")) { excelFont = new Font("Arial", font.getStyle(), (int) (font.getSize() / verticalPixelsPerPoint)); } else { excelFont = new Font( font.getName(), font.getStyle(), (int) (font.getSize() / verticalPixelsPerPoint)); } FontDetails d = StaticFontMetrics.getFontDetails(excelFont); int width = (int) ((d.getStringWidth(str) * 8) + 12); int height = (int) ((font.getSize() / verticalPixelsPerPoint) + 6) * 2; y -= (font.getSize() / verticalPixelsPerPoint) + 2 * verticalPixelsPerPoint; // we want to draw the shape from the top-left HSSFTextbox textbox = escherGroup.createTextbox(new HSSFChildAnchor(x, y, x + width, y + height)); textbox.setNoFill(true); textbox.setLineStyle(HSSFShape.LINESTYLE_NONE); HSSFRichTextString s = new HSSFRichTextString(str); HSSFFont hssfFont = matchFont(excelFont); s.applyFont(hssfFont); textbox.setString(s); }
public void fillRect(int x, int y, int width, int height) { HSSFSimpleShape shape = escherGroup.createShape(new HSSFChildAnchor(x, y, x + width, y + height)); shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); shape.setLineStyle(HSSFShape.LINESTYLE_NONE); shape.setFillColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue()); shape.setLineStyleColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue()); }
public void drawOval(int x, int y, int width, int height) { HSSFSimpleShape shape = escherGroup.createShape(new HSSFChildAnchor(x, y, x + width, y + height)); shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL); shape.setLineWidth(0); shape.setLineStyleColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue()); shape.setNoFill(true); }
/** * Fills a (closed) polygon, as defined by a pair of arrays, which hold the <i>x</i> and <i>y</i> * coordinates. * * <p>This draws the polygon, with <code>nPoint</code> line segments. The first <code> * nPoint - 1</code> line segments are drawn between sequential points (<code> * xPoints[i],yPoints[i],xPoints[i+1],yPoints[i+1]</code>). The final line segment is a closing * one, from the last point to the first (assuming they are different). * * <p>The area inside of the polygon is defined by using an even-odd fill rule (also known as the * alternating rule), and the area inside of it is filled. * * @param xPoints array of the <code>x</code> coordinates. * @param yPoints array of the <code>y</code> coordinates. * @param nPoints the total number of points in the polygon. * @see java.awt.Graphics#drawPolygon(int[], int[], int) */ public void fillPolygon(int xPoints[], int yPoints[], int nPoints) { int right = findBiggest(xPoints); int bottom = findBiggest(yPoints); int left = findSmallest(xPoints); int top = findSmallest(yPoints); HSSFPolygon shape = escherGroup.createPolygon(new HSSFChildAnchor(left, top, right, bottom)); shape.setPolygonDrawArea(right - left, bottom - top); shape.setPoints(addToAll(xPoints, -left), addToAll(yPoints, -top)); shape.setLineStyleColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue()); shape.setFillColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue()); }
/** * HSSFShape tree bust be built correctly Check file with such records structure: -patriarch * --shape --group ---group ----shape ----shape ---shape ---group ----shape ----shape */ public void testDrawingGroups() { HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls"); HSSFSheet sheet = wb.getSheet("groups"); HSSFPatriarch patriarch = sheet.getDrawingPatriarch(); assertEquals(patriarch.getChildren().size(), 2); HSSFShapeGroup group = (HSSFShapeGroup) patriarch.getChildren().get(1); assertEquals(3, group.getChildren().size()); HSSFShapeGroup group1 = (HSSFShapeGroup) group.getChildren().get(0); assertEquals(2, group1.getChildren().size()); group1 = (HSSFShapeGroup) group.getChildren().get(2); assertEquals(2, group1.getChildren().size()); }
public void drawLine(int x1, int y1, int x2, int y2, int width) { HSSFSimpleShape shape = escherGroup.createShape(new HSSFChildAnchor(x1, y1, x2, y2)); shape.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE); shape.setLineWidth(width); shape.setLineStyleColor(foreground.getRed(), foreground.getGreen(), foreground.getBlue()); }
public void testRemoveShapes() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(); HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); HSSFSimpleShape rectangle = patriarch.createSimpleShape(new HSSFClientAnchor()); rectangle.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE); int idx = wb.addPicture(new byte[] {1, 2, 3}, Workbook.PICTURE_TYPE_JPEG); patriarch.createPicture(new HSSFClientAnchor(), idx); patriarch.createCellComment(new HSSFClientAnchor()); HSSFPolygon polygon = patriarch.createPolygon(new HSSFClientAnchor()); polygon.setPoints(new int[] {1, 2}, new int[] {2, 3}); patriarch.createTextbox(new HSSFClientAnchor()); HSSFShapeGroup group = patriarch.createGroup(new HSSFClientAnchor()); group.createTextbox(new HSSFChildAnchor()); group.createPicture(new HSSFChildAnchor(), idx); assertEquals(patriarch.getChildren().size(), 6); assertEquals(group.getChildren().size(), 2); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 12); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1); wb = HSSFTestDataSamples.writeOutAndReadBack(wb); sheet = wb.getSheetAt(0); patriarch = sheet.getDrawingPatriarch(); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 12); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1); assertEquals(patriarch.getChildren().size(), 6); group = (HSSFShapeGroup) patriarch.getChildren().get(5); group.removeShape(group.getChildren().get(0)); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 10); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1); wb = HSSFTestDataSamples.writeOutAndReadBack(wb); sheet = wb.getSheetAt(0); patriarch = sheet.getDrawingPatriarch(); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 10); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1); group = (HSSFShapeGroup) patriarch.getChildren().get(5); patriarch.removeShape(group); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 8); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1); wb = HSSFTestDataSamples.writeOutAndReadBack(wb); sheet = wb.getSheetAt(0); patriarch = sheet.getDrawingPatriarch(); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 8); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1); assertEquals(patriarch.getChildren().size(), 5); HSSFShape shape = patriarch.getChildren().get(0); patriarch.removeShape(shape); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 6); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1); assertEquals(patriarch.getChildren().size(), 4); wb = HSSFTestDataSamples.writeOutAndReadBack(wb); sheet = wb.getSheetAt(0); patriarch = sheet.getDrawingPatriarch(); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 6); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1); assertEquals(patriarch.getChildren().size(), 4); HSSFPicture picture = (HSSFPicture) patriarch.getChildren().get(0); patriarch.removeShape(picture); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 5); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1); assertEquals(patriarch.getChildren().size(), 3); wb = HSSFTestDataSamples.writeOutAndReadBack(wb); sheet = wb.getSheetAt(0); patriarch = sheet.getDrawingPatriarch(); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 5); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 1); assertEquals(patriarch.getChildren().size(), 3); HSSFComment comment = (HSSFComment) patriarch.getChildren().get(0); patriarch.removeShape(comment); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 3); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0); assertEquals(patriarch.getChildren().size(), 2); wb = HSSFTestDataSamples.writeOutAndReadBack(wb); sheet = wb.getSheetAt(0); patriarch = sheet.getDrawingPatriarch(); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 3); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0); assertEquals(patriarch.getChildren().size(), 2); polygon = (HSSFPolygon) patriarch.getChildren().get(0); patriarch.removeShape(polygon); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 2); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0); assertEquals(patriarch.getChildren().size(), 1); wb = HSSFTestDataSamples.writeOutAndReadBack(wb); sheet = wb.getSheetAt(0); patriarch = sheet.getDrawingPatriarch(); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 2); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0); assertEquals(patriarch.getChildren().size(), 1); HSSFTextbox textbox = (HSSFTextbox) patriarch.getChildren().get(0); patriarch.removeShape(textbox); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 0); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0); assertEquals(patriarch.getChildren().size(), 0); wb = HSSFTestDataSamples.writeOutAndReadBack(wb); sheet = wb.getSheetAt(0); patriarch = sheet.getDrawingPatriarch(); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getShapeToObjMapping().size(), 0); assertEquals(HSSFTestHelper.getEscherAggregate(patriarch).getTailRecords().size(), 0); assertEquals(patriarch.getChildren().size(), 0); }