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

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

Introduction

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

Prototype

public ParagraphAlignment getAlignment() 

Source Link

Document

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

Usage

From source file:apachepoitest.DocumentPropertyChecker.java

public static Boolean checkIfParagraphHasProperty(XWPFParagraph p, String property, String value) {
    try {//www  . j a  v  a2 s  .c  o m
        switch (property) {
        case "LINE SPACING":
            XWPFParagraphClone pc;
            pc = new XWPFParagraphClone(p.getCTP(), p.getBody());
            return pc.getCTSpacing(false).getLine().floatValue() / 240 == Float.parseFloat(value);
        case "NUMBERING FORMAT":
            return p.getNumFmt().equalsIgnoreCase(value);
        case "ALIGN":
            return p.getAlignment().toString().equalsIgnoreCase(value);
        default:
            System.out.println("Property " + property + " does not exist!");
            return false;
        }
    } catch (NullPointerException e) {
        return false;
    }
}

From source file:apachepoitest.DocumentPropertyEnumerator.java

public static void showParagraphProperties(List<XWPFParagraph> lp) {
    int i1 = 1;/*  w w  w . ja v  a 2 s  .c  o m*/
    for (XWPFParagraph p : lp) {
        //System.out.println(p.getStyleID() + " " + sl1.getStyle(p.getStyleID()).getCTStyle().xmlText());
        System.out.println("____________________________________");
        if (p.getParagraphText().trim().length() > 0) {
            System.out.println("\n#" + i1++ + " LINE: " + p.getParagraphText());
            System.out.println("ALIGNMENT: " + p.getAlignment().toString());
            //Uncomment to display other properties
            /*
            System.out.println("BORDER BETWEEN: " + p.getBorderBetween().toString());
            System.out.println("BORDER BOTTOM: " + p.getBorderBottom().toString());
            System.out.println("BORDER LEFT: " + p.getBorderLeft().toString());
            System.out.println("BORDER RIGHT: " + p.getBorderRight().toString());
            System.out.println("BORDER TOP: " + p.getBorderTop().toString());
            System.out.println("BODY ELEMENT TYPE: " + p.getElementType().toString());
            System.out.println("FOOTNOTE: " + p.getFootnoteText());
            System.out.println("INDENTATION 1ST LINE: " + p.getIndentationFirstLine());
            System.out.println("INDENTATION HANGING: " + p.getIndentationHanging());
            System.out.println("INDENTATION LEFT: " + p.getIndentationLeft());
            System.out.println("INDENTATION RIGHT: " + p.getIndentationRight());
            System.out.println("NUMBERING FORMAT: " + p.getNumFmt());
            System.out.println("NUMERIC STYLE ILVL: " + p.getNumIlvl());
            System.out.println("SPACING AFTER: " + p.getSpacingAfter());
            System.out.println("SPACING AFTER LINES: " + p.getSpacingAfterLines());
            System.out.println("SPACING BEFORE: " + p.getSpacingBefore());
            System.out.println("SPACING BEFORE LINES: " + p.getSpacingBeforeLines());
            System.out.println("SPACING LINE RULE: " + p.getSpacingLineRule());
            System.out.println("VERTICAL ALIGNMENT: " + p.getVerticalAlignment());
            */
        } // can also use .searchText to look for a string
        else {
            // Uncomment to display lines
            //System.out.println("\n#" + i1++ + " LINE: <SPACE>");
        }

        showParagraphElementProperties(p.getRuns());
    }
}

From source file:apachepoitest.DocumentPropertyEnumerator.java

