【问题标题】:iTextSharp Duplicating AcrofieldsiTextSharp 复制 Acrofields
【发布时间】:2018-04-02 12:09:44
【问题描述】:

我的文档第 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());

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

【问题讨论】:

  • 在 PDF 中,如果同一类型的两个字段具有相同的名称,如果一个接收到一个值,它们都会更新。您是否为这两个字段分配了相同的名称?
  • 如果您给它们提供了相同的名称“tfNotes”,您是否验证了 PDF 是根据需要使用这些名称生成的,并且它们没有被作者覆盖(即 page1_tfNotes、page2_tfNotes) .
  • 我确定现有字段名为 tfNotes,但是当我查看第二页时,单元格中没有任何内容。如果我查看 RUPS,它只是 Annots 下该页面上的一个空数组,所以我知道这里出了点问题。它实际上并没有创建重复字段。

标签: c# pdf itext acrofields


【解决方案1】:

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

                    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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    相关资源
    最近更新 更多