use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.
the class SimpleImages method main.
public static void main(String[] args) throws IOException, InvalidFormatException {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p = doc.createParagraph();
XWPFRun r = p.createRun();
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();
// 200x200 pixels
r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200));
r.addBreak(BreakType.PAGE);
}
FileOutputStream out = new FileOutputStream("images.docx");
doc.write(out);
out.close();
doc.close();
}
use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.
the class SimpleTable method createSimpleTable.
public static void createSimpleTable() throws Exception {
XWPFDocument doc = new XWPFDocument();
try {
XWPFTable table = doc.createTable(3, 3);
table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE");
// table cells have a list of paragraphs; there is an initial
// paragraph created when the cell is created. If you create a
// paragraph in the document to put in the cell, it will also
// appear in the document following the table, which is probably
// not the desired result.
XWPFParagraph p1 = table.getRow(0).getCell(0).getParagraphs().get(0);
XWPFRun r1 = p1.createRun();
r1.setBold(true);
r1.setText("The quick brown fox");
r1.setItalic(true);
r1.setFontFamily("Courier");
r1.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
r1.setTextPosition(100);
table.getRow(2).getCell(2).setText("only text");
OutputStream out = new FileOutputStream("simpleTable.docx");
try {
doc.write(out);
} finally {
out.close();
}
} finally {
doc.close();
}
}
use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.
the class SimpleTable method createStyledTable.
/**
* Create a table with some row and column styling. I "manually" add the
* style name to the table, but don't check to see if the style actually
* exists in the document. Since I'm creating it from scratch, it obviously
* won't exist. When opened in MS Word, the table style becomes "Normal".
* I manually set alternating row colors. This could be done using Themes,
* but that's left as an exercise for the reader. The cells in the last
* column of the table have 10pt. "Courier" font.
* I make no claims that this is the "right" way to do it, but it worked
* for me. Given the scarcity of XWPF examples, I thought this may prove
* instructive and give you ideas for your own solutions.
* @throws Exception
*/
public static void createStyledTable() throws Exception {
// Create a new document from scratch
XWPFDocument doc = new XWPFDocument();
try {
// -- OR --
// open an existing empty document with styles already defined
//XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx"));
// Create a new table with 6 rows and 3 columns
int nRows = 6;
int nCols = 3;
XWPFTable table = doc.createTable(nRows, nCols);
// Set the table style. If the style is not defined, the table style
// will become "Normal".
CTTblPr tblPr = table.getCTTbl().getTblPr();
CTString styleStr = tblPr.addNewTblStyle();
styleStr.setVal("StyledTable");
// Get a list of the rows in the table
List<XWPFTableRow> rows = table.getRows();
int rowCt = 0;
int colCt = 0;
for (XWPFTableRow row : rows) {
// get table row properties (trPr)
CTTrPr trPr = row.getCtRow().addNewTrPr();
// set row height; units = twentieth of a point, 360 = 0.25"
CTHeight ht = trPr.addNewTrHeight();
ht.setVal(BigInteger.valueOf(360));
// get the cells in this row
List<XWPFTableCell> cells = row.getTableCells();
// add content to each cell
for (XWPFTableCell cell : cells) {
// get a table cell properties element (tcPr)
CTTcPr tcpr = cell.getCTTc().addNewTcPr();
// set vertical alignment to "center"
CTVerticalJc va = tcpr.addNewVAlign();
va.setVal(STVerticalJc.CENTER);
// create cell color element
CTShd ctshd = tcpr.addNewShd();
ctshd.setColor("auto");
ctshd.setVal(STShd.CLEAR);
if (rowCt == 0) {
// header row
ctshd.setFill("A7BFDE");
} else if (rowCt % 2 == 0) {
// even row
ctshd.setFill("D3DFEE");
} else {
// odd row
ctshd.setFill("EDF2F8");
}
// get 1st paragraph in cell's paragraph list
XWPFParagraph para = cell.getParagraphs().get(0);
// create a run to contain the content
XWPFRun rh = para.createRun();
// style cell as desired
if (colCt == nCols - 1) {
// last column is 10pt Courier
rh.setFontSize(10);
rh.setFontFamily("Courier");
}
if (rowCt == 0) {
// header row
rh.setText("header row, col " + colCt);
rh.setBold(true);
para.setAlignment(ParagraphAlignment.CENTER);
} else {
// other rows
rh.setText("row " + rowCt + ", col " + colCt);
para.setAlignment(ParagraphAlignment.LEFT);
}
colCt++;
}
// for cell
colCt = 0;
rowCt++;
}
// for row
// write the file
OutputStream out = new FileOutputStream("styledTable.docx");
try {
doc.write(out);
} finally {
out.close();
}
} finally {
doc.close();
}
}
use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.
the class TestEncryptor method inPlaceRewrite.
@Test
@Ignore
public void inPlaceRewrite() throws Exception {
File f = TempFile.createTempFile("protected_agile", ".docx");
// File f = new File("protected_agile.docx");
FileOutputStream fos = new FileOutputStream(f);
InputStream fis = POIDataSamples.getPOIFSInstance().openResourceAsStream("protected_agile.docx");
IOUtils.copy(fis, fos);
fis.close();
fos.close();
NPOIFSFileSystem fs = new NPOIFSFileSystem(f, false);
// decrypt the protected file - in this case it was encrypted with the default password
EncryptionInfo encInfo = new EncryptionInfo(fs);
Decryptor d = encInfo.getDecryptor();
boolean b = d.verifyPassword(Decryptor.DEFAULT_PASSWORD);
assertTrue(b);
// do some strange things with it ;)
InputStream docIS = d.getDataStream(fs);
XWPFDocument docx = new XWPFDocument(docIS);
docx.getParagraphArray(0).insertNewRun(0).setText("POI was here! All your base are belong to us!");
docx.getParagraphArray(0).insertNewRun(1).addBreak();
// and encrypt it again
Encryptor e = encInfo.getEncryptor();
e.confirmPassword("AYBABTU");
docx.write(e.getDataStream(fs));
docx.close();
docIS.close();
docx.close();
fs.close();
}
use of org.apache.poi.xwpf.usermodel.XWPFDocument in project poi by apache.
the class TestAllExtendedProperties method testGetAllExtendedProperties.
public void testGetAllExtendedProperties() throws IOException {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("TestPoiXMLDocumentCorePropertiesGetKeywords.docx");
CTProperties ctProps = doc.getProperties().getExtendedProperties().getUnderlyingProperties();
assertEquals("Microsoft Office Word", ctProps.getApplication());
assertEquals("14.0000", ctProps.getAppVersion());
assertEquals(57, ctProps.getCharacters());
assertEquals(66, ctProps.getCharactersWithSpaces());
assertEquals("", ctProps.getCompany());
assertNull(ctProps.getDigSig());
assertEquals(0, ctProps.getDocSecurity());
assertNotNull(ctProps.getDomNode());
CTVectorVariant vec = ctProps.getHeadingPairs();
assertEquals(2, vec.getVector().sizeOfVariantArray());
assertEquals("Title", vec.getVector().getVariantArray(0).getLpstr());
assertEquals(1, vec.getVector().getVariantArray(1).getI4());
assertFalse(ctProps.isSetHiddenSlides());
assertEquals(0, ctProps.getHiddenSlides());
assertFalse(ctProps.isSetHLinks());
assertNull(ctProps.getHLinks());
assertNull(ctProps.getHyperlinkBase());
assertTrue(ctProps.isSetHyperlinksChanged());
assertFalse(ctProps.getHyperlinksChanged());
assertEquals(1, ctProps.getLines());
assertTrue(ctProps.isSetLinksUpToDate());
assertFalse(ctProps.getLinksUpToDate());
assertNull(ctProps.getManager());
assertFalse(ctProps.isSetMMClips());
assertEquals(0, ctProps.getMMClips());
assertFalse(ctProps.isSetNotes());
assertEquals(0, ctProps.getNotes());
assertEquals(1, ctProps.getPages());
assertEquals(1, ctProps.getParagraphs());
assertNull(ctProps.getPresentationFormat());
assertTrue(ctProps.isSetScaleCrop());
assertFalse(ctProps.getScaleCrop());
assertTrue(ctProps.isSetSharedDoc());
assertFalse(ctProps.getSharedDoc());
assertFalse(ctProps.isSetSlides());
assertEquals(0, ctProps.getSlides());
assertEquals("Normal.dotm", ctProps.getTemplate());
CTVectorLpstr vec2 = ctProps.getTitlesOfParts();
assertEquals(1, vec2.getVector().sizeOfLpstrArray());
assertEquals("Example Word 2010 Document", vec2.getVector().getLpstrArray(0));
assertEquals(3, ctProps.getTotalTime());
assertEquals(10, ctProps.getWords());
// Check the digital signature part
// Won't be there in this file, but we
// need to do this check so that the
// appropriate parts end up in the
// smaller ooxml schemas file
CTDigSigBlob blob = ctProps.getDigSig();
assertNull(blob);
blob = CTDigSigBlob.Factory.newInstance();
blob.setBlob(new byte[] { 2, 6, 7, 2, 3, 4, 5, 1, 2, 3 });
}
Aggregations