Example usage for org.apache.poi.xwpf.usermodel BreakType PAGE

List of usage examples for org.apache.poi.xwpf.usermodel BreakType PAGE

Introduction

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

Prototype

BreakType PAGE

To view the source code for org.apache.poi.xwpf.usermodel BreakType PAGE.

Click Source Link

Document

Specifies that the current break shall restart itself on the next page of the document when the document is displayed in page view.

Usage

From source file:authorslilhelper.FXMLDocumentController.java

License:Open Source License

public void createPrintable() {
    try {//from w  w w  . j ava2  s  . c  o m

        //add the LAST paragraph to paragraphs here
        /*int endOfPara = s.length() - 1;
        char cur = 'a';
        char next = 'b';
        char nextNext = 'c';
        int variableIndex = endOfPara;
                
        while(!((cur == ' ' || cur == '\n') && (next == '\n')))
        {
            cur = s.charAt(variableIndex);
            next = s.charAt(variableIndex - 1);
            //nextNext = s.charAt(variableIndex - 2);
            variableIndex--;
        }
                
        String blah = s.substring(variableIndex, endOfPara);
        paragraphs.add(blah);*/

        /*    if(current == '@')            //read in the new document and make all the chapter headers bold
            {}*/

        final Stage dialog = new Stage();
        dialog.initModality(Modality.APPLICATION_MODAL);

        final TextField textField = new TextField();
        Button submit = new Button();
        Button cancel = new Button();

        cancel.setText("Cancel");
        cancel.setAlignment(Pos.CENTER);
        submit.setText("Continue");
        submit.setAlignment(Pos.BOTTOM_RIGHT);
        // submit.

        final VBox dialogVbox = new VBox(20);
        dialogVbox.getChildren().add(new Text("What is the name/title of your book? "));
        dialogVbox.getChildren().add(textField);
        dialogVbox.getChildren().add(submit);
        dialogVbox.getChildren().add(cancel);

        Scene dialogScene = new Scene(dialogVbox, 300, 200);
        dialog.setScene(dialogScene);
        dialog.setTitle("Give your book a title ");
        dialog.show();

        submit.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent anEvent) {

                System.out.println("Hi create Printable: " + characterCapacity);

                save();

                Primary.appendText("\n\n" + "----End of Document----" + "\n");

                ArrayList paragraphs = new ArrayList();
                String s = Primary.getText();
                char current = ' ';
                char previous = ' ';
                String aParagraph = "";
                int beginningIndex = 0;

                for (int i = 0; i < s.length(); i++) {
                    current = s.charAt(i);

                    /*if(i > 0)
                    {
                        previous = s.charAt(i - 1);
                    }*/

                    if (current == '\n' && previous == '\n') {
                        aParagraph = s.substring(beginningIndex, i);

                        //todo: remove every 90th character (eg when % 90 == 0) from aParagraph before adding it
                        //THIS IS THE "IMPORTANT" BLOCK YOU BRACKETED OUT ON A WHIM
                        /*                for(int a = 0; a < aParagraph.length(); a++)
                        {   //lecturer
                            //use your "fame" to your advantage
                            //yt channel- 
                            if(a >= characterCapacity)//surprising her, as the connection grew closer/stronger and stronger, chapter titles, last paragraph getting cut off
                            {//a state of deep & profound relaxation. Sitara cared about nothing/little. It was the l itself that made the [first] entreaty, wherein she could, in idle 'conversation' beckon it/request it to mve; or to perform any operation allowed/conceivable by its physical makeup.
                                if(a % characterCapacity == 0)         //author's note: there are things in there that are pure fantasy/while the vast majority is pure fantasy, as well as things in there that are based on my real life, loosely and even closely/nearly identically, in some cases
                                {//"a polite request works better than an assertion of dominance--something you're unfamiliar with, I'm sure."
                                    StringBuilder builder = new StringBuilder(aParagraph);
                                    System.out.println(aParagraph.charAt(a));
                                    builder.deleteCharAt(a);
                                    aParagraph = builder.toString();
                                }
                            }
                        }*/

                        /*    int b = 0;
                            for(int a = 0; a < aParagraph.length(); a++)
                            {   //lecturer
                                //use your "fame" to your advantage
                                //yt channel- 
                                //if(b == characterCapacity)//surprising her, as the connection grew closer/stronger and stronger, chapter titles, last paragraph getting cut off
                                //{//a state of deep & profound relaxation. Sitara cared about nothing/little. It was the l itself that made the [first] entreaty, wherein she could, in idle 'conversation' beckon it/request it to mve; or to perform any operation allowed/conceivable by its physical makeup.
                                    //if(a % (characterCapacity) == 0)         //author's note: there are things in there that are pure fantasy/while the vast majority is pure fantasy, as well as things in there that are based on my real life, loosely and even closely/nearly identically, in some cases
                                    //{//"a polite request works better than an assertion of dominance--something you're unfamiliar with, I'm sure."
                                if(b == characterCapacity) 
                                {
                                    StringBuilder builder = new StringBuilder(aParagraph);
                                        //builder.deleteCharAt(a);
                                        //builder.deleteCharAt(a-1);
                                        System.out.println(aParagraph.charAt(a));
                                        aParagraph = builder.toString();
                                
                            b = 0;
                                    //}
                                }
                                b++;
                            }*/

                        paragraphs.add(aParagraph);

                        /*if(s.substring(i, s.length()-1).length() <= 90)
                        {
                            String finalParagraph = s.substring(i, s.length()-1);               //light-blue tiles
                            paragraphs.add(finalParagraph);
                        }*/

                        while (s.charAt(i) == ' ') {
                            i++;
                        }

                        beginningIndex = i;
                    }

                    previous = current;
                }

                String title = textField.getText();

                dialog.close();

                int result = JOptionPane.showConfirmDialog(null,
                        "In the next screen, enter a title/a unique label for your manuscript.\n If needed, this can include a version number or type.\n It does not need to have the same title as your book. \n You do NOT need to include any extensions, such as .docx--this will\n be taken care of automatically.",
                        "No extension required", JOptionPane.OK_CANCEL_OPTION); //[before returning to the login screen?]

                if (result == 0) {
                    JFileChooser chooser = new JFileChooser();
                    int value = chooser.showOpenDialog(null);
                    String saveFile = "";

                    //                            JOptionPane.showMessageDialog(null, "In the next screen, enter a title/a unique label for your manuscript. If needed, this can include a version number or type. It does not need to have the same title as your book. You do NOT need to include any extensions, such as .docx--this will be taken care of automatically.");
                    if (value == JFileChooser.APPROVE_OPTION) {
                        saveFile = chooser.getSelectedFile().getPath() + ".docx";
                    }

                    try {
                        FileOutputStream outstream = new FileOutputStream(saveFile);
                        //FileOutputStream outstream = new FileOutputStream("C:/Users/Quiggy/Desktop/Test.docx");
                        XWPFDocument doc = new XWPFDocument();

                        XWPFParagraph titlePara = doc.createParagraph();
                        titlePara.setAlignment(ParagraphAlignment.CENTER);
                        XWPFRun titleRun = titlePara.createRun();
                        titleRun.setBold(true);
                        titleRun.setFontSize(18);
                        //titleRun.setText("\n\n\n\n\n\n" + title + "\n\n\n\n\n\n\n\n");
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.addBreak();
                        titleRun.setText(title);
                        titleRun.addBreak(BreakType.PAGE);

                        XWPFParagraph contents = doc.createParagraph();
                        titlePara.setAlignment(ParagraphAlignment.CENTER);
                        XWPFRun contentsRun = contents.createRun();
                        contentsRun.setBold(true);
                        contentsRun.setFontSize(16);
                        contentsRun.setText("Contents: ");
                        contentsRun.addBreak();
                        contentsRun.addBreak();

                        XWPFParagraph chaptersNames = doc.createParagraph();
                        titlePara.setAlignment(ParagraphAlignment.CENTER);

                        int chapterCount = 0;
                        for (int c = 0; c < paragraphs.size(); c++) {
                            /*XWPFParagraph p = doc.createParagraph(); //Paragraph
                            XWPFRun run = p.createRun();  //Where the text will be written from
                            run.setText("\t");*/

                            XWPFRun chaptersRun = chaptersNames.createRun();
                            chaptersRun.setBold(false);
                            chaptersRun.setFontSize(12);
                            //chaptersRun.setText("A chapter \t\t\t\t\t\t\t\t ... ");

                            String currentParagraph = paragraphs.get(c).toString();
                            for (int d = 0; d < currentParagraph.length(); d++) {
                                if (currentParagraph.charAt(d) == '~') {
                                    chapterCount++;
                                    currentParagraph = currentParagraph.substring(d + 1,
                                            currentParagraph.length() - 2);
                                    if ((currentParagraph.indexOf("chapter") == -1)
                                            && (currentParagraph.indexOf("Chapter") == -1)
                                            && (currentParagraph.indexOf("CHAPTER") == -1)) {
                                        chaptersRun.setText("Chapter " + chapterCount + ": " + currentParagraph
                                                + "\t\t\t\t\t\t\t ... ");
                                    } else {
                                        chaptersRun.setText(currentParagraph + "\t\t\t\t\t\t\t ... ");
                                    }
                                    chaptersRun.addBreak();

                                } else {
                                    //run.setBold(false);
                                }
                            }

                            //doc.createParagraph().createRun().setText("           " + paragraphs.get(c).toString());
                            //doc.createParagraph().createRun().addBreak();
                        }

                        XWPFParagraph breakPara = doc.createParagraph();
                        titlePara.setAlignment(ParagraphAlignment.CENTER);
                        XWPFRun breakRun = breakPara.createRun();
                        breakRun.setBold(false);
                        breakRun.setFontSize(12);
                        breakRun.addBreak(BreakType.PAGE);

                        XWPFParagraph para = doc.createParagraph();
                        para.setAlignment(ParagraphAlignment.CENTER);
                        XWPFRun paraRun = para.createRun();
                        paraRun.setBold(true);
                        paraRun.setFontSize(12);//paraRun.setFontSize(16);
                        //paraRun.setText("Hello World");
                        //paraRun.setText(Primary.getText());

                        //doc.createParagraph().createRun().addBreak();                 //inserts a line break
                        //doc.createParagraph().createRun().setText("blah");            //creates another paragraph

                        for (int c = 0; c < paragraphs.size(); c++) {
                            /*XWPFParagraph p = doc.createParagraph(); //Paragraph
                            XWPFRun run = p.createRun();  //Where the text will be written from
                            run.setText("\t");*/

                            XWPFParagraph p = doc.createParagraph(); //Paragraph
                            XWPFRun run = p.createRun(); //Where the text will be written from

                            String currentParagraph = paragraphs.get(c).toString();
                            for (int d = 0; d < currentParagraph.length(); d++) {
                                if (currentParagraph.charAt(d) == '~') {
                                    currentParagraph = currentParagraph.substring(d + 1,
                                            currentParagraph.length() - 2);

                                    run.setBold(true);

                                } else {
                                    //run.setBold(false);
                                }
                            }

                            run.setText("           " + currentParagraph);
                            run.addBreak(); //NEW!

                            //doc.createParagraph().createRun().setText("           " + paragraphs.get(c).toString());
                            //doc.createParagraph().createRun().addBreak();
                        }

                        doc.write(outstream);
                        outstream.close();
                    } catch (Exception io) {
                    }
                    //JOptionPane.showConfirmDialog(null, "Are you sure you want to create the word document?");

                    //Makes the chapter titles bold and removes the "@" & "-"

                    JOptionPane.showMessageDialog(null,
                            "Document Successfully Created. The program/AA will now exit");
                    try {
                        if (Desktop.isDesktopSupported()) {
                            Desktop.getDesktop().open(new File(saveFile));
                        }
                    } catch (IOException ioe) {
                        ioe.printStackTrace();
                    }
                    System.exit(0);
                }
                if (result == 1) {

                }
                /*if(result == 2)
                {
                        
                }*/

            }
        });

        cancel.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent anEvent) {
                dialog.close();
                //close the window here
            }
        });

    } catch (Exception e) {

    }
}

