iTextSharp修剪前导空间并导致列不对齐 [英] iTextSharp trims leading space and causes misalignment of columns

查看:176
本文介绍了iTextSharp修剪前导空间并导致列不对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从iTextSharp的版本4.1.2 => 5.1.3移动时,我遇到了从文本生成PDF时发生的错误。问题是当一行的第一个字符有一个前导空格时,那个前导空格会被截断。这是右对齐列的问题。

When moving from the versions 4.1.2 => 5.1.3 of iTextSharp I have come across a bug that happens when generating a PDF from text. The problem is that when the first character of a line has a leading spaces then that leading space gets truncated. This is a problem with a right justified columns.

示例:(破折号=空格)

Example: (dashes= spaces)

输入


------ Header

------- ------- 1

-------------- 2

0123456789

------Header
--------------1
--------------2
0123456789

输出


- --- Header

------------- 1

------------- 2

0123456789 ~~~注意不正确的对齐,因为此列没有前导空格!

-----Header
-------------1
-------------2
0123456789 ~~~Notice improper alignment because this column did not have leading space!

有问题的代码已经缩小到文件iTextSharp / text / pdf / PdfChunck.cs方法TrimFirstSpace。
从PdfDocument类调用此方法,同时流出字节。问题是没有代码注释这个方法试图完成什么。

The problematic code has been narrowed down to the file "iTextSharp/text/pdf/PdfChunck.cs" method "TrimFirstSpace". This method is called from the PdfDocument class while streaming out the bytes. The problem is that there is no code comments as to what this method trying to be accomplish.

我应该改变什么来使这项工作正常?似乎在这里评论ELSE条件应该解决这个问题。

What should I change to make this work right? It seems like commenting out the ELSE condition in here should fix this.

public float TrimFirstSpace()
{
    BaseFont ft = font.Font;
    if (ft.FontType == BaseFont.FONT_TYPE_CJK && ft.GetUnicodeEquivalent(' ') != ' ')
    {
            if (value.Length > 1 && value.StartsWith("\u0001"))
            {
                value = value.Substring(1);
                return font.Width('\u0001');
            }
        }
        else
        {
            if (value.Length > 1 && value.StartsWith(" "))
            {
                value = value.Substring(1);
                return font.Width(' ');
            }
        }
        return 0;
    }


推荐答案

较新的代码更改解决了问题。 if语句很重要。

Newer code changes address the issue. The if statement is important.

OLD

chunk = overflow;
chunk.TrimFirstSpace();


bool newlineSplit = chunk.IsNewlineSplit();
chunk = overflow;
if (!newlineSplit)
    chunk.TrimFirstSpace();

http://sourceforge.net/p/itextsharp/code/518/tree/trunk/src/core/ iTextSharp / text / pdf / PdfDocument.cs#l415

这篇关于iTextSharp修剪前导空间并导致列不对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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