使用iTextSharp和PDFStamper在PDF中使用不同的字体 [英] Using different fonts in PDF using iTextSharp and PDFStamper

查看:908
本文介绍了使用iTextSharp和PDFStamper在PDF中使用不同的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iTextSharp加载现有PDF并使用PdfStamper添加文本。我希望完全控制文本,这意味着我希望能够控制字体(仅限TrueType),字体大小和坐标。现在,我正在使用ShowTextAligned将文本添加到某些协调,并使用setFontAndSize来设置字体和字体大小。这是我添加文本的代码:

I'm using iTextSharp to load an existing PDF and adding text using the PdfStamper. I want full control over the text, meaning I want to be able to control the font (only TrueType), font size and coordinates. Right now, I'm using ShowTextAligned to add text to certain coordinaties and setFontAndSize to set the font and font size. This is my code to add text:

    private void AddText(BaseFont font, string text, int x, int y, int size)
    {
        pdf.BeginText();
        pdf.SetFontAndSize(font, size);
        pdf.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, x, y, 0);
        pdf.EndText();
    }

以下函数用于加载TrueType字体:

The following function is used to load the TrueType font:

    public BaseFont GetFont(string font, string encoding)
    {
        if (!(font.EndsWith(".ttf") || font.EndsWith(".TTF")))
            font += ".ttf";

        BaseFont basefont;

        basefont = BaseFont.CreateFont(ConfigurationManager.AppSettings["fontdir"] + font, encoding, BaseFont.NOT_EMBEDDED);

        if (basefont == null)
            throw new Exception("Could not load font '" + font + "' with encoding '" + encoding + "'");

        return basefont;
    }

以下代码用于加载现有PDF:

The following code is used to load the existing PDF:

        Stream outputPdfStream = Response.OutputStream;
        PdfReader pdfReader = new PdfReader(new RandomAccessFileOrArray(HttpContext.Current.Request.MapPath("PdfTemplates/" + ConfigurationManager.AppSettings["pdf_template"])), null);
        PdfStamper pdfStamper = new PdfStamper(pdfReader, outputPdfStream);

        pdf = pdfStamper.GetOverContent(1);

这一切都很完美,除非我尝试使用不同的字体。因此,当使用不同的字体多次调用AddText时,PDF将在openend时显示一般错误。我想知道是否可以使用ShowTextAligned函数使用不同的字体,如果是,如何使用?

This all works perfectly, except when I try to use different fonts. So when AddText is called multiple times with different fonts, the PDF will display a generic error when openend. I wonder if it is possible to use different fonts using the ShowTextAligned function and if it is, how?

推荐答案

感谢您的回答马克,但我已经解决了这个问题。我的Content-Type标题出现问题,我用它来告诉浏览器PDF的大小。这导致浏览器在实际下载整个PDF之前停止下载。添加新字体时,PDF大小将超过Content-Type标头中指定的大小,从而导致PDF格式错误。它已经解决了,多种字体工作得很好: - )。

Thanks for your answer Mark, however I already solved the issue. There was a problem with my Content-Type header I use to tell the browser how large the PDF is. This caused the browser to stop downloading before the entire PDF was actually downloaded. When adding a new font, the PDF size would just exceed the size specified in the Content-Type header, thus resulting in a bad PDF. It's solved now, multiple fonts work just fine :-).

这篇关于使用iTextSharp和PDFStamper在PDF中使用不同的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