From source file:ch.admin.searchreplace.SimpleDocument.java

License:Apache License

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setBorderBottom(Borders.DOUBLE);/*  ww w .  j a va  2s.  com*/
    p1.setBorderTop(Borders.DOUBLE);

    p1.setBorderRight(Borders.DOUBLE);
    p1.setBorderLeft(Borders.DOUBLE);
    p1.setBorderBetween(Borders.SINGLE);

    p1.setVerticalAlignment(TextAlignment.TOP);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("The quick brown fox");
    r1.setBold(true);
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.RIGHT);

    // BORDERS
    p2.setBorderBottom(Borders.DOUBLE);
    p2.setBorderTop(Borders.DOUBLE);
    p2.setBorderRight(Borders.DOUBLE);
    p2.setBorderLeft(Borders.DOUBLE);
    p2.setBorderBetween(Borders.SINGLE);

    XWPFRun r2 = p2.createRun();
    r2.setText("jumped over the lazy dog");
    r2.setStrike(true);
    r2.setFontSize(20);

    XWPFRun r3 = p2.createRun();
    r3.setText("and went away");
    r3.setStrike(true);
    r3.setFontSize(20);
    r3.setSubscript(VerticalAlign.SUPERSCRIPT);

    XWPFParagraph p3 = doc.createParagraph();
    p3.setWordWrap(true);
    p3.setPageBreak(true);

    // p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
    p3.setAlignment(ParagraphAlignment.BOTH);
    p3.setSpacingLineRule(LineSpacingRule.EXACT);

    p3.setIndentationFirstLine(600);

    XWPFRun r4 = p3.createRun();
    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);
    // This would imply that this break shall be treated as a simple line break, and break the line after that
    // word:

    XWPFRun r5 = p3.createRun();
    r5.setTextPosition(-10);
    r5.setText("For in that sleep of death what dreams may come");
    r5.addCarriageReturn();
    r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect"
            + "That makes calamity of so long life;");
    r5.addBreak();
    r5.setText("For who would bear the whips and scorns of time,"
            + "The oppressor's wrong, the proud man's contumely,");

    r5.addBreak(BreakClear.ALL);
    r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns"
            + ".......");

    FileOutputStream out = new FileOutputStream("simple.docx");
    doc.write(out);
    out.close();

}

