翻譯|使用教程|編輯:黃竹雯|2019-03-06 13:53:53.000|閱讀 596 次
概述:本文主要介紹如何使用Aspose.Words將HTML轉換為PDF、將檔轉換為MHTML和電子郵件等。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.Words無需Microsoft Word也可在任何平臺上滿足Word文檔的一切操作需求。本文將與大家分享如何將word和圖像轉換為PDF。
【下載Aspose.Words for .NET最新試用版】
要將HTML轉換為PDF,只需調用Document.Save方法并指定擴展名為“.PDF”即可。如果要從外部源加載圖像和CSS等,可以使用IResourceSavingCallback。下面的代碼示例演示將HTML轉換為PDF并從外部源加載圖像。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET public class ImageLoadingWithCredentialsHandler : IResourceLoadingCallback { public ImageLoadingWithCredentialsHandler() { mWebClient = new WebClient(); } public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args) { if (args.ResourceType == ResourceType.Image) { Uri uri = new Uri(args.Uri); if (uri.Host == "www.aspose.com") mWebClient.Credentials = new NetworkCredential("User1", "akjdlsfkjs"); else mWebClient.Credentials = new NetworkCredential("SomeOtherUserID", "wiurlnlvs"); // Download the bytes from the location referenced by the URI. byte[] imageBytes = mWebClient.DownloadData(args.Uri); args.SetData(imageBytes); return ResourceLoadingAction.UserProvided; } else { return ResourceLoadingAction.Default; } } private WebClient mWebClient; }
使用Aspose.Words我們可以檢查字體資源是否應該以base 64編碼嵌入到HTML中。默認情況下,該值為false,字體將寫入單獨的文件。如果此選項設置為true,則字體將嵌入到Base64編碼的文檔CSS中。 該屬性僅影響HTML格式,不會影響EPUB和MHTML。這是HtmlSaveOptions.ExportFontResources選項的擴展,ExportFontsAsBase64僅在此屬性設置為true時才有效。下面的示例演示如何使用Base64編碼將字體導出為HTML。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_LoadingAndSaving(); string fileName = "Document.doc"; Document doc = new Document(dataDir + fileName); HtmlSaveOptions saveOptions = new HtmlSaveOptions(); saveOptions.ExportFontResources = true; saveOptions.ExportFontsAsBase64 = true; dataDir = dataDir + "ExportFontsAsBase64_out.html"; doc.Save(dataDir, saveOptions);
使用Aspose.Words,我們可以指定一個文件夾,在將文檔導出為HTML時,可以保存圖像,字體和外部CSS等所有資源。默認值為空字符串。ResourceFolder是指定應寫入所有資源的文件夾的最簡單方法。我們可以使用FontsFolder等單獨的屬性將字體保存到指定的文件夾,使用ImagesFolder將圖像保存到指定的文件夾。
使用ResourceFolderAlias屬性,我們還可以指定用于構造寫入HTML文檔的所有資源的URI的文件夾的名稱,這是指定應如何構造所有資源文件的URI的最簡單方法。可以通過ImagesFolderAlias和FontsFolderAliasproperties分別為圖像和字體指定相同的信息。 但是,CSS沒有單獨的屬性。 FontsFolder,FontsFolderAlias,ImagesFolder,ImagesFolderAlias和CssStyleSheetFileName屬性的行為不會更改。請注意,CssStyleSheetFileName屬性用于指定文件夾名稱和文件名。
指定相對路徑時,FontsFolder和ImagesFolder相對于代碼程序集所在的文件夾,ResourceFolder和CssStyleSheetFileName相對于HTML文檔所在的輸出文件夾。在下面的示例中,ResourceFolder指定此路徑相對于輸出文件夾的相對路徑,其中保存HTML文檔,http://example.com/resources 別名用于構造所有資源的URL。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_LoadingAndSaving(); string fileName = "Document.doc"; Document doc = new Document(dataDir + fileName); HtmlSaveOptions saveOptions = new HtmlSaveOptions(); saveOptions.CssStyleSheetType = CssStyleSheetType.External; saveOptions.ExportFontResources = true; saveOptions.ResourceFolder = dataDir + @"\Resources"; saveOptions.ResourceFolderAlias = "//example.com/resources"; doc.Save(dataDir + "ExportResourcesUsingHtmlSaveOptions.html", saveOptions);
Aspose.Words可以以MHTML(Web Archive)格式保存任何文檔。這使得Aspose.Words和Aspose.Email一起使用可以生成和發送內容豐富的電子郵件。例如,你可以將預定義的DOC,OOXML或RTF文檔加載到Aspose.Words中,用數據填充之后另存為MHTML,然后使用Aspose.Email發送電子郵件。 下面的代碼示例演示了如何使用Aspose.Email將Aspose.Words中的文檔保存為MHTML和電子郵件。
// For complete examples and data files, please go to //github.com/aspose-words/Aspose.Words-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_LoadingAndSaving(); // Load the document into Aspose.Words. Document doc = new Document(dataDir + "Test File (docx).docx"); // Save into a memory stream in MHTML format. Stream stream = new MemoryStream(); doc.Save(stream, SaveFormat.Mhtml); // Rewind the stream to the beginning so Aspose.Email can read it. stream.Position = 0; // Create an Aspose.Network MIME email message from the stream. MailMessage message = MailMessage.Load(stream, new MhtmlLoadOptions()); message.From = "your_from@email.com"; message.To = "your_to@email.com"; message.Subject = "Aspose.Words + Aspose.Email MHTML Test Message"; // Send the message using Aspose.Email SmtpClient client = new SmtpClient(); client.Host = "your_smtp.com"; client.Send(message);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn