0

我的文档第 1 页上有一个 acroform。我正在创建一个空白的第二页,我也想在此页面上显示其中一个字段。如果用户在任一页面上更改此字段的值,我希望它们都显示新值。我发现一些帖子显示了这种类型的解决方案,但它没有按预期工作:

                    PdfFormField fNotes = PdfFormField.CreateTextField(writer, false, false, 500);
                string fieldNotes = "tfNotes";
                fNotes.FieldName = fieldNotes;
                PdfFormField widgetNotes = PdfFormField.CreateEmpty(writer);

                PdfPCell notesCell = new PdfPCell(new Paragraph("test", helv8))
                {
                    BorderWidthLeft = 0,
                    BorderWidthRight = 0,
                    BorderWidthTop = 0,
                    BorderWidthBottom = .5f
                };
                TextField tField = new TextField(writer, new iTextSharp.text.Rectangle(0, 0, 100, 100), fieldNotes);
                writer.AddAnnotation(fNotes);
                writer.AddAnnotation(widgetNotes);

在此示例中,第 1 页上的字段名为 tfNotes。如果我理解正确,我需要创建对 acrofield 的第二个小部件引用。我认为上面的代码将用于一个尚不存在的字段,在我的情况下它确实存在。有没有更简单的方法可以在第 1 页按名称获取字段并在第 2 页创建对其的重复引用?

感谢您的时间!

编辑:

当我将表格从等式中取出时,这可以按预期工作,但是一旦我尝试将重复的字段放入 PdfPCell 中,它就会显示在页面上的 0,0 处,宽度和高度为 0。出于某种原因,矩形单元格返回为 0,0,0,0。

PdfPCell notesCell = new PdfPCell()
                {
                    BorderWidthLeft = 0,
                    BorderWidthRight = 0,
                    BorderWidthTop = 0,
                    BorderWidthBottom = .5f
                };
                //Notes Cell Rectangle
                float llxNotes = notesCell.GetLeft(0);
                float llyNotes = notesCell.GetBottom(0);
                float urxNotes = notesCell.GetRight(0);
                float uryNotes = notesCell.GetTop(0);

                //Notes Duplicate
                string fieldNotesName = "tfQuoteNotes";
                TextField tField = new TextField(writer, new iTextSharp.text.Rectangle(llxNotes, llyNotes, urxNotes, uryNotes), fieldNotesName)
                {
                    FieldName = fieldNotesName
                };
                tField.SetRotationFromPage(doc.PageSize);
                writer.AddAnnotation(tField.GetTextField());

我想我现在需要做的就是为单元格找到正确的矩形坐标。

4

1 回答 1

0

所以我缺少的部分是一个 CellEvent,我不完全理解为什么,但这是任何需要它的人的最终工作片段:

                    PdfPCell notesCell = new PdfPCell()
                {
                    BorderWidthLeft = 0,
                    BorderWidthRight = 0,
                    BorderWidthTop = 0,
                    BorderWidthBottom = .5f
                };

                //Notes Cell Rectangle
                float llxNotes = notesCell.GetLeft(0);
                float llyNotes = notesCell.GetBottom(0);
                float urxNotes = notesCell.GetRight(0);
                float uryNotes = notesCell.GetTop(0);

                //Notes Duplicate
                string fieldNotesName = "tfQuoteNotes";
                TextField tField = new TextField(writer, new iTextSharp.text.Rectangle(llxNotes, llyNotes, urxNotes, uryNotes), fieldNotesName)
                {
                    FieldName = fieldNotesName
                };
                tField.SetRotationFromPage(doc.PageSize); //Needed since this page is rotated 90 from the first page

                iTextSharp.text.pdf.events.FieldPositioningEvents events = new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tField.GetTextField());
                writer.AddAnnotation(tField.GetTextField());
                notesCell.CellEvent = events; //This somehow ignores the original rectangle (0,0,0,0) in the TextField definition above and changes it to the cell's rectangle
于 2018-04-02T15:19:59.413 回答