From source file:com.ecm.poi.SimpleDocument.java

License:Apache License

@Test
public void xwpfTest() throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setBorderBottom(Borders.DOUBLE);// www .  j  ava  2 s.  co m
    p1.setBorderTop(Borders.DOUBLE);

    p1.setBorderRight(Borders.DOUBLE);
    p1.setBorderLeft(Borders.DOUBLE);
    p1.setBorderBetween(Borders.SINGLE);

    p1.setVerticalAlignment(TextAlignment.TOP);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("The quick brown fox");
    r1.setBold(true);
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.RIGHT);

    //BORDERS
    p2.setBorderBottom(Borders.DOUBLE);
    p2.setBorderTop(Borders.DOUBLE);
    p2.setBorderRight(Borders.DOUBLE);
    p2.setBorderLeft(Borders.DOUBLE);
    p2.setBorderBetween(Borders.SINGLE);

    XWPFRun r2 = p2.createRun();
    r2.setText("jumped over the lazy dog");
    r2.setStrike(true);
    r2.setFontSize(20);

    XWPFRun r3 = p2.createRun();
    r3.setText("and went away");
    r3.setStrike(true);
    r3.setFontSize(20);
    r3.setSubscript(VerticalAlign.SUPERSCRIPT);

    XWPFParagraph p3 = doc.createParagraph();
    p3.setWordWrap(true);
    p3.setPageBreak(true);

    //p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
    p3.setAlignment(ParagraphAlignment.BOTH);
    p3.setSpacingLineRule(LineSpacingRule.EXACT);

    p3.setIndentationFirstLine(600);

    XWPFRun r4 = p3.createRun();
    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);
    //This would imply that this break shall be treated as a simple line break, and break the line after that word:

    XWPFRun r5 = p3.createRun();
    r5.setTextPosition(-10);
    r5.setText("For in that sleep of death what dreams may come");
    r5.addCarriageReturn();
    r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect"
            + "That makes calamity of so long life;");
    r5.addBreak();
    r5.setText("For who would bear the whips and scorns of time,"
            + "The oppressor's wrong, the proud man's contumely,");

    r5.addBreak(BreakClear.ALL);
    r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns"
            + ".......");

    FileOutputStream out = new FileOutputStream("d:\\temp\\simple.docx");
    doc.write(out);
    out.close();

}