public static void showParagraphPropertiesOnly(List<XWPFParagraph> lp) {
    int i1 = 1;/*from w w  w. j  a  va2 s  .  com*/
    for (XWPFParagraph p : lp) {
        //System.out.println(p.getStyleID() + " " + sl1.getStyle(p.getStyleID()).getCTStyle().xmlText());
        System.out.println("____________________________________");
        if (p.getParagraphText().trim().length() > 0) {
            System.out.println("\n#" + i1++ + " LINE: " + p.getParagraphText());
            System.out.println("ALIGNMENT: " + p.getAlignment().toString());
            //Uncomment to display other properties

            System.out.println("BORDER BETWEEN: " + p.getBorderBetween().toString());
            System.out.println("BORDER BOTTOM: " + p.getBorderBottom().toString());
            System.out.println("BORDER LEFT: " + p.getBorderLeft().toString());
            System.out.println("BORDER RIGHT: " + p.getBorderRight().toString());
            System.out.println("BORDER TOP: " + p.getBorderTop().toString());
            System.out.println("BODY ELEMENT TYPE: " + p.getElementType().toString());
            System.out.println("FOOTNOTE: " + p.getFootnoteText());
            System.out.println("INDENTATION 1ST LINE: " + p.getIndentationFirstLine());
            System.out.println("INDENTATION HANGING: " + p.getIndentationHanging());
            System.out.println("INDENTATION LEFT: " + p.getIndentationLeft());
            System.out.println("INDENTATION RIGHT: " + p.getIndentationRight());
            System.out.println("NUMBERING FORMAT: " + p.getNumFmt());
            System.out.println("NUMERIC STYLE ILVL: " + p.getNumIlvl());
            System.out.println("STYLE: " + p.getBody().getXWPFDocument().getStyles().getStyle(p.getStyleID()));

            XWPFParagraphClone pc;
            pc = new XWPFParagraphClone(p.getCTP(), p.getBody());

            System.out.println("SPACING VALUE: " + pc.getCTSpacing(false).getLine().floatValue() / 240);
            System.out.println("SPACING AFTER: " + p.getSpacingAfter());
            System.out.println("SPACING AFTER LINES: " + p.getSpacingAfterLines());
            System.out.println("SPACING BEFORE: " + p.getSpacingBefore());
            System.out.println("SPACING BEFORE LINES: " + p.getSpacingBeforeLines());
            System.out.println("SPACING LINE RULE: " + p.getSpacingLineRule());
            System.out.println("VERTICAL ALIGNMENT: " + p.getVerticalAlignment());

        } // can also use .searchText to look for a string
        else {
            // Uncomment to display lines
            //System.out.println("\n#" + i1++ + " LINE: <SPACE>");
        }
    }
}

From source file:com.project3.utils.poiold.DocumentPropertyEnumerator.java

public static void showAllParagraphProperties(List<XWPFParagraph> lp) {
    int i1 = 1;/*from  w  w  w . j  a  v a2 s  . com*/
    for (XWPFParagraph p : lp) {
        //System.out.println(p.getStyleID() + " " + sl1.getStyle(p.getStyleID()).getCTStyle().xmlText());
        System.out.println("____________________________________");
        if (p.getParagraphText().trim().length() > 0) {
            System.out.println("\n#" + i1++ + " LINE: " + p.getParagraphText());
            System.out.println("ALIGNMENT: " + p.getAlignment().toString());

            System.out.println("BORDER BETWEEN: " + p.getBorderBetween().toString());
            System.out.println("BORDER BOTTOM: " + p.getBorderBottom().toString());
            System.out.println("BORDER LEFT: " + p.getBorderLeft().toString());
            System.out.println("BORDER RIGHT: " + p.getBorderRight().toString());
            System.out.println("BORDER TOP: " + p.getBorderTop().toString());
            System.out.println("BODY ELEMENT TYPE: " + p.getElementType().toString());
            System.out.println("FOOTNOTE: " + p.getFootnoteText());
            System.out.println("INDENTATION 1ST LINE: " + p.getIndentationFirstLine());
            System.out.println("INDENTATION HANGING: " + p.getIndentationHanging());
            System.out.println("INDENTATION LEFT: " + p.getIndentationLeft());
            System.out.println("INDENTATION RIGHT: " + p.getIndentationRight());
            System.out.println("NUMBERING FORMAT: " + p.getNumFmt());
            System.out.println("NUMERIC STYLE ILVL: " + p.getNumIlvl());
            System.out.println("STYLE: " + p.getBody().getXWPFDocument().getStyles().getStyle(p.getStyleID()));

            XWPFParagraphClone pc;
            pc = new XWPFParagraphClone(p.getCTP(), p.getBody());

            System.out.println("SPACING VALUE: " + pc.getCTSpacing(false).getLine().floatValue() / 240);
            System.out.println("SPACING AFTER: " + p.getSpacingAfter());
            System.out.println("SPACING AFTER LINES: " + p.getSpacingAfterLines());
            System.out.println("SPACING BEFORE: " + p.getSpacingBefore());
            System.out.println("SPACING BEFORE LINES: " + p.getSpacingBeforeLines());
            System.out.println("SPACING LINE RULE: " + p.getSpacingLineRule());
            System.out.println("VERTICAL ALIGNMENT: " + p.getVerticalAlignment());

        } // can also use .searchText to look for a string
        else {
            // Uncomment to display lines
            //System.out.println("\n#" + i1++ + " LINE: <SPACE>");
        }
    }
}

