web-dev-qa-db-fra.com

Conversion de docx en pdf en java

J'essaie de convertir un fichier docx qui contient un tableau et des images dans un fichier au format pdf

J'ai cherché partout mais je n'ai pas trouvé de solution adéquate, mais j'ai demandé à donner une solution correcte:

voici ce que j'ai essayé:

import Java.io.File;
import Java.io.FileInputStream;
import Java.io.FileNotFoundException;
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 TestCon {

    public static void main(String[] args) {
        TestCon cwoWord = new TestCon();
        System.out.println("Start");
        cwoWord.ConvertToPDF("D:\\Test.docx", "D:\\Test1.pdf");
    }

    public void ConvertToPDF(String docPath, String pdfPath) {
        try {
            InputStream doc = new FileInputStream(new File(docPath));
            XWPFDocument document = new XWPFDocument(doc);
            PdfOptions options = PdfOptions.create();
            OutputStream out = new FileOutputStream(new File(pdfPath));
            PdfConverter.getInstance().convert(document, out, options);
            System.out.println("Done");
        } catch (FileNotFoundException ex) {
            System.out.println(ex.getMessage());
        } catch (IOException ex) {

            System.out.println(ex.getMessage());
        }
    }

}

Exception:

Exception in thread "main" Java.lang.IllegalAccessError: tried to access method org.Apache.poi.util.POILogger.log(ILjava/lang/Object;)V from class org.Apache.poi.openxml4j.opc.PackageRelationshipCollection
at org.Apache.poi.openxml4j.opc.PackageRelationshipCollection.parseRelationshipsPart(PackageRelationshipCollection.Java:313)
at org.Apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.Java:162)
at org.Apache.poi.openxml4j.opc.PackageRelationshipCollection.<init>(PackageRelationshipCollection.Java:130)
at org.Apache.poi.openxml4j.opc.PackagePart.loadRelationships(PackagePart.Java:559)
at org.Apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.Java:112)
at org.Apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.Java:83)
at org.Apache.poi.openxml4j.opc.PackagePart.<init>(PackagePart.Java:128)
at org.Apache.poi.openxml4j.opc.ZipPackagePart.<init>(ZipPackagePart.Java:78)
at org.Apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.Java:239)
at org.Apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.Java:665)
at org.Apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.Java:274)
at org.Apache.poi.util.PackageHelper.open(PackageHelper.Java:39)
at org.Apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.Java:121)
at test.TestCon.ConvertToPDF(TestCon.Java:31)
at test.TestCon.main(TestCon.Java:25)

Mon exigence est de créer un code Java pour convertir un docx existant en pdf avec un format et un alignement appropriés.

Veuillez suggérer.

Bocaux utilisés: 

 Updated jars

8
user1999397

Il vous manque des bibliothèques.

Je suis capable d'exécuter votre code en ajoutant les bibliothèques suivantes:

 Apache POI 3.15 
 org.Apache.poi.xwpf.converter.core-1.0.6.jar 
 org.Apache.poi.xwpf.converter.pdf-1.0.6.jar 
 fr.opensagres.xdocreport.itext.extension-2.0.0.jar 
 itext-2.1.7.jar 
 ooxml-schemas-1.3.jar 

J'ai converti avec succès un document Word de 6 pages (.docx) avec des tableaux, des images et divers formats.

10
VivekRatanSinha

En plus de VivekRatanSinha answer , j'aimerais publier le code complet et les bocaux requis pour les personnes qui en ont besoin à l'avenir.

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 cwoWord = new WordConvertPDF();
        cwoWord.ConvertToPDF("D:/Test.docx", "D:/Test.pdf");
    }

    public void ConvertToPDF(String docPath, String pdfPath) {
        try {
            InputStream doc = new FileInputStream(new File(docPath));
            XWPFDocument document = new XWPFDocument(doc);
            PdfOptions options = PdfOptions.create();
            OutputStream out = new FileOutputStream(new File(pdfPath));
            PdfConverter.getInstance().convert(document, out, options);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

et JARS:

 required jars

Prendre plaisir :)

15
user1999397

J'ai fait beaucoup de recherches et trouvé que Documents4j est la meilleure api gratuite pour convertir docx en pdf. L'alignement, la police de caractères de tous les documents4j font du bon travail. 

Dépendances Maven:

<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-local</artifactId>
    <version>1.0.3</version>
</dependency>
<dependency>
    <groupId>com.documents4j</groupId>
    <artifactId>documents4j-transformer-msoffice-Word</artifactId>
    <version>1.0.3</version>
</dependency>

Utilisez le code ci-dessous pour convertir docx en pdf.

import Java.io.File;
import Java.io.FileInputStream;
import Java.io.FileOutputStream;
import Java.io.InputStream;
import Java.io.OutputStream;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

public class Document4jApp {

    public static void main(String[] args) {

        File inputWord = new File("Tests.docx");
        File outputFile = new File("Test_out.pdf");
        try  {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            outputStream.close();
            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
0
Shakthifuture

J'utilise ce code.

private byte[] toPdf(ByteArrayOutputStream docx) {
    InputStream isFromFirstData = new ByteArrayInputStream(docx.toByteArray());

    XWPFDocument document = new XWPFDocument(isFromFirstData);
    PdfOptions options = PdfOptions.create();

    //make new file in c:\temp\
    OutputStream out = new FileOutputStream(new File("c:\\tmp\\HelloWord.pdf"));
    PdfConverter.getInstance().convert(document, out, options);

    //return byte array for return in http request.
    ByteArrayOutputStream pdf = new ByteArrayOutputStream();
    PdfConverter.getInstance().convert(document, pdf, options);

    document.write(pdf);
    document.close();
    return pdf.toByteArray();
}
0
Jesús Sánchez

Vous devez ajouter ces dépendances maven

  <dependency>
        <groupId>org.Apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.Apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.Apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>4.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.Apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>4.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.Apache.poi</groupId>
        <artifactId>poi-excelant</artifactId>
        <version>4.0.1</version>
    </dependency>

    <dependency>
        <groupId>org.Apache.poi</groupId>
        <artifactId>poi-examples</artifactId>
        <version>4.0.1</version>
    </dependency>

    <dependency>
        <groupId>fr.opensagres.xdocreport</groupId>
        <artifactId>org.Apache.poi.xwpf.converter.core</artifactId>
        <version>1.0.6</version>
    </dependency>

    <dependency>
        <groupId>fr.opensagres.xdocreport</groupId>
        <artifactId>org.Apache.poi.xwpf.converter.pdf</artifactId>
        <version>1.0.6</version>
    </dependency>
0
iraqi eagle