From source file:com.ecm.poi.SimpleImages.java

License:Apache License

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();

    XWPFRun r = p.createRun();//  w ww  .  j  av a2  s .c o m
    //        args[0] = "d:\\temp\\Koala.jpg";
    String imgFile = "d:\\temp\\img\\Koala.jpg";
    if (null != imgFile) {
        int format = 0;

        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");
        }

        r.setText(imgFile);
        r.addBreak();
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
        r.addBreak(BreakType.PAGE);
    }

    FileOutputStream out = new FileOutputStream("d:\\temp\\images.docx");
    doc.write(out);
    out.close();
}

From source file:com.foc.vaadin.gui.layouts.validationLayout.FVValidationLayout.java

License:Apache License

public void poiAddPictureTest() {
    try {//  w  w  w  .  j a v a  2 s.c om
        XWPFDocument document = new XWPFDocument();
        XWPFParagraph paragraph = document.createParagraph();

        XWPFRun run = paragraph.createRun();
        int format;

        String imgFilePath = "C://Users//user//Pictures//forward.png";

        format = XWPFDocument.PICTURE_TYPE_PNG;
        run.setText(imgFilePath);
        run.addBreak();
        run.setText("This is where a picture would be.\r\nAnd here would be another image");
        run.addBreak(BreakType.PAGE);

        FileOutputStream out = new FileOutputStream("C://Users//user//Desktop//images.docx");
        byte[] byts = new byte[1000000];
        out.write(byts);
        document.addPictureData(byts, format);
        document.write(out);
        out.close();
    } catch (Exception e) {

    }
}

