Java Code Examples for org.apache.poi.xwpf.usermodel.XWPFRun#addBreak()

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFRun#addBreak() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextRenderPolicy.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
public static void renderTextRun(XWPFRun run, Object data) {
    XWPFRun textRun = run;
    if (data instanceof HyperLinkTextRenderData) {
        textRun = createHyperLinkRun(run, data);
    }

    TextRenderData wrapperData = wrapper(data);
    String text = null == wrapperData.getText() ? "" : wrapperData.getText();
    StyleUtils.styleRun(textRun, wrapperData.getStyle());

    String[] split = text.split(REGEX_LINE_CHARACTOR, -1);
    if (split.length > 0) {
        textRun.setText(split[0], 0);
        boolean lineAtTable = split.length > 1 && !(data instanceof HyperLinkTextRenderData)
                && TableTools.isInsideTable(run);
        for (int i = 1; i < split.length; i++) {
            if (lineAtTable) {
                textRun.addBreak(BreakType.TEXT_WRAPPING);
            } else {
                textRun.addCarriageReturn();
            }
            textRun.setText(split[i]);
        }
    }
}
 
Example 2
Source File: PDF2WordExample.java    From tutorials with MIT License 6 votes vote down vote up
private static void generateDocFromPDF(String filename) throws IOException {
	XWPFDocument doc = new XWPFDocument();

	String pdf = filename;
	PdfReader reader = new PdfReader(pdf);
	PdfReaderContentParser parser = new PdfReaderContentParser(reader);

	for (int i = 1; i <= reader.getNumberOfPages(); i++) {
		TextExtractionStrategy strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
		String text = strategy.getResultantText();
		XWPFParagraph p = doc.createParagraph();
		XWPFRun run = p.createRun();
		run.setText(text);
		run.addBreak(BreakType.PAGE);
	}
	FileOutputStream out = new FileOutputStream("src/output/pdf.docx");
	doc.write(out);
	out.close();
	reader.close();
	doc.close();
}
 
Example 3
Source File: M2DocEvaluator.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Insert the given text into to given {@link XWPFRun}. This take care of new lines and tabulations.
 * 
 * @param paragraph
 *            the {@link XWPFParagraph} to modify
 * @param srcRun
 *            the {@link XWPFRun} to populate
 * @param text
 *            the text to insert
 * @return the last inserted {@link XWPFRun}.
 */
private XWPFRun insertString(XWPFParagraph paragraph, XWPFRun srcRun, String text) {
    int fragmentStart = 0;

    XWPFRun inserted;
    for (int i = 0; i < text.length(); i++) {
        final char current = text.charAt(i);
        switch (current) {
            case '\n':
                inserted = insertFragment(paragraph, srcRun, text.substring(fragmentStart, i));
                inserted.addBreak();
                fragmentStart = i + 1;
                break;
            case '\t':
                inserted = insertFragment(paragraph, srcRun, text.substring(fragmentStart, i));
                inserted.addTab();
                fragmentStart = i + 1;
                break;
            case '\r':
                // CHECKSTYLE:OFF
                if (i + 1 < text.length() && text.charAt(i + 1) == '\n') {
                    inserted = insertFragment(paragraph, srcRun, text.substring(fragmentStart, i));
                    inserted.addBreak();
                    i++;
                    fragmentStart = i + 1;
                }
                // CHECKSTYLE:ON
                break;

            default:
                break;
        }
    }

    return insertFragment(paragraph, srcRun, text.substring(fragmentStart, text.length()));
}
 
Example 4
Source File: ETLWordDocumentGenerator.java    From WhiteRabbit with Apache License 2.0 5 votes vote down vote up
private static void addSourceTablesAppendix(CustomXWPFDocument document, ETL etl) {
	XWPFParagraph paragraph = document.createParagraph();
	XWPFRun run = paragraph.createRun();
	run.addBreak(BreakType.PAGE);
	run.setText("Appendix: source tables");
	run.setFontSize(18);
	
	for (Table sourceTable : etl.getSourceDatabase().getTables()) {
		paragraph = document.createParagraph();
		run = paragraph.createRun();
		run.setText("Table: " + sourceTable.getName());
		run.setFontSize(14);
		
		createDocumentParagraph(document, sourceTable.getComment());
		
		XWPFTable table = document.createTable(sourceTable.getFields().size() + 1, 4);
		// table.setWidth(2000);
		XWPFTableRow header = table.getRow(0);
		setTextAndHeaderShading(header.getCell(0), "Field");
		setTextAndHeaderShading(header.getCell(1), "Type");
		setTextAndHeaderShading(header.getCell(2), "Most freq. value");
		setTextAndHeaderShading(header.getCell(3), "Comment");
		int rowNr = 1;
		for (Field sourceField : sourceTable.getFields()) {
			XWPFTableRow row = table.getRow(rowNr++);
			row.getCell(0).setText(sourceField.getName());
			row.getCell(1).setText(sourceField.getType());
			row.getCell(2).setText(sourceField.getValueCounts().getMostFrequentValue());
			createCellParagraph(row.getCell(3), sourceField.getComment().trim());
		}
		
	}
	
	run.setFontSize(18);
}
 
