Example usage for org.apache.poi.xwpf.usermodel XWPFParagraph setAlignment

List of usage examples for org.apache.poi.xwpf.usermodel XWPFParagraph setAlignment

Introduction

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

Prototype

public void setAlignment(ParagraphAlignment align) 

Source Link

Document

Specifies the paragraph alignment which shall be applied to text in this paragraph.

Usage

From source file:Modelo.EscribirWord.java

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

    //ASPOSE//from   ww w.j  a v  a  2  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:org.cgiar.ccafs.marlo.action.summaries.AnualReportPOISummaryAction.java

License:Open Source License

public void createPageFooter() {
    CTP ctp = CTP.Factory.newInstance();

    // this add page number incremental
    ctp.addNewR().addNewPgNum();/*from w w w. j  av  a  2  s .c o m*/

    XWPFParagraph codePara = new XWPFParagraph(ctp, document);
    XWPFParagraph[] paragraphs = new XWPFParagraph[1];
    paragraphs[0] = codePara;

    // position of number
    codePara.setAlignment(ParagraphAlignment.CENTER);

    CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();

    try {
        XWPFHeaderFooterPolicy headerFooterPolicy = new XWPFHeaderFooterPolicy(document, sectPr);
        headerFooterPolicy.createFooter(STHdrFtr.DEFAULT, paragraphs);
    } catch (IOException e) {
        LOG.error("Failed to createFooter. Exception: " + e.getMessage());
    }
}

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

License:Open Source License

/**
 * Footer title//from w  ww  .j  a  v  a  2 s . com
 * 
 * @param document
 * @param text
 * @throws IOException
 */
public void pageFooter(XWPFDocument document, String text) throws IOException {
    CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
    XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
    CTP ctpFooter = CTP.Factory.newInstance();
    CTR ctrFooter = ctpFooter.addNewR();
    CTText ctFooter = ctrFooter.addNewT();
    ctFooter.setStringValue(text);
    XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
    footerParagraph.setAlignment(ParagraphAlignment.LEFT);
    XWPFParagraph[] parsFooter = new XWPFParagraph[1];
    parsFooter[0] = footerParagraph;
    policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
}

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

License:Open Source License

/**
 * Header title//  www.  j  av a  2s  .co  m
 * 
 * @param document
 * @param text
 * @throws IOException
 */
public void pageHeader(XWPFDocument document, String text) throws IOException {
    CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
    XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
    CTP ctpHeader = CTP.Factory.newInstance();
    CTR ctrHeader = ctpHeader.addNewR();
    CTText ctHeader = ctrHeader.addNewT();
    ctHeader.setStringValue(text);
    XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
    headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
    XWPFParagraph[] parsHeader = new XWPFParagraph[1];
    parsHeader[0] = headerParagraph;
    policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
}

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

License:Open Source License

/**
 * Head 1 Title/*from   w  w  w.j  a v  a2s  .c o  m*/
 * 
 * @param h1
 * @param text
 */
public void textHead1Title(XWPFParagraph h1, String text) {
    h1.setAlignment(ParagraphAlignment.BOTH);
    XWPFRun h1Run = h1.createRun();
    this.addParagraphTextBreak(h1Run, text);
    h1Run.setColor(TITLE_FONT_COLOR);
    h1Run.setBold(true);
    h1Run.setFontFamily(FONT_TYPE);
    h1Run.setFontSize(16);
}

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

License:Open Source License

public void textHead2Title(XWPFParagraph h2, String text) {
    h2.setAlignment(ParagraphAlignment.BOTH);
    XWPFRun h2Run = h2.createRun();/*from  ww w.ja va 2s .c o m*/
    this.addParagraphTextBreak(h2Run, text);
    h2Run.setColor(TITLE_FONT_COLOR);
    h2Run.setBold(true);
    h2Run.setFontFamily(FONT_TYPE);
    h2Run.setFontSize(14);
}

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

License:Open Source License

public void textHead3Title(XWPFParagraph h2, String text) {
    h2.setAlignment(ParagraphAlignment.BOTH);
    XWPFRun h2Run = h2.createRun();/*from   ww  w.ja  va2s  .  c o m*/
    this.addParagraphTextBreak(h2Run, text);
    h2Run.setColor(TITLE_FONT_COLOR);
    h2Run.setBold(true);
    h2Run.setFontFamily(FONT_TYPE);
    h2Run.setFontSize(12);
}

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

License:Open Source License

public void textHeadCoverTitle(XWPFParagraph h1, String text) {
    h1.setAlignment(ParagraphAlignment.CENTER);
    XWPFRun h1Run = h1.createRun();/*  w ww . j a  v a 2s . c o  m*/
    this.addParagraphTextBreak(h1Run, text);
    h1Run.setColor(TEXT_FONT_COLOR);
    h1Run.setBold(false);
    h1Run.setFontFamily(FONT_TYPE);
    h1Run.setFontSize(26);
}

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

License:Open Source License

public void textNotes(XWPFParagraph paragraph, String text) {
    paragraph.setAlignment(ParagraphAlignment.BOTH);
    XWPFRun paragraphRun = paragraph.createRun();
    this.addParagraphTextBreak(paragraphRun, text);
    paragraphRun.setColor(TEXT_FONT_COLOR);
    paragraphRun.setBold(false);//w w  w.j a  v a2 s . co m
    paragraphRun.setFontFamily(FONT_TYPE);
    paragraphRun.setFontSize(10);
}

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

License:Open Source License

public void textParagraph(XWPFParagraph paragraph, String text) {
    paragraph.setAlignment(ParagraphAlignment.BOTH);
    XWPFRun paragraphRun = paragraph.createRun();
    this.addParagraphTextBreak(paragraphRun, text);
    paragraphRun.setColor(TEXT_FONT_COLOR);
    paragraphRun.setBold(false);//  w w w .  j a v a 2  s.c o m
    paragraphRun.setFontFamily(FONT_TYPE);
    paragraphRun.setFontSize(11);
}