From source file:com.foc.vaadin.gui.layouts.validationLayout.FVValidationLayout.java

License:Apache License

public static void fillWordDocument(XWPFDocument doc) {
    try {/*from  w w  w  . j a  va 2s. co m*/
        XWPFParagraph p1 = doc.createParagraph();
        p1.setAlignment(ParagraphAlignment.CENTER);
        p1.setBorderBottom(Borders.DOUBLE);
        p1.setBorderTop(Borders.DOUBLE);

        p1.setBorderRight(Borders.DOUBLE);
        p1.setBorderLeft(Borders.DOUBLE);
        p1.setBorderBetween(Borders.SINGLE);

        p1.setVerticalAlignment(TextAlignment.TOP);

        XWPFRun r1 = p1.createRun();
        r1.setBold(true);
        r1.setText("The quick brown fox");
        r1.setBold(true);
        r1.setFontFamily("Courier");
        r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
        r1.setTextPosition(100);

        XWPFParagraph p2 = doc.createParagraph();
        p2.setAlignment(ParagraphAlignment.RIGHT);

        //BORDERS
        p2.setBorderBottom(Borders.DOUBLE);
        p2.setBorderTop(Borders.DOUBLE);
        p2.setBorderRight(Borders.DOUBLE);
        p2.setBorderLeft(Borders.DOUBLE);
        p2.setBorderBetween(Borders.SINGLE);

        XWPFRun r2 = p2.createRun();
        r2.setText("jumped over the lazy dog");
        r2.setStrike(true);
        r2.setFontSize(20);

        XWPFRun r3 = p2.createRun();
        r3.setText("and went away");
        r3.setStrike(true);
        r3.setFontSize(20);
        r3.setSubscript(VerticalAlign.SUPERSCRIPT);

        XWPFParagraph p3 = doc.createParagraph();
        p3.setWordWrap(true);
        p3.setPageBreak(true);

        p3.setAlignment(ParagraphAlignment.BOTH);
        p3.setSpacingLineRule(LineSpacingRule.EXACT);

        p3.setIndentationFirstLine(600);

        XWPFRun r4 = p3.createRun();
        r4.setTextPosition(200);
        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);

        //This would imply that this break shall be treated as a simple line break, and break the line after that word:

        XWPFRun r5 = p3.createRun();
        r5.setTextPosition(-10);
        r5.setText("For in that sleep of death what dreams may come");
        r5.addCarriageReturn();
        r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect"
                + "That makes calamity of so long life;");
        r5.addBreak();
        r5.setText("For who would bear the whips and scorns of time,"
                + "The oppressor's wrong, the proud man's contumely,");

        r5.addBreak(BreakClear.ALL);
        r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns"
                + ".......");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.gezipu360.cashier.bean.word.SimpleDocument.java

