溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用Aspose.words在Java中將Word和圖像轉(zhuǎn)換為PDF

發(fā)布時(shí)間:2021-11-30 14:33:59 來源:億速云 閱讀:389 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“怎么使用Aspose.words在Java中將Word和圖像轉(zhuǎn)換為PDF”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么使用Aspose.words在Java中將Word和圖像轉(zhuǎn)換為PDF”吧!

將Word文檔轉(zhuǎn)換為PDF

從Word到PDF的轉(zhuǎn)換是一個(gè)相當(dāng)復(fù)雜的過程,需要幾個(gè)計(jì)算階段。Aspose.Words布局引擎模仿了Microsoft Word的頁面布局引擎的工作方式,使PDF輸出文檔看起來與Microsoft Word中所看到的盡可能接近。使用Aspose.Words,您可以通過編程方式將文檔從DOC或DOCX格式轉(zhuǎn)換為PDF,而無需使用Microsoft Office。本文介紹了如何執(zhí)行此轉(zhuǎn)換。

請(qǐng)注意,文檔中的頁數(shù)會(huì)影響轉(zhuǎn)換時(shí)間。

將DOC或DOCX轉(zhuǎn)換為PDF

在Aspose.Words中從DOC或DOCX文檔格式轉(zhuǎn)換為PDF格式非常容易,只需兩行代碼即可完成:

  • 通過使用擴(kuò)展名指定文檔名稱,使用其構(gòu)造函數(shù)之一將文檔加載到 Document對(duì)象中。

  • 調(diào)用Document對(duì)象上的Document.Save方法 之一,并通過輸入擴(kuò)展名為“ .PDF”的文件名將所需的輸出格式指定為PDF。

下面的代碼示例演示如何使用Save方法將文檔從DOCX轉(zhuǎn)換為PDF:

// 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);

請(qǐng)注意,使用相同的技術(shù),可以將任何流程布局格式的文檔轉(zhuǎn)換為PDF格式。

轉(zhuǎn)換為各種PDF標(biāo)準(zhǔn)

Aspose.Words提供 PdfCompliace 枚舉以支持將DOC或DOCX轉(zhuǎn)換為各種PDF格式標(biāo)準(zhǔn)(例如PDF 1.7,PDF 1.5等)。下面的代碼示例演示如何將文檔轉(zhuǎn)換為PDF使用1.7 PdfSaveOptions 與符合PDF17:

// The path to the documents directory.
Document originalDoc = new Document(dataDir + "Document.docx");

// Provide PDFSaveOption compliance to PDF17
// or just convert without SaveOptions
PdfSaveOptions pso = new PdfSaveOptions();
pso.setCompliance(PdfCompliance.PDF_17);

originalDoc.save(dataDir + "Output.pdf", pso);
將圖像轉(zhuǎn)換為PDF

轉(zhuǎn)換為PDF不受Microsoft Word文檔格式的限制。Aspose.Words支持的任何格式,包括以編程方式創(chuàng)建的格式,都可以轉(zhuǎn)換為PDF。例如,我們可以將單頁圖像(例如JPEG,PNG,BMP,EMF或WMF)以及多頁圖像(例如TIFF和GIF)轉(zhuǎn)換為PDF。

下面的代碼示例演示如何將JPEG和TIFF圖像轉(zhuǎn)換為PDF:

//將指定格式的圖像轉(zhuǎn)換為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);
 }

到此,相信大家對(duì)“怎么使用Aspose.words在Java中將Word和圖像轉(zhuǎn)換為PDF”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI