HTML 转 PDF 土耳其语字符问题

发布于 2024-09-28 18:11:14 字数 2124 浏览 7 评论 0原文

我想使用 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)
        {

        }
    }
}

I want to convert a ASP.NET web page to pdf using ITextSharp. I did write some code but I can not make it show the Turkish Characters. Can anyone help me?

Here is the code:

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)
        {

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暖阳 2024-10-05 18:11:14
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);

您应该在 itextsharp 操作命令中将字体作为参数传递,如下所示:

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

您可能需要考虑使用具有 pdf 导出功能的报告工具,而不是直接使用 pdf,这可能是一个真正令人头痛的问题。

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..

灯下孤影 2024-10-05 18:11:14

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

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

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

You'll need to make sure you are writing the text in a Font that supports the Turkish character set (or at least the caracters you are trying to write out).

I don't know what HtmlTextWriter does in terms of font use - it will presumably use one of the standard built-in fonts that are unlikely to support the characters you want to print if the fall outside of the Latin1 or Latin1-extended Unicode ranges.

I use BaseFont.createFont(...) to have an external Font included in my PDF in iText (Java) - one which supports all the characters that I am writing. You might be able to create your Font object and then pass it to HtmlTextWriter?

南冥有猫 2024-10-05 18:11:14
PdfFont font = PdfFontFactory.CreateFont("Helvetica", "CP1254",PdfFontFactory.EmbeddingStrategy.FORCE_NOT_EMBEDDED);
doc.Add(new Paragraph("P").SetFont(font));
PdfFont font = PdfFontFactory.CreateFont("Helvetica", "CP1254",PdfFontFactory.EmbeddingStrategy.FORCE_NOT_EMBEDDED);
doc.Add(new Paragraph("P").SetFont(font));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文