Aspose.Words無需Microsoft Word也可在任何平臺上滿足Word文檔的一切操作需求。本文將與大家分享如何將word和圖像轉換爲PDF。
將HTML轉換爲PDF
要將HTML轉換爲PDF,只需調用Document.Save方法並指定擴展名爲“.PDF”即可。如果要從外部源加載圖像和CSS等,可以使用IResourceSavingCallback。下面的代碼示例演示將HTML轉換爲PDF並從外部源加載圖像。
// For complete examples and data files, please go to https://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; }
在Base64編碼中將字體導出爲HTML
使用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 https://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);
使用HtmlSaveOptions屬性
使用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 https://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 = "http://example.com/resources"; doc.Save(dataDir + "ExportResourcesUsingHtmlSaveOptions.html", saveOptions);
如何將文檔轉換爲MHTML和電子郵件
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 https://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 = "[email protected]";
message.To = "[email protected]";
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);
【下載Aspose.Words for .NET最新試用版】