Example 5
Source File: M2DocEvaluator.java    From M2Doc with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Insert the given {@link MPagination}.
 * 
 * @param paragraph
 *            the {@link XWPFParagraph} to modify
 * @param run
 *            the {@link XWPFRun} to insert to
 * @param mPagination
 *            the {@link MPagination}
 * @return the last inserted {@link XWPFParagraph}
 */
private XWPFParagraph insertMPagination(XWPFParagraph paragraph, XWPFRun run, MPagination mPagination) {
    final XWPFParagraph res;
    switch (mPagination) {
        case newColumn:
            insertFieldRunReplacement(paragraph, run, "").addBreak(BreakType.COLUMN);
            res = paragraph;
            break;

        case newParagraph:
            res = createNewParagraph(generatedDocument, (XWPFParagraph) run.getParent());
            break;

        case newPage:
            insertFieldRunReplacement(paragraph, run, "").addBreak(BreakType.PAGE);
            res = paragraph;
            break;

        case newTableOfContent:
            CTP ctP = paragraph.getCTP();
            CTSimpleField toc = ctP.addNewFldSimple();
            toc.setInstr("TOC \\h");
            toc.setDirty(STOnOff.TRUE);
            res = paragraph;
            break;

        case newTextWrapping:
            run.addBreak(BreakType.TEXT_WRAPPING);
            res = paragraph;
            break;

        case ligneBreak:
            run.addBreak();
            res = paragraph;
            break;

        default:
            throw new IllegalStateException("Not supported MPagination.");
    }

    return res;
}
 
Example 6
Source File: ETLWordDocumentGenerator.java    From WhiteRabbit with Apache License 2.0 4 votes vote down vote up
private static void addTargetTableSection(CustomXWPFDocument document, ETL etl, Table targetTable) throws InvalidFormatException, FileNotFoundException {
	XWPFParagraph paragraph = document.createParagraph();
	XWPFRun run = paragraph.createRun();
	run.addBreak(BreakType.PAGE);
	
	run.setText("Table name: " + targetTable.getName());
	run.setFontSize(18);
	
	createDocumentParagraph(document, targetTable.getComment());
	
	for (ItemToItemMap tableToTableMap : etl.getTableToTableMapping().getSourceToTargetMaps())
		if (tableToTableMap.getTargetItem() == targetTable) {
			Table sourceTable = (Table) tableToTableMap.getSourceItem();
			Mapping<Field> fieldtoFieldMapping = etl.getFieldToFieldMapping(sourceTable, targetTable);
			
			paragraph = document.createParagraph();
			run = paragraph.createRun();
			run.setText("Reading from " + tableToTableMap.getSourceItem());
			run.setFontSize(14);
			
			createDocumentParagraph(document, tableToTableMap.getLogic());
			
			createDocumentParagraph(document, tableToTableMap.getComment());
			
			// Add picture of field to field mapping
			MappingPanel mappingPanel = new MappingPanel(fieldtoFieldMapping);
			mappingPanel.setShowOnlyConnectedItems(true);
			int height = mappingPanel.getMinimumSize().height;
			mappingPanel.setSize(800, height);
			
			BufferedImage im = new BufferedImage(800, height, BufferedImage.TYPE_INT_ARGB);
			im.getGraphics().setColor(Color.WHITE);
			im.getGraphics().fillRect(0, 0, im.getWidth(), im.getHeight());
			mappingPanel.paint(im.getGraphics());
			document.addPicture(im, 600, height * 6 / 8);
			
			// Add table of field to field mapping
			XWPFTable table = document.createTable(fieldtoFieldMapping.getTargetItems().size() + 1, 4);
			// table.setWidth(2000);
			XWPFTableRow header = table.getRow(0);
			setTextAndHeaderShading(header.getCell(0), "Destination Field");
			setTextAndHeaderShading(header.getCell(1), "Source Field");
			setTextAndHeaderShading(header.getCell(2), "Logic");
			setTextAndHeaderShading(header.getCell(3), "Comment");
			int rowNr = 1;
			for (MappableItem targetField : fieldtoFieldMapping.getTargetItems()) {
				XWPFTableRow row = table.getRow(rowNr++);
				row.getCell(0).setText(targetField.getName());
				
				StringBuilder source = new StringBuilder();
				StringBuilder logic = new StringBuilder();
				StringBuilder comment = new StringBuilder();
				for (ItemToItemMap fieldToFieldMap : fieldtoFieldMapping.getSourceToTargetMaps()) {
					if (fieldToFieldMap.getTargetItem() == targetField) {
						if (source.length() != 0)
							source.append("\n");
						source.append(fieldToFieldMap.getSourceItem().getName().trim());
						
						if (logic.length() != 0)
							logic.append("\n");
						logic.append(fieldToFieldMap.getLogic().trim());
						
						if (comment.length() != 0)
							comment.append("\n");
						comment.append(fieldToFieldMap.getComment().trim());
					}
				}
				
				for (Field field : targetTable.getFields()) {
					if (field.getName().equals(targetField.getName())) {
						if (comment.length() != 0)
							comment.append("\n");
						comment.append(field.getComment().trim());
					}
				}
				
				createCellParagraph(row.getCell(1), source.toString());
				createCellParagraph(row.getCell(2), logic.toString());
				createCellParagraph(row.getCell(3), comment.toString());
			}
		}
	
}