使用Aspose.PDF for .NET將PDF轉換爲HTML格式示例解讀(6)——在style.css中設置字體的URL前綴

PDF是當今最流行的文檔格式之一,各類應用程序將其用做最終輸出。因爲支持多種數據類型和可移植性,所以它是建立和共享內容的首選格式。做爲對開發文檔管理應用程序感興趣的.NET應用程序開發人員,可能但願嵌入處理功能,以讀取PDF文檔並將其轉換爲其餘文件格式,例如HTML。css

Aspose.PDF for .NET是一種高級PDF處理和解析API,用於在跨平臺應用程序中執行文檔管理和操做任務。API能夠輕鬆用於生成,修改,轉換,渲染,保護和打印PDF文檔,而無需使用Adobe Acrobat。html

在本文中,咱們將探索並演示Aspose.PDF for .NET API的強大轉換功能,以使用多種選項讀取PDF文件並將其轉換爲HTML。字體


PDF轉HTML-在style.css中設置字體的URL前綴

Aspose.PDF for .NET提供了將HTML文件轉換爲PDF並將PDF文件轉換爲HTML的功能。在PDF到HTML的轉換過程當中,PDF中使用的TrueType字體保存在文件系統中。spa

在高版本中,可使用自定義資源節省策略爲字體設置自定義URL:
orm

 

//文檔目錄的路徑。
string dataDir = RunExamples.GetDataDir_AsposePdf_DocumentConversion_PDFToHTMLFormat();

Document doc = new Document(dataDir + "input.pdf");
string outHtmlFile = dataDir + "PrefixForFonts_out.html";
_desiredFontDir = Path.GetDirectoryName(outHtmlFile) + @"\36296_files\";
if (!Directory.Exists(_desiredFontDir))
{
    Directory.CreateDirectory(_desiredFontDir);
}
// 重置字體名稱計數器-此計數器將在咱們的自定義代碼中使用
// 生成惟一字體文件名

_fontNumberForUniqueFontFileNames = 0;

// 使用自定義保存策略建立HtmlSaveOption,它將完成全部工做
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.CustomResourceSavingStrategy = new HtmlSaveOptions.ResourceSavingStrategy(CustomResourcesProcessing);
doc.Save(outHtmlFile, saveOptions);

客戶處理實例htm

private static string CustomResourcesProcessing(SaveOptions.ResourceSavingInfo resourceSavingInfo)
{
    //-----------------------------------------------------------------------------
    // 這只是可能實現資源的客戶處理的示例
    //在結果HTML中引用
    //-----------------------------------------------------------------------------

    // 1)在這種狀況下,咱們只須要作一些特別的事情
    //     使用字體,所以讓咱們保留全部其餘資源的處理
    //     轉換自己
    if (resourceSavingInfo.ResourceType != SaveOptions.NodeLevelResourceType.Font)
    {
        resourceSavingInfo.CustomProcessingCancelled = true;
        return "";
    }

    // 若是提供了字體資源,請自行處理
    // 1)將提供的帶有短名稱的字體寫入所需的文件夾
    //    您能夠輕鬆地作任何事情-這只是實現之一

    _fontNumberForUniqueFontFileNames++;
    string shortFontFileName = (_fontNumberForUniqueFontFileNames.ToString() + Path.GetExtension(resourceSavingInfo.SupposedFileName));
    string outFontPath = _desiredFontDir + "\\" + shortFontFileName;

    System.IO.BinaryReader fontBinaryReader = new BinaryReader(resourceSavingInfo.ContentStream);
    System.IO.File.WriteAllBytes(outFontPath, fontBinaryReader.ReadBytes((int)resourceSavingInfo.ContentStream.Length));


    // 返回所需的URI,CSS中將使用該URI引用字體
    string fontUrl = "http:// Localhost:255/document-viewer/GetFont/" + shortFontFileName;
    return fontUrl;
}

若是您有任何下載或其餘需求,請隨時加入Aspose技術交流羣(642018183)資源

相關文章
相關標籤/搜索