Java applications convert Word (DOC / DOCX) documents to PDF

With the help of Spire.Doc, we can easily save the word document as PDF. This sentence introduces how to save Word (.doc..docx) as PDF from the following three directions.

Convert Word document to PDF directly with Java application </ b>


import com.spire.doc.*;
public class toPDF {
    public static void main(String[] args) {
        String inputFile="data/convertedTemplate.docx";
        String outputFile="output/toPDF.pdf";
        //Load sample document
        Document document = new Document();
        document.loadFromFile(inputFile);

        //Save document
        document.saveToFile(outputFile, FileFormat.PDF);
    }
}

Conversion result file of Word document with password encrypted to PDF file </ b>

You can set a password-protected PDF to convert the security of PDF documents.


import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;
import com.spire.pdf.security.*;

public class toPdfWithPassword {
    public static void main(String[] args) {

        String inputFile="data/convertedTemplate.docx";
        String outputFile="output/toPdfWithPassword.pdf";

        //Load sample document
        Document document = new Document();
        document.loadFromFile(inputFile);

        ToPdfParameterList toPdf = new ToPdfParameterList();

        //password setting
        String password = "E-iceblue";
        toPdf.getPdfSecurity().encrypt(password, password, PdfPermissionsFlags.None, PdfEncryptionKeySize.Key_128_Bit);

        //Save document
        document.saveToFile(outputFile, toPdf);
    }
}

Word document when setting image quality </ b>

The size of the output PDF document is clearly larger than the original Word document, especially after converting many Word documents of the images in the document. At the same time, you can set the image quality to set the size of the resulting PDF file. Java's spire .doc provides documentation methods. setjpegquality () sets the quality of JPEG images from 0 to 100. The default set of output image quality is 80% of the original.


import com.spire.doc.*;
public class ShapeAsImage {
    public static void main(String[] args) throws Exception {

        //Load sample document
        Document document = new Document();
        document.loadFromFile("Sample.docx");

        //Set image quality
        document.setJPEGQuality(40);

        ToPdfParameterList pdf = new ToPdfParameterList();
        pdf.setDisableLink(true);

        //Save document
        document.saveToFile("output/ImageQuality.pdf", FileFormat.PDF);
    }
}

Using the free Spire .doc for Java is very easy. Thanks to your reading, I hope it helps.

Recommended Posts