iTextSharp trims leading space and causes misalignment of columns

北慕城南 提交于 2019-12-25 17:52:24

问题


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)

Input:

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

Output:

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

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.

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;
    }

回答1:


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

OLD

chunk = overflow;
chunk.TrimFirstSpace();

New

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



来源:https://stackoverflow.com/questions/14981437/itextsharp-trims-leading-space-and-causes-misalignment-of-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!