/** * Sets font style (bold/italic) for a paragraph run * * @param runElement Run * @param italic wheter to set italic style * @param bold wheter to set bold style */ private void setFontStyle(R runElement, boolean italic, boolean bold) { RPr runProps = runElement.getRPr(); if (null == runProps) runProps = objectFactory.createRPr(); if (italic) runProps.setI(objectFactory.createBooleanDefaultTrue()); if (bold) runProps.setB(objectFactory.createBooleanDefaultTrue()); runElement.setRPr(runProps); }
private void addCellStyle( Tc tableCell, BandElement be, String content, P paragraph, Map<String, Object> style) { if (style != null) { // inner html text if (content.startsWith("<html>")) { try { wordMLPackage .getMainDocumentPart() .addAltChunk(AltChunkType.Html, content.getBytes(), tableCell); tableCell.getContent().add(paragraph); } catch (Docx4JException e) { e.printStackTrace(); } return; } Text text = factory.createText(); text.setValue(content); R run = factory.createR(); run.getContent().add(text); paragraph.getContent().add(run); setHorizontalAlignment(paragraph, style); RPr runProperties = factory.createRPr(); setFont(tableCell, style, runProperties); setCellMargins(tableCell, style); setBackground(tableCell, style); setVerticalAlignment(tableCell, style); setCellBorders(tableCell, style); if (be != null) { setTextDirection(tableCell, be.getTextRotation()); } run.setRPr(runProperties); tableCell.getContent().add(paragraph); } }
public org.docx4j.wml.P.Hyperlink newHyperlink(MainDocumentPart mdp, String text, String url) { try { // We need to add a relationship to word/_rels/document.xml.rels but since its external, we // don't use // the usual wordMLPackage.getMainDocumentPart().addTargetPart mechanism org.docx4j.relationships.ObjectFactory factory = new org.docx4j.relationships.ObjectFactory(); org.docx4j.relationships.Relationship rel = factory.createRelationship(); rel.setType(Namespaces.HYPERLINK); rel.setTarget(url); rel.setTargetMode("External"); mdp.getRelationshipsPart().addRelationship(rel); // addRelationship sets the rel's @Id org.docx4j.wml.P.Hyperlink hyp = new org.docx4j.wml.P.Hyperlink(); hyp.setId(rel.getId()); R run = Context.getWmlObjectFactory().createR(); hyp.getContent().add(run); RPr rpr = new RPr(); RStyle rStyle = new RStyle(); rStyle.setVal("Hyperlink"); rpr.setRStyle(rStyle); run.setRPr(rpr); Text t = new Text(); t.setValue(text); run.getContent().add(t); // String hpl = "<w:hyperlink r:id=\"" + rel.getId() // + "\" xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" " // + "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" >" + // "<w:r>" + "<w:rPr>" // + "<w:rStyle w:val=\"Hyperlink\" />" + // "</w:rPr>" + "<w:t>" + text + "</w:t>" + "</w:r>" + "</w:hyperlink>"; // return (org.docx4j.wml.P.Hyperlink) XmlUtils.unmarshalString(hpl); return hyp; } catch (Exception e) { e.printStackTrace(); return null; } }
/** * This is where we add the actual styling information. In order to do this we first create a * paragraph. Then we create a text with the content of the cell as the value. Thirdly, we create * a so-called run, which is a container for one or more pieces of text having the same set of * properties, and add the text to it. We then add the run to the content of the paragraph. So far * what we've done still doesn't add any styling. To accomplish that, we'll create run properties * and add the styling to it. These run properties are then added to the run. Finally the * paragraph is added to the content of the table cell. */ private void addStyling(Tc tableCell, String content, boolean bold, String fontSize) { P paragraph = factory.createP(); Text text = factory.createText(); text.setValue(content); R run = factory.createR(); run.getContent().add(text); paragraph.getContent().add(run); RPr runProperties = factory.createRPr(); if (bold) { addBoldStyle(runProperties); } if (fontSize != null && !fontSize.isEmpty()) { setFontSize(runProperties, fontSize); } run.setRPr(runProperties); tableCell.getContent().add(paragraph); }