Search in sources :

Example 11 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project tutorials by eugenp.

the class PDF2WordExample method generateDocFromPDF.

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();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) TextExtractionStrategy(com.itextpdf.text.pdf.parser.TextExtractionStrategy) SimpleTextExtractionStrategy(com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) PdfReaderContentParser(com.itextpdf.text.pdf.parser.PdfReaderContentParser) SimpleTextExtractionStrategy(com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) PdfReader(com.itextpdf.text.pdf.PdfReader)

Example 12 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project tutorials-java by Artister.

the class WordIntegrationTest method whenParsingOutputDocument_thenCorrect.

@Test
public void whenParsingOutputDocument_thenCorrect() throws Exception {
    Path msWordPath = Paths.get(WordDocument.output);
    XWPFDocument document = new XWPFDocument(Files.newInputStream(msWordPath));
    List<XWPFParagraph> paragraphs = document.getParagraphs();
    document.close();
    XWPFParagraph title = paragraphs.get(0);
    XWPFRun titleRun = title.getRuns().get(0);
    assertEquals("Build Your REST API with Spring", title.getText());
    assertEquals("009933", titleRun.getColor());
    assertTrue(titleRun.isBold());
    assertEquals("Courier", titleRun.getFontFamily());
    assertEquals(20, titleRun.getFontSize());
    assertEquals("from HTTP fundamentals to API Mastery", paragraphs.get(1).getText());
    assertEquals("What makes a good API?", paragraphs.get(3).getText());
    assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph1), paragraphs.get(4).getText());
    assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph2), paragraphs.get(5).getText());
    assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph3), paragraphs.get(6).getText());
}
Also used : Path(java.nio.file.Path) XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) Test(org.junit.jupiter.api.Test)

Example 13 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project sw360portal by sw360.

the class DocxGenerator method addLicenses.

private void addLicenses(XWPFDocument document, LicenseInfoParsingResult parsingResult, boolean includeObligations) throws TException {
    XWPFRun licensesTitleRun = document.createParagraph().createRun();
    addNewLines(licensesTitleRun, 1);
    addFormattedText(licensesTitleRun, "Licenses", FONT_SIZE, true);
    for (String licenseName : getReleasesLicenses(parsingResult)) {
        XWPFParagraph licensePara = document.createParagraph();
        licensePara.setSpacingAfter(0);
        addBookmarkHyperLink(licensePara, licenseName, licenseName);
        if (includeObligations) {
            addLicenseObligations(document, licenseName);
        }
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) CommonUtils.nullToEmptyString(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)

Example 14 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project sw360portal by sw360.

the class DocxGenerator method addCopyrights.

private void addCopyrights(XWPFDocument document, LicenseInfoParsingResult parsingResult) {
    XWPFRun copyrightTitleRun = document.createParagraph().createRun();
    addFormattedText(copyrightTitleRun, "Copyrights", FONT_SIZE, true);
    for (String copyright : getReleaseCopyrights(parsingResult)) {
        XWPFParagraph copyPara = document.createParagraph();
        copyPara.setSpacingAfter(0);
        setText(copyPara.createRun(), copyright);
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) CommonUtils.nullToEmptyString(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)

Example 15 with XWPFRun

use of org.apache.poi.xwpf.usermodel.XWPFRun in project poi by apache.

the class SimpleImages method main.

public static void main(String[] args) throws IOException, InvalidFormatException {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();
    XWPFRun r = p.createRun();
    for (String imgFile : args) {
        int format;
        if (imgFile.endsWith(".emf"))
            format = XWPFDocument.PICTURE_TYPE_EMF;
        else if (imgFile.endsWith(".wmf"))
            format = XWPFDocument.PICTURE_TYPE_WMF;
        else if (imgFile.endsWith(".pict"))
            format = XWPFDocument.PICTURE_TYPE_PICT;
        else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
            format = XWPFDocument.PICTURE_TYPE_JPEG;
        else if (imgFile.endsWith(".png"))
            format = XWPFDocument.PICTURE_TYPE_PNG;
        else if (imgFile.endsWith(".dib"))
            format = XWPFDocument.PICTURE_TYPE_DIB;
        else if (imgFile.endsWith(".gif"))
            format = XWPFDocument.PICTURE_TYPE_GIF;
        else if (imgFile.endsWith(".tiff"))
            format = XWPFDocument.PICTURE_TYPE_TIFF;
        else if (imgFile.endsWith(".eps"))
            format = XWPFDocument.PICTURE_TYPE_EPS;
        else if (imgFile.endsWith(".bmp"))
            format = XWPFDocument.PICTURE_TYPE_BMP;
        else if (imgFile.endsWith(".wpg"))
            format = XWPFDocument.PICTURE_TYPE_WPG;
        else {
            System.err.println("Unsupported picture: " + imgFile + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
            continue;
        }
        r.setText(imgFile);
        r.addBreak();
        // 200x200 pixels
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200));
        r.addBreak(BreakType.PAGE);
    }
    FileOutputStream out = new FileOutputStream("images.docx");
    doc.write(out);
    out.close();
    doc.close();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) FileInputStream(java.io.FileInputStream)

Aggregations

XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)24 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)22 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)13 FileOutputStream (java.io.FileOutputStream)10 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)6 File (java.io.File)5 OutputStream (java.io.OutputStream)5 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)5 XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)5 CommonUtils.nullToEmptyString (org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)5 CTTcPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr)4 FileInputStream (java.io.FileInputStream)3 XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)3 XmlCursor (org.apache.xmlbeans.XmlCursor)3 IOException (java.io.IOException)2 Path (java.nio.file.Path)2 XWPFFooter (org.apache.poi.xwpf.usermodel.XWPFFooter)2 XWPFHeader (org.apache.poi.xwpf.usermodel.XWPFHeader)2 Test (org.junit.Test)2 CTTblWidth (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth)2