Search in sources :

Example 26 with Text

use of org.docx4j.wml.Text in project TranskribusCore by Transkribus.

the class DocxBuilder method getDocxTable.

private static Tbl getDocxTable(WordprocessingMLPackage wPMLpackage, boolean isWordBased, int rows, int cols, List<HashMap<Integer, TrpTableCellType>> allRows, int tablesize, MainDocumentPart mdp) throws Exception {
    int writableWidthTwips = wPMLpackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
    int cellWidthTwips = new Double(Math.floor((writableWidthTwips / cols))).intValue();
    Tbl table = TblFactory.createTable(0, 0, cellWidthTwips);
    TblGrid tblGrid = factory.createTblGrid();
    for (int i = 0; i < cols; i++) {
        TblGridCol col = factory.createTblGridCol();
        col.setW(BigInteger.valueOf(cellWidthTwips));
        tblGrid.getGridCol().add(col);
    }
    table.setTblGrid(tblGrid);
    int i = 0;
    for (HashMap<Integer, TrpTableCellType> entry : allRows) {
        Tr row = factory.createTr();
        table.getContent().add(row);
        i++;
        int d = 0;
        if (entry.keySet().size() != cols) {
            logger.debug("size of entries does not match columns ");
        }
        for (Integer key : entry.keySet()) {
            Tc cell = factory.createTc();
            row.getContent().add(cell);
            String rowSpan = null;
            int colSpan = 1;
            boolean mergedVertical = false;
            int colsize = cellWidthTwips;
            if (entry.get(key) != null) {
                if (entry.get(key).getRowSpan() > 1) {
                    mergedVertical = true;
                    rowSpan = "restart";
                }
                // PageXmlUtils.buildPolygon(entry.get(key).getCoords().getPoints()).getBounds().getMaxX();
                double maxX = entry.get(key).getBoundingBox().getMaxX();
                // PageXmlUtils.buildPolygon(entry.get(key).getCoords().getPoints()).getBounds().getMinX();
                double minX = entry.get(key).getBoundingBox().getMinX();
                double colsizeRel = maxX - minX;
                double colsizetmp = colsizeRel / (double) tablesize;
                // logger.debug("colsizetmp " + colsizetmp);
                colsize = (int) (writableWidthTwips * colsizetmp);
                colSpan = entry.get(key).getColSpan();
                // logger.debug("colsize " + colsize);
                // logger.debug("text in this cell is " + entry.get(key).getUnicodeTextFromLines());
                int colID = entry.get(key).getCol();
                int rowID = entry.get(key).getRow();
            } else {
                // logger.debug("no cell for this column ");
                mergedVertical = true;
            }
            applyGridSpan(cell, colSpan, rowSpan, colsize, mergedVertical);
            P columnPara = factory.createP();
            // P columnPara = (P) column.getContent().get(0);
            cell.getContent().add(columnPara);
            d++;
            Text tx = factory.createText();
            R run = factory.createR();
            if (entry.get(key) != null) {
                // old solution till now: tx.setValue(entry.get(key).getUnicodeTextFromLines());
                if (entry.get(key).getUnicodeTextFromLines() != "") {
                    exportTextRegion(entry.get(key), isWordBased, columnPara, mdp);
                }
            }
            run.getContent().add(tx);
            columnPara.getContent().add(run);
        }
    }
    return table;
}
Also used : TrpTableCellType(eu.transkribus.core.model.beans.pagecontent_trp.TrpTableCellType) Text(org.docx4j.wml.Text) Tc(org.docx4j.wml.Tc) BigInteger(java.math.BigInteger) P(org.docx4j.wml.P) R(org.docx4j.wml.R) TblGrid(org.docx4j.wml.TblGrid) Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl) TblGridCol(org.docx4j.wml.TblGridCol)

Example 27 with Text

use of org.docx4j.wml.Text in project TranskribusCore by Transkribus.

the class DocxBuilder method getFormattedTextForLineElement.

/*
 * was version before we esported tables too
 */
