Question

19
Views
HARISH KUMAR BODDU (BHKUMAR10)
VIRTUSA

VIRTUSA
US
BHKUMAR10 Member since 2010 3 posts
VIRTUSA
Posted: March 31, 2021
Last activity: March 31, 2021
Posted: 31 Mar 2021 14:56 EDT
Last activity: 31 Mar 2021 16:00 EDT

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?

 

***Edited by Moderator Marissa to change type from Pega Academy to Product, update Platform Capability tags****
Pega Platform Case Management Healthcare and Life Sciences