1

I am developing a project which needs a docx file to be converted to pdf. I found same question already posted and used the code which was provided by "Kishan C S". It uses docx4J2.8.1 The code is working fine , pdf is generated but only problem I am facing is that the docx file contains logo.jpg (images header part) which are not converted. Only textual format is converted to pdf. I am posting the code which I have used. Please let me know what how can I solve the problem

P.S: link I referred Convert docx file into PDF with Java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;
import java.util.List;
import org.apache.log4j.Level; 
import org.apache.log4j.LogManager; 
import org.apache.log4j.Logger; 
import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings; 
import org.docx4j.fonts.IdentityPlusMapper; 
import org.docx4j.fonts.Mapper; 
import org.docx4j.fonts.PhysicalFont; 
import org.docx4j.fonts.PhysicalFonts; 
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class DocxConverter {


    public static void main(String[] args) throws FileNotFoundException, Docx4JException, Exception {
        InputStream is = new FileInputStream(new File("D:\\Test\\C_IN0004_AppointmentLetter.docx"));
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
            List sections = wordMLPackage.getDocumentModel().getSections();
            for (int i = 0; i < sections.size(); i++) {
                wordMLPackage.getDocumentModel().getSections().get(i).getPageDimensions();
            }
            Mapper fontMapper = new IdentityPlusMapper();
            PhysicalFont font = PhysicalFonts.getPhysicalFonts().get("Comic Sans MS");//set your desired font 
            fontMapper.getFontMappings().put("Algerian", font);
            wordMLPackage.setFontMapper(fontMapper);
            PdfSettings pdfSettings = new PdfSettings();
            org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
            //To turn off logger
            List<Logger> loggers = Collections.<Logger> list(LogManager.getCurrentLoggers());
            loggers.add(LogManager.getRootLogger());
            for (Logger logger : loggers) {
                logger.setLevel(Level.OFF);
            }
            OutputStream out = new FileOutputStream(new File("D:\\Test\\C_IN0004_AppointmentLetter.pdf"));
            conversion.output(out, pdfSettings);
            System.out.println("DONE!!"); 
    }
}
Community
  • 1
  • 1
Aditya Jadhav
  • 71
  • 1
  • 2
  • 9
  • I highly recommend you using documents4j instead. It works perfect with images, tables.... http://documents4j.com/#/ – AMB Apr 06 '17 at 07:13
  • Hello Aditya, I am using this code from long time the code works well on all kinds of images like jpeg,gif etc... – KishanCS Apr 06 '17 at 07:14
  • what kind of docx file are you using ie is that a generated from ms or custom one – KishanCS Apr 06 '17 at 07:15
  • 1
    http://stackoverflow.com/a/41972327/6032482 -- read this as you can have better knowledge to see the docx file from your perspective and solve your issue – KishanCS Apr 06 '17 at 07:17
  • Hello Kishan,Thanks for your quick reply, I was actually looking for pvt. message you but found no option so posted the question again.. I did mention the code is working fine, but in my case images are not converted. I tried making a new file (in MS Word 2007)which contains only few text and images..but it generates same out put...any idea what I might be missing – Aditya Jadhav Apr 06 '17 at 08:13

0 Answers0