Example usage for org.apache.poi.xwpf.usermodel XWPFDocument createParagraph

List of usage examples for org.apache.poi.xwpf.usermodel XWPFDocument createParagraph

Introduction

In this page you can find the example usage for org.apache.poi.xwpf.usermodel XWPFDocument createParagraph.

Prototype

public XWPFParagraph createParagraph() 

Source Link

Document

Appends a new paragraph to this document

Usage

From source file:Management.PruebaDoc.java

public static void main(String[] args) throws IOException {

    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p3 = doc.createParagraph();
    XWPFRun r4 = p3.createRun();//w  ww. j  a v a 2s  .  c o  m
    r4.setTextPosition(20);
    r4.setText("To be, or not to be: that is the question: " + "Whether 'tis nobler in the mind to suffer "
            + "The slings and arrows of outrageous fortune, " + "Or to take arms against a sea of troubles, "
            + "And by opposing end them? To die: to sleep; ");
    r4.addBreak(BreakType.PAGE);
    r4.setText("No more; and by a sleep to say we end " + "The heart-ache and the thousand natural shocks "
            + "That flesh is heir to, 'tis a consummation " + "Devoutly to be wish'd. To die, to sleep; "
            + "To sleep: perchance to dream: ay, there's the rub; " + ".......");
    r4.setItalic(true);

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(
                "C:\\Users\\hilsierivan\\Documents\\NetBeansProjects\\StickMaps\\web\\MapImages\\simple.docx");
    } catch (FileNotFoundException ex) {
        Logger.getLogger(PruebaDoc.class.getName()).log(Level.SEVERE, null, ex);
    }
    doc.write(out);
    out.close();

}

From source file:Modelo.EscribirWord.java

public void cambiarLogo() throws FileNotFoundException, IOException, InvalidFormatException, Exception {

    //ASPOSE/*  www .  ja va2 s .  c o  m*/
    //    Document doc = new Document("HojaInventarioObjeto.doc");
    ////    doc.save("Out.pdf");

    //        HWPFDocument doc = new HWPFDocument(new FileInputStream("HojaInventarioTemplate.doc"));

    XWPFDocument document = new XWPFDocument(new FileInputStream("HojaInventarioTemplate.docx"));
    String imageOldName = "logo";
    try {
        //            LOG.info("replaceImage: old=" + "logo" + ", new=" + "imagePathNew");
        String imagePathNew = "PlayStation_1_Logo.png";

        int newImageWidth = 1;
        int newImageHeight = 2;

        int imageParagraphPos = -1;
        XWPFParagraph imageParagraph = null;

        List<IBodyElement> documentElements = document.getBodyElements();
        for (IBodyElement documentElement : documentElements) {
            imageParagraphPos++;
            if (documentElement instanceof XWPFParagraph) {
                imageParagraph = (XWPFParagraph) documentElement;
                if (imageParagraph != null && imageParagraph.getCTP() != null
                        && imageParagraph.getCTP().toString().trim().indexOf(imageOldName) != -1) {
                    break;
                }
            }
        }

        if (imageParagraph == null) {
            throw new Exception("Unable to replace image data due to the exception:\n" + "'" + imageOldName
                    + "' not found in in document.");
        }
        ParagraphAlignment oldImageAlignment = imageParagraph.getAlignment();

        // remove old image
        document.removeBodyElement(imageParagraphPos - 1);

        // now add new image

        // BELOW LINE WILL CREATE AN IMAGE
        // PARAGRAPH AT THE END OF THE DOCUMENT.
        // REMOVE THIS IMAGE PARAGRAPH AFTER 
        // SETTING THE NEW IMAGE AT THE OLD IMAGE POSITION
        XWPFParagraph newImageParagraph = document.createParagraph();
        XWPFRun newImageRun = newImageParagraph.createRun();
        //newImageRun.setText(newImageText);
        newImageParagraph.setAlignment(oldImageAlignment);

        try (FileInputStream is = new FileInputStream(imagePathNew)) {
            newImageRun.addPicture(is, XWPFDocument.PICTURE_TYPE_JPEG, imagePathNew, Units.toEMU(newImageWidth),
                    Units.toEMU(newImageHeight));
        }

        // set new image at the old image position
        document.setParagraph(newImageParagraph, imageParagraphPos);

        // NOW REMOVE REDUNDANT IMAGE FORM THE END OF DOCUMENT
        document.removeBodyElement(document.getBodyElements().size() - 1);

        //            return document;
    } catch (Exception e) {
        throw new Exception("Unable to replace image '" + imageOldName + "' due to the exception:\n" + e);
    } finally {
        // cleanup code
    }

}

From source file:nl.architolk.ldt.processors.WordSerializer.java

License:Open Source License