// private static void writeDocxForTranscript(MainDocumentPart mdp, TrpPageType trpPage,
// boolean wordBased, boolean preserveLineBreaks) {
// boolean rtl = false;
// 
// List<TrpTextRegionType> textRegions = trpPage.getTextRegions(true);
// 
// for (int j=0; j<textRegions.size(); ++j) {
// TrpTextRegionType r = textRegions.get(j);
// 
// 
// //			if (exportTags){
// //				getTagsForShapeElement(r);
// //			}
// 
// /*
// * create one paragraph for each text region
// * but only if there is some text in it
// */
// String helper = r.getUnicodeText().replaceAll("\n", "");
// 
// //logger.debug("region unicode text " + helper);
// 
// if (!helper.equals("")){
// 
// org.docx4j.wml.P  p = factory.createP();
// mdp.addObject(p);
// 
// List<TextLineType> lines = r.getTextLine();
// for (int i=0; i<lines.size(); ++i) {
// TrpTextLineType trpL = (TrpTextLineType) lines.get(i);
// 
// try {
// if (wordBased && trpL.getWord().size()>0){
// getFormattedTextForLineElement(trpL.getWord(), p, mdp);
// }
// else {
// getFormattedTextForShapeElement(trpL, p, mdp);
// 
// }
// 
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// 
// /*add line break after each text line
// * or omit this if explicitely wished to have dense lines
// * No line break at end of paragraph
// */
// if (preserveLineBreaks && !(i+1==lines.size()) ){
// Br br = factory.createBr(); // this Br element is used break the current and go for next line
// p.getContent().add(br);
// }
// 
// 
// }
// //
// //				linesTexts[i] = ((trpL.getUnicodeText().equals("") || wordBased) && trpL.getWord().size()>0) ? getRtfTextForLineFromWords(trpL) : getRtfTextForShapeElement(trpL);
// //				linesTexts[i] = RtfText.text(linesTexts[i], "\n");
// }
// 
// }
// 
// }
private static void getFormattedTextForLineElement(List<WordType> words, P p, MainDocumentPart mdp) throws Exception {
    int wordCount = 0;
    int nrWords = words.size();
    for (WordType word : words) {
        getFormattedTextForShapeElement((ITrpShapeType) word, p, mdp);
        // add empty space after each word
        if (wordCount < nrWords - 1) {
            org.docx4j.wml.Text t = factory.createText();
            t.setValue(" ");
            t.setSpace("preserve");
            org.docx4j.wml.R run = factory.createR();
            p.getContent().add(run);
            run.getContent().add(t);
        }
        wordCount++;
    }
}
Also used : R(org.docx4j.wml.R) Text(org.docx4j.wml.Text) WordType(eu.transkribus.core.model.beans.pagecontent.WordType)

Example 28 with Text

use of org.docx4j.wml.Text in project TranskribusCore by Transkribus.

the class DocxBuilder method addComplexField.

private static void addComplexField(P p, String instrText, String instrText2) {
    org.docx4j.wml.ObjectFactory wmlObjectFactory = Context.getWmlObjectFactory();
    // Create object for r
    R r = wmlObjectFactory.createR();
    p.getContent().add(r);
    // Create object for fldChar (wrapped in JAXBElement)
    FldChar fldchar = wmlObjectFactory.createFldChar();
    JAXBElement<org.docx4j.wml.FldChar> fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar);
    r.getContent().add(fldcharWrapped);
    fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN);
    // Create object for instrText (wrapped in JAXBElement)
    Text text = wmlObjectFactory.createText();
    JAXBElement<org.docx4j.wml.Text> textWrapped = wmlObjectFactory.createRInstrText(text);
    r.getContent().add(textWrapped);
    text.setValue(instrText);
    text.setSpace("preserve");
    R r2 = wmlObjectFactory.createR();
    p.getContent().add(r2);
    if (!instrText2.equals("")) {
        r2.getContent().add(wmlObjectFactory.createRTab());
        text = wmlObjectFactory.createText();
        JAXBElement<org.docx4j.wml.Text> textWrapped2 = wmlObjectFactory.createRInstrText(text);
        r2.getContent().add(textWrapped2);
        text.setValue(instrText2);
        text.setSpace("preserve");
    }
    // R r = factory.createR();
    // r.setRsidRPr("TOC entry text");
    // R.Tab tab = new R.Tab();
    // r.getRunContent().add(tab);
    // p.getParagraphContent().add(r);
    // org.docx4j.wml.Text idx = wmlObjectFactory.createText();
    // JAXBElement<org.docx4j.wml.Text> textWrapped2 = wmlObjectFactory.createRInstrText(idx);
    // r.getContent().add( textWrapped2);
    // idx.setValue( idxText);
    // idx.setSpace( "preserve");
    // Create object for fldChar (wrapped in JAXBElement)
    fldchar = wmlObjectFactory.createFldChar();
    fldcharWrapped = wmlObjectFactory.createRFldChar(fldchar);
    r2.getContent().add(fldcharWrapped);
    fldchar.setFldCharType(org.docx4j.wml.STFldCharType.END);
}
Also used : R(org.docx4j.wml.R) FldChar(org.docx4j.wml.FldChar) Text(org.docx4j.wml.Text)

Example 29 with Text

use of org.docx4j.wml.Text in project mdw-designer by CenturyLinkCloud.

the class DocxBuilder method addBulletList.

