Java:using apache POI how to convert ms word file to pdf?

Got It solved import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.extractor.WordExtractor; import org.apache.poi.hwpf.usermodel.Range; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class TestCon { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub POIFSFileSystem fs = null; Document document = new … Read more

Merge and align center cell using apache poi

My solution was to merge the cells by their positions, then created a cell (reference to the first block of the merged cells) to assign a value and then set the alignment throught the CellUtil // Merges the cells CellRangeAddress cellRangeAddress = new CellRangeAddress(start, start, j, j + 1); sheet.addMergedRegion(cellRangeAddress); // Creates the cell Cell … Read more

Java POI the supplied data appears to be in the Office 2007+ XML

According to the Apache POI Quick Guide, the POIFSFileSystem (or similarly, NPOIFSFileSystem) is only used with .xls (Excel versions through 2003) documents. The equivalent for .xlsx documents (Excel 2007+) is OPCPackage. OPCPackage pkg = OPCPackage.open(new File(“file.xlsx”)); You can create an XSSFWorkbook from the OPCPackage: XSSFWorkbook wb = new XSSFWorkbook(pkg); Or you can just create it … Read more

How to change font color of particular cell apache poi 3.9

You’re currently creating some of your cells twice, which is why it’s all going wrong Firstly, I’d suggest you move the cell style creation to nearer the top of your code. Remember – cell styles are scoped to a workbook, so don’t create one per cell! CellStyle style = wb.createCellStyle(); Font font = wb.createFont(); font.setColor(HSSFColor.HSSFColorPredefined.BLACK.getIndex()); … Read more

Reading date values from excel cell using POI HSSF API

You could take a look at: HSSFDateUtil.isCellDateFormatted() See the POI Horrible Spreadsheet Format API for more details on HSSFDateUtil: http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFDateUtil.html That also provides some helper methods for returning Excel getExcelDate() and Java dates getJavaDate(). You need to be somewhat wary of different date formats though…

How to speed up autosizing columns in apache POI?

Solution which worked for me: It was possible to avoid merged regions, so I could iterate through the other cells and finally autosize to the largest cell like this: int width = ((int)(maxNumCharacters * 1.14388)) * 256; sheet.setColumnWidth(i, width); where 1.14388 is a max character width of the “Serif” font and 256 font units. Performance … Read more

Converting docx into pdf in java

In addition to the VivekRatanSinha answer, i would i like to post full code and required jars for the people who need it in future. Code: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.poi.xwpf.converter.pdf.PdfConverter; import org.apache.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.xwpf.usermodel.XWPFDocument; public class WordConvertPDF { public static void main(String[] args) { WordConvertPDF … Read more

tech