如何知道文本是否超过itextSharp中的填充框 [英] How to know if text exceeds the fill box in itextSharp

查看:148
本文介绍了如何知道文本是否超过itextSharp中的填充框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码:

PdfReader PDFReader = new PdfReader("c:\\file.pdf");

FileStream Stream = new FileStream("c:\\new.pdf", FileMode.Create, FileAccess.Write);

PdfStamper PDFStamper = new PdfStamper(PDFReader, Stream);

for (int iCount = 0; iCount < PDFStamper.Reader.NumberOfPages; iCount++)
{
    iTextSharp.text.Rectangle PageSize = PDFReader.GetCropBox(iCount + 1);
    PdfContentByte PDFData = PDFStamper.GetOverContent(iCount + 1);
    BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
    PDFData.BeginText();
    PDFData.SetColorFill(CMYKColor.RED);
    PDFData.SetFontAndSize(baseFont, 20);
    PDFData.ShowTextAligned(PdfContentByte.ALIGN, SAMPLE DOCUMENT", (PageSize.Right + PageSize.Left) / 2, (PageSize.Top + PageSize.Bottom) / 2, 45);
    PDFData.EndText();
}

PDFStamper.Close();
PDFReader.Close();

但有时叠加的文本超出了页面大小,因为我已经将字体硬编码为20.那么,有没有办法知道叠加的文本是否会超过页面大小?因为我想要使用我将使用的代码:

But sometimes the overlaid text exceeds the page size because I have hard coded the font as 20. So, is there any way to know if the overlaid text will exceed the page size? Because I want to use code like I will then use:

if(pagesize exceeds)
  reduce font size by 1 point and check again .....

如果上述方法不起作用,那我的下一步就是使用其中包含重叠文本且背景为透明的PNG图像。然后根据页面大小调整图像大小,然后将其覆盖。

If the above doesn't work, then my next step is to use a PNG image that has the overlaid text in it and its background as transparent. then resize the image according to the pagesize and then overlay it.

但是,我更喜欢第一部分。如果没有,那么我将选择第二个选项。

however, I will prefer the first part. if not, then I will go for the second option.

推荐答案

在进行一些小的计算后,此方法应计算用于此类垂直文本的最大字体大小并应用它:

After doing some minor calculations, this method should calculate the maximum font size to use for such a vertical text and apply it:

void Stamp(PdfContentByte cb, Rectangle rect, BaseFont bf, string text)
{
    int altitude = Math.Max(bf.GetAscent(text), bf.GetDescent(text));
    int width = bf.GetWidth(text);
    double horizontalFit = Math.Sqrt(2.0) * 1000 * (rect.Left + rect.Right) / (width + 2 * altitude);
    double verticalFit = Math.Sqrt(2.0) * 1000 * (rect.Bottom + rect.Top) / (width + 2 * altitude);
    double fit = Math.Min(horizontalFit, verticalFit);

    cb.BeginText();
    cb.SetColorFill(CMYKColor.RED);
    cb.SetFontAndSize(bf, (float) fit);
    cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, text, (rect.Right + rect.Left) / 2, (rect.Top + rect.Bottom) / 2, 45);
    cb.EndText();
}

您可以这样称呼:

using (PdfReader reader = new PdfReader(source))
using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create, FileAccess.Write)))
{
    for (int iCount = 0; iCount < reader.NumberOfPages; iCount++)
    {
        Rectangle PageSize = reader.GetCropBox(iCount + 1);
        PdfContentByte PDFData = stamper.GetOverContent(iCount + 1);
        BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
        Stamp(PDFData, PageSize, baseFont, "SAMPLE DOCUMENT");
    }
}

(顺便说一句,我使用了你的 BaseFont 但你应该知道iText(夏普)将忽略 BaseFont.EMBEDDED 标准的14字体,如 BaseFont.HELVETICA 。)

(By the way, I used your BaseFont but you should be aware that iText(Sharp) will ignore BaseFont.EMBEDDED for standard 14 fonts like BaseFont.HELVETICA.)

结果如下所示:

PS:如果您(如您的问题中所表达的)确实不想使用大于20的字体大小,则必须最低 适合值再次为20。

PS: If you (as expressed in your question) really don't want to use a font size above 20, you have to Min the fit value again with 20.

这篇关于如何知道文本是否超过itextSharp中的填充框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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