public void addBulletList(List<String> items) throws Exception {
    if (bulletNum == null) {
        bulletNum = getNdp().addAbstractListNumberingDefinition((Numbering.AbstractNum) XmlUtils.unmarshalString(getFragment("bulletNumbering")));
        bulletNumId = bulletNum.getNumId();
    } else {
        bulletNumId = BigInteger.valueOf(ndp.restart(bulletNumId.longValue(), 0, 1));
    }
    for (String item : items) {
        ObjectFactory factory = Context.getWmlObjectFactory();
        P listP = factory.createP();
        Text t = factory.createText();
        t.setValue(item);
        R listRun = factory.createR();
        listRun.getContent().add(t);
        listP.getContent().add(listRun);
        PPr listPpr = factory.createPPr();
        listP.setPPr(listPpr);
        listPpr.setNumPr(createBulletListNumPr());
        PStyle pStyle = factory.createPPrBasePStyle();
        pStyle.setVal("ListParagraph");
        listPpr.setPStyle(pStyle);
        getMdp().addObject(listP);
    }
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) ObjectFactory(org.docx4j.wml.ObjectFactory) Text(org.docx4j.wml.Text) PStyle(org.docx4j.wml.PPrBase.PStyle)

Example 30 with Text

use of org.docx4j.wml.Text in project flexmark-java by vsch.

the class ComboDocxConverterIssuesSpecTest method addSpecExample.

@Override
public void addSpecExample(SpecExample example, Node node, DataHolder options, boolean ignoredCase, String actualHTML, String actualAST) {
    if (!DUMP_ALL_TESTS_FILES)
        return;
    final boolean failed = !ignoredCase && !actualHTML.equals(example.getHtml());
    // add source information
    myDocxContext.createP("Heading3");
    if (ignoredCase) {
        // does not match, need more stuff
        myDocxContext.createColor().setVal("BB002F");
        myDocxContext.addBold();
        Text text = myDocxContext.addWrappedText();
        text.setValue("Ignored ");
        text.setSpace(RunFormatProvider.SPACE_PRESERVE);
    } else if (failed) {
        // does not match, need more stuff
        myDocxContext.createColor().setVal("BB002F");
        myDocxContext.addBold();
        Text text = myDocxContext.addWrappedText();
        text.setValue("Failed ");
        text.setSpace(RunFormatProvider.SPACE_PRESERVE);
    } else {
        // does not match, need more stuff
        myDocxContext.createColor().setVal("008000");
        myDocxContext.addBold();
        Text text = myDocxContext.addWrappedText();
        text.setValue("Passed ");
        text.setSpace(RunFormatProvider.SPACE_PRESERVE);
    }
    StringBuilder header = new StringBuilder();
    final String section = example.getSection();
    header.append(section == null ? "" : section.trim()).append(": ").append(example.getExampleNumber());
    final String optionsSet = example.getOptionsSet();
    myDocxContext.text(header.toString());
    if (optionsSet != null) {
        myDocxContext.createHpsMeasure(28);
        myDocxContext.createColor().setVal("7B56A0");
        Text text = myDocxContext.addWrappedText();
        text.setValue((SpecReader.OPTIONS_STRING + "(") + optionsSet + ")");
        text.setSpace(RunFormatProvider.SPACE_PRESERVE);
    }
    myDocxContext.createHorizontalLine();
    RENDERER.withOptions(options).render(node, myPackage);
    myDocxContext.createHorizontalLine();
    if (example.hasComment()) {
        final String comment = example.getComment();
        String[] lines = comment.split("\n\\s*\n");
        for (String line : lines) {
            String trimmed = line.trim();
            if (!trimmed.isEmpty()) {
                myDocxContext.createP(myDocxContext.getRenderingOptions().LOOSE_PARAGRAPH_STYLE);
                myDocxContext.createColor().setVal("808080");
                Text text = myDocxContext.addWrappedText();
                text.setValue(trimmed);
            }
        }
    }
    myDocxContext.renderFencedCodeLines(example.getSource().split("\n"));
    // noinspection StatementWithEmptyBody
    if (failed) {
    // could add expected and actual text but that would be too much
    }
}
Also used : Text(org.docx4j.wml.Text)

Aggregations

Text (org.docx4j.wml.Text)62 R (org.docx4j.wml.R)53 P (org.docx4j.wml.P)32 PPr (org.docx4j.wml.PPr)14 RPr (org.docx4j.wml.RPr)12 Jc (org.docx4j.wml.Jc)10 CTVerticalJc (org.docx4j.wml.CTVerticalJc)9 File (java.io.File)8 Tc (org.docx4j.wml.Tc)7 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)6 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)6 STVerticalJc (org.docx4j.wml.STVerticalJc)6 BigInteger (java.math.BigInteger)5 Br (org.docx4j.wml.Br)5 ObjectFactory (org.docx4j.wml.ObjectFactory)5 Inline (org.docx4j.dml.wordprocessingDrawing.Inline)4 BinaryPartAbstractImage (org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage)4 CTShd (org.docx4j.wml.CTShd)4 Ftr (org.docx4j.wml.Ftr)4 Tbl (org.docx4j.wml.Tbl)4