Java Code Examples for org.apache.poi.xwpf.usermodel.XWPFDocument#createParagraph()

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFDocument#createParagraph() . 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: M2DocTestUtils.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates a new {@link DocumentTemplate} with the given {@link DocumentTemplate#getBody() body}. The body is linked to {@link XWPFRun}.
 * 
 * @param body
 *            the {@link Block}
 * @return a new {@link DocumentTemplate}
 */
@SuppressWarnings("resource")
public static DocumentTemplate createDocumentTemplate(Block body) {
    final DocumentTemplate res = TemplatePackage.eINSTANCE.getTemplateFactory().createDocumentTemplate();
    final Resource r = new XMIResourceImpl();
    r.getContents().add(res);

    final XWPFDocument document = new XWPFDocument();
    res.setDocument(document);
    res.setBody(body);

    final XWPFParagraph paragraph = document.createParagraph();

    linkRuns(paragraph, body);

    return res;
}
 
Example 2
Source File: M2DocUtils.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets or create the first {@link XWPFRun} of the given {@link XWPFDocument}.
 * 
 * @param document
 *            the {@link XWPFDocument}
 * @return the first {@link XWPFRun} of the given {@link XWPFDocument} or the created one if none existed
 */
public static XWPFRun getOrCreateFirstRun(XWPFDocument document) {
    final XWPFRun res;

    final XWPFParagraph paragraph;
    if (!document.getParagraphs().isEmpty()) {
        paragraph = document.getParagraphs().get(0);
    } else {
        paragraph = document.createParagraph();
    }
    if (!paragraph.getRuns().isEmpty()) {
        res = paragraph.getRuns().get(0);
    } else {
        res = paragraph.createRun();
    }

    return res;
}
 
Example 3
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 4
Source File: TocTest.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws FileNotFoundException, IOException {
    XWPFDocument doc = new XWPFDocument(new FileInputStream("toc.docx"));

    XWPFStyles styles = doc.getStyles();
    System.out.println(styles);

    XWPFParagraph p = doc.createParagraph();
    XWPFRun run = p.createRun();
    run.setText("Heading 1");
    p.getCTP().addNewPPr();
    p.getCTP().getPPr().addNewPStyle();
    p.setStyle(HEADING1);

    XWPFParagraph tocPara = doc.createParagraph();
    CTP ctP = tocPara.getCTP();
    CTSimpleField toc = ctP.addNewFldSimple();
    toc.setInstr("TOC \\h");
    toc.setDirty(STOnOff.TRUE);

    System.out.println(doc.isEnforcedUpdateFields());

    // doc.enforceUpdateFields();

    // doc.createTOC();

    doc.write(new FileOutputStream("CreateWordBookmark.docx"));
    doc.close();
}
 
Example 5
Source File: XWPFParagraphWrapperTest.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
@Test
public void testWrapper() throws FileNotFoundException, IOException {

    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph paragraph = doc.createParagraph();

    XWPFParagraphWrapper wrapper = new XWPFParagraphWrapper(paragraph);
    XWPFRun hyperRun = wrapper.insertNewHyperLinkRun(0, "http:deepoove.com");
    hyperRun.setText("Deepoove0");

    XWPFRun newRun = wrapper.insertNewRun(0);
    newRun.setText("website0");
    newRun = wrapper.insertNewRun(0);
    newRun.setText("website1");
    newRun = wrapper.insertNewRun(0);
    newRun.setText("website2");

    hyperRun = wrapper.insertNewHyperLinkRun(0, "http:deepoove.com");
    hyperRun.setText("Deepoove1");

    XWPFFieldRun fieldRun = wrapper.insertNewField(0);
    CTSimpleField ctField = fieldRun.getCTField();
    ctField.setInstr(" CREATEDATE  \\* MERGEFORMAT ");
    fieldRun.setText("2019");

    assertEquals(paragraph.getRuns().size(), 6);

    for (int i = 6; i >= 0; i--) {
        wrapper.removeRun(i);
    }
    assertEquals(paragraph.getRuns().size(), 0);

    doc.close();

}