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();
}
}
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();
}
}
}
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();
}
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();
}
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();
}
Aggregations