License:Apache License

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setBorderBottom(Borders.DOUBLE);//from  w  w  w.j  a  v a 2s  .c  o m
    p1.setBorderTop(Borders.DOUBLE);

    p1.setBorderRight(Borders.DOUBLE);
    p1.setBorderLeft(Borders.DOUBLE);
    p1.setBorderBetween(Borders.SINGLE);

    p1.setVerticalAlignment(TextAlignment.TOP);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("The quick brown fox");
    r1.setBold(true);
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.RIGHT);

    //BORDERS
    p2.setBorderBottom(Borders.DOUBLE);
    p2.setBorderTop(Borders.DOUBLE);
    p2.setBorderRight(Borders.DOUBLE);
    p2.setBorderLeft(Borders.DOUBLE);
    p2.setBorderBetween(Borders.SINGLE);

    XWPFRun r2 = p2.createRun();
    r2.setText("jumped over the lazy dog");
    r2.setStrike(true);
    r2.setFontSize(20);

    XWPFRun r3 = p2.createRun();
    r3.setText("and went away");
    r3.setStrike(true);
    r3.setFontSize(20);
    r3.setSubscript(VerticalAlign.SUPERSCRIPT);

    XWPFParagraph p3 = doc.createParagraph();
    p3.setWordWrap(true);
    p3.setPageBreak(true);

    //p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
    p3.setAlignment(ParagraphAlignment.BOTH);
    p3.setSpacingLineRule(LineSpacingRule.EXACT);

    p3.setIndentationFirstLine(600);

    XWPFRun r4 = p3.createRun();
    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);
    //This would imply that this break shall be treated as a simple line break, and break the line after that word:

    XWPFRun r5 = p3.createRun();
    r5.setTextPosition(-10);
    r5.setText("For in that sleep of death what dreams may come");
    r5.addCarriageReturn();
    r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect"
            + "That makes calamity of so long life;");
    r5.addBreak();
    r5.setText("For who would bear the whips and scorns of time,"
            + "The oppressor's wrong, the proud man's contumely,");

    r5.addBreak(BreakClear.ALL);
    r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns"
            + ".......");

    FileOutputStream out = new FileOutputStream("simple.docx");
    doc.write(out);
    out.close();

}

