Conversion

Conversion (20)

Java: Convert Word to HTML

2021-10-21 08:50:06 Written by support iceblue

An HTML (Hyper Text Markup Language) file is a webpage coded in HTML that can be displayed in a Web browser. It is widely used on the Web as most static webpages have an .html extension. In some cases, you need to convert some document formats (such as Word) to HTML. This tutorial will demonstrate how to convert Word to HTML using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.11.0</version>
    </dependency>
</dependencies>
    

Convert Word to HTML

Spire.Doc for Java can easily convert Word to HTML using Document.saveToFile() method. You can find the steps as blow.

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Save the document as an HTML file using Document.saveToFile() method.
  • Java
import com.spire.doc.*;

public class WordToHtml {
    public static void main(String[] args) {
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.docx");

        //Save the document as HTML 
        document.saveToFile("output/toHtml.html", FileFormat.Html);
    }
}

Java: Convert Word to HTML

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Java: Convert Word to TIFF

2021-10-14 08:50:58 Written by support iceblue

TIFF (Tagged Image File Format) is a flexible file format for storing raster graphics images. It's popular and widely supported by image-manipulation, scanning, faxing and word processing applications etc. The ability to store image data in a lossless format makes a TIFF file to be a useful image archive. In some cases, developers may need to convert documents in other format like Word to TIFF. In this article, we will describe how to achieve this task in Java using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.11.0</version>
    </dependency>
</dependencies>
    

Convert Word to TIFF

Spire.Doc for Java provides the Document.saveToTiff() method for converting Word to TIFF. This method accepts a String parameter which specifies the file path of the converted TIFF.

You can follow the steps below to convert a Word document to TIFF format:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Save the document to TIFF using Document.saveToTiff() method.
  • Java
import com.spire.doc.Document;

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

        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Save the document as multi-page TIFF
        document.saveToTiff("toTIFF.tiff");
    }
}

Java: Convert Word to TIFF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

RTF, short for Rich Text Format, is created by Microsoft in 1987 for the purpose of cross-platform document interchange. It is supported by many word processing applications, and the most popular one among them is Microsoft Word. In certain circumstances, you may need to convert an RTF document to Word Doc/Docx format or convert a Word Doc/Docx document to RTF. In this article, you will learn how to accomplish this task using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.11.0</version>
    </dependency>
</dependencies>
    

Convert RTF to Word Doc/Docx

The following are the steps to convert an RTF document to Word Doc/Docx using Spire.Doc for Java:

  • Create a Document instance.
  • Load an RTF document using Document.loadFromFile(String, FileFormat) method.
  • Save the RTF to Doc/Docx format using Document.saveToFile(String, FileFormat) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class RtfToDocDocx {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();

        //Load a RTF document
        document.loadFromFile("Input.rtf", FileFormat.Rtf);

        //Save the document to Doc
        document.saveToFile("toDoc.doc", FileFormat.Doc);

        //Save the document to Docx
        document.saveToFile("toDocx.docx", FileFormat.Docx_2013);
    }
}

Java: Convert RTF to Word Doc/Docx and Vice Versa

Convert Word Doc/Docx to RTF

The steps to convert a Word Doc/Docx document to RTF is very similar with that of the above example:

  • Create a Document instance.
  • Load a Doc or Docx document using Document.loadFromFile(String) method.
  • Save the Doc/Docx document to RTF using Document.saveToFile(String, FileFormat) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class DocDocxToRtf {
    public static void main(String[] args){
        //Create a Document instance
        Document document = new Document();

        //Load a Docx document
        document.loadFromFile("input.docx");

        //Load a Doc document
        //document.loadFromFile("sample.doc");

        //Save the document to RTF
        document.saveToFile("toRTF.rtf", FileFormat.Rtf);
    }
}

Java: Convert RTF to Word Doc/Docx and Vice Versa

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Convert HTML to PDF in Java

2020-09-29 08:40:24 Written by support iceblue

This article will demonstrate how to use Spire.Doc for Java to convert HTML string and HTML file to PDF in Java applications.

HTML String to PDF

import com.spire.doc.*;
import java.io.*;

public class htmlStringToWord {

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

