Search in sources :

Example 1 with XWPFDocument

use of org.apache.poi.xwpf.usermodel.XWPFDocument in project LAMSADE-tools by LAntoine.

the class SetCoordinates method setDetails.

/**
	 * Add the user details to the source file and return the result in the
	 * destination file
	 *
	 * @param source
	 * @param destinationFile
	 * @param user
	 * @throws IOException
	 * @throws InvalidFormatException
	 */
public static void setDetails(UserDetails user) throws IOException, InvalidFormatException {
    String source = "com/github/lamsadetools/setCoordinates/papier_en_tete.docx";
    String destination = "com/github/lamsadetools/setCoordinates/papier_en_tete_CloneTest.docx";
    ClassLoader classLoader = SetCoordinates.class.getClassLoader();
    File sourceFile = new File(classLoader.getResource(source).getFile());
    File destinationFile = new File(classLoader.getResource(destination).getFile());
    copy(sourceFile, destinationFile);
    try (XWPFDocument doc = new XWPFDocument(OPCPackage.open(sourceFile))) {
        XWPFHeaderFooterPolicy policy = doc.getHeaderFooterPolicy();
        for (XWPFParagraph p : policy.getFirstPageHeader().getParagraphs()) {
            List<XWPFRun> runs = p.getRuns();
            if (runs != null) {
                for (XWPFRun r : runs) {
                    String text = r.getText(0);
                    if ((text != null) && text.contains("Prenom")) {
                        System.out.println("contains prenom");
                        text = text.replace("Prenom", user.getFirstName());
                        r.setText(text, 0);
                    } else if ((text != null) && text.contains("Nom")) {
                        text = text.replace("Nom", user.getName());
                        r.setText(text, 0);
                    } else if ((text != null) && text.contains("e-mail")) {
                        text = text.replace("e-mail", user.getEmail());
                        r.setText(text, 0);
                    } else if ((text != null) && text.contains("tel.")) {
                        text = text.replace("tel.", user.getNumber());
                        r.setText(text, 0);
                    } else if ((text != null) && text.contains("Fonction")) {
                        text = text.replace("Fonction", user.getFunction());
                        r.setText(text, 0);
                    }
                }
            }
        }
        try (FileOutputStream fos = new FileOutputStream(destinationFile)) {
            doc.write(fos);
        }
    }
    long start = System.currentTimeMillis();
    System.err.println("Generate papier_en_tete.html with " + (System.currentTimeMillis() - start) + "ms");
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy) File(java.io.File)

Example 2 with XWPFDocument

use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.

the class XWPFFileHandler method handleFile.

@Override
public void handleFile(InputStream stream, String path) throws Exception {
    // ignore password protected files
    if (POIXMLDocumentHandler.isEncrypted(stream))
        return;
    XWPFDocument doc = new XWPFDocument(stream);
    new POIXMLDocumentHandler().handlePOIXMLDocument(doc);
}
Also used : XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument)

Example 3 with XWPFDocument

use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.

the class BetterHeaderFooterExample method main.

public static void main(String[] args) throws IOException {
    XWPFDocument doc = new XWPFDocument();
    XWPFParagraph p = doc.createParagraph();
    XWPFRun r = p.createRun();
    r.setText("Some Text");
    r.setBold(true);
    r = p.createRun();
    r.setText("Goodbye");
    // create header/footer functions insert an empty paragraph
    XWPFHeader head = doc.createHeader(HeaderFooterType.DEFAULT);
    head.createParagraph().createRun().setText("header");
    XWPFFooter foot = doc.createFooter(HeaderFooterType.DEFAULT);
    foot.createParagraph().createRun().setText("footer");
    OutputStream os = new FileOutputStream(new File("header2.docx"));
    doc.write(os);
    os.close();
    doc.close();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) XWPFFooter(org.apache.poi.xwpf.usermodel.XWPFFooter) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) File(java.io.File) XWPFHeader(org.apache.poi.xwpf.usermodel.XWPFHeader)