From source file:com.gezipu360.cashier.bean.word.SimpleImages.java

License:Apache License

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();

    XWPFRun r = p.createRun();//from  w  ww  . j  av a2s .  c  o m

    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();
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
        r.addBreak(BreakType.PAGE);
    }

    FileOutputStream out = new FileOutputStream("images.docx");
    doc.write(out);
    out.close();
}

From source file:com.glodon.tika.SimpleDocument.java

License:Apache License

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p1 = doc.createParagraph();
    p1.setAlignment(ParagraphAlignment.CENTER);
    p1.setBorderBottom(Borders.DOUBLE);//w ww  . java2  s . com
    p1.setBorderTop(Borders.DOUBLE);

    p1.setBorderRight(Borders.DOUBLE);
    p1.setBorderLeft(Borders.DOUBLE);
    p1.setBorderBetween(Borders.SINGLE);

    p1.setVerticalAlignment(TextAlignment.TOP);

    XWPFRun r1 = p1.createRun();
    r1.setBold(true);
    r1.setText("The quick brown fox");
    r1.setBold(true);
    r1.setFontFamily("Courier");
    r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
    r1.setTextPosition(100);

    XWPFParagraph p2 = doc.createParagraph();
    p2.setAlignment(ParagraphAlignment.RIGHT);

    //BORDERS
    p2.setBorderBottom(Borders.DOUBLE);
    p2.setBorderTop(Borders.DOUBLE);
    p2.setBorderRight(Borders.DOUBLE);
    p2.setBorderLeft(Borders.DOUBLE);
    p2.setBorderBetween(Borders.SINGLE);

    XWPFRun r2 = p2.createRun();
    r2.setText("jumped over the lazy dog");
    r2.setStrikeThrough(true);
    r2.setFontSize(20);

    XWPFRun r3 = p2.createRun();
    r3.setText("and went away");
    r3.setStrikeThrough(true);
    r3.setFontSize(20);
    r3.setSubscript(VerticalAlign.SUPERSCRIPT);

    XWPFParagraph p3 = doc.createParagraph();
    p3.setWordWrapped(true);
    p3.setPageBreak(true);

    //p3.setAlignment(ParagraphAlignment.DISTRIBUTE);
    p3.setAlignment(ParagraphAlignment.BOTH);
    p3.setSpacingLineRule(LineSpacingRule.EXACT);

    p3.setIndentationFirstLine(600);

    XWPFRun r4 = p3.createRun();
    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);
    //This would imply that this break shall be treated as a simple line break, and break the line after that word:

    XWPFRun r5 = p3.createRun();
    r5.setTextPosition(-10);
    r5.setText("For in that sleep of death what dreams may come");
    r5.addCarriageReturn();
    r5.setText("When we have shuffled off this mortal coil," + "Must give us pause: there's the respect"
            + "That makes calamity of so long life;");
    r5.addBreak();
    r5.setText("For who would bear the whips and scorns of time,"
            + "The oppressor's wrong, the proud man's contumely,");

    r5.addBreak(BreakClear.ALL);
    r5.setText("The pangs of despised love, the law's delay," + "The insolence of office and the spurns"
            + ".......");

    FileOutputStream out = new FileOutputStream("simple.docx");
    doc.write(out);
    out.close();
    doc.close();
}

From source file:com.glodon.tika.SimpleImages.java

License:Apache License

public static void main(String[] args) throws IOException, InvalidFormatException {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();

    XWPFRun r = p.createRun();/* ww  w .  j  av a 2  s. com*/

    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();
        r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
        r.addBreak(BreakType.PAGE);
    }

    FileOutputStream out = new FileOutputStream("images.docx");
    doc.write(out);
    out.close();
    doc.close();
}