6

我想使用 ITextSharp 将 ASP.NET 网页转换为 pdf。我确实写了一些代码,但我不能让它显示土耳其字符。谁能帮我?

这是代码:

using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

using System.Web.UI;
using System.Web;
using iTextSharp.text.html.simpleparser;
using System.Text;
using System.Text.RegularExpressions;

namespace Presentation
{
    public partial class TemporaryStudentFormPrinter : System.Web.UI.Page
    {
        protected override void Render(HtmlTextWriter writer)
        {
            MemoryStream mem = new MemoryStream();
            StreamWriter twr = new StreamWriter(mem);
            HtmlTextWriter myWriter = new HtmlTextWriter(twr);
            base.Render(myWriter);
            myWriter.Flush();
            myWriter.Dispose();
            StreamReader strmRdr = new StreamReader(mem);
            strmRdr.BaseStream.Position = 0;
            string pageContent = strmRdr.ReadToEnd();
            strmRdr.Dispose();
            mem.Dispose();
            writer.Write(pageContent);
            CreatePDFDocument(pageContent);
        }
        public void CreatePDFDocument(string strHtml)
        {
            string strFileName = HttpContext.Current.Server.MapPath("test.pdf");
            Document document = new Document(PageSize.A4, 80, 50, 30, 65);
            PdfWriter.GetInstance(document, new FileStream(strFileName, FileMode.Create));

            StringReader se = new StringReader(strHtml);
            HTMLWorker obj = new HTMLWorker(document);

            document.Open();

            obj.Parse(se);
            document.Close();
            ShowPdf(strFileName);
        }
        public void ShowPdf(string strFileName)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
            Response.ContentType = "application/pdf";
            Response.WriteFile(strFileName);
            Response.Flush();
            Response.Clear();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
4

2 回答 2

11
iTextSharp.text.pdf.BaseFont STF_Helvetica_Turkish = iTextSharp.text.pdf.BaseFont.CreateFont("Helvetica", "CP1254", iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED);

iTextSharp.text.Font fontNormal = new iTextSharp.text.Font(STF_Helvetica_Turkish, 12, iTextSharp.text.Font.NORMAL);

You should pass the font as an argument in itextsharp manipulation commands like that :

pdftable.AddCell(new Phrase(nn.InnerText.Trim(), fontNormal));

You might want to consider working with reporting tools with pdf exporting capability instead of direclty working with pdf which can be a real headache..

于 2010-10-26T13:45:23.730 回答
0

您需要确保使用支持土耳其语字符集的字体(或者至少是您尝试写出的字符)来编写文本。

我不知道 HtmlTextWriter 在字体使用方面做了什么 - 如果超出 Latin1 或 Latin1 扩展的 Unicode 范围,它可能会使用不太可能支持您要打印的字符的标准内置字体之一.

BaseFont.createFont(...)曾经在 iText (Java) 中的 PDF 中包含一个外部字体——一种支持我正在编写的所有字符的字体。您也许可以创建您的 Font 对象,然后将其传递给 HtmlTextWriter?

于 2010-10-26T11:45:58.573 回答