2014-02-18 61 views
1

我正在使用名爲Aspose.Pdf的PDF生成庫。我喜歡這個圖書館,但是我遇到了一個問題,那就是讓中文字符在PDF中出現。我使用下面的代碼來生成C#.NET MVC的PDF:Aspose.Pdf:爲什麼中文字符「空白」?

var pdf = new Aspose.Pdf.Generator.Pdf(); 
pdf.IsLandscape = true; 
pdf.HtmlInfo.CharsetApplyingLevelOfForce = Aspose.Pdf.Generator.HtmlInfo.CharsetApplyingForceLevel.EnforceUseAlways; 
pdf.HtmlInfo.CharSet = "UTF-8"; 
pdf.HtmlInfo.BadHtmlHandlingStrategy = Aspose.Pdf.Generator.BadHtmlHandlingStrategy.TreatAsPlainText; 
pdf.BindHTML(htmlString); 
pdf.SetUnicode(); 
pdf.Save(fullFilePath); 

的htmlString變量中的值是:

<!DOCTYPE html> 
<html> 
<body style="font-family: 'Arial Unicode MS',Arial,sans-serif; "> 
    <center><h3>Corrective Action Plans PDF</h3></center> 
    <table style="padding: 5px; font-size: 8pt; border: 1px solid #000000;" cellpadding="5"> 
      <tr style="border: 1px solid #000000;" bgcolor="#4D94FF"> 
        <th style="border: 1px solid #000000;" width="11%"> 
         ID 
        </th> 
        <th style="border: 1px solid #000000;" width="11%"> 
         Status 
        </th> 
        <th style="border: 1px solid #000000;" width="11%"> 
         審計類型 
        </th> 
        <th style="border: 1px solid #000000;" width="11%"> 
         審覈表格 
        </th> 
        <th style="border: 1px solid #000000;" width="11%"> 
         Category 
        </th> 
        <th style="border: 1px solid #000000;" width="11%"> 
         問題 
        </th> 
        <th style="border: 1px solid #000000;" width="11%"> 
         Location ID 
        </th> 
        <th style="border: 1px solid #000000;" width="11%"> 
         Location 
        </th> 
        <th style="border: 1px solid #000000;" width="11%"> 
         Start Date 
        </th> 
      </tr> 
      <tr style="border: 1px solid #000000;" bgcolor="#CCCCCC"> 
        <td style="border: 1px solid #000000;" width="11%"> 
         1260 
        </td> 
        <td style="border: 1px solid #000000;" width="11%"> 
         New 
        </td> 
        <td style="border: 1px solid #000000;" width="11%"> 
         CAM Forms 
        </td> 
        <td style="border: 1px solid #000000;" width="11%"> 
         CAM Form 4 - All Action Plan Options 
        </td> 
        <td style="border: 1px solid #000000;" width="11%"> 
         CAM4: Use Action Plan 
        </td> 
        <td style="border: 1px solid #000000;" width="11%"> 
         Droplist Non-Compliant 
        </td> 
        <td style="border: 1px solid #000000;" width="11%"> 
         017 
        </td> 
        <td style="border: 1px solid #000000;" width="11%"> 
         ANGLETON 
        </td> 
        <td style="border: 1px solid #000000;" width="11%"> 
         2014/2/13 14:00:00 
        </td> 
      </tr> 
    </table> 
</body> 
</html> 

正如你所看到的,也有在HTML中國人物標記在表格的標題行中。但是,所產生的PDF文檔中的值正在「空白」。

任何人都可以幫助我嗎?

回答

4

這是一個非常具體的問題,所以我不確定我甚至會得到對此的迴應。但是,我絕望了,無論如何都伸出了手。 :)

在任何情況下,我推動並設法找到解決方案。我通過將我的C#更改爲以下來修復它:

var pdf = new Aspose.Pdf.Generator.Pdf(); 
pdf.IsLandscape = true; 

Aspose.Pdf.Generator.Section section = pdf.Sections.Add(); 
Aspose.Pdf.Generator.Text text = new Aspose.Pdf.Generator.Text(section, htmlString); 
text.IsHtmlTagSupported = true; 
text.IsHtml5Supported = true; 
text.TextInfo.FontName = "Arial Unicode MS"; 
text.IfHtmlTagSupportedOverwriteHtmlFontNames = true; 
section.Paragraphs.Add(text); 
pdf.SetUnicode(); 

pdf.Save(fullFilePath); 
2

我有類似的問題,但設置有很多不同。
我沒有使用BindHTML和HTML數據,而不是說,我使用XML輸入:

var pdf = new Aspose.Pdf.Generator.Pdf(); 
pdf.BindXML(thexml); 
pdf.Save(fullFilePath); 

其中thexml包含如何應生成PDF對象一個漫長而複雜的描述。一直Aspose PDF XML規範,一切正常。除了某些內部文本或內部html數據包含非拉丁字符時。

我嘗試過使用西里爾文,日文和波蘭文的例子 - 前兩個中的所有'特殊'字符都被渲染爲空格,波蘭文中所有特殊字符都被強制爲最接近的拉丁字符(ć-> c,Ł-> L等)。

Btw。我用Aspose.PDF v9.6.0.0

帶着這樣的疑問幫助/答案,我能得到它有些一起工作:

var pdf = new Aspose.Pdf.Generator.Pdf(); 
pdf.TextInfo.IsUnicode = true; 
pdf.SetUnicode(); 
pdf.BindXML(thexml); 
pdf.Save(fullFilePath); 

您呈現似乎沒有其他設置改變一切。另外,我很奇怪我必須按照你所看到的設置兩次IsUnicode。 SetUnicode還不夠。

以上所述,西里爾文和波蘭文都可以,但日文仍然是空白的。儘管如此,這對我來說已經足夠了。也許它也會爲別人分好。

相關問題