C# 使用iTextSharp为不同语言生成pdf

C# 使用iTextSharp为不同语言生成pdf,c#,pdf,itextsharp,html-to-pdf,C#,Pdf,Itextsharp,Html To Pdf,我尝试了不同的选择,但没有成功。该代码生成了英语的pdf,但不适用于其他语言 using (var ms = new MemoryStream()) { // Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF var doc = new Document(); {

我尝试了不同的选择,但没有成功。该代码生成了英语的pdf,但不适用于其他语言

using (var ms = new MemoryStream())
            {
                // Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
                var doc = new Document();
                {
                    // Create a writer that's bound to our PDF abstraction and our stream
                    var writer = PdfWriter.GetInstance(doc, ms);
                    {
                        // Open the document for writing
                        doc.Open();

                        string finalHtml = string.Empty;

                        // Read your html by database or file here and store it into finalHtml e.g. a string
                        // XMLWorker also reads from a TextReader and not directly from a string
                        using (var srHtml = new StringReader(sHtmlText))
                        {
                            // Parse the HTML
                            iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
                        }                            
                        doc.Close();
                    }
                }

                // After all of the PDF "stuff" above is done and closed but **before** we
                // close the MemoryStream, grab all of the active bytes from the stream
                return new PDFFormFillerResult(ms, PDFFormFillerResultType.Success, string.Empty);
                //bytes = ms.ToArray();
            }

我们不得不购买另一个能够理解UNICODE字符的第三方工具。

我能够创建英语和日语的多语言PDF。 实现这一目标的前提是: 1.拥有支持您计划使用的语言的适当字体。 2.从iTextSharp实现IFontProvider接口,并注册将要使用的多语言字体

如果您遵循这两个步骤,就可以创建多语言PDF

代码示例:

public class smartUIFontProvider: IFontProvider
    {
      public Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color)
        {
            string myFont = @"C:\Tasks\Projects\SampleProject\iTextDemo_PDF\SmartFontUI\SmartFontUI.otf";
            iTextSharp.text.pdf.BaseFont bfR;
            bfR = iTextSharp.text.pdf.BaseFont.CreateFont(myFont,
              iTextSharp.text.pdf.BaseFont.IDENTITY_H,
              iTextSharp.text.pdf.BaseFont.EMBEDDED);
            iTextSharp.text.BaseColor clrBlack =
               new iTextSharp.text.BaseColor(0, 0, 0);
            iTextSharp.text.Font fntHead =
              new iTextSharp.text.Font(bfR, 12, iTextSharp.text.Font.NORMAL, clrBlack);
            return fntHead;
        }
       public bool IsRegistered(string fontname)
        {
            return true;
        }
    } 

