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;
}
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++;
}
}
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);
}
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);
}
}
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
}
}
Aggregations