Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
在以下场景中,我们将使用一个 PDF 表单,其中包含三个表单字段,分别命名为 ID、Name 和 Gender。
上述表单有一页,包含三个字段,分别命名为“ID”、“Name”和“Gender”。我们将从以下 Excel 工作表中提取数据到 DataTable 对象中。
我们需要创建一个 AutoFiller 类的对象,并绑定上述图片中的 PDF 表单,并使用 ImportDataTable 方法使用 DataTable 对象中的数据填写表单字段。 一旦调用该方法,将生成一个新的 PDF 表单文件,其中包含五页,表单根据 Excel 工作表中的数据填写。输入的 PDF 表单是单页的,而结果是五页,因为 Excel 工作表中的数据行数为 5。DataTable 类提供了使用工作表的第一行作为列名的能力。
![]() |
![]() |
---|---|
![]() |
![]() |
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ExportExcelToPdfForm()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Excel();
var workbook = new Workbook();
// Creating a file stream containing the Excel file to be opened
using (FileStream fstream = new FileStream(dataDir + "newBook1.xls", FileMode.Open))
{
// Opening the Excel file through the file stream
workbook.Open(fstream);
// Accessing the first worksheet in the Excel file
var worksheet = workbook.Worksheets[0];
// Exporting the contents of 7 rows and 2 columns starting from 1st cell to DataTable
System.Data.DataTable dataTable = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxRow + 1, worksheet.Cells.MaxColumn + 1, true);
// Create an object of AutoFiller class
using (var autoFiller = new Aspose.Pdf.Facades.AutoFiller())
{
// The input pdf file that contains form fields
autoFiller.InputFileName = dataDir + "DataTableExample.pdf";
// The resultant pdf, that will contain the form fields filled with information from DataTable
autoFiller.OutputFileName = dataDir + "DataTableExample_out.pdf";
// Call the method to import the data from DataTable object into Pdf form fields
autoFiller.ImportDataTable(dataTable);
// Save PDF document
autoFiller.Save();
}
}
}
要从 XLSX 填充,请使用以下代码片段:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void FillFromXLSX()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Excel();
// Create an object of AutoFiller class
using (var autoFiller = new Aspose.Pdf.Facades.AutoFiller())
{
// Bind PDF document
autoFiller.BindPdf(dataDir + "Sample-Form-01.pdf");
System.Data.DataTable dataTable = GenerateDataTable();
// Call the method to import the data from DataTable object into Pdf form fields
autoFiller.ImportDataTable(dataTable);
// Save PDF document
autoFiller.Save(dataDir + "Sample-Form-01_out.pdf");
}
}
Aspose.PDF for .NET 允许您在 PDF 文档中生成数据表:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static System.Data.DataTable GenerateDataTable()
{
string[] names = new[] { "Olivia", "Oliver", "Amelia", "George", "Isla", "Harry", "Ava", "Noah" };
// Create a new DataTable
var table = new System.Data.DataTable("Students");
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable
var column = new System.Data.DataColumn
{
DataType = System.Type.GetType("System.Int32"),
ColumnName = "id",
ReadOnly = true,
Unique = true
};
// Add the Column to the DataColumnCollection
table.Columns.Add(column);
// Create second column
column = new System.Data.DataColumn
{
DataType = System.Type.GetType("System.String"),
ColumnName = "First Name",
AutoIncrement = false,
Caption = "First Name",
ReadOnly = false,
Unique = false
};
// Add the column to the table
table.Columns.Add(column);
// Make the ID column the primary key column
var primaryKeyColumns = new System.Data.DataColumn[1];
primaryKeyColumns[0] = table.Columns["id"];
table.PrimaryKey = primaryKeyColumns;
// Create three new DataRow objects and add
// them to the DataTable
var rand = new Random();
System.Data.DataRow row;
for (int i = 1; i <= 4; i++)
{
row = table.NewRow();
row["id"] = i;
row["First Name"] = names[rand.Next(names.Length)];
table.Rows.Add(row);
}
return table;
}
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.