static void Main (string[] args)
        {
            // step 1
            Document document = new Document();
            Byte[] bytes;
            var fileName = "resources\\test_" + System.DateTime.Now.ToString("ddMMyyyy_hhmm") + ".pdf";
            var testFile = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), fileName);

            MemoryStream ms = new MemoryStream();
            // step 2

            using (var writer = PdfWriter.GetInstance(document, ms))
            {
                document.Open();

                String example_html = @" <!DOCTYPE html> <html> <head> </head> <body> <h1>Change of Control Application</h1> <br/> <p>Please fill out this form if there are updates to your current organization and its relationship Company has on file with the legal binding entity. Company will review the information and may ask you to provide further information before Company agrees to the requested changes.</p> <br/> <table class='bodyText1LIC general' style='width:700px' id='tblInputForm' > <tr> <td class='bodyText1LIC' colspan='4'>&nbsp;</td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Licensee Information</h3> </td> </tr> <tr> <td class='bodyText1SerNo'>1.</td> <td class='bodyText1LIC'>Licensee Name&nbsp;</td> <td valign='top' class='txtboxtd'> 字詰めなどの調整をおすすめします </td> <td> &nbsp;</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'>&nbsp;</td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Licensee Primary Contact Information </h3> </td> </tr> <tr> <td class='bodyText1SerNo'>2.</td> <td class='bodyText1LIC'>Name</td> <td class='txtboxtd'> めなどの調整 </td> <td> &nbsp;</td> </tr> <tr> <td class='bodyText1SerNo'>3.</td> <td class='bodyText1LIC'>Job Title</td> <td class='txtboxtd'> T</td> <td> &nbsp;</td> </tr> <tr> <td class='bodyText1SerNo'>4.</td> <td class='bodyText1LIC'>Mailing Address</td> <td class='txtboxtd'>めなどの調整</td> <td> &nbsp;</td> </tr> <tr> <td class='bodyText1SerNo'>5.</td> <td class='bodyText1LIC'>Telephone Number&nbsp;</td> <td class='txtboxtd'>T</td> <td>&nbsp;</td> </tr> <tr> <td class='bodyText1SerNo'>6.</td> <td class='bodyText1LIC'>Email Address&nbsp;</td> <td class='txtboxtd'>abc@test.com</td> <td>&nbsp;</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'>&nbsp;</td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3> Organizational Structure</h3> </td> </tr> <tr> <td class='bodyText1SerNo'> 7.</td> <td class='bodyText1LIC' colspan='3'> Will your Company's Name Change?&nbsp;</td> </tr> <tr> <td></td> <td colspan='3'> Yes</td> </tr> <tr class='nc' id='TrCompanyName'> <td></td> <td class='bodyText1LIC' colspan='2'> a) &nbsp; &nbsp;New company name&nbsp;</td> <td class='txtboxtd'> DSFDASFASDFDSA</td> </tr> <tr class='nc' id='TrEffectiveDate'> <td></td> <td class='bodyText1LIC' colspan='2'> b) &nbsp; &nbsp;Effective Date Company change will take place &nbsp;</td> <td class='txtboxtd'> 04/20/2017</td> </tr> <tr> <td class='bodyText1SerNo'>8.</td> <td class='bodyText1LIC' colspan='3'>Has your Company been Reincorporated or under gone an Internal Reorganization?&nbsp;</td> </tr> <tr> <td></td> <td colspan='3'> No</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'>&nbsp;</td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Assets / Stocks to be Acquired </h3> </td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Assets to be Acquired</h3> </td> </tr> <tr> <td class='bodyText1SerNo'>9.</td> <td class='bodyText1LIC' colspan='3'>Will some or all of your Company’s Assets be Acquired? &nbsp;</td> </tr> <tr> <td></td> <td colspan='3'> No</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'>&nbsp;</td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Stocks to be Acquired </h3> </td> </tr> <tr> <td class='bodyText1SerNo'>10.</td> <td class='bodyText1LIC' colspan='3'>Will some or all of your Company’s Assets be Acquired? &nbsp;</td> </tr> <tr> <td></td> <td colspan='3'> No</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'>&nbsp;</td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3> Effects of Change to Licensee</h3> </td> </tr> <tr> <td  class='bodyText1SerNo'> 11.</td> <td class='bodyText1LIC' colspan='3'> Please indicate which Company is the Surviving Legal Entity? &nbsp;</td> </tr> <tr> <td></td> <td class='txtboxtd' colspan='3'> T</td> </tr> <tr> <td  class='bodyText1SerNo'>12.</td> <td class='bodyText1LIC' colspan='3'>Please provide the Company that will be the Licensee?&nbsp;</td> </tr> <tr> <td class='txtboxtd'>&nbsp;</td> <td colspan='3'> T</td> </tr> <tr> <td valign='top' class='bodyText1SerNo'>13.</td> <td class='bodyText1LIC' colspan='3'>Will any companies exist to operate as separate Subsidiaries or Affiliates ?  &nbsp;</td> </tr> <tr> <td></td> <td colspan='3'> No</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'>&nbsp;</td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Existing Agreements</h3> </td> </tr> <tr> <td class='bodyText1SerNo'>14.</td> <td class='bodyText1LIC' colspan='3'>Please identify if any Licenses that will be affected by the Changes described above &nbsp;</td> </tr> <tr> <td></td> <td colspan='3'> No</td> </tr> <tr> <td class='bodyText1LIC' colspan='4'>&nbsp;</td> </tr> <tr> <td class='tableBody1' colspan='4'> <h3>Corporate Documentation</h3> </td> </tr> <tr> <td valign='top' class='bodyText1SerNo'>15.</td> <td class='bodyText1LIC' colspan='3'>Please Select from 1 of the 2 options listed below</td> </tr> <tr> <td></td> <td colspan='3'> Please Describe the Full Legal Structure of this Organization Change in relation to Licensing.</td> </tr> <tr> <td></td> <td colspan='3'> ASDFSDAFDSAFADSFDASF &nbsp;</td> </tr> </table> </body> </html>";
                String example_css = @"html { font-family: SmartFontUI; font-size: 14px; }  h1, h2, strong { font-family: SmartFontUI; font-weight: bold; display: inline; }  h1 { font-size: 18pt; }   .main { font-size: 12pt; color: black; font-family: SmartFontUI, Arial, Sans-Serif; background-color: white; text-align: left; line-height: 1.4em; margin: 2%; } .mainDesc { width: 100%; margin: 10px 0; }  span { float: left; display: block; width: 100%; margin-bottom: 5px; }  .bodyText1SerNo { width: 2em; }  .bodyText1LIC { width: 22em; }  .txtboxspan { width: 32em; }  .tableBody1 { width: 100%; } .nc { margin-left: 100px; } .subtitle { font-size: 16pt; }";
                FontFactory.Register(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "SmartFontUI.otf"), "SmartFontUI");

                XMLWorkerHelper worker = XMLWorkerHelper.GetInstance();

                MemoryStream msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html));
                MemoryStream msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css));

                smartUIFontProvider smartUIFontObj = new smartUIFontProvider();

                worker.ParseXHtml(writer, document, msHtml, msCss, System.Text.Encoding.UTF8, smartUIFontObj);
                // step 5
                document.Close();

            }

            bytes = ms.ToArray();
            System.IO.File.WriteAllBytes(testFile, bytes);
        }