        String inputHtml = "InputHtml.txt";
        //Create a new document
        Document document = new Document();
        //Add a section
        Section sec = document.addSection();

        String htmlText = readTextFromFile(inputHtml);
        //add a paragraph and append html string.
        sec.addParagraph().appendHTML(htmlText);

        //Save to PDF
        document.saveToFile("HTMLstringToPDF.pdf", FileFormat.PDF);
    }
    public static String readTextFromFile(String fileName) throws IOException{
        StringBuffer sb = new StringBuffer();
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String content = null;
        while ((content = br.readLine()) != null) {
            sb.append(content);
        }
        return sb.toString();
    }
}

Java Convert HTML to PDF

Convert HTML file to PDF

import com.spire.doc.*;
import com.spire.doc.documents.XHTMLValidationType;

public class htmlFileToWord {

    public static void main(String[] args) throws Exception {
        // Load the sample HTML file
        Document document = new Document();
        document.loadFromFile("InputHtmlFile.html", FileFormat.Html, XHTMLValidationType.None);

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

Effective screenshot:

Java Convert HTML to PDF

Java: Convert Word to PDF

2024-07-05 07:54:00 Written by support iceblue

Transforming a Microsoft Word file into a portable PDF format is a versatile skill with numerous benefits. PDFs maintain the original document's visual integrity, ensuring your work appears consistent across devices and platforms. Meanwhile, the conversion process offers enhanced security, as PDFs are less vulnerable to unauthorized editing compared to editable Word documents.

This article presents a collection of code examples that showcase how to convert Word documents to PDF format in Java using Spire.Doc for Java. In addition to the core conversion process, the examples also demonstrate how to configure various options to customize the resulting PDF output.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.11.0</version>
    </dependency>
</dependencies>
    

Convert Word to PDF in Java

Converting a Word document to PDF without specifying any customization options can be achieved by first loading the document using the Document.loadFromFile() method, and then saving it as a PDF file using the Document.saveToFile() method.

The steps to convert Word to PDF using Java are as follows.

  • Create a Document object.
  • Load a Word document using Document.loadFromFile() method.
  • Save the document to PDF using Doucment.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class ConvertWordToPdf {

    public static void main(String[] args) {

        // Create a Document object
        Document doc = new Document();

        // Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");

        // Save the document to PDF
        doc.saveToFile("ToPDF.pdf", FileFormat.PDF);

        // Dispose resources
        doc.dispose();
    }
}

Convert Word to PDF/A in Java

Spire.Doc for Java offers the ToPdfParameterList class to configure options for the Word-to-PDF conversion. For example, you can set the conformance level as Pdf/A-1a. To apply these customized settings, pass the ToPdfParameterList object as a parameter to the Document.saveToFile() method.

The steps to convert Word to PDF/A using Java are as follows.

  • Create a Document object.
  • Load a Word document from a given file path.
  • Create a ToPdfParameterList object, which is used to specify the conversion options.
  • Set the conformance level as Pdf_A_1_A using ToPdfParameterList.setPdfConformanceLevel() method.
  • Convert the Word document to PDF/A using Doucment.saveToFile(String fileName, ToPdfParameterList paramList) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;
import com.spire.pdf.PdfConformanceLevel;

public class ConvertWordToPdfa {

    public static void main(String[] args) {

        // Create a Document object
        Document doc = new Document();

        // Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");

        // Create a ToPdfParameterList object
        ToPdfParameterList parameters  = new ToPdfParameterList();

        // Set the conformance level for PDF
        parameters.setPdfConformanceLevel(PdfConformanceLevel.Pdf_A_1_A);

        // Save the document to a PDF file
        doc.saveToFile("ToPdfA.pdf", parameters);

        // Dispose resources
        doc.dispose();
    }
}

Convert Word to Password-Protected PDF in Java

To encrypt the generated PDF document with a password, you can use the ToPdfParameterList.getPdfSecurity().encrypt() method. By passing the ToPdfParameterList object as a parameter to the Document.saveToFile() method, these encryption settings will be applied during the saving process.

The steps to convert a Word document to an encrypted PDF using Java are as follows:

  • Create a Document object.
  • Load a Word document from a given file path.
  • Create a ToPdfParameterList object, which is used to specify the conversion options.
  • Set an open password and a permission password for the generated PDF document using ToPdfParameterList.PdfSecurity.Encrypt() method.
  • Convert the Word document to a password-protected PDF file using Doucment.saveToFile(String fileName, ToPdfParameterList paramList) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

public class ConvertWordToPasswordProtectedPdf {

    public static void main(String[] args) {

        // Create a Document object
        Document doc = new Document();

        // Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");

        // Create a ToPdfParameterList object
        ToPdfParameterList parameters = new ToPdfParameterList();

        // Set open password and permission password for PDF
        parameters.getPdfSecurity().encrypt("openPsd", "permissionPsd", PdfPermissionsFlags.None, PdfEncryptionKeySize.Key_256_Bit);

        // Save the document to PDF
        doc.saveToFile("PasswordProtected.pdf", parameters);

        // Dispose resources
        doc.dispose();
    }
}

Convert a Specific Section in Word to PDF in Java

Spire.Doc's Section.deepClone() method allows you to create a copy of a certain section, and the SectionCollection.add() method enables you to add the copied section to the section collection of another document. Using these two methods, you can easily create a document containing the desired section from the source document.

To convert a specific section of a Word document to a PDF file, follow these steps.

  • Create a Document object.
  • Load a Word document from a given file path.
  • Create another Document object for holding one section from the source document.
  • Create a copy of a certain section of the source document using Section.deepClone() method.
  • Add the copy to the new document using Document.getSections.Add() method.
  • Save the new Word document to PDF using Doucment.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;

public class ConvertSectionToPdf {

    public static void main(String[] args) {

        // Create a Document object
        Document doc = new Document();

        // Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");

        // Get a specific section of the document
        Section section = doc.getSections().get(1);

        // Create a new document object
        Document newDoc = new Document();

        // Clone the default style to the new document
        doc.cloneDefaultStyleTo(newDoc);

        // Clone the section to the new document
        newDoc.getSections().add(section.deepClone());

        // Save the new document to PDF
        newDoc.saveToFile("SectionToPDF.pdf", FileFormat.PDF);

        // Dispose resources
        doc.dispose();
        newDoc.dispose();
    }
}

Change Page Size when Converting Word to PDF in Java

The PageSetup.setPageSize() method provides a means to change the page size of a Word document, allowing you to select from standard paper sizes or define a custom size. This page configuration will be maintained during the conversion from Word to PDF.

The steps to change page size while converting Word to PDF using Java are as follows.

  • Create a Document object.
  • Load a Word document from a given file path.
  • Iterate through the sections in the document, and change the page size of each section to a standard paper size or a custom size using Section.getPageSetup().setPageSize() method.
  • Convert the Word document to PDF using Doucment.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.PageSize;

import java.awt.*;
import java.awt.geom.Dimension2D;

public class ChangePageSizeDuringConversion {

    public static void main(String[] args) {

        // Create a Document object
        Document doc = new Document();

        // Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");

        // Iterate through the sections in the document
        for (Object section: doc.getSections()
             ) {

            // Change the page size of each section to a standard page size
            ((Section)section).getPageSetup().setPageSize(PageSize.B5);

            // Change the page size of each section to a custom size
            // Dimension2D dimension2D = new Dimension();
            // dimension2D.setSize(500, 700);
            // ((Section)section).getPageSetup().setPageSize(dimension2D);
        }

        // Save the document to PDF
        doc.saveToFile("ChangePageSize.pdf", FileFormat.PDF);

        // Dispose resources
        doc.dispose();
    }
}

Set Image Quality when Converting Word to PDF in Java

With Spire.Doc for Java, you can control the image quality within the document by adjusting the value passed to the Document.setJPEGQuality() method. For example, setting the value to 50 will reduce the image quality to 50% of its original state.

The steps to set image quality while converting Word to PDF using Java are as follows.

  • Create a Document object.
  • Load a Word document for a given file path.
  • Set the image quality using Document.setJPEGQuality() method.
  • Convert the document to PDF using Doucment.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class SetImageQualityDuringConversion {

    public static void main(String[] args) {
        
        // Create a Document object
        Document doc = new Document();

        // Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");

        // Set the image quality to 50% of the original quality
        doc.setJPEGQuality(50);

        // Preserve original image quality
        // doc.setJPEGQuality(100);

        // Save the document to PDF
        doc.saveToFile("SetImageQuality.pdf", FileFormat.PDF);

        // Dispose resources
        doc.dispose();
    }
}

Embed Fonts when Converting Word to PDF in Java

To embed all fonts used in the Word document in the generated PDF document, you can use the ToPdfParameterList.isEmbeddedAllFonts() method. Alternatively, if you prefer to specify a specific list of fonts to embed, you can use the ToPdfParameterList.setEmbeddedFontNameList() method.

The following are the steps to embed fonts while converting Word to PDF using Java.

  • Create a Document object.
  • Load a Word document from a given file path.
  • Create a ToPdfParameterList object, which is used to specify the conversion options.
  • Embed all fonts in the generated PDF by passing true to the ToPdfParameterList.isEmbeddedAllFonts() method.
  • Convert the Word document to PDF with fonts embedded using Doucment.saveToFile(String fileName, ToPdfParameterList paramList) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.ToPdfParameterList;

public class EmbedFontsDuringConversion {

    public static void main(String[] args) {

        // Create a Document object
        Document doc = new Document();

        // Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");

        // Create a ToPdfParameterList object
        ToPdfParameterList parameters = new ToPdfParameterList();

        // Embed all the fonts used in Word in the generated PDF
        parameters.isEmbeddedAllFonts(true);

        // Save the document to PDF
        doc.saveToFile("EmbedFonts.pdf", FileFormat.PDF);

        // Dispose resources
        doc.dispose();
    }
}

Create Bookmarks when Converting Word to PDF in Java

While converting a Word document to PDF using Spire.Doc for Java, you can opt to automatically generate bookmarks based on existing bookmarks or headings. This can be achieved by passing true to either ToPdfParameterList.setCreateWordsBookmarks() method or ToPdfParameterList.setCreateWordBookmarksUsingHeadings() method.

The steps to create bookmarks while converting Word to PDF using Java are as follows.

  • Create a Document object.
  • Load a Word document from a given file path.
  • Create a ToPdfParameterList object, which is used to specify the conversion options.
  • Generate bookmarks in PDF based on the existing bookmarks of the Word document by passing true to ToPdfParameterList.setCreateWordsBookmarks() method.
  • Convert the Word document to PDF using Doucment.SaveToFile(String fileName, ToPdfParameterList paramList) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;

public class GenerateBookmarksDuringConversion {

    public static void main(String[] args) {

        // Create a Document object
        Document doc = new Document();

        // Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");

        // Create a ToPdfParameterList object
        ToPdfParameterList parameters = new ToPdfParameterList();

        // Create bookmarks in PDF from existing bookmarks in Word
        parameters.setCreateWordBookmarks(true);

        // Create bookmarks from Word headings
        // parameters.setCreateWordBookmarksUsingHeadings(true);

        // Save the document to PDF
        doc.saveToFile("CreateBookmarks.pdf", parameters);

        // Dispose resources
        doc.dispose();
    }
}

Disable Hyperlinks when Converting Word to PDF in Java

To prevent hyperlinks from being included during the Word-to-PDF conversion, you can do so by passing true to the ToPdfParameterList.setDisableLink() method.

The detailed steps for this process are outlined below.

  • Create a Document object.
  • Load a Word document from a given file path.
  • Create a ToPdfParameterList object, which is used to specify the conversion options.
  • Disable hyperlinks by setting the ToPdfParameterList.setDisableLink() method to true.
  • Convert the Word document to PDF using Doucment.saveToFile(String fileName, ToPdfParameterList paramList) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.ToPdfParameterList;

public class DisableHyperlinksDuringConversion {

    public static void main(String[] args) {

        // Create a Document object
        Document doc = new Document();

        // Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx");

        // Create a ToPdfParameterList object
        ToPdfParameterList parameters = new ToPdfParameterList();

        // Disable hyperlinks
        parameters.setDisableLink(true);

        // Save the document to PDF
        doc.saveToFile("DisableHyperlinks.pdf", parameters);

        // Dispose resources
        doc.dispose();
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

There are many reasons why you might need to convert Word documents to images. For example, a lot of devices can open and display images directly without any special software, and when images are transmitted their content is difficult to be tampered with. In this article, you will learn how to convert Word to popular image formats such as JPG, PNG and SVG using Spire.Doc for Java.

Install Spire.Doc for Java

First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>12.11.0</version>
    </dependency>
</dependencies>
    

Convert Word to JPG in Java

Spire.Doc for Java offers the Document.saveToImages() method to convert a whole Word document into individual BufferedImage images. Then, each BufferedImage can be saved as a BMP, EMF, JPEG, PNG, GIF, or WMF file. The following are the steps to convert Word to JPG using this library.

  • Create a Document object.
  • Load a Word document using Document.loadFromFile() method.
  • Convert the document to BufferedImage images using Document.saveToImages() method.
  • Loop through the image collection to get the specific one.
  • Re-write the image with different color space.
  • Write the BufferedImage to a JPG file.
  • Java
import com.spire.doc.Document;
import com.spire.doc.documents.ImageType;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ConvertWordToJPG {

    public static void main(String[] args) throws IOException {

        //Create a Document object
        Document doc = new Document();

        //Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\ConvertTemplate.docx");

        //Convert the whole document into individual buffered images
        BufferedImage[] images = doc.saveToImages(ImageType.Bitmap);

        //Loop through the images
        for (int i = 0; i < images.length; i++) {

            //Get the specific image
            BufferedImage image = images[i];

            //Re-write the image with a different color space
            BufferedImage newImg = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
            newImg.getGraphics().drawImage(image, 0, 0, null);

            //Write to a JPG file
            File file = new File("C:\\Users\\Administrator\\Desktop\\Images\\" + String.format(("Image-%d.jpg"), i));
            ImageIO.write(newImg, "JPEG", file);
        }
    }
}

Convert Word to SVG in Java

Using Spire.Doc for Java, you can save a Word document as a list of byte arrays. Each byte array can then be written as a SVG file. The detailed steps to convert Word to SVG are as follows.

  • Create a Document object.
  • Load a Word file using Document.loadFromFile() method.
  • Save the document as a list of byte arrays using Document.saveToSVG() method.
  • Loop through the items in the list to get a specific byte array.
  • Write the byte array to a SVG file.
  • Java
import com.spire.doc.Document;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class ConvertWordToSVG {

    public static void main(String[] args) throws IOException {

        //Create a Document object
        Document doc = new Document();

        //Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\ConvertTemplate.docx");

        //Save the document as a list of byte arrays
        List<byte[]> svgBytes = doc.saveToSVG();

        //Loop through the items in the list
        for (int i = 0; i < svgBytes.size(); i++)
        {
            //Get a specific byte array
            byte[] byteArray = svgBytes.get(i);

            //Specify the output file name
            String outputFile = String.format("Image-%d.svg", i);

            //Write the byte array to a SVG file
            try (FileOutputStream stream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\Images\\" + outputFile)) {
                stream.write(byteArray);
            }
        }
    }
}

Convert Word to PNG with Customized Resolution

An image with higher resolution is generally more clear. You can customize the image resolution while converting Word to PNG by following the following steps.

  • Create a Document object.
  • Load a Word file using Document.loadFromFile() method.
  • Convert the document to BufferedImage images with the specified resolution using Document.saveToImages() method.
  • Loop through the image collection to get the specific one and save it as a PNG file.
  • Java
import com.spire.doc.Document;
import com.spire.doc.documents.ImageType;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ConvertWordToPNG {

    public static void main(String[] args) throws IOException {

        //Create a Document object
        Document doc = new Document();

        //Load a Word document
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\ConvertTemplate.docx");

        //Convert the whole document into individual buffered images with customized resolution
        BufferedImage[] images = doc.saveToImages(0, doc.getPageCount(), ImageType.Bitmap, 150, 150);

        //Loop through the images
        for (int i = 0; i < images.length; i++) {

            //Get the specific image
            BufferedImage image = images[i];

            //Write to a PNG file
            File file = new File("C:\\Users\\Administrator\\Desktop\\Images\\" + String.format(("Image-%d.png"), i));
            ImageIO.write(image, "PNG", file);
        }
    }
}

Java: Convert Word to Images (JPG, PNG and SVG)

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Page 2 of 2
page 2