为有中文需求的客户提供多渠道中文技术支持.

Sun Nov 07, 2021 2:39 am

使用 Free Spire.PDF for .NET(dll only) 版本: 7.8.9

请问使用Grid生成的表格,可以在单元格内插入Forms中的Checkbox吗?
即在某一个Cell内,插入一行文字,换行,再插一个CheckBox

CCHMich30
 
Posts: 4
Joined: Sun Nov 07, 2021 2:20 am

Mon Nov 08, 2021 10:03 am

您好,

感谢您的咨询。
我们的Spire.PDF不支持直接在表格的单元格内添加复选框,我们的复选框域是根据位置来添加的。您可以通过查找单元格中的文本来获取文本位置和文本高度,再根据文本位置和高度来添加复选框域。参考代码按如下:
Code: Select all
             PdfDocument document = new PdfDocument();
            PdfPageBase page = document.Pages.Add();

            String[] data = {
                    "VendorN\r\name;Address;City",
                    "Cacor Corporation;161 Southfield Rd;Southfield",
                    "Underwater;50 N 3rd Street;Indianapolis",
                    "J.W. Luscher Mfg.;65 Addams Street;Berkely" ,
                    "E-iceblue; ; "};
            //create a grid
            PdfGrid grid = new PdfGrid();

            //add rows
            for (int r = 0; r < data.Length; r++)

            { PdfGridRow row = grid.Rows.Add(); }
            //add columns
            grid.Columns.Add(3);

            //set the width of columns
            float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
            grid.Columns[0].Width = width * 0.15f;
            grid.Columns[1].Width = width * 0.15f;
            grid.Columns[2].Width = width * 0.15f;

            //set the height of rows
            float height = page.Canvas.ClientSize.Height - (grid.Rows.Count + 1);
            grid.Rows[0].Height = 12.5f;
            grid.Rows[1].Height = 12.5f;
            grid.Rows[2].Height = 12.5f;
            grid.Rows[3].Height = 12.5f;
            grid.Rows[4].Height = 25f;

            //insert data to grid
            for (int r = 0; r < data.Length; r++)
            {
                String[] rowData = data[r].Split(';');
                for (int c = 0; c < rowData.Length; c++)

                { grid.Rows[r].Cells[c].Value = rowData[c]; }
            }
            grid.Rows[0].Style.Font = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Bold), true);

            float ColumnsWidth = grid.Columns[0].Width + grid.Columns[1].Width;
            page.Canvas.DrawLine(PdfPens.Red, new PointF(10 + ColumnsWidth, 30), new PointF(10 + ColumnsWidth, 30));
            grid.Draw(page, new PointF(10, 30));
            MemoryStream stream = new MemoryStream();
            // save as stream
            document.SaveToStream(stream);
            document.Close();

            PdfDocument pdf = new PdfDocument();
            // load stream
            pdf.LoadFromStream(stream);
            PdfPageBase page = pdf.Pages[0];
            //find all text
            PdfTextFindCollection pdfTexts = page.FindAllText();
            PdfTextFind[] result = pdfTexts.Finds;

            foreach (PdfTextFind find in result)
            {
                // Determine whether the text to be searched is "E-iceblue"
                if (find.MatchText == "E-iceblue")
                {
                    float x = find.Position.X;
                    float y = find.Position.Y;
                    float heights = find.Size.Height;
                    document.AllowCreateForm = true;
                    PdfCheckBoxField checkboxField = new PdfCheckBoxField(page, "checkboxTest");
                    float checkboxWidth = 15;
                    float checkboxHeight = 15;
                    checkboxField.Bounds = new RectangleF(x, y + heights, checkboxWidth, checkboxHeight);
                    checkboxField.BorderWidth = 0.75f;
                    checkboxField.Checked = true;
                    checkboxField.Style = PdfCheckBoxStyle.Check;
                    checkboxField.Required = true;
                    document.Form.Fields.Add(checkboxField);
                }
            }
            pdf.SaveToFile("Grid1.pdf");

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Wed Nov 10, 2021 6:07 am

Hello Annika

非常感谢,此方法可行,
可是获得的位置与实际的文本位置存在着一段偏移,这个该如何解决?
float x = find.Position.X;
float y = find.Position.Y;
2021-11-10_14h00_36.png

CCHMich30
 
Posts: 4
Joined: Sun Nov 07, 2021 2:20 am

Wed Nov 10, 2021 9:56 am

您好,

感谢你的反馈。
为了帮助我们快速有效的调查您的问题,请提供您的样本PDF文件。您可以附在这里或通过电子邮件(support@e-iceblue.com)发送给我们。提前感谢。

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Thu Nov 11, 2021 5:19 am

样表如附件
299.zip

CCHMich30
 
Posts: 4
Joined: Sun Nov 07, 2021 2:20 am

Thu Nov 11, 2021 7:50 am

您好,

感谢您的分享。
我使用下面的代码测试了您的PDF文件,发现获得的文本位置和实际的文本位置是一致的。我附上我的结果文件供你参考。请使用下面的代码再次测试。如果测试之后还是有问题,请提供你的测试环境(例如操作系统信息(如Windows 7, 64位))和应用程序类型(例如控制台应用程序(. Net Framework 4.5))供我们进一步调查。 提前感谢。
Code: Select all
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("299.pdf");
PdfPageBase pages = pdf.Pages[0];
PdfTextFind[] result = pages.FindText("合格", TextFindParameter.WholeWord).Finds;
for (int i = 0; i < result.Length;i++)
{
    float x = result[i].Position.X;
    float y = result[i].Position.Y;
    float heights = result[i].Size.Height;
    pdf.AllowCreateForm = true;
    PdfCheckBoxField checkboxField = new PdfCheckBoxField(pages, "checkboxTest"+i);
    float checkboxWidth = 12;
    float checkboxHeight = 12;
    checkboxField.Bounds = new RectangleF(x, y + heights, checkboxWidth, checkboxHeight);
    checkboxField.BorderWidth = 0.75f;
    checkboxField.Checked = false;
    checkboxField.Style = PdfCheckBoxStyle.Check;
    checkboxField.Required = true;
    checkboxField.BorderColor = Color.Black;
    pdf.Form.Fields.Add(checkboxField);
}
pdf.SaveToFile("output.pdf");

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Thu Nov 11, 2021 8:55 am

Annika,

感谢这几天的支持,问题已经解决,
测量的位置是准确的,问题出现在我的基准不对 :D

CCHMich30
 
Posts: 4
Joined: Sun Nov 07, 2021 2:20 am

Thu Nov 11, 2021 9:05 am

不客气!

有任何问题,欢迎再咨询我们。

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Return to 中文技术支持