翻譯|使用教程|編輯:李顯亮|2019-12-31 09:31:53.713|閱讀 380 次
概述:在本文中,我們將探索并演示Aspose.PDF for .NET API的強(qiáng)大轉(zhuǎn)換功能,將PDF轉(zhuǎn)換為HTML后,如何嵌入所有資源(字體、圖像、CSS等)到單個(gè)HTML中。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
PDF是當(dāng)今最流行的文檔格式之一,各種應(yīng)用程序?qū)⑵溆米髯?終輸出。由于支持多種數(shù)據(jù)類型和可移植性,因此它是創(chuàng)建和共享內(nèi)容的首選格式。作為對(duì)開(kāi)發(fā)文檔管理應(yīng)用程序感興趣的.NET應(yīng)用程序開(kāi)發(fā)人員,可能希望嵌入處理功能,以讀取PDF文檔并將其轉(zhuǎn)換為其他文件格式,例如HTML。
Aspose.PDF for .NET是一種高級(jí)PDF處理和解析API,用于在跨平臺(tái)應(yīng)用程序中執(zhí)行文檔管理和操作任務(wù)。API可以輕松用于生成,修改,轉(zhuǎn)換,渲染,保護(hù)和打印PDF文檔,而無(wú)需使用Adobe Acrobat。
在本文中,我們將探索并演示Aspose.PDF for .NET API的強(qiáng)大轉(zhuǎn)換功能,將PDF轉(zhuǎn)換為HTML后,如何嵌入所有資源(字體、圖像、CSS等)到單個(gè)HTML中。
點(diǎn)擊下載最新版Aspose.PDF for .NET
PDF到HTML的轉(zhuǎn)換是Aspose.PDF for Java的最強(qiáng)大功能之一,因?yàn)樗试S生成高保真HTML文檔。轉(zhuǎn)換期間,PDF文件中的所有資源(字體,圖像和CSS)都保存在創(chuàng)建輸出HTML的同一目錄中的單獨(dú)文件夾中。但是,用戶需要新的功能,可將PDF文件轉(zhuǎn)換為HTML格式并生成嵌入了所有資源的單個(gè)HTML文件。此功能的實(shí)現(xiàn)符合數(shù)據(jù)URI方案。為此,將值EmbedAllIntoHtml添加到HtmlSaveOptions.PartsEmbeddingModes枚舉中。以下代碼段顯示了如何使用它。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat(); // Load source PDF file Document doc = new Document(dataDir + "input.pdf"); // Instantiate HTML Save options object HtmlSaveOptions newOptions = new HtmlSaveOptions(); // Enable option to embed all resources inside the HTML newOptions.PartsEmbeddingMode = HtmlSaveOptions.PartsEmbeddingModes.EmbedAllIntoHtml; // This is just optimization for IE and can be omitted newOptions.LettersPositioningMethod = HtmlSaveOptions.LettersPositioningMethods.UseEmUnitsAndCompensationOfRoundingErrorsInCss; newOptions.RasterImagesSavingMode = HtmlSaveOptions.RasterImagesSavingModes.AsEmbeddedPartsOfPngPageBackground; newOptions.FontSavingMode = HtmlSaveOptions.FontSavingModes.SaveInAllFormats; // Output file path string outHtmlFile = "SingleHTML_out.html"; doc.Save(outHtmlFile, newOptions);
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();
// Select desirable page size
float newPageWidth = 400f;
float newPageHeight = 400f;
// Tune PdfPageEditor class 
Aspose.Pdf.Facades.PdfPageEditor pdfEditor = new Aspose.Pdf.Facades.PdfPageEditor();
// Bind source PDF file
pdfEditor.BindPdf(dataDir + "input.pdf");
// Set the page dimensions 
pdfEditor.PageSize = new Aspose.Pdf.PageSize(newPageWidth, newPageHeight);
// Set vertical alignment for page as center aligned
pdfEditor.VerticalAlignmentType = Aspose.Pdf.VerticalAlignment.Center;
// Set Horizontal alignment for page as center aligned
pdfEditor.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
// This scales page content to fit width,
// Comment it out or set Zoom to 1.0F if You don't want to scale
// Content and only want to change page's size (i.e. crop it)
float zoom = Math.Min((float)newPageWidth / (float)pdfEditor.Document.Pages[1].Rect.Width,
                    (float)newPageHeight / (float)pdfEditor.Document.Pages[1].Rect.Height);
pdfEditor.Zoom = zoom;// (float)595;
// Create stream object to hold file with updated dimensions
MemoryStream output = new MemoryStream();
// Save file to stream object
pdfEditor.Save(output);
// Then reload scaled document and save it to HTML
Document exportDoc = new Document(output);
HtmlSaveOptions htmlOptions = new HtmlSaveOptions();
// This code shows page boreder in result - sometimes it comes in handy to see borders
SaveOptions.BorderPartStyle borderStyle = new SaveOptions.BorderPartStyle();
borderStyle.LineType = SaveOptions.HtmlBorderLineType.Dotted;
borderStyle.Color = System.Drawing.Color.Gray;
htmlOptions.PageBorderIfAny = new SaveOptions.BorderInfo(borderStyle);
// Conversion to HTML itself
exportDoc.Save(dataDir + "SetOutputFileDimensions_out.html", htmlOptions);
// Close the stream object
output.Close();
還想要更多嗎?您可以點(diǎn)擊閱讀【2019 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問(wèn)或需求,請(qǐng)隨時(shí)加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。					本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn