翻譯|使用教程|編輯:李顯亮|2021-01-15 09:49:04.160|閱讀 1101 次
概述:HTML通常用于在網頁,Web應用程序和不同平臺上顯示數據和信息。有時可能需要將HTML轉換為JPG,PNG,TIFF,BMP等圖像。本文將介紹如何在Java中將HTML轉換為JPG,PNG等圖像。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
HTML通常用于在網頁,Web應用程序和不同平臺上顯示數據和信息。有時可能需要將HTML轉換為JPG,PNG,TIFF,BMP等圖像。可以在基于Java的應用程序中使用Java語言以編程方式執行轉換。
此外,該API負責將HTML呈現為圖像,無需擔心文件格式的基本細節。只需使用API調用,HTML文件就可以很好地呈現。讓我們詳細探討HTML轉換:
Aspose.HTML for Java API支持使用Java語言進行編輯,處理以及轉換HTML文件。點擊下方按鈕下方使用。
只需幾行代碼即可輕松地將HTML文件轉換為圖像。讓我們通過以下步驟學習HTML到JPG圖像的轉換:
下面的代碼段顯示了如何使用Java代碼將HTML轉換為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圖像格式是另一種流行的圖像類型,由于某些圖像功能,通常會被首選。例如,PNG圖像支持圖片的透明度。您可以按照以下步驟將HTML文件呈現為PNG圖像:
以下代碼說明了如何使用Java以編程方式將HTML轉換為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圖像文件格式之所以流行是因為幾乎在所有系統環境中都得到了廣泛的支持。您可以通過幾個簡單的步驟輕松地將HTML轉換為TIFF圖像:
下面的代碼詳細說明了如何將Java語言中的HTML轉換為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轉換的其他幾種方法和屬性,讓我們探討設置輸出文件的頁面大小,背景顏色等。您可以使用以下附加步驟,輕松地將HTML轉換為BMP圖像:
下面的代碼演示了如何使用Java將HTML文件轉換為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(); } }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn