loading...

Java convert word to pdf

eiceblue profile image E-iceblue Product Family ・2 min read

Before sharing documents, word to PDF conversion is a widely used function when we operate the Word document. We could find many online word editor to convert a single or a large number of word documents to PDF. At the same time, the batch conversion from Doc /docx to PDF programmatically needs to be completed from server side to save time and energy.
If you have not use Spire.Doc yet, please get the Spire.Doc for Java from Maven. With the help of Spire.Doc, we could convert the word document with textbox, header/footer, table, TOC, hyperlinks, comments, bookmarks, image and shapes to PDF in high quality. This article will focus on demonstrate how to use Spire.Doc for Java to convert Word (DOC / DOCX) in Java applications from the following three aspects.

  • Convert word document to PDF directly in Java application.
  • Convert Word document to PDF with encrypted password for the resulted PDF file
  • Set Image Quality when convert Word document to PDF

Convert word document to PDF directly in Java application.
To convert the Word document to PDF, Spire.Doc for Java offers a method of document.saveToFile() to save Word(.doc,.docx) as PDF by three lines of codes.

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

        //save the document to a PDF file.
        document.saveToFile(outputFile, FileFormat.PDF);
    }
}

Set Image Quality when convert Word document to PDF
To ensure the security of the resulted PDF, Spire.Doc for Java supports to create password encrypted PDF directly when convert Word to PDF.

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/Sample.docx";
        String outputFile="output/toPdfWithPassword.pdf";

        //create word document
        Document document = new Document();
        document.loadFromFile(inputFile);

        //create a parameter
        ToPdfParameterList toPdf = new ToPdfParameterList();

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

        //save doc file.
        document.saveToFile(outputFile, toPdf);
    }
}

Set Image Quality when convert Word document to PDF
After we convert Word document, especially with a lot of images in the document, the size of output PDF document is obviously larger than the original Word document. At the same time, we could set the image quality to set the size of the resulted PDF file. Spire.Doc for java offers a method of document.setJPEGQuality() to set the JPEG image quality from 0 to 100. The default set of the output image quality is 80% of the original.

import com.spire.doc.*;


public class ImageQuality {
    public static void main(String[] args) throws Exception {

        //Create Word document.
        Document document = new Document();

        //Load the file from disk.
        document.loadFromFile("Sample.docx");

        //Set the output image quality to be 40% of the original image. The default set of the output image quality is 80% of the original.
        document.setJPEGQuality(40);

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

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

Thanks for your reading.

Posted on by:

Discussion

markdown guide