公共类smartUIFontProvider:IFontProvider
{
公共字体GetFont(字符串fontname、字符串编码、布尔嵌入、浮点大小、整数样式、基色)
{
字符串myFont=@“C:\Tasks\Projects\SampleProject\iTextDemo\u PDF\SmartFontUI\SmartFontUI.otf”;
iTextSharp.text.pdf.BaseFont bfR;
bfR=iTextSharp.text.pdf.BaseFont.CreateFont(myFont,
iTextSharp.text.pdf.BaseFont.IDENTITY\u H,
iTextSharp.text.pdf.BaseFont.EMBEDDED);
iTextSharp.text.BaseColor CLR黑色=
新的iTextSharp.text.BaseColor(0,0,0);
iTextSharp.text.Font fntHead=
新的iTextSharp.text.Font(bfR,12,iTextSharp.text.Font.NORMAL,黑色);
返回fntHead;
}
公共bool已注册(字符串fontname)
{
返回true;
}
} 
静态void Main(字符串[]参数)
{
//第一步
文档=新文档();
字节[]字节;
var fileName=“resources\\test\”+System.DateTime.Now.ToString(“ddMMyyyy\u hhmm”)+“.pdf”;
var testFile=Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.getExecutionGassembly().Location),文件名);
MemoryStream ms=新的MemoryStream();
//步骤2
使用(var writer=PdfWriter.GetInstance(document,ms))
{
document.Open();
字符串示例\u html=@"控制权变更申请
如果您当前的组织及其公司与具有法律约束力的实体之间的关系有更新,请填写此表。公司将审查信息,并可能要求您在公司同意请求的变更之前提供进一步的信息。


信息1.被许可人名称字詰めなどの調整をおすすめします 被许可人主要联系信息2.姓名めなどの調整 3.职务T 4.邮寄地址めなどの調整 5.电话号码T 6.电子邮件地址abc@test.com组织结构7.贵公司名称是否会变更?是a)新公司名称DSFDASFASDFDSA b)生效日期公司变更将于2017年4月20日发生8.贵公司是否已重新合并或进行内部重组?否拟收购的资产/股票拟收购的资产9.贵公司的部分或全部资产是否将被收购?无需收购的股票10.贵公司的部分或全部资产是否将被收购?被许可方变更不产生任何影响11.请说明哪家公司是存续的法人实体?T 12.请提供将成为被许可方的公司?T 13.是否存在作为独立子公司或附属公司运营的公司?无现有协议14.请确定是否有任何受上述变更影响的许可证无公司文件15.请选择f下面列出的两个选项中的rom 1请描述该组织与许可相关的全部法律结构变更。asdfsdasafadsfdasf”; 字符串示例_css=@“html{font-family:SmartFontUI;font-size:14px;}h1、h2、strong{font-family:SmartFontUI;font-weight:bold;display:inline;}h1{font-size:18pt;}.main{font-size:12pt;color:black;font-family:SmartFontUI,Arial,Sans-Serif;背景色:白色;文本对齐:左侧;行高:1.4em;边距:2%;}”.mainDesc{width:100%;margin:10px 0;}span{float:left;display:block;width:100%;margin bottom:5px;}.bodyText1SerNo{width:2em;}.bodyText1LIC{width:22em;}.txtboxspan{width:32em;}.tableBody1{width:100%;}.nc{margin left:100px;}; Register(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts)、“SmartFontUI.otf”)、“SmartFontUI”); XMLWorkerHelper worker=XMLWorkerHelper.GetInstance(); MemoryStream msHtml=新的MemoryStream(System.Text.Encoding.UTF8.GetBytes(示例_html)); MemoryStream msCss=新的MemoryStream(System.Text.Encoding.UTF8.GetBytes(示例_css)); smartUIFontProvider smartUIFontObj=新的smartUIFontProvider(); ParseXHtml(writer、document、msHtml、msCss、System.Text.Encoding.UTF8、smartUIFontObj); //步骤5 document.Close(); } 字节=ms.ToArray(); System.IO.File.writealBytes(testFile,字节); }
您尝试过哪些选项?失败的是什么?没有失败的是将英文内容作为输出并忽略和其他语言字符。您遇到了什么错误,失败在哪里?@Kenneth没有错误,正在创建文件,其他语言字符未呈现。您是指字符集吗?此程序没有不要区分不同的语言。只要字符相同,就没有什么不同。哪个能理解UNICODE字符?iTextSharp也能。顺便说一句,你的“答案”不能回答这个问题。正如