iTextSharp中多行文本表单字段的对齐问题 [英] Alignment issue with multiline text form fields in iTextSharp

查看:361
本文介绍了iTextSharp中多行文本表单字段的对齐问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个应用程序,以编程方式从数据库中填写PDF模板中的表单,并将结果保存到磁盘。除了没有按预期渲染的任何多行文本字段外,一切都正常工作。它们应该是顶部,左对齐,线之间没有间隙。

I have written an application to programatically fill out a form in a PDF template from a database and save the result to disk. Everything is working correctly apart from any multi-line text fields which are not rendering as expected. They should be top, left aligned with no gaps between lines.

我得到的结果是:

The result I get is here :

然而,当我使用任何PDF阅读器点击表单字段时,该字段纠正自己的期望。请参阅:

However, when I click into the form field using any PDF reader, the field corrects itself to what I would expect. See :

我使用的代码很漂亮。

            PdfStamper stamper = new PdfStamper(reader, ms, '\0', false);

            AcroFields form = stamper.AcroFields;
            List<DocumentField> fields = GetData(id);
            foreach (DocumentField field in fields)
            {
                form.SetField(field.FieldName, field.Value);
            }

            stamper.FormFlattening = true;
            stamper.Close();
            reader.Close();

我正在使用 System.Environment.NewLine 添加carraige返回。有没有人知道可能导致这种行为的原因以及使左上角没有大间隙的解决方案。谢谢。

I am using System.Environment.NewLine to add the carraige returns. Does anyone know what might be causing this behaviour and the solution to make the top left aligned without the large gaps. Thanks.

使用解决方案更新

我删除了该字段并重新添加它和它表现得如预期的那样。实际上似乎问题是我使用的是一种名为'Cambria'的字体,如果我将字段设置回使用该字体,则返回行为。

I removed the field and re-added it and it behaved as expected. What actually seems to be the problem is that I was using a font called 'Cambria' which if I set the field back to using that font, the behaviour returned.

推荐答案

请查看写入的 MultiLineField 测试你的指控。在此示例中,我们使用最简单的多行文本字段创建表单:

Please take a look at the MultiLineField that was written to test your allegation. In this example, we create a form with the simplest possible multi-line text field:

Rectangle rect = new Rectangle(36, 720, 144, 806);
TextField tf = new TextField(writer, rect, "text");
tf.setOptions(TextField.MULTILINE);
writer.addAnnotation(tf.getTextField());

然后我们填写并展平这个表格如下:

Then we fill and flatten this form like this:

public void createPdf(String filename) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(createForm());
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(filename));
    AcroFields form = stamper.getAcroFields();
    form.setField("text", "A B C D E F\nG H I J K L M N\nO P Q R S T U\r\nV W X Y Z\n\nAlphabet street");
    stamper.setFormFlattening(true);
    stamper.close();
}

仔细查看字符串

"A B C D E F\nG H I J K L M N\nO P Q R S T U\r\nV W X Y Z\n\nAlphabet street"

我介绍一些换行符( \ n )甚至回车/换行序列( \\\ n )。这些引入了一条新线。只有当我引入多个换行符( \ n \ n )时,才会引入额外的差距:

I introduce some newline characters (\n) and even a carriage return/newline sequence (\r\n). These introduce a single new line. Only when I introduce more than one newline character (\n\n), an extra gap is introduced:

有三个您的代码行为不同的可能原因:

There are three possible reasons why your code behaves differently:


  1. 您的表单不同。您的文本字段有一些非常具体的内容。如果是,请分享您的表格以供检查。 [更新:>如评论中所述,文本字段确实有一些特别之处;更新字段解决了问题。]

  2. 您添加了太多新行。也许21 High Street已经出现了换行符。也许你的系统上的换行序列有些奇怪。请注意,这是PDF:它与操作系统无关,只需使用 / n 就足够了。

  3. 也许您使用的是非官方版本iText。这对我们(iText集团)来说非常令人沮丧,但有些人发现有必要创建包含代码的分支,这些代码的行为与官方iText的行为方式不同。确保您使用的是iText的正版副本。

  1. Your form is different. There's something very specific about your text field. If so, please share your form for inspection. [Update: as mentioned in the comments, there was indeed something special about the text field; updating the field solved the problem.]
  2. You are adding too many newlines. Maybe "21 High Street," is already followed by a newline. Maybe there's something odd about the newline sequence on your system. Note that this is PDF: it is OS independent, just using /n is sufficient.
  3. Maybe you're using an unofficial version of iText. It is very frustrating for us (the iText Group), but some people found it necessary to create forks that contain code that simply doesn't behave the way the official iText behaves. Make sure that you're using a genuine copy of iText.

在任何情况下:您在示例中共享的屏幕截图都没有匹配你的代码。在您的代码中,您有:

In any case: the screen shots you shared in your example do not match your code. In your code, you have:

stamper.FormFlattening = true;

这与显示交互性的屏幕截图不一致。这只有在以下情况下才有可能:

This is inconsistent with your screen shots that show interactivity. This is only possible if you have:

stamper.FormFlattening = false;

或者如果此行不存在。

这篇关于iTextSharp中多行文本表单字段的对齐问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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