Search in sources :

Example 36 with XWPFRun

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

the class TableReader method main.

public static void main(String[] args) {
    try {
        XWPFDocument xdoc = openDocument("test.docx");
        Iterator<IBodyElement> bodyElementIterator = xdoc.getBodyElementsIterator();
        while (bodyElementIterator.hasNext()) {
            IBodyElement element = bodyElementIterator.next();
            if ("TABLE".equalsIgnoreCase(element.getElementType().name())) {
                List<XWPFTable> tableList = element.getBody().getTables();
                for (XWPFTable table : tableList) {
                    System.out.println("Total Number of Rows of Table:" + table.getNumberOfRows());
                    for (int rowNum = 0; rowNum < table.getRows().size(); rowNum++) {
                        XWPFTableRow row = table.getRow(rowNum);
                        for (int colNum = 0; colNum < row.getTableCells().size(); colNum++) {
                            XWPFTableCell cell = row.getCell(colNum);
                            if (cell.getText().contains(findText)) {
                                List<XWPFParagraph> paragraphs = cell.getParagraphs();
                                for (XWPFParagraph paragraph : paragraphs) {
                                    List<XWPFRun> runElemens = paragraph.getRuns();
                                    for (XWPFRun runElement : runElemens) {
                                        String text = runElement.getText(0);
                                        if (text.contains(findText)) {
                                            System.out.println(findText + " ~> " + replaceText);
                                            runElement.setText(String.format("%-30s", text.replaceFirst(findText, replaceText)), 0);
                                        }
                                    }
                                }
                            }
                            System.out.println("-->" + cell.getText());
                        }
                    }
                }
            }
        }
        saveDocument(xdoc, "new.docx");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) IBodyElement(org.apache.poi.xwpf.usermodel.IBodyElement) IOException(java.io.IOException) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument)

Example 37 with XWPFRun

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

the class StyleReader method main.

public static void main(String[] args) {
    try {
        FileInputStream fis = new FileInputStream("test.docx");
        XWPFDocument xdoc = new XWPFDocument(OPCPackage.open(fis));
        List<XWPFParagraph> paragraphList = xdoc.getParagraphs();
        for (XWPFParagraph paragraph : paragraphList) {
            for (XWPFRun rn : paragraph.getRuns()) {
                System.out.println(rn.isBold());
                System.out.println(rn.isHighlighted());
                System.out.println(rn.isCapitalized());
                System.out.println(rn.getFontSize());
            }
            System.out.println("********************************************************************");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) FileInputStream(java.io.FileInputStream)

Example 38 with XWPFRun

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

the class DocxGenerator method fillReleaseDetailList.

private void fillReleaseDetailList(XWPFDocument document, Collection<LicenseInfoParsingResult> projectLicenseInfoResults, boolean includeObligations) throws TException {
    addFormattedText(document.createParagraph().createRun(), "Detailed Releases Information", FONT_SIZE + 2, true);
    setText(document.createParagraph().createRun(), "Please note the following license conditions and copyright " + "notices applicable to Open Source Software and/or other components (or parts thereof):");
    addNewLines(document, 0);
    for (LicenseInfoParsingResult parsingResult : projectLicenseInfoResults) {
        addReleaseTitle(document, parsingResult);
        if (parsingResult.getStatus() == LicenseInfoRequestStatus.SUCCESS) {
            addCopyrights(document, parsingResult);
            addLicenses(document, parsingResult, includeObligations);
        } else {
            XWPFRun errorRun = document.createParagraph().createRun();
            String errorText = nullToEmptyString(parsingResult.getMessage());
            String filename = getFilename(parsingResult);
            addFormattedText(errorRun, String.format("Error reading license information: %s", errorText), FONT_SIZE, false, ALERT_COLOR);
            addFormattedText(errorRun, String.format("Source file: %s", filename), FONT_SIZE, false, ALERT_COLOR);
        }
        addNewLines(document, 1);
    }
    addPageBreak(document);
}
Also used : XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) CommonUtils.nullToEmptyString(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)

Example 39 with XWPFRun

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

the class DocxGenerator method addLicenseObligations.

private void addLicenseObligations(XWPFDocument document, String spdxLicense) throws TException {
    List<License> sw360Licenses = getLicenses();
    XWPFRun todoTitleRun = document.createParagraph().createRun();
    addNewLines(todoTitleRun, 0);
    Set<String> todos = getTodosFromLicenses(spdxLicense, sw360Licenses);
    addFormattedText(todoTitleRun, "Obligations for license " + spdxLicense + ":", FONT_SIZE, true);
    for (String todo : todos) {
        XWPFParagraph copyPara = document.createParagraph();
        copyPara.setSpacingAfter(0);
        XWPFRun todoRun = copyPara.createRun();
        setText(todoRun, todo);
        addNewLines(todoRun, 1);
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) License(org.eclipse.sw360.datahandler.thrift.licenses.License) CommonUtils.nullToEmptyString(org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)

Example 40 with XWPFRun

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

the class DocxGenerator method fillLicenseList.

private void fillLicenseList(XWPFDocument document, Collection<LicenseInfoParsingResult> projectLicenseInfoResults) {
    List<LicenseNameWithText> licenseNameWithTexts = OutputGenerator.getSortedLicenseNameWithTexts(projectLicenseInfoResults);
    XWPFRun licenseHeaderRun = document.createParagraph().createRun();
    addFormattedText(licenseHeaderRun, "License texts", FONT_SIZE + 2, true);
    addNewLines(document, 0);
    for (LicenseNameWithText licenseNameWithText : licenseNameWithTexts) {
        XWPFParagraph licenseParagraph = document.createParagraph();
        licenseParagraph.setStyle(STYLE_HEADING);
        String licenseName = licenseNameWithText.isSetLicenseName() ? licenseNameWithText.getLicenseName() : UNKNOWN_LICENSE_NAME;
        addBookmark(licenseParagraph, licenseName, licenseName);
        addNewLines(document, 0);
        setText(document.createParagraph().createRun(), nullToEmptyString(licenseNameWithText.getLicenseText()));
        addNewLines(document, 1);
    }
}
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)

Aggregations

XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)45 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)39 XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)20 FileOutputStream (java.io.FileOutputStream)11 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)10 XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)8 IOException (java.io.IOException)7 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)7 File (java.io.File)5 OutputStream (java.io.OutputStream)5 CommonUtils.nullToEmptyString (org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)5 FileInputStream (java.io.FileInputStream)4 XmlCursor (org.apache.xmlbeans.XmlCursor)4 CheckedServiceException (com.bc.pmpheep.service.exception.CheckedServiceException)3 HashMap (java.util.HashMap)3 XWPFHeaderFooterPolicy (org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy)3 IORuntimeException (cn.hutool.core.io.IORuntimeException)2 POIException (cn.hutool.poi.exceptions.POIException)2 Path (java.nio.file.Path)2 Matcher (java.util.regex.Matcher)2