使用 Free Spire.PDF for .NET(dll only) 版本: 7.8.9
请问使用Grid生成的表格,可以在单元格内插入Forms中的Checkbox吗?
即在某一个Cell内,插入一行文字,换行,再插一个CheckBox
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");
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");