Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkbookProperties.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkbookProperties.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkbookProperties.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkbookProperties.java Fri Sep 22 21:16:09 2017 @@ -29,37 +29,34 @@ import org.apache.poi.xssf.usermodel.XSS public class WorkbookProperties { public static void main(String[]args) throws IOException { + try (XSSFWorkbook workbook = new XSSFWorkbook()) { + workbook.createSheet("Workbook Properties"); - XSSFWorkbook workbook = new XSSFWorkbook(); - workbook.createSheet("Workbook Properties"); + POIXMLProperties props = workbook.getProperties(); - POIXMLProperties props = workbook.getProperties(); - - /** + /* * Extended properties are a predefined set of metadata properties * that are specifically applicable to Office Open XML documents. * Extended properties consist of 24 simple properties and 3 complex properties stored in the * part targeted by the relationship of type */ - POIXMLProperties.ExtendedProperties ext = props.getExtendedProperties(); - ext.getUnderlyingProperties().setCompany("Apache Software Foundation"); - ext.getUnderlyingProperties().setTemplate("XSSF"); + POIXMLProperties.ExtendedProperties ext = props.getExtendedProperties(); + ext.getUnderlyingProperties().setCompany("Apache Software Foundation"); + ext.getUnderlyingProperties().setTemplate("XSSF"); - /** + /* * Custom properties enable users to define custom metadata properties. */ - - POIXMLProperties.CustomProperties cust = props.getCustomProperties(); - cust.addProperty("Author", "John Smith"); - cust.addProperty("Year", 2009); - cust.addProperty("Price", 45.50); - cust.addProperty("Available", true); - - FileOutputStream out = new FileOutputStream("workbook.xlsx"); - workbook.write(out); - out.close(); - workbook.close(); - } - + POIXMLProperties.CustomProperties cust = props.getCustomProperties(); + cust.addProperty("Author", "John Smith"); + cust.addProperty("Year", 2009); + cust.addProperty("Price", 45.50); + cust.addProperty("Available", true); + + try (FileOutputStream out = new FileOutputStream("workbook.xlsx")) { + workbook.write(out); + } + } + } } \ No newline at end of file
Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithBorders.java Fri Sep 22 21:16:09 2017 @@ -34,32 +34,32 @@ import org.apache.poi.xssf.usermodel.XSS */ public class WorkingWithBorders { public static void main(String[] args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - Sheet sheet = wb.createSheet("borders"); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); + Sheet sheet = wb.createSheet("borders"); - // Create a row and put some cells in it. Rows are 0 based. - Row row = sheet.createRow((short) 1); + // Create a row and put some cells in it. Rows are 0 based. + Row row = sheet.createRow((short) 1); - // Create a cell and put a value in it. - Cell cell = row.createCell((short) 1); - cell.setCellValue(4); + // Create a cell and put a value in it. + Cell cell = row.createCell((short) 1); + cell.setCellValue(4); - // Style the cell with borders all around. - CellStyle style = wb.createCellStyle(); - style.setBorderBottom(BorderStyle.THIN); - style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); - style.setBorderLeft(BorderStyle.THIN); - style.setLeftBorderColor(IndexedColors.GREEN.getIndex()); - style.setBorderRight(BorderStyle.THIN); - style.setRightBorderColor(IndexedColors.BLUE.getIndex()); - style.setBorderTop(BorderStyle.MEDIUM_DASHED); - style.setTopBorderColor(IndexedColors.BLACK.getIndex()); - cell.setCellStyle(style); + // Style the cell with borders all around. + CellStyle style = wb.createCellStyle(); + style.setBorderBottom(BorderStyle.THIN); + style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); + style.setBorderLeft(BorderStyle.THIN); + style.setLeftBorderColor(IndexedColors.GREEN.getIndex()); + style.setBorderRight(BorderStyle.THIN); + style.setRightBorderColor(IndexedColors.BLUE.getIndex()); + style.setBorderTop(BorderStyle.MEDIUM_DASHED); + style.setTopBorderColor(IndexedColors.BLACK.getIndex()); + cell.setCellStyle(style); - // Write the output to a file - FileOutputStream fileOut = new FileOutputStream("xssf-borders.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + // Write the output to a file + try (FileOutputStream fileOut = new FileOutputStream("xssf-borders.xlsx")) { + wb.write(fileOut); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithFonts.java Fri Sep 22 21:16:09 2017 @@ -33,75 +33,75 @@ import org.apache.poi.xssf.usermodel.XSS */ public class WorkingWithFonts { public static void main(String[] args) throws IOException { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - Sheet sheet = wb.createSheet("Fonts"); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); + Sheet sheet = wb.createSheet("Fonts"); - Font font0 = wb.createFont(); - font0.setColor(IndexedColors.BROWN.getIndex()); - CellStyle style0 = wb.createCellStyle(); - style0.setFont(font0); - - Font font1 = wb.createFont(); - font1.setFontHeightInPoints((short)14); - font1.setFontName("Courier New"); - font1.setColor(IndexedColors.RED.getIndex()); - CellStyle style1 = wb.createCellStyle(); - style1.setFont(font1); - - Font font2 = wb.createFont(); - font2.setFontHeightInPoints((short)16); - font2.setFontName("Arial"); - font2.setColor(IndexedColors.GREEN.getIndex()); - CellStyle style2 = wb.createCellStyle(); - style2.setFont(font2); - - Font font3 = wb.createFont(); - font3.setFontHeightInPoints((short)18); - font3.setFontName("Times New Roman"); - font3.setColor(IndexedColors.LAVENDER.getIndex()); - CellStyle style3 = wb.createCellStyle(); - style3.setFont(font3); - - Font font4 = wb.createFont(); - font4.setFontHeightInPoints((short)18); - font4.setFontName("Wingdings"); - font4.setColor(IndexedColors.GOLD.getIndex()); - CellStyle style4 = wb.createCellStyle(); - style4.setFont(font4); - - Font font5 = wb.createFont(); - font5.setFontName("Symbol"); - CellStyle style5 = wb.createCellStyle(); - style5.setFont(font5); - - Cell cell0 = sheet.createRow(0).createCell(1); - cell0.setCellValue("Default"); - cell0.setCellStyle(style0); - - Cell cell1 = sheet.createRow(1).createCell(1); - cell1.setCellValue("Courier"); - cell1.setCellStyle(style1); - - Cell cell2 = sheet.createRow(2).createCell(1); - cell2.setCellValue("Arial"); - cell2.setCellStyle(style2); - - Cell cell3 = sheet.createRow(3).createCell(1); - cell3.setCellValue("Times New Roman"); - cell3.setCellStyle(style3); - - Cell cell4 = sheet.createRow(4).createCell(1); - cell4.setCellValue("Wingdings"); - cell4.setCellStyle(style4); - - Cell cell5 = sheet.createRow(5).createCell(1); - cell5.setCellValue("Symbol"); - cell5.setCellStyle(style5); - - // Write the output to a file - FileOutputStream fileOut = new FileOutputStream("xssf-fonts.xlsx"); - wb.write(fileOut); - fileOut.close(); - wb.close(); + Font font0 = wb.createFont(); + font0.setColor(IndexedColors.BROWN.getIndex()); + CellStyle style0 = wb.createCellStyle(); + style0.setFont(font0); + + Font font1 = wb.createFont(); + font1.setFontHeightInPoints((short) 14); + font1.setFontName("Courier New"); + font1.setColor(IndexedColors.RED.getIndex()); + CellStyle style1 = wb.createCellStyle(); + style1.setFont(font1); + + Font font2 = wb.createFont(); + font2.setFontHeightInPoints((short) 16); + font2.setFontName("Arial"); + font2.setColor(IndexedColors.GREEN.getIndex()); + CellStyle style2 = wb.createCellStyle(); + style2.setFont(font2); + + Font font3 = wb.createFont(); + font3.setFontHeightInPoints((short) 18); + font3.setFontName("Times New Roman"); + font3.setColor(IndexedColors.LAVENDER.getIndex()); + CellStyle style3 = wb.createCellStyle(); + style3.setFont(font3); + + Font font4 = wb.createFont(); + font4.setFontHeightInPoints((short) 18); + font4.setFontName("Wingdings"); + font4.setColor(IndexedColors.GOLD.getIndex()); + CellStyle style4 = wb.createCellStyle(); + style4.setFont(font4); + + Font font5 = wb.createFont(); + font5.setFontName("Symbol"); + CellStyle style5 = wb.createCellStyle(); + style5.setFont(font5); + + Cell cell0 = sheet.createRow(0).createCell(1); + cell0.setCellValue("Default"); + cell0.setCellStyle(style0); + + Cell cell1 = sheet.createRow(1).createCell(1); + cell1.setCellValue("Courier"); + cell1.setCellStyle(style1); + + Cell cell2 = sheet.createRow(2).createCell(1); + cell2.setCellValue("Arial"); + cell2.setCellStyle(style2); + + Cell cell3 = sheet.createRow(3).createCell(1); + cell3.setCellValue("Times New Roman"); + cell3.setCellStyle(style3); + + Cell cell4 = sheet.createRow(4).createCell(1); + cell4.setCellValue("Wingdings"); + cell4.setCellStyle(style4); + + Cell cell5 = sheet.createRow(5).createCell(1); + cell5.setCellValue("Symbol"); + cell5.setCellStyle(style5); + + // Write the output to a file + try (FileOutputStream fileOut = new FileOutputStream("xssf-fonts.xlsx")) { + wb.write(fileOut); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPageSetup.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPageSetup.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPageSetup.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPageSetup.java Fri Sep 22 21:16:09 2017 @@ -30,9 +30,9 @@ import org.apache.poi.xssf.usermodel.XSS public class WorkingWithPageSetup { public static void main(String[]args) throws Exception { - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); + try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook(); - /** + /* * It's possible to set up repeating rows and columns in your printouts by using the setRepeatingRowsAndColumns() function in the Workbook object. * * This function Contains 5 parameters: @@ -42,42 +42,41 @@ public class WorkingWithPageSetup { * The fourth and fifth parameters specify the range for the rows to repeat. * To stop the columns from repeating pass in -1 as the start and end rows. */ - Sheet sheet1 = wb.createSheet("new sheet"); - Sheet sheet2 = wb.createSheet("second sheet"); + Sheet sheet1 = wb.createSheet("new sheet"); + Sheet sheet2 = wb.createSheet("second sheet"); - // Set the columns to repeat from column 0 to 2 on the first sheet - Row row1 = sheet1.createRow(0); - row1.createCell(0).setCellValue(1); - row1.createCell(1).setCellValue(2); - row1.createCell(2).setCellValue(3); - Row row2 = sheet1.createRow(1); - row2.createCell(1).setCellValue(4); - row2.createCell(2).setCellValue(5); - - - Row row3 = sheet2.createRow(1); - row3.createCell(0).setCellValue(2.1); - row3.createCell(4).setCellValue(2.2); - row3.createCell(5).setCellValue(2.3); - Row row4 = sheet2.createRow(2); - row4.createCell(4).setCellValue(2.4); - row4.createCell(5).setCellValue(2.5); - - // Set the columns to repeat from column 0 to 2 on the first sheet - sheet1.setRepeatingColumns(CellRangeAddress.valueOf("A:C")); - // Set the the repeating rows and columns on the second sheet. - CellRangeAddress cra = CellRangeAddress.valueOf("E2:F3"); - sheet2.setRepeatingColumns(cra); - sheet2.setRepeatingRows(cra); - - //set the print area for the first sheet - wb.setPrintArea(0, 1, 2, 0, 3); - - - FileOutputStream fileOut = new FileOutputStream("xssf-printsetup.xlsx"); - wb.write(fileOut); - fileOut.close(); - - wb.close(); + // Set the columns to repeat from column 0 to 2 on the first sheet + Row row1 = sheet1.createRow(0); + row1.createCell(0).setCellValue(1); + row1.createCell(1).setCellValue(2); + row1.createCell(2).setCellValue(3); + Row row2 = sheet1.createRow(1); + row2.createCell(1).setCellValue(4); + row2.createCell(2).setCellValue(5); + + + Row row3 = sheet2.createRow(1); + row3.createCell(0).setCellValue(2.1); + row3.createCell(4).setCellValue(2.2); + row3.createCell(5).setCellValue(2.3); + Row row4 = sheet2.createRow(2); + row4.createCell(4).setCellValue(2.4); + row4.createCell(5).setCellValue(2.5); + + // Set the columns to repeat from column 0 to 2 on the first sheet + sheet1.setRepeatingColumns(CellRangeAddress.valueOf("A:C")); + // Set the the repeating rows and columns on the second sheet. + CellRangeAddress cra = CellRangeAddress.valueOf("E2:F3"); + sheet2.setRepeatingColumns(cra); + sheet2.setRepeatingRows(cra); + + //set the print area for the first sheet + wb.setPrintArea(0, 1, 2, 0, 3); + + + try (FileOutputStream fileOut = new FileOutputStream("xssf-printsetup.xlsx")) { + wb.write(fileOut); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPictures.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPictures.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPictures.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithPictures.java Fri Sep 22 21:16:09 2017 @@ -39,45 +39,38 @@ public class WorkingWithPictures { public static void main(String[] args) throws IOException { //create a new workbook - Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - try { + try (Workbook wb = new XSSFWorkbook()) { CreationHelper helper = wb.getCreationHelper(); - + //add a picture in this workbook. InputStream is = new FileInputStream(args[0]); byte[] bytes = IOUtils.toByteArray(is); is.close(); int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG); - + //create sheet Sheet sheet = wb.createSheet(); - + //create drawing Drawing<?> drawing = sheet.createDrawingPatriarch(); - + //add a picture shape ClientAnchor anchor = helper.createClientAnchor(); anchor.setCol1(1); anchor.setRow1(1); Picture pict = drawing.createPicture(anchor, pictureIdx); - + //auto-size picture pict.resize(2); - + //save workbook String file = "picture.xls"; - if(wb instanceof XSSFWorkbook) - { + if (wb instanceof XSSFWorkbook) { file += "x"; // NOSONAR } - OutputStream fileOut = new FileOutputStream(file); - try { + try (OutputStream fileOut = new FileOutputStream(file)) { wb.write(fileOut); - } finally { - fileOut.close(); } - } finally { - wb.close(); } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithRichText.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithRichText.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithRichText.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/WorkingWithRichText.java Fri Sep 22 21:16:09 2017 @@ -28,41 +28,34 @@ import java.io.OutputStream; public class WorkingWithRichText { public static void main(String[] args) throws Exception { - - XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook(); - try { + try (XSSFWorkbook wb = new XSSFWorkbook()) { XSSFSheet sheet = wb.createSheet(); XSSFRow row = sheet.createRow(2); - + XSSFCell cell = row.createCell(1); XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox"); - + XSSFFont font1 = wb.createFont(); font1.setBold(true); font1.setColor(new XSSFColor(new java.awt.Color(255, 0, 0))); rt.applyFont(0, 10, font1); - + XSSFFont font2 = wb.createFont(); font2.setItalic(true); font2.setUnderline(XSSFFont.U_DOUBLE); font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0))); rt.applyFont(10, 19, font2); - + XSSFFont font3 = wb.createFont(); font3.setColor(new XSSFColor(new java.awt.Color(0, 0, 255))); rt.append(" Jumped over the lazy dog", font3); - + cell.setCellValue(rt); // Write the output to a file - OutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx"); - try { + try (OutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx")) { wb.write(fileOut); - } finally { - fileOut.close(); } - } finally { - wb.close(); } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BetterHeaderFooterExample.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BetterHeaderFooterExample.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BetterHeaderFooterExample.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/BetterHeaderFooterExample.java Fri Sep 22 21:16:09 2017 @@ -31,26 +31,26 @@ import org.apache.poi.xwpf.usermodel.XWP public class BetterHeaderFooterExample { public static void main(String[] args) throws IOException { - XWPFDocument doc = new XWPFDocument(); + try (XWPFDocument doc = new XWPFDocument()) { - XWPFParagraph p = doc.createParagraph(); + 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(); + 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"); + + try (OutputStream os = new FileOutputStream(new File("header2.docx"))) { + doc.write(os); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/HeaderFooterTable.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/HeaderFooterTable.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/HeaderFooterTable.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/HeaderFooterTable.java Fri Sep 22 21:16:09 2017 @@ -41,65 +41,65 @@ import org.openxmlformats.schemas.wordpr public class HeaderFooterTable { 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); + try (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"); + + try (OutputStream os = new FileOutputStream(new File("headertable.docx"))) { + doc.write(os); + } } - - // 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(); } - } Modified: poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocument.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocument.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocument.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocument.java Fri Sep 22 21:16:09 2017 @@ -36,95 +36,95 @@ import org.apache.poi.xwpf.usermodel.XWP public class SimpleDocument { public static void main(String[] args) throws Exception { - XWPFDocument doc = new XWPFDocument(); + try (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); + 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(); + 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" + "......."); + + try (FileOutputStream out = new FileOutputStream("simple.docx")) { + doc.write(out); + } + } } } Modified: poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocumentWithHeader.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocumentWithHeader.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocumentWithHeader.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleDocumentWithHeader.java Fri Sep 22 21:16:09 2017 @@ -35,42 +35,36 @@ import org.openxmlformats.schemas.wordpr */ public class SimpleDocumentWithHeader { - private static XWPFParagraph[] pars; + public static void main(String[] args) throws IOException { + try (XWPFDocument doc = new XWPFDocument()) { - public static void main(String[] args) { - XWPFDocument doc = new XWPFDocument(); + XWPFParagraph p = doc.createParagraph(); - 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(); + 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"); + XWPFParagraph[] 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); + } } - } } \ No newline at end of file Modified: poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleImages.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleImages.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleImages.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleImages.java Fri Sep 22 21:16:09 2017 @@ -35,41 +35,41 @@ import org.apache.poi.xwpf.usermodel.XWP public class SimpleImages { public static void main(String[] args) throws IOException, InvalidFormatException { - XWPFDocument doc = new XWPFDocument(); - XWPFParagraph p = doc.createParagraph(); + try (XWPFDocument doc = new XWPFDocument()) { + XWPFParagraph p = doc.createParagraph(); - XWPFRun r = p.createRun(); + XWPFRun r = p.createRun(); - for(String imgFile : args) { - int format; + 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; + 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); } - r.setText(imgFile); - r.addBreak(); - r.addPicture(new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels - r.addBreak(BreakType.PAGE); + try (FileOutputStream out = new FileOutputStream("images.docx")) { + doc.write(out); + } } - - FileOutputStream out = new FileOutputStream("images.docx"); - doc.write(out); - out.close(); - doc.close(); } Modified: poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleTable.java URL: http://svn.apache.org/viewvc/poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleTable.java?rev=1809354&r1=1809353&r2=1809354&view=diff ============================================================================== --- poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleTable.java (original) +++ poi/trunk/src/examples/src/org/apache/poi/xwpf/usermodel/examples/SimpleTable.java Fri Sep 22 21:16:09 2017 @@ -69,9 +69,7 @@ public class SimpleTable { } public static void createSimpleTable() throws Exception { - XWPFDocument doc = new XWPFDocument(); - - try { + try (XWPFDocument doc = new XWPFDocument()) { XWPFTable table = doc.createTable(3, 3); table.getRow(1).getCell(1).setText("EXAMPLE OF TABLE"); @@ -93,14 +91,9 @@ public class SimpleTable { table.getRow(2).getCell(2).setText("only text"); - OutputStream out = new FileOutputStream("simpleTable.docx"); - try { + try (OutputStream out = new FileOutputStream("simpleTable.docx")) { doc.write(out); - } finally { - out.close(); } - } finally { - doc.close(); } } @@ -115,14 +108,11 @@ public class SimpleTable { * 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 { + try (XWPFDocument doc = new XWPFDocument()) { // -- OR -- // open an existing empty document with styles already defined //XWPFDocument doc = new XWPFDocument(new FileInputStream("base_document.docx")); @@ -201,14 +191,9 @@ public class SimpleTable { } // for row // write the file - OutputStream out = new FileOutputStream("styledTable.docx"); - try { + try (OutputStream out = new FileOutputStream("styledTable.docx")) { doc.write(out); - } finally { - out.close(); } - } finally { - doc.close(); } } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
