Search in sources :

Example 6 with RelationPart

use of org.apache.poi.POIXMLDocumentPart.RelationPart in project poi by apache.

the class TestXSSFDrawing method testNew.

@Test
public void testNew() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet = wb1.createSheet();
    //multiple calls of createDrawingPatriarch should return the same instance of XSSFDrawing
    XSSFDrawing dr1 = sheet.createDrawingPatriarch();
    XSSFDrawing dr2 = sheet.createDrawingPatriarch();
    assertSame(dr1, dr2);
    List<RelationPart> rels = sheet.getRelationParts();
    assertEquals(1, rels.size());
    RelationPart rp = rels.get(0);
    assertTrue(rp.getDocumentPart() instanceof XSSFDrawing);
    XSSFDrawing drawing = rp.getDocumentPart();
    String drawingId = rp.getRelationship().getId();
    //there should be a relation to this drawing in the worksheet
    assertTrue(sheet.getCTWorksheet().isSetDrawing());
    assertEquals(drawingId, sheet.getCTWorksheet().getDrawing().getId());
    XSSFConnector c1 = drawing.createConnector(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 2, 2));
    c1.setLineWidth(2.5);
    c1.setLineStyle(1);
    XSSFShapeGroup c2 = drawing.createGroup(new XSSFClientAnchor(0, 0, 0, 0, 0, 0, 5, 5));
    assertNotNull(c2);
    XSSFSimpleShape c3 = drawing.createSimpleShape(new XSSFClientAnchor(0, 0, 0, 0, 2, 2, 3, 4));
    c3.setText(new XSSFRichTextString("Test String"));
    c3.setFillColor(128, 128, 128);
    XSSFTextBox c4 = drawing.createTextbox(new XSSFClientAnchor(0, 0, 0, 0, 4, 4, 5, 6));
    XSSFRichTextString rt = new XSSFRichTextString("Test String");
    rt.applyFont(0, 5, wb1.createFont());
    rt.applyFont(5, 6, wb1.createFont());
    c4.setText(rt);
    c4.setNoFill(true);
    assertEquals(4, drawing.getCTDrawing().sizeOfTwoCellAnchorArray());
    List<XSSFShape> shapes = drawing.getShapes();
    assertEquals(4, shapes.size());
    assertTrue(shapes.get(0) instanceof XSSFConnector);
    assertTrue(shapes.get(1) instanceof XSSFShapeGroup);
    assertTrue(shapes.get(2) instanceof XSSFSimpleShape);
    //
    assertTrue(shapes.get(3) instanceof XSSFSimpleShape);
    // Save and re-load it
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sheet = wb2.getSheetAt(0);
    // Check
    dr1 = sheet.createDrawingPatriarch();
    CTDrawing ctDrawing = dr1.getCTDrawing();
    // Connector, shapes and text boxes are all two cell anchors
    assertEquals(0, ctDrawing.sizeOfAbsoluteAnchorArray());
    assertEquals(0, ctDrawing.sizeOfOneCellAnchorArray());
    assertEquals(4, ctDrawing.sizeOfTwoCellAnchorArray());
    shapes = dr1.getShapes();
    assertEquals(4, shapes.size());
    assertTrue(shapes.get(0) instanceof XSSFConnector);
    assertTrue(shapes.get(1) instanceof XSSFShapeGroup);
    assertTrue(shapes.get(2) instanceof XSSFSimpleShape);
    //
    assertTrue(shapes.get(3) instanceof XSSFSimpleShape);
    // Ensure it got the right namespaces
    String xml = ctDrawing.toString();
    assertTrue(xml.contains("xmlns:xdr=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\""));
    assertTrue(xml.contains("xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\""));
    checkRewrite(wb2);
    wb2.close();
}
Also used : CTDrawing(org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing) RelationPart(org.apache.poi.POIXMLDocumentPart.RelationPart) Test(org.junit.Test)

Example 7 with RelationPart

use of org.apache.poi.POIXMLDocumentPart.RelationPart in project poi by apache.

the class TestXSSFDrawing method testReadTextBoxParagraphs.

/**
     * Test reading multiple paragraphs from a textbox in an existing file
     */