protected void readInput(final PipelineContext pipelineContext, final ProcessorInput input, Config config,
        OutputStream outputStream) {

    try {//from w w  w .  java2s  . co m
        // Test
        BigInteger markId = BigInteger.ONE;
        // Read the input as a DOM
        final Document domDocument = readInputAsDOM(pipelineContext, input);

        // create document (docx)
        XWPFDocument doc = new XWPFDocument();

        //iterate through paragraphs;
        NodeList paragraphNodes = domDocument.getElementsByTagName("p");

        for (short i = 0; i < paragraphNodes.getLength(); i++) {
            Node paragraphNode = paragraphNodes.item(i);
            if (paragraphNode.getNodeType() == Node.ELEMENT_NODE) {
                //Create new paragraph
                XWPFParagraph paragraph = doc.createParagraph();

                //iterate through paragraph parts
                NodeList textNodes = paragraphNode.getChildNodes();
                for (short r = 0; r < textNodes.getLength(); r++) {
                    Node textNode = textNodes.item(r);
                    if (textNode.getNodeType() == Node.TEXT_NODE) {
                        XWPFRun run = paragraph.createRun();
                        run.setText(textNode.getTextContent());
                    }
                    if (textNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element textElement = (Element) textNode;
                        if (textNode.getLocalName().toUpperCase().equals("B")) {
                            //Eigenlijk op een andere plaats, maar nu ff voor de test
                            String anchor = textElement.getAttribute("id");
                            if (!anchor.isEmpty()) {
                                CTBookmark bookStart = paragraph.getCTP().addNewBookmarkStart();
                                bookStart.setName(anchor);
                                bookStart.setId(markId);
                            }
                            XWPFRun run = paragraph.createRun();
                            run.setBold(true);
                            run.setText(textNode.getTextContent());
                            if (!anchor.isEmpty()) {
                                CTMarkupRange bookEnd = paragraph.getCTP().addNewBookmarkEnd();
                                bookEnd.setId(markId);
                                markId = markId.add(BigInteger.ONE);
                            }
                        } else if (textNode.getLocalName().toUpperCase().equals("A")) {
                            addHyperlink(paragraph, textNode.getTextContent(),
                                    textElement.getAttribute("href"));
                        } else {
                            XWPFRun run = paragraph.createRun();
                            run.setText(textNode.getTextContent());
                        }
                    }
                }
            }
        }
        // write workbook to stream
        doc.write(outputStream);
        outputStream.close();

    } catch (Exception e) {
        throw new OXFException(e);
    }

}

From source file:oop.nhom5.de3.model.IOFileWord.java

public static boolean writeWord(String text, String fileName) {
    //Blank Document
    XWPFDocument document = new XWPFDocument();
    //Write the Document in file system
    try {//from   ww  w  .  j  a va  2  s .c o m
        FileOutputStream out = new FileOutputStream(new File(fileName));
        String[] lines = text.split("\n");
        //create Paragraph
        for (String line : lines) {
            XWPFParagraph paragraph = document.createParagraph();
            XWPFRun run = paragraph.createRun();
            run.setText(line);
        }

        document.write(out);
        out.close();
        return true;
    } catch (IOException | HeadlessException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.cgiar.ccafs.marlo.utils.POISummary.java

License:Open Source License

public void textLineBreak(XWPFDocument document, int breakNumber) {
    for (int i = 0; i < breakNumber; i++) {
        document.createParagraph();
    }/*from   w ww  .j av a  2 s  .c o m*/
}

From source file:org.eclipse.sw360.licenseinfo.outputGenerators.DocxGenerator.java

License:Open Source License

private void fillReleaseDetailList(XWPFDocument document,
        Collection<LicenseInfoParsingResult> projectLicenseInfoResults, boolean includeObligations)
        throws TException {
    addFormattedText(document.createParagraph().createRun(), "Detailed Releases Information", FONT_SIZE + 2,
            true);// www.  ja v a 2 s  . co m
    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);
}

From source file:org.eclipse.sw360.licenseinfo.outputGenerators.DocxGenerator.java

License:Open Source License

private void addReleaseTitle(XWPFDocument document, LicenseInfoParsingResult parsingResult) {
    String releaseTitle = getComponentLongName(parsingResult);
    XWPFParagraph releaseTitleParagraph = document.createParagraph();
    releaseTitleParagraph.setStyle(STYLE_HEADING);
    addBookmark(releaseTitleParagraph, releaseTitle, releaseTitle);
    addNewLines(document, 0);//from   www .j a va  2s  .co  m
}

From source file:org.eclipse.sw360.licenseinfo.outputGenerators.DocxGenerator.java

License:Open Source License

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);/*  ww w .  ja  v  a  2 s  .c o  m*/
        setText(copyPara.createRun(), copyright);
    }
}

From source file:org.eclipse.sw360.licenseinfo.outputGenerators.DocxGenerator.java

License:Open Source License

private void addLicenses(XWPFDocument document, LicenseInfoParsingResult parsingResult,
        boolean includeObligations) throws TException {
    XWPFRun licensesTitleRun = document.createParagraph().createRun();
    addNewLines(licensesTitleRun, 1);//from   w  w  w. j  a v a2 s  .c o m
    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);
        }
    }
}

From source file:org.eclipse.sw360.licenseinfo.outputGenerators.DocxGenerator.java

License:Open Source License

private void addLicenseObligations(XWPFDocument document, String spdxLicense) throws TException {
    List<License> sw360Licenses = getLicenses();
    XWPFRun todoTitleRun = document.createParagraph().createRun();
    addNewLines(todoTitleRun, 0);//ww w  . ja  v a2s  . c  o  m
    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);
    }
}