Search in sources :

Example 6 with XWPFDocument

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

the class SimpleDocumentWithHeader method main.

public static void main(String[] args) {
    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");
    CTP ctP = CTP.Factory.newInstance();
    CTText t = ctP.addNewR().addNewT();
    t.setStringValue("header");
    pars = new XWPFParagraph[1];
    p = new XWPFParagraph(ctP, doc);
    pars[0] = p;
    XWPFHeaderFooterPolicy hfPolicy = doc.createHeaderFooterPolicy();
    hfPolicy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, pars);
    ctP = CTP.Factory.newInstance();
    t = ctP.addNewR().addNewT();
    t.setStringValue("My Footer");
    pars[0] = new XWPFParagraph(ctP, doc);
    hfPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, pars);
    try {
        OutputStream os = new FileOutputStream(new File("header.docx"));
        doc.write(os);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) CTText(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) XWPFHeaderFooterPolicy(org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy) IOException(java.io.IOException) File(java.io.File) CTP(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)

Example 7 with XWPFDocument

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

the class LoadEmbedded method loadEmbedded.

public static void loadEmbedded(XSSFWorkbook workbook) throws IOException, InvalidFormatException, OpenXML4JException, XmlException {
    for (PackagePart pPart : workbook.getAllEmbedds()) {
        String contentType = pPart.getContentType();
        if (contentType.equals("application/vnd.ms-excel")) {
            // Excel Workbook - either binary or OpenXML
            HSSFWorkbook embeddedWorkbook = new HSSFWorkbook(pPart.getInputStream());
            embeddedWorkbook.close();
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
            // Excel Workbook - OpenXML file format
            XSSFWorkbook embeddedWorkbook = new XSSFWorkbook(pPart.getInputStream());
            embeddedWorkbook.close();
        } else if (contentType.equals("application/msword")) {
            // Word Document - binary (OLE2CDF) file format
            HWPFDocument document = new HWPFDocument(pPart.getInputStream());
            document.close();
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
            // Word Document - OpenXML file format
            XWPFDocument document = new XWPFDocument(pPart.getInputStream());
            document.close();
        } else if (contentType.equals("application/vnd.ms-powerpoint")) {
            // PowerPoint Document - binary file format
            HSLFSlideShow slideShow = new HSLFSlideShow(pPart.getInputStream());
            slideShow.close();
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
            // PowerPoint Document - OpenXML file format
            XMLSlideShow slideShow = new XMLSlideShow(pPart.getInputStream());
            slideShow.close();
        } else {
            // Any other type of embedded object.
            System.out.println("Unknown Embedded Document: " + contentType);
            InputStream inputStream = pPart.getInputStream();
            inputStream.close();
        }
    }
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) InputStream(java.io.InputStream) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 8 with XWPFDocument

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

the class EmbeddedObjects method main.

public static void main(String[] args) throws Exception {
    XSSFWorkbook workbook = new XSSFWorkbook(args[0]);
    for (PackagePart pPart : workbook.getAllEmbedds()) {
        String contentType = pPart.getContentType();
        InputStream is = pPart.getInputStream();
        Closeable document;
        if (contentType.equals("application/vnd.ms-excel")) {
            // Excel Workbook - either binary or OpenXML
            document = new HSSFWorkbook(is);
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
            // Excel Workbook - OpenXML file format
            document = new XSSFWorkbook(is);
        } else if (contentType.equals("application/msword")) {
            // Word Document - binary (OLE2CDF) file format
            document = new HWPFDocument(is);
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
            // Word Document - OpenXML file format
            document = new XWPFDocument(is);
        } else if (contentType.equals("application/vnd.ms-powerpoint")) {
            // PowerPoint Document - binary file format
            document = new HSLFSlideShow(is);
        } else if (contentType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation")) {
            // PowerPoint Document - OpenXML file format
            document = new XMLSlideShow(is);
        } else {
            // Any other type of embedded object.
            document = is;
        }
        document.close();
        is.close();
    }
    workbook.close();
}
Also used : HWPFDocument(org.apache.poi.hwpf.HWPFDocument) InputStream(java.io.InputStream) Closeable(java.io.Closeable) XMLSlideShow(org.apache.poi.xslf.usermodel.XMLSlideShow) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) PackagePart(org.apache.poi.openxml4j.opc.PackagePart) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 9 with XWPFDocument

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

the class TestPOIXMLProperties method testTransitiveSetters.

@Test
public void testTransitiveSetters() throws IOException {
    XWPFDocument doc = new XWPFDocument();
    CoreProperties cp = doc.getProperties().getCoreProperties();
    Date dateCreated = LocaleUtil.getLocaleCalendar(2010, 6, 15, 10, 0, 0).getTime();
    cp.setCreated(new Nullable<Date>(dateCreated));
    assertEquals(dateCreated, cp.getCreated());
    XWPFDocument doc2 = XWPFTestDataSamples.writeOutAndReadBack(doc);
    doc.close();
    cp = doc2.getProperties().getCoreProperties();
    Date dt3 = cp.getCreated();
    assertEquals(dateCreated, dt3);
    doc2.close();
}
Also used : CoreProperties(org.apache.poi.POIXMLProperties.CoreProperties) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) Date(java.util.Date) Test(org.junit.Test)

Example 10 with XWPFDocument

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

the class TestDocumentProtection method testIntegration.

@Test
public void testIntegration() throws IOException {
    XWPFDocument doc1 = new XWPFDocument();
    XWPFParagraph p1 = doc1.createParagraph();
    XWPFRun r1 = p1.createRun();
    r1.setText("Lorem ipsum dolor sit amet.");
    doc1.enforceCommentsProtection();
    File tempFile = TempFile.createTempFile("documentProtectionFile", ".docx");
    FileOutputStream out = new FileOutputStream(tempFile);
    doc1.write(out);
    out.close();
    FileInputStream inputStream = new FileInputStream(tempFile);
    XWPFDocument doc2 = new XWPFDocument(inputStream);
    inputStream.close();
    assertTrue(doc2.isEnforcedCommentsProtection());
    doc2.close();
    doc1.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) TempFile(org.apache.poi.util.TempFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

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