Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java?rev=1821764&r1=1821763&r2=1821764&view=diff ============================================================================== --- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java (original) +++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFChart.java Sat Jan 20 23:01:18 2018 @@ -17,13 +17,8 @@ package org.apache.poi.xwpf.usermodel; -import static org.apache.poi.POIXMLTypeLoader.DEFAULT_XML_OPTIONS; - import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; - -import javax.xml.namespace.QName; import org.apache.poi.POIXMLException; import org.apache.poi.openxml4j.opc.PackagePart; @@ -31,40 +26,52 @@ import org.apache.poi.util.Beta; import org.apache.poi.util.IOUtils; import org.apache.poi.xddf.usermodel.chart.XDDFChart; import org.apache.xmlbeans.XmlException; -import org.apache.xmlbeans.XmlOptions; -import org.openxmlformats.schemas.drawingml.x2006.chart.CTChartSpace; +import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; /** * Represents a Chart in a .docx file */ @Beta public class XWPFChart extends XDDFChart { + /** + * default width of chart in emu + */ + public static final int DEFAULT_WIDTH = 500000; + + /** + * default height of chart in emu + */ + public static final int DEFAULT_HEIGHT = 500000; // lazy initialization private Long checksum; /** + * this object is used to modify drawing properties + */ + private CTInline ctInline; + + /** + * constructor to + * Create a new chart in document + * + * @since POI 4.0.0 + */ + protected XWPFChart() { + super(); + } + + /** * Construct a chart from a package part. * * @param part the package part holding the chart data, - * the content type must be <code>application/vnd.openxmlformats-officedocument.drawingml.chart+xml</code> - * + * the content type must be <code>application/vnd.openxmlformats-officedocument.drawingml.chart+xml</code> * @since POI 4.0.0 */ protected XWPFChart(PackagePart part) throws IOException, XmlException { super(part); } - @Override - protected void commit() throws IOException { - XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS); - xmlOptions.setSaveSyntheticDocumentElement(new QName(CTChartSpace.type.getName().getNamespaceURI(), "chartSpace", "c")); - - try (OutputStream out = getPackagePart().getOutputStream()) { - chartSpace.save(out, xmlOptions); - } - } - public Long getChecksum() { if (this.checksum == null) { InputStream is = null; @@ -120,4 +127,160 @@ public class XWPFChart extends XDDFChart public int hashCode() { return getChecksum().hashCode(); } + + /** + * initialize in line object + * + * @param inline this object is used to adjust the margin and dimension of chart + * @since POI 4.0.0 + */ + protected void setAttachTo(CTInline ctInline) { + this.ctInline = ctInline; + } + + /** + * set chart height + * + * @param height height of chart + * @since POI 4.0.0 + */ + public void setChartHeight(long height) { + ctInline.getExtent().setCy(height); + } + + /** + * set chart width + * + * @param width width of chart + * @since POI 4.0.0 + */ + public void setChartWidth(long width) { + ctInline.getExtent().setCx(width); + } + + /** + * get chart height + * + * @since POI 4.0.0 + */ + public long getChartHeight() { + return ctInline.getExtent().getCy(); + } + + /** + * get chart width + * + * @since POI 4.0.0 + */ + public long getChartWidth() { + return ctInline.getExtent().getCx(); + } + + /** + * set chart height and width + * + * @param width width of chart + * @param height height of chart + * @since POI 4.0.0 + */ + public void setChartBoundingBox(long width, long height) { + this.setChartWidth(width); + this.setChartHeight(height); + } + + /** + * set margin from top + * + * @param margin margin from top + * @since POI 4.0.0 + */ + public void setChartTopMargin(long margin) { + ctInline.setDistT(margin); + } + + /** + * get margin from Top + * + * @param margin + * @since POI 4.0.0 + */ + public long getChartTopMargin(long margin) { + return ctInline.getDistT(); + } + + /** + * set margin from bottom + * + * @param margin margin from Bottom + * @since POI 4.0.0 + */ + public void setChartBottomMargin(long margin) { + ctInline.setDistB(margin); + } + + /** + * get margin from Bottom + * + * @param margin + * @since POI 4.0.0 + */ + public long getChartBottomMargin(long margin) { + return ctInline.getDistB(); + } + + /** + * set margin from left + * + * @param margin margin from left + * @since POI 4.0.0 + */ + public void setChartLeftMargin(long margin) { + ctInline.setDistL(margin); + } + + /** + * get margin from left + * + * @param margin + * @since POI 4.0.0 + */ + public long getChartLeftMargin(long margin) { + return ctInline.getDistL(); + } + + /** + * set margin from Right + * + * @param margin from right + * @since POI 4.0.0 + */ + public void setChartRightMargin(long margin) { + ctInline.setDistR(margin); + } + + /** + * get margin from Right + * + * @param margin + * @since POI 4.0.0 + */ + public long getChartRightMargin(long margin) { + return ctInline.getDistR(); + } + + /** + * set chart margin + * + * @param top margin from top + * @param right margin from right + * @param bottom margin from bottom + * @param left margin from left + * @since POI 4.0.0 + */ + public void setChartMargin(long top, long right, long bottom, long left) { + this.setChartBottomMargin(bottom); + this.setChartRightMargin(right); + this.setChartLeftMargin(left); + this.setChartRightMargin(right); + } }
Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java?rev=1821764&r1=1821763&r2=1821764&view=diff ============================================================================== --- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java (original) +++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java Sat Jan 20 23:01:18 2018 @@ -63,6 +63,7 @@ import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlOptions; +import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1; @@ -245,7 +246,6 @@ public class XWPFDocument extends POIXML } else if (relation.equals(XWPFRelation.CHART.getRelation())) { //now we can use all methods to modify charts in XWPFDocument XWPFChart chartData = (XWPFChart) p; -// chartData.onDocumentRead(); // ??? there is nothing to be done there!!! charts.add(chartData); } else if (relation.equals(XWPFRelation.GLOSSARY_DOCUMENT.getRelation())) { // We don't currently process the glossary itself @@ -356,6 +356,7 @@ public class XWPFDocument extends POIXML public List<XWPFChart> getCharts() { return Collections.unmodifiableList(charts); } + /** * @see org.apache.poi.xwpf.usermodel.IBody#getTableArray(int) */ @@ -375,7 +376,7 @@ public class XWPFDocument extends POIXML } public XWPFFooter getFooterArray(int pos) { - if(pos >=0 && pos < footers.size()) { + if (pos >= 0 && pos < footers.size()) { return footers.get(pos); } return null; @@ -389,7 +390,7 @@ public class XWPFDocument extends POIXML } public XWPFHeader getHeaderArray(int pos) { - if(pos >=0 && pos < headers.size()) { + if (pos >= 0 && pos < headers.size()) { return headers.get(pos); } return null; @@ -468,11 +469,12 @@ public class XWPFDocument extends POIXML public XWPFHeaderFooterPolicy getHeaderFooterPolicy() { return headerFooterPolicy; } + public XWPFHeaderFooterPolicy createHeaderFooterPolicy() { if (headerFooterPolicy == null) { -// if (! ctDocument.getBody().isSetSectPr()) { -// ctDocument.getBody().addNewSectPr(); -// } + // if (! ctDocument.getBody().isSetSectPr()) { + // ctDocument.getBody().addNewSectPr(); + // } headerFooterPolicy = new XWPFHeaderFooterPolicy(this); } return headerFooterPolicy; @@ -493,7 +495,7 @@ public class XWPFDocument extends POIXML CTOnOff titlePg = ctSectPr.addNewTitlePg(); titlePg.setVal(STOnOff.ON); } - // } else if (type == HeaderFooterType.EVEN) { + // } else if (type == HeaderFooterType.EVEN) { // TODO Add support for Even/Odd headings and footers } return hfPolicy.createHeader(STHdrFtr.Enum.forInt(type.toInt())); @@ -515,7 +517,7 @@ public class XWPFDocument extends POIXML CTOnOff titlePg = ctSectPr.addNewTitlePg(); titlePg.setVal(STOnOff.ON); } - // } else if (type == HeaderFooterType.EVEN) { + // } else if (type == HeaderFooterType.EVEN) { // TODO Add support for Even/Odd headings and footers } return hfPolicy.createFooter(STHdrFtr.Enum.forInt(type.toInt())); @@ -1014,7 +1016,7 @@ public class XWPFDocument extends POIXML ctDocument.getBody().setTblArray(pos, table.getCTTbl()); } - /** + /** * Verifies that the documentProtection tag in settings.xml file <br> * specifies that the protection is enforced (w:enforcement="1") <br> * <br> @@ -1608,4 +1610,59 @@ public class XWPFDocument extends POIXML public XWPFDocument getXWPFDocument() { return this; } + + /** + * This method is used to create template for chart XML + * no need to read MS-Word file and modify charts + * + * @return This method return object of XWPFChart Object with default height and width + * @throws InvalidFormatException + * @throws IOException + * @since POI 4.0.0 + */ + public XWPFChart createChart() throws InvalidFormatException, IOException { + return createChart(XWPFChart.DEFAULT_WIDTH, XWPFChart.DEFAULT_HEIGHT); + } + + /** + * This method is used to create template for chart XML + * no need to read MS-Word file and modify charts + * + * @param width width of chart in document + * @param height height of chart in document + * @return This method return object of XWPFChart + * @throws InvalidFormatException + * @throws IOException + * @since POI 4.0.0 + */ + public XWPFChart createChart(int width, int height) throws InvalidFormatException, IOException { + + //get chart number + int chartNumber = getPackagePart().getPackage(). + getPartsByContentType(XWPFRelation.CHART.getContentType()).size() + 1; + + //create relationship in document for new chart + RelationPart rp = createRelationship( + XWPFRelation.CHART, XWPFFactory.getInstance(), chartNumber, false); + + //get chart relationship id + String chartId = rp.getRelationship().getId(); + + //create paragraph and run object + XWPFRun xRun = this.createParagraph().createRun(); + + CTInline inline = xRun.addChart(width, height, chartId); + + //get package part of xwpfchart object + XWPFChart xwpfChart = rp.getDocumentPart(); + + xwpfChart.setChartIndex(chartNumber); + + //set in line object into xwpfchart object + xwpfChart.setAttachTo(inline); + + //add chart object to chart list + charts.add(xwpfChart); + return xwpfChart; + } } Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java?rev=1821764&r1=1821763&r2=1821764&view=diff ============================================================================== --- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java (original) +++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java Sat Jan 20 23:01:18 2018 @@ -20,9 +20,11 @@ package org.apache.poi.xwpf.usermodel; import java.util.HashMap; import java.util.Map; +import org.apache.poi.POIXMLDocument; import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLRelation; import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * @author Yegor Kozlov @@ -112,6 +114,14 @@ public final class XWPFRelation extends "/word/theme/theme#.xml", null ); + + public static final XWPFRelation WORKBOOK_RELATIONSHIP = new XWPFRelation( + "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + POIXMLDocument.PACK_OBJECT_REL_TYPE, + "/word/embeddings/Microsoft_Excel_Worksheet#.xlsx", + XSSFWorkbook.class + ); + public static final XWPFRelation CHART = new XWPFRelation( "application/vnd.openxmlformats-officedocument.drawingml.chart+xml", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart", Modified: poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java?rev=1821764&r1=1821763&r2=1821764&view=diff ============================================================================== --- poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java (original) +++ poi/trunk/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java Sat Jan 20 23:01:18 2018 @@ -39,6 +39,8 @@ import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlString; import org.apache.xmlbeans.XmlToken; import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl; +import org.openxmlformats.schemas.drawingml.x2006.chart.CTChart; +import org.openxmlformats.schemas.drawingml.x2006.chart.CTRelId; import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip; import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties; import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject; @@ -242,9 +244,9 @@ public class XWPFRun implements ISDTCont return true; final STOnOff.Enum val = onoff.getVal(); return ( - (STOnOff.TRUE == val) || - (STOnOff.X_1 == val) || - (STOnOff.ON == val) + (STOnOff.TRUE == val) || + (STOnOff.X_1 == val) || + (STOnOff.ON == val) ); } @@ -256,7 +258,7 @@ public class XWPFRun implements ISDTCont public String getLang() { CTRPr pr = run.getRPr(); Object lang = pr == null || !pr.isSetLang() ? null : pr.getLang().getVal(); - return (String)lang; + return (String) lang; } /** @@ -917,7 +919,7 @@ public class XWPFRun implements ISDTCont * @param width width in EMUs. To convert to / from points use {@link org.apache.poi.util.Units} * @param height height in EMUs. To convert to / from points use {@link org.apache.poi.util.Units} * @throws InvalidFormatException If the format of the picture is not known. - * @throws IOException If reading the picture-data from the stream fails. + * @throws IOException If reading the picture-data from the stream fails. * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_EMF * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_WMF * @see org.apache.poi.xwpf.usermodel.Document#PICTURE_TYPE_PICT @@ -929,12 +931,12 @@ public class XWPFRun implements ISDTCont throws InvalidFormatException, IOException { String relationId; XWPFPictureData picData; - + // Work out what to add the picture to, then add both the // picture and the relationship for it // TODO Should we have an interface for this sort of thing? if (parent.getPart() instanceof XWPFHeaderFooter) { - XWPFHeaderFooter headerFooter = (XWPFHeaderFooter)parent.getPart(); + XWPFHeaderFooter headerFooter = (XWPFHeaderFooter) parent.getPart(); relationId = headerFooter.addPictureData(pictureData, pictureType); picData = (XWPFPictureData) headerFooter.getRelationById(relationId); } else { @@ -1026,6 +1028,62 @@ public class XWPFRun implements ISDTCont } /** + * this method add chart template into document + * + * @param width set width of chart object + * @param height set height of chart object + * @param chartRelId relation id of chart in document relation file + * @throws InvalidFormatException + * @throws IOException + * @since POI 4.0.0 + */ + @Internal + public CTInline addChart(int width, int height, String chartRelId) + throws InvalidFormatException, IOException { + try { + CTInline inline = run.addNewDrawing().addNewInline(); + + //xml part of chart in document + String xml = + "<a:graphic xmlns:a=\"" + CTGraphicalObject.type.getName().getNamespaceURI() + "\">" + + "<a:graphicData uri=\"" + CTChart.type.getName().getNamespaceURI() + "\">" + + "<c:chart xmlns:c=\"" + CTChart.type.getName().getNamespaceURI() + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" r:id=\"" + chartRelId + "\" />" + + "</a:graphicData>" + + "</a:graphic>"; + + InputSource is = new InputSource(new StringReader(xml)); + + org.w3c.dom.Document doc = DocumentHelper.readDocument(is); + + inline.set(XmlToken.Factory.parse(doc.getDocumentElement(), DEFAULT_XML_OPTIONS)); + + // Setup the inline with 0 margin + inline.setDistT(0); + inline.setDistR(0); + inline.setDistB(0); + inline.setDistL(0); + + CTNonVisualDrawingProps docPr = inline.addNewDocPr(); + long id = getParent().getDocument().getDrawingIdManager().reserveNew(); + docPr.setId(id); + //This name is not visible in Word anywhere. + docPr.setName("chart " + id); + + CTPositiveSize2D extent = inline.addNewExtent(); + //set hegiht and width of drawaing object; + extent.setCx(width); + extent.setCy(height); + + return inline; + } catch (XmlException e) { + throw new IllegalStateException(e); + } catch (SAXException e) { + throw new IllegalStateException(e); + } + } + + + /** * Returns the embedded pictures of the run. These * are pictures which reference an external, * embedded picture image such as a .png or .jpg @@ -1040,7 +1098,7 @@ public class XWPFRun implements ISDTCont public String toString() { String phonetic = getPhonetic(); if (phonetic.length() > 0) { - return text() +" ("+ phonetic +")"; + return text() + " (" + phonetic + ")"; } else { return text(); } @@ -1071,7 +1129,6 @@ public class XWPFRun implements ISDTCont } /** - * * @return the phonetic (ruby) string associated with this run or an empty String if none exists */ public String getPhonetic() { @@ -1096,9 +1153,8 @@ public class XWPFRun implements ISDTCont } /** - * - * @param rubyObj rubyobject - * @param text buffer to which to append the content + * @param rubyObj rubyobject + * @param text buffer to which to append the content * @param extractPhonetic extract the phonetic (rt) component or the base component */ private void handleRuby(XmlObject rubyObj, StringBuilder text, boolean extractPhonetic) { @@ -1124,7 +1180,7 @@ public class XWPFRun implements ISDTCont } else { if (extractPhonetic && inRT) { _getText(o, text); - } else if (! extractPhonetic && inBase) { + } else if (!extractPhonetic && inBase) { _getText(o, text); } } Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFChart.java URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFChart.java?rev=1821764&r1=1821763&r2=1821764&view=diff ============================================================================== --- poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFChart.java (original) +++ poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFChart.java Sat Jan 20 23:01:18 2018 @@ -20,6 +20,7 @@ package org.apache.poi.xwpf.usermodel; import java.io.IOException; import java.util.List; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.xddf.usermodel.chart.XDDFBarChartData; import org.apache.poi.xddf.usermodel.chart.XDDFChartData; import org.apache.poi.xwpf.XWPFTestDataSamples; @@ -37,8 +38,7 @@ public class TestXWPFChart extends TestC /** * test method to check charts are not null */ - public void testRead() throws IOException - { + public void testRead() throws IOException { XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx"); List<XWPFChart> charts = sampleDoc.getCharts(); assertNotNull(charts); @@ -58,11 +58,10 @@ public class TestXWPFChart extends TestC /** * test method to add chart title and check whether it's set */ - public void testChartTitle() throws IOException - { + public void testChartTitle() throws IOException { XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx"); List<XWPFChart> charts = sampleDoc.getCharts(); - XWPFChart chart=charts.get(0); + XWPFChart chart = charts.get(0); CTChart ctChart = chart.getCTChart(); CTTitle title = ctChart.getTitle(); CTTx tx = title.addNewTx(); @@ -75,16 +74,42 @@ public class TestXWPFChart extends TestC r.setT("XWPF CHART"); assertEquals("XWPF CHART", chart.getCTChart().getTitle().getTx().getRich().getPArray(0).getRArray(0).getT()); } + /** * test method to check relationship */ - public void testChartRelation() throws IOException - { + public void testChartRelation() throws IOException { XWPFDocument sampleDoc = XWPFTestDataSamples.openSampleDocument("61745.docx"); List<XWPFChart> charts = sampleDoc.getCharts(); - XWPFChart chart=charts.get(0); + XWPFChart chart = charts.get(0); assertEquals(XWPFRelation.CHART.getContentType(), chart.getPackagePart().getContentType()); assertEquals("/word/document.xml", chart.getParent().getPackagePart().getPartName().getName()); assertEquals("/word/charts/chart1.xml", chart.getPackagePart().getPartName().getName()); } + + /** + * test method to check adding chart in document + */ + public static void testAddChartsToNewDocument() throws InvalidFormatException, IOException { + + XWPFDocument document = new XWPFDocument(); + + XWPFChart chart = document.createChart(); + assertEquals(1, document.getCharts().size()); + assertNotNull(chart); + assertNotNull(chart.getCTChartSpace()); + assertNotNull(chart.getCTChart()); + assertEquals(XWPFChart.DEFAULT_HEIGHT, chart.getChartHeight()); + assertEquals(XWPFChart.DEFAULT_WIDTH, chart.getChartWidth()); + + XWPFChart chart2 = document.createChart(); + assertEquals(2, document.getCharts().size()); + assertNotNull(chart2); + assertNotNull(chart2.getCTChartSpace()); + assertNotNull(chart2.getCTChart()); + chart.setChartHeight(500500); + assertEquals(500500, chart.getChartHeight()); + + assertNotNull(XWPFTestDataSamples.writeOutAndReadBack(document)); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@poi.apache.org For additional commands, e-mail: commits-h...@poi.apache.org