c# - ITextSharp,定位文本

标签 c# pdf itext

我们使用 ITextSharp 的原因我不明白,我们还没有这本书,我有一个恼人的问题,我很感激帮助。

我正在使用页脚,但无法按照我的意愿将其对齐。

我的函数需要一个字符串列表,但它通常是 4 个字符串,我希望每个字符串都排成一行。它不像 itextsharp 那样接缝可以处理带有换行符的字符串,所以这就是列表的原因。

现在这对我来说位置不正确,第一个字符串看起来没问题,但是第二个字符串有点长,它在文档外面有一半,第三个字符串也是如此,第四个甚至不可见,即使你有 1剩余空间厘米。

感谢您的帮助!

public PdfTemplate AddPageText(IList<string> stringList, PdfWriter writer) 
{
    var cb = writer.DirectContent;
    PdfTemplate footerTemplate = cb.CreateTemplate(450, 120); //55);

    footerTemplate.BeginText();
    BaseFont bf2 = BaseFont.CreateFont(BaseFont.TIMES_ITALIC, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
    footerTemplate.SetFontAndSize(bf2, 9);
    footerTemplate.SetColorStroke(BaseColor.DARK_GRAY);
    footerTemplate.SetColorFill(BaseColor.GRAY);

    footerTemplate.SetLineWidth(3);
    footerTemplate.LineTo(50, footerTemplate.YTLM);

    int y = 10;
    foreach (string text in stringList)
    {
        float widthoftext = 500.0f - bf2.GetWidthPoint(text, 9);
        footerTemplate.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, text, widthoftext, 50 - y, 0);
        y += y;
    }
    footerTemplate.EndText();

    return footerTemplate;

}

最佳答案

如果您要放置字符串并使用 DirectContent,则您应对内容负责。在这种情况下,您需要计算字符串矩形并相应地换行。

不过,我建议改用带有文本单元格的表格。表格包装文本并处理您正在处理的一些问题。

关于c# - ITextSharp,定位文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7834458/

相关文章:

java - PDFBox+充气城堡-签名PDF

c# - iTextSharp 签署在 MemoryStream 中创建的 PDF

c# - 使用 iTextSharp 复制 PDF 表单

c# - iText 7 从字节数组合并文档

c# - 删除文本窗口窗体上的白色轮廓

c# - 访问 Swashbuckle swagger RootUrl/swagger/docs 不提供任何输出

c# - WPF 将异常消息从 ThreadPool 线程传递到 UI

c# - 无法让 Dynamic Linq 工作

android - DroidText 发生了什么事?

php - 未捕获的异常 'ImagickException',消息为 'Unable to read the file'