Example 4 with XWPFDocument

use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.

the class HeaderFooterTable method main.

public static void main(String[] args) throws IOException {
    XWPFDocument doc = new XWPFDocument();
    // Create a header with a 1 row, 3 column table
    // changes made for issue 57366 allow a new header or footer
    // to be created empty. This is a change. You will have to add
    // either a paragraph or a table to the header or footer for
    // the document to be considered valid.
    XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
    XWPFTable tbl = hdr.createTable(1, 3);
    // Set the padding around text in the cells to 1/10th of an inch
    int pad = (int) (.1 * 1440);
    tbl.setCellMargins(pad, pad, pad, pad);
    // Set table width to 6.5 inches in 1440ths of a point
    tbl.setWidth((int) (6.5 * 1440));
    // Can not yet set table or cell width properly, tables default to
    // autofit layout, and this requires fixed layout
    CTTbl ctTbl = tbl.getCTTbl();
    CTTblPr ctTblPr = ctTbl.addNewTblPr();
    CTTblLayoutType layoutType = ctTblPr.addNewTblLayout();
    layoutType.setType(STTblLayoutType.FIXED);
    // Now set up a grid for the table, cells will fit into the grid
    // Each cell width is 3120 in 1440ths of an inch, or 1/3rd of 6.5"
    BigInteger w = new BigInteger("3120");
    CTTblGrid grid = ctTbl.addNewTblGrid();
    for (int i = 0; i < 3; i++) {
        CTTblGridCol gridCol = grid.addNewGridCol();
        gridCol.setW(w);
    }
    // Add paragraphs to the cells
    XWPFTableRow row = tbl.getRow(0);
    XWPFTableCell cell = row.getCell(0);
    XWPFParagraph p = cell.getParagraphArray(0);
    XWPFRun r = p.createRun();
    r.setText("header left cell");
    cell = row.getCell(1);
    p = cell.getParagraphArray(0);
    r = p.createRun();
    r.setText("header center cell");
    cell = row.getCell(2);
    p = cell.getParagraphArray(0);
    r = p.createRun();
    r.setText("header right cell");
    // Create a footer with a Paragraph
    XWPFFooter ftr = doc.createFooter(HeaderFooterType.DEFAULT);
    p = ftr.createParagraph();
    r = p.createRun();
    r.setText("footer text");
    OutputStream os = new FileOutputStream(new File("headertable.docx"));
    doc.write(os);
    doc.close();
}
Also used : CTTblGridCol(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGridCol) XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) XWPFFooter(org.apache.poi.xwpf.usermodel.XWPFFooter) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CTTblPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr) CTTblLayoutType(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblLayoutType) XWPFHeader(org.apache.poi.xwpf.usermodel.XWPFHeader) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) CTTblGrid(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid) FileOutputStream(java.io.FileOutputStream) BigInteger(java.math.BigInteger) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) File(java.io.File)

Example 5 with XWPFDocument

use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.

the class SimpleDocument method main.

public static void main(String[] args) throws Exception {
    XWPFDocument doc = new XWPFDocument();
    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.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.setSpacingBetween(15, 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();
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument)

Aggregations

XWPFDocument (org.apache.poi.xwpf.usermodel.XWPFDocument)51 Test (org.junit.Test)15 FileOutputStream (java.io.FileOutputStream)11 File (java.io.File)9 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)9 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)9 InputStream (java.io.InputStream)6 OutputStream (java.io.OutputStream)6 FileInputStream (java.io.FileInputStream)4 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)4 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)3 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)3 XMLSlideShow (org.apache.poi.xslf.usermodel.XMLSlideShow)3 XWPFWordExtractor (org.apache.poi.xwpf.extractor.XWPFWordExtractor)3 XWPFFooter (org.apache.poi.xwpf.usermodel.XWPFFooter)3 XWPFHeader (org.apache.poi.xwpf.usermodel.XWPFHeader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ZipFile (java.util.zip.ZipFile)2 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)2 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)2