@Test
public void testReadTextBoxParagraphs() throws IOException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithTextBox.xlsx");
    XSSFSheet sheet = wb.getSheetAt(0);
    //the sheet has one relationship and it is XSSFDrawing
    List<RelationPart> rels = sheet.getRelationParts();
    assertEquals(1, rels.size());
    RelationPart rp = rels.get(0);
    assertTrue(rp.getDocumentPart() instanceof XSSFDrawing);
    XSSFDrawing drawing = rp.getDocumentPart();
    //sheet.createDrawingPatriarch() should return the same instance of XSSFDrawing
    assertSame(drawing, sheet.createDrawingPatriarch());
    String drawingId = rp.getRelationship().getId();
    //there should be a relation to this drawing in the worksheet
    assertTrue(sheet.getCTWorksheet().isSetDrawing());
    assertEquals(drawingId, sheet.getCTWorksheet().getDrawing().getId());
    List<XSSFShape> shapes = drawing.getShapes();
    assertEquals(1, shapes.size());
    assertTrue(shapes.get(0) instanceof XSSFSimpleShape);
    XSSFSimpleShape textbox = (XSSFSimpleShape) shapes.get(0);
    List<XSSFTextParagraph> paras = textbox.getTextParagraphs();
    assertEquals(3, paras.size());
    // check content of second paragraph
    assertEquals("Line 2", paras.get(1).getText());
    // check content of entire textbox
    assertEquals("Line 1\nLine 2\nLine 3", textbox.getText());
    // check attributes of paragraphs
    assertEquals(TextAlign.LEFT, paras.get(0).getTextAlign());
    assertEquals(TextAlign.CENTER, paras.get(1).getTextAlign());
    assertEquals(TextAlign.RIGHT, paras.get(2).getTextAlign());
    Color clr = paras.get(0).getTextRuns().get(0).getFontColor();
    assertArrayEquals(new int[] { 255, 0, 0 }, new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
    clr = paras.get(1).getTextRuns().get(0).getFontColor();
    assertArrayEquals(new int[] { 0, 255, 0 }, new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
    clr = paras.get(2).getTextRuns().get(0).getFontColor();
    assertArrayEquals(new int[] { 0, 0, 255 }, new int[] { clr.getRed(), clr.getGreen(), clr.getBlue() });
    checkRewrite(wb);
    wb.close();
}
Also used : RelationPart(org.apache.poi.POIXMLDocumentPart.RelationPart) Test(org.junit.Test)

Example 8 with RelationPart

use of org.apache.poi.POIXMLDocumentPart.RelationPart in project poi by apache.

the class TestXSLFBugs method tika705.

/**
     * Slide relations with anchors in them
     */
@Test
public void tika705() throws Exception {
    XMLSlideShow ss = XSLFTestDataSamples.openSampleDocument("with_japanese.pptx");
    // Should have one slide
    assertEquals(1, ss.getSlides().size());
    XSLFSlide slide = ss.getSlides().get(0);
    // Check the relations from this
    Collection<RelationPart> rels = slide.getRelationParts();
    // Should have 6 relations:
    //   1 external hyperlink (skipped from list)
    //   4 internal hyperlinks
    //   1 slide layout
    assertEquals(5, rels.size());
    int layouts = 0;
    int hyperlinks = 0;
    for (RelationPart p : rels) {
        if (p.getRelationship().getRelationshipType().equals(XSLFRelation.HYPERLINK.getRelation())) {
            hyperlinks++;
        } else if (p.getDocumentPart() instanceof XSLFSlideLayout) {
            layouts++;
        }
    }
    assertEquals(1, layouts);
    assertEquals(4, hyperlinks);
    // Hyperlinks should all be to #_ftn1 or #ftnref1
    for (RelationPart p : rels) {
        if (p.getRelationship().getRelationshipType().equals(XSLFRelation.HYPERLINK.getRelation())) {
            URI target = p.getRelationship().getTargetURI();
            //noinspection StatementWithEmptyBody
            if (target.getFragment().equals("_ftn1") || target.getFragment().equals("_ftnref1")) {
            // Good
            } else {
                fail("Invalid target " + target.getFragment() + " on " + target);
            }
        }
    }
    ss.close();
}
Also used : RelationPart(org.apache.poi.POIXMLDocumentPart.RelationPart) URI(java.net.URI) DrawPaint(org.apache.poi.sl.draw.DrawPaint) SolidPaint(org.apache.poi.sl.usermodel.PaintStyle.SolidPaint) TexturePaint(org.apache.poi.sl.usermodel.PaintStyle.TexturePaint) Test(org.junit.Test)

Example 9 with RelationPart

use of org.apache.poi.POIXMLDocumentPart.RelationPart in project poi by apache.

the class TestXSSFBugs method bug47504.

@Test
public void bug47504() throws IOException {
    XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("47504.xlsx");
    assertEquals(1, wb1.getNumberOfSheets());
    XSSFSheet sh = wb1.getSheetAt(0);
    XSSFDrawing drawing = sh.createDrawingPatriarch();
    List<RelationPart> rels = drawing.getRelationParts();
    assertEquals(1, rels.size());
    assertEquals("Sheet1!A1", rels.get(0).getRelationship().getTargetURI().getFragment());
    // And again, just to be sure
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    assertEquals(1, wb2.getNumberOfSheets());
    sh = wb2.getSheetAt(0);
    drawing = sh.createDrawingPatriarch();
    rels = drawing.getRelationParts();
    assertEquals(1, rels.size());
    assertEquals("Sheet1!A1", rels.get(0).getRelationship().getTargetURI().getFragment());
    wb2.close();
}
Also used : RelationPart(org.apache.poi.POIXMLDocumentPart.RelationPart) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Aggregations

RelationPart (org.apache.poi.POIXMLDocumentPart.RelationPart)9 Test (org.junit.Test)8 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)2 CTDrawing (org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing)2 URI (java.net.URI)1 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)1 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)1 DrawPaint (org.apache.poi.sl.draw.DrawPaint)1 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)1 TexturePaint (org.apache.poi.sl.usermodel.PaintStyle.TexturePaint)1 ClientAnchor (org.apache.poi.ss.usermodel.ClientAnchor)1