本篇內(nèi)容主要講解“怎么使用Aspose.words在Java中將Word和圖像轉(zhuǎn)換為PDF”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么使用Aspose.words在Java中將Word和圖像轉(zhuǎn)換為PDF”吧!
創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營銷推廣、網(wǎng)站重做改版、貴州網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5建站、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為貴州等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
從Word到PDF的轉(zhuǎn)換是一個相當(dāng)復(fù)雜的過程,需要幾個計算階段。Aspose.Words布局引擎模仿了Microsoft Word的頁面布局引擎的工作方式,使PDF輸出文檔看起來與Microsoft Word中所看到的盡可能接近。使用Aspose.Words,您可以通過編程方式將文檔從DOC或DOCX格式轉(zhuǎn)換為PDF,而無需使用Microsoft Office。本文介紹了如何執(zhí)行此轉(zhuǎn)換。
請注意,文檔中的頁數(shù)會影響轉(zhuǎn)換時間。
將DOC或DOCX轉(zhuǎn)換為PDF
在Aspose.Words中從DOC或DOCX文檔格式轉(zhuǎn)換為PDF格式非常容易,只需兩行代碼即可完成:
通過使用擴(kuò)展名指定文檔名稱,使用其構(gòu)造函數(shù)之一將文檔加載到 Document對象中。
調(diào)用Document對象上的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);
請注意,使用相同的技術(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不受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); | |
} |
到此,相信大家對“怎么使用Aspose.words在Java中將Word和圖像轉(zhuǎn)換為PDF”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
文章標(biāo)題:怎么使用Aspose.words在Java中將Word和圖像轉(zhuǎn)換為PDF
URL鏈接:http://chinadenli.net/article34/jiiese.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、ChatGPT、品牌網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)