From source file:Modelo.EscribirWord.java

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

    //ASPOSE/*w ww  .j  a  v  a  2  s  .  co 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.articleEditor.insertContent.POIDocxReader.java

License:Apache License

protected void processParagraph(XWPFParagraph paragraph) throws BadLocationException {
    parAttrs = new SimpleAttributeSet();
    ParagraphAlignment alignment = paragraph.getAlignment();
    if (alignment == ParagraphAlignment.CENTER) {
        StyleConstants.setAlignment(parAttrs, StyleConstants.ALIGN_CENTER);
    } else if (alignment == ParagraphAlignment.LEFT) {
        StyleConstants.setAlignment(parAttrs, StyleConstants.ALIGN_LEFT);
    } else if (alignment == ParagraphAlignment.RIGHT) {
        StyleConstants.setAlignment(parAttrs, StyleConstants.ALIGN_RIGHT);
    } else if (alignment == ParagraphAlignment.BOTH || alignment == ParagraphAlignment.DISTRIBUTE) {
        StyleConstants.setAlignment(parAttrs, StyleConstants.ALIGN_JUSTIFIED);
    }/*  w ww  .ja  v a 2  s  .c o m*/
    List<TabStop> tabs = new ArrayList<>();
    int leftIndentation = paragraph.getIndentationLeft();
    if (leftIndentation > 0) {
        float indentation = leftIndentation / INDENTS_MULTIPLIER;
        StyleConstants.setLeftIndent(parAttrs, indentation);
        /*TabStop stop = new TabStop(pos, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
         tabs.add(stop);*/
    }
    int rightIndentation = paragraph.getIndentationRight();
    if (rightIndentation > 0) {
        float indentation = rightIndentation / INDENTS_MULTIPLIER;
        StyleConstants.setLeftIndent(parAttrs, indentation);
        /*TabStop stop = new TabStop(pos, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE);
         tabs.add(stop);*/
    }
    /*TabSet tabSet = new TabSet(tabs.toArray(new TabStop[tabs.size()]));
     StyleConstants.setTabSet(parAttrs, tabSet);*/
    int firstLineIndentation = paragraph.getIndentationFirstLine();
    if (firstLineIndentation > 0) {
        float indentation = firstLineIndentation / INDENTS_MULTIPLIER;
        StyleConstants.setFirstLineIndent(parAttrs, indentation);
        /*TabStop stop = new TabStop(pos, TabStop.ALIGN_RIGHT, TabStop.LEAD_NONE);
         tabs.add(stop);*/
    }

    int spacingBefore = paragraph.getSpacingBefore();
    if (spacingBefore > 0) {
        int before = spacingBefore / INDENTS_MULTIPLIER;
        StyleConstants.setSpaceAbove(parAttrs, before);
    }
    int spacingAfter = paragraph.getSpacingAfter();
    if (spacingAfter > 0) {
        int after = spacingAfter / INDENTS_MULTIPLIER;
        StyleConstants.setSpaceAbove(parAttrs, after);
    }
    LineSpacingRule spacingLine = paragraph.getSpacingLineRule();
    if (spacingLine == LineSpacingRule.AT_LEAST || spacingLine == LineSpacingRule.AUTO) {
        float spacing = spacingLine.getValue() / 240;
        StyleConstants.setLineSpacing(parAttrs, spacing);
    }
    document.setParagraphAttributes(currentOffset, 1, parAttrs, true);
    for (XWPFRun run : paragraph.getRuns()) {
        processRun(run);
    }
}