Convert word to PDF stream and embed in another PDF
Hi,
I have a requirement to convert word attachment to PDF stream and embed the converted PDF stream into a different pdf document.
And i have tried the below mentioned code to convert word to PDF.
Hi,
I have a requirement to convert word attachment to PDF stream and embed the converted PDF stream into a different pdf document.
And i have tried the below mentioned code to convert word to PDF.
try {
com.lowagie.text.Document document = new com.lowagie.text.Document();
java.io.ByteArrayInputStream bais = new java.io.ByteArrayInputStream(strLine.getBytes());
org.apache.poi.poifs.filesystem.POIFSFileSystem fs = new org.apache.poi.poifs.filesystem.POIFSFileSystem(bais);
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
org.apache.poi.hwpf.HWPFDocument doc=new org.apache.poi.hwpf.HWPFDocument(fs);
org.apache.poi.hwpf.extractor.WordExtractor we=new org.apache.poi.hwpf.extractor.WordExtractor(doc);
com.lowagie.text.pdf.PdfWriter writer = com.lowagie.text.pdf.PdfWriter.getInstance(document,baos);
org.apache.poi.hwpf.usermodel.Range range=doc.getRange();
document.open();
writer.setPageEmpty(true);
document.newPage();
writer.setPageEmpty(true);
String[] paragraphs = we.getParagraphText();
String output="";
for (String para : paragraphs) {
output = output + "\n" + para.toString() + "\n";
}
String[] splitter = output.split("\\n");
for (int i = 0; i < splitter.length; i++) {
com.lowagie.text.Chunk chunk = new com.lowagie.text.Chunk(splitter[i]);
document.add(chunk);
com.lowagie.text.Paragraph par=new com.lowagie.text.Paragraph();
par.add("");
document.add(par);
}
document.close();
String pdfStream = Base64Util.encodeToString(baos.toByteArray());
strLine=pdfStream;
baos.flush();
But the PDF generated with resultant stream is getting corrupted or not able to open the PDF document, Could someone please advise on this?