This tutorial explains how to insert PDF into Word as image in Java. It guides you on performing this task through instructions to set the environment, a list of programming steps, and sample code demonstrating how to insert a PDF as an image into Word in Java. A few variations in the code will also be discussed to control the conversion process and create a custom output file.
Steps to Insert PDF Image into Word in Java
- Set the environment to use Aspose.Total for Java to develop the application
- Apply a license for the PDF and Word libraries
- Load the source PDF file into the Document class object from the aspose.pdf library
- Create a new empty Word file using the Document class object from the aspose.words library
- Set the output image resolution and other parameters for the rendering of the PDF pages to PNG
- Iterate through each page in the PDF, convert it to an image, and save it in the Word document
- Insert a page break after inserting each image in the Word file except the last page
- Save the resultant Word file on the disk
These steps describe how to insert a PDF picture into Word in Java. Begin by loading the source PDF file, creating an empty Word file, and iterating through each page. Convert each PDF page to a PNG image using a defined resolution and other rendering parameters, save each image to a new page in the Word file, and save the resultant Word file.
Code to Insert PDF as Image into Word in Java
This code demonstrates how to insert PDF image into Word in Java. If you want to match the size of the inserted image to the source PDF page, you may use PageSetup.setPageWidth or setPageHeight so the output image does not look stretched or cropped. For controlling the image quality and file size, you may try CompressionType options exposed via the image device.
This article explains the process to render text-based PDF pages as image in a Word file. To convert a scanned PDF to Word, refer to the article Convert Scanned PDF to Word using Java.