2

I'm looking for the best way to convert docx file to pdf in Java, this is what I've tried:

File wordFile = new File("wordFile.docx"), target = new File("target.pdf");
IConverter converter;
Future<Boolean> conversion = converter.convert(wordFile)
.as(DocumentType.MS_WORD)
.to(target)
.as(DocumentType.PDF)
.prioritizeWith(1000) // optional
.schedule();

The problem is that I cannot find IConverter class in my program...

Matteo Baldi
  • 5,613
  • 10
  • 39
  • 51
mohamed.bc
  • 47
  • 4
  • 10

2 Answers2

3

You're clearly triying to use documents4j, so I suggest you to read carefully the documentation there. It seems you have not included documents4j libraries in your project (you need at least the documents4j-api dependency but I suggest you to give a look at documents4j-local).

You can add the required lib directly with Maven (just add the dependency below) OR get directly the jar.

<dependency> 
<groupId>com.documents4j</groupId> 
<artifactId>documents4j-api</artifactId> 
<version>1.0.2</version> 
<type>pom</type> 
</dependency>
Matteo Baldi
  • 5,613
  • 10
  • 39
  • 51
  • can you give me the link to download documents4j jar file ? – mohamed.bc Nov 30 '16 at 16:23
  • what about this : "DocumentType.MS_WORD" and "DocumentType.PDF" it doesn't work – mohamed.bc Nov 30 '16 at 16:35
  • 1
    They are in the document4j api: package com.documents4j.api... public static final DocumentType MS_WORD = new DocumentType(Value.APPLICATION, Value.WORD_ANY); public static final DocumentType PDF = new DocumentType(Value.APPLICATION, Value.PDF); – Matteo Baldi Nov 30 '16 at 16:36
  • Is it possible to concatenate two .docx files ?? – mohamed.bc Dec 01 '16 at 09:39
  • That's out of scope for the current question, please CHECK the documentation in the link I've provided. If you don't find anything there, ask a new question on stack ("How to concatenate multiple docx file with documents4j") – Matteo Baldi Dec 01 '16 at 09:41
  • i know that, i want to change my work to concatenate two .docx files – mohamed.bc Dec 01 '16 at 10:43
0

I would like you to try this code as it gives me PDF converter output file i am not sure about accuracy .

InputStream is = new FileInputStream(new File("your Docx PAth"));
            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("Your OutPut PDF path"));
            conversion.output(out, pdfSettings);
            System.out.println("DONE!!");

Hopefully this solution would be fine for your problem. Thank you!!

KishanCS
  • 1,357
  • 1
  • 19
  • 38