Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Aspose.words to convert Word and images to PDF in Java

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use Aspose.words to convert Word and images into PDF in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Aspose.words to convert Word and images into PDF in Java.

Convert Word documents to PDF

The conversion from Word to PDF is a rather complex process that requires several stages of computation. The Aspose.Words layout engine mimics the way Microsoft Word's page layout engine works, making the PDF output document look as close as possible to what you see in Microsoft Word. With Aspose.Words, you can programmatically convert documents from DOC or DOCX format to PDF without using Microsoft Office. This article describes how to perform this transformation.

Note that the number of pages in the document affects the conversion time.

Convert DOC or DOCX to PDF

It is very easy to convert from DOC or DOCX document format to PDF format in Aspose.Words, which takes only two lines of code:

Load the document into the Document object using one of its constructors by specifying the document name with the extension.

Call one of the Document.Save methods on the Document object and specify the desired output format as PDF by entering a file name with the extension ".PDF".

The following code example shows how to convert a document from DOCX to PDF using the Save method:

/ / Load the document from disk.Document doc = new Document (dataDir + "Template.doc"); / / Save the document in PDF format.dataDir = dataDir + "output.pdf"; doc.save (dataDir)

Note that using the same technique, documents in any process layout format can be converted to PDF format.

Convert to various PDF standards

Aspose.Words provides PdfCompliace enumerations to support the conversion of DOC or DOCX to various PDF format standards (for example, PDF 1.7, PDF 1.5, etc.). The following code example shows how to convert a document to PDF using 1.7 PdfSaveOptions and conforming to PDF17:

/ / The path to the documents directory.Document originalDoc = new Document (dataDir + "Document.docx"); / / Provide PDFSaveOption compliance to PDF17// or just convert without SaveOptionsPdfSaveOptions pso = new PdfSaveOptions (); pso.setCompliance (PdfCompliance.PDF_17); originalDoc.save (dataDir + "Output.pdf", pso); convert the image to PDF

Conversion to PDF is not limited by Microsoft Word document format. Any format supported by Aspose.Words, including formats created programmatically, can be converted to PDF. For example, we can convert single-page images (such as JPEG,PNG,BMP,EMF or WMF) and multi-page images (such as TIFF and GIF) to PDF.

The following code example shows how to convert JPEG and TIFF images to PDF:

/ / converts the image in the specified format to PDF. ConvertImageToPDF (dataDir + "Test.jpg", dataDir + "TestJpg_out.pdf"); ConvertImageToPDF (dataDir + "Test.tiff", dataDir + "TestTif_out.pdf");

* Converts an image to PDF using Aspose.Words for Java.** @ param inputFileName File name of input image file.* @ param outputFileName Output PDF file name.* @ throws Exception*/public static void ConvertImageToPDF (String inputFileName, String outputFileName) throws Exception {/ / Create Aspose.Words.Document and DocumentBuilder.// The builder makes it simple to add content to the document.Document doc = new Document (); DocumentBuilder builder = new DocumentBuilder (doc) / / Load images from the disk using the appropriate reader.// The file formats that can be loaded depends on the image readers available on the machine.ImageInputStream iis = ImageIO.createImageInputStream (new File (inputFileName)); ImageReader reader = ImageIO.getImageReaders (iis). Next (); reader.setInput (iis, false); / / Get the number of frames in the image.int framesCount = reader.getNumImages (true); / / Loop through all frames.for (int frameIdx = 0; frameIdx < framesCount) FrameIdx++) {/ / Insert a section break before each new page, in case of a multi-frame image.if (frameIdx! = 0) builder.insertBreak (BreakType.SECTION_BREAK_NEW_PAGE); / / Select active frame.BufferedImage image = reader.read (frameIdx); / / We want the size of the page to be the same as the size of the image.// Convert pixels to points to size the page to the actual image size.PageSetup ps = builder.getPageSetup (); ps.setPageWidth (ConvertUtil.pixelToPoint (image.getWidth () Ps.setPageHeight (ConvertUtil.pixelToPoint (image.getHeight (); / / Insert the image into the document and position it at the top left corner of the page.builder.insertImage (image,RelativeHorizontalPosition.PAGE,0,RelativeVerticalPosition.PAGE,0,ps.getPageWidth (), ps.getPageHeight (), WrapType.NONE);} if (iis! = null) {iis.close (); reader.dispose ();} doc.save (outputFileName);}

At this point, I believe you have a deeper understanding of "how to use Aspose.words to convert Word and images into PDF in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report