首页 > 解决方案 > 使用 Aspose PDF .Net 时添加文本框字段会引发“IndexOutBoundsException”

问题描述

我在 .Net 中使用 Aspose PDF 来创建 PDF。我在我的代码中添加了文本框字段。在添加字段之前,我的代码运行良好,并且 pdf 已成功创建。由于我为文本框字段添加了代码,因此我的代码在保存文件时会引发上述异常。

在测试期间,它生成了一次文本字段,但在多次失败之后。以下是我用于在表格中创建文本字段的代码

public static void CreateTextboxTable(Document doc, Page page)
    {
        Table table = new Table();
        table.ColumnWidths = "250 250";
        table.Border = new BorderInfo(BorderSide.All, .5f, Color.FromRgb(System.Drawing.Color.Black));
        // Set the border for table cells
        table.DefaultCellBorder = new BorderInfo(BorderSide.All, .5f, Color.FromRgb(System.Drawing.Color.Black));
        table.Margin.Top = 5;
        table.DefaultCellPadding = new MarginInfo(3, 3, 3, 3);
        page.Paragraphs.Add(table);

        Row row1 = table.Rows.Add();
        Cell cell = new Cell();
        cell = row1.Cells.Add("Full Name:");

        Cell fieldCell = row1.Cells.Add();
        // Create a field
        TextBoxField textBoxField = new TextBoxField(doc);
        textBoxField.PartialName = "textbox1";
        textBoxField.Value = "";
        textBoxField.Width = 150;
        textBoxField.Height = 20;
        //textBoxField.Margin.Top = 20;
        Border border = new Border(textBoxField);
        border.Width = 1;
        textBoxField.Border = border;
        textBoxField.Color = Color.FromRgb(System.Drawing.Color.Black);
        // Add field to the document
        doc.Form.Add(textBoxField, 1);
        fieldCell.Paragraphs.Add(textBoxField);
    }

执行此行时发生异常:

pdfDoc.Save(位置);

如果我删除对该函数的调用,则生成 pdf。堆栈跟踪也如下:

at Aspose.Pdf.Forms.Field.5kzkyaw9ehlf8lze3k4btj845lj3sczy    (    )
at Aspose.Pdf.Forms.TextBoxField.5kzkyaw9ehlf8lze3k4btj845lj3sczy    (    )
at Aspose.Pdf.Forms.Field.hulrgtdcn4a36jvghassc3d9p8uvbscd    (    , Rectangle )
at ​  .(Field )
at ​  .()
at Aspose.Pdf.Cell.(Table , Double , Page , Double , Double , Boolean )
at Aspose.Pdf.Row.(Table , Page , Double , Double , Int32 )
at Aspose.Pdf.Row.(Table , Double& , Double& , Boolean , Double , Double , Page , Int32&  )
at Aspose.Pdf.Table.(Double& , Double& , Boolean , Double , Double , Page , Boolean , List`1  )
at ​  .()
at Aspose.Pdf.Page.(Page )
at Aspose.Pdf.Document.ProcessParagraphs()
at Aspose.Pdf.Document.(Stream , SaveOptions )
at Aspose.Pdf.Document.(String )
at Aspose.Pdf.Document.Save(String outputFileName)
at AsposePdfApp.Common.Helper.CreatePdf() in \Common\Helper.cs:line 42

这个堆栈跟踪给出的提示问题是文本框字段。

请指导我此代码中的问题在哪里。谢谢

标签: c#.netasposeaspose.pdf

解决方案


推荐阅读