翻譯|使用教程|編輯:李顯亮|2021-01-15 09:49:04.160|閱讀 1101 次
概述:HTML通常用于在網(wǎng)頁(yè),Web應(yīng)用程序和不同平臺(tái)上顯示數(shù)據(jù)和信息。有時(shí)可能需要將HTML轉(zhuǎn)換為JPG,PNG,TIFF,BMP等圖像。本文將介紹如何在Java中將HTML轉(zhuǎn)換為JPG,PNG等圖像。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
HTML通常用于在網(wǎng)頁(yè),Web應(yīng)用程序和不同平臺(tái)上顯示數(shù)據(jù)和信息。有時(shí)可能需要將HTML轉(zhuǎn)換為JPG,PNG,TIFF,BMP等圖像??梢栽诨贘ava的應(yīng)用程序中使用Java語(yǔ)言以編程方式執(zhí)行轉(zhuǎn)換。
此外,該API負(fù)責(zé)將HTML呈現(xiàn)為圖像,無(wú)需擔(dān)心文件格式的基本細(xì)節(jié)。只需使用API調(diào)用,HTML文件就可以很好地呈現(xiàn)。讓我們?cè)敿?xì)探討HTML轉(zhuǎn)換:
Aspose.HTML for Java API支持使用Java語(yǔ)言進(jìn)行編輯,處理以及轉(zhuǎn)換HTML文件。點(diǎn)擊下方按鈕下方使用。
只需幾行代碼即可輕松地將HTML文件轉(zhuǎn)換為圖像。讓我們通過(guò)以下步驟學(xué)習(xí)HTML到JPG圖像的轉(zhuǎn)換:
下面的代碼段顯示了如何使用Java代碼將HTML轉(zhuǎn)換為JPG圖像:
// Load input HTML document com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html"); try { // Initialize ImageSaveOptions com.aspose.html.saving.ImageSaveOptions options = new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Jpeg); // Convert HTML to output JPG image com.aspose.html.converters.Converter.convertHTML(document, options, "output.jpg"); } finally { if (document != null) { document.dispose(); } }
PNG圖像格式是另一種流行的圖像類型,由于某些圖像功能,通常會(huì)被首選。例如,PNG圖像支持圖片的透明度。您可以按照以下步驟將HTML文件呈現(xiàn)為PNG圖像:
以下代碼說(shuō)明了如何使用Java以編程方式將HTML轉(zhuǎn)換為PNG圖像:
// Initialize an HTML document from the html file com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html"); try { // Initialize ImageSaveOptions com.aspose.html.saving.ImageSaveOptions options = new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Png); // Convert HTML to PNG com.aspose.html.converters.Converter.convertHTML(document, options, "output.png"); } finally { if (document != null) { document.dispose(); } }
TIFF圖像文件格式之所以流行是因?yàn)閹缀踉谒邢到y(tǒng)環(huán)境中都得到了廣泛的支持。您可以通過(guò)幾個(gè)簡(jiǎn)單的步驟輕松地將HTML轉(zhuǎn)換為TIFF圖像:
下面的代碼詳細(xì)說(shuō)明了如何將Java語(yǔ)言中的HTML轉(zhuǎn)換為TIFF圖像:
// Initialize an HTML document from the html file com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(dataDir + "document.html"); try { // Set up the page-size 3000x1000 pixels and change the background color to green com.aspose.html.saving.ImageSaveOptions options = new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Tiff); com.aspose.html.rendering.PageSetup pageSetup = new com.aspose.html.rendering.PageSetup(); com.aspose.html.drawing.Page anyPage = new com.aspose.html.drawing.Page(); anyPage.setSize( new com.aspose.html.drawing.Size( com.aspose.html.drawing.Length.fromPixels(3000), com.aspose.html.drawing.Length.fromPixels(1000) ) ); pageSetup.setAnyPage(anyPage); options.setPageSetup(pageSetup); // Set background color for output image options.setBackgroundColor(com.aspose.html.drawing.Color.getGreen()); // Call the ConvertHTML to convert 'document.html' into tiff image com.aspose.html.converters.Converter.convertHTML(dataDir + "document.html", options, dataDir + "output.tiff"); } finally { if (document != null) { document.dispose(); } }
探索HTML到Image轉(zhuǎn)換的其他幾種方法和屬性,讓我們探討設(shè)置輸出文件的頁(yè)面大小,背景顏色等。您可以使用以下附加步驟,輕松地將HTML轉(zhuǎn)換為BMP圖像:
下面的代碼演示了如何使用Java將HTML文件轉(zhuǎn)換為TIFF圖像:
// Initialize an HTML document from the html file com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html"); try { // Initialize ImageSaveOptions com.aspose.html.saving.ImageSaveOptions options = new com.aspose.html.saving.ImageSaveOptions(com.aspose.html.rendering.image.ImageFormat.Bmp); // Convert HTML to BMP com.aspose.html.converters.Converter.convertHTML(document, options, "output.bmp"); } finally { if (document != null) { document.dispose(); } }
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn