Skip to content

Commit 9ca8977

Browse files
authored
Merge branch 'release/26.4.0' into examples/2cab5d23-facades-extract-images-and-tex
2 parents 8c37eb3 + 1ebad90 commit 9ca8977

30 files changed

Lines changed: 2833 additions & 0 deletions
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf.Facades;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
// Paths for the template PDF and the final output PDF
10+
const string templatePath = "template.pdf";
11+
const string outputPath = "output_with_date_header.pdf";
12+
13+
// Ensure the template file exists
14+
if (!File.Exists(templatePath))
15+
{
16+
Console.Error.WriteLine($"Template file not found: {templatePath}");
17+
return;
18+
}
19+
20+
// 1. Use AutoFiller to generate a PDF from the template.
21+
// In this example no data table is imported; the template is simply copied.
22+
using (AutoFiller autoFiller = new AutoFiller())
23+
{
24+
// Bind the template PDF file
25+
autoFiller.BindPdf(templatePath);
26+
27+
// Save the generated PDF into a memory stream for further processing
28+
using (MemoryStream generatedPdf = new MemoryStream())
29+
{
30+
autoFiller.Save(generatedPdf);
31+
generatedPdf.Position = 0; // Reset stream position for reading
32+
33+
// 2. Add a header with the current date to each page using PdfFileStamp.
34+
using (PdfFileStamp pdfStamp = new PdfFileStamp())
35+
{
36+
// Bind the generated PDF stream
37+
pdfStamp.BindPdf(generatedPdf);
38+
39+
// Prepare the header text (current date) as FormattedText.
40+
// FormattedText constructor requires System.Drawing.Color for the text color.
41+
string currentDate = DateTime.Now.ToString("D"); // e.g., "Monday, 15 April 2026"
42+
var headerText = new FormattedText(
43+
currentDate, // Text to display
44+
System.Drawing.Color.Black, // Text color
45+
"Helvetica", // Font name
46+
EncodingType.Winansi, // Encoding
47+
false, // Do not embed the font
48+
12); // Font size
49+
50+
// Add the header to all pages. The second argument is the top margin (in points).
51+
pdfStamp.AddHeader(headerText, 20f);
52+
53+
// Save the final PDF with the header.
54+
pdfStamp.Save(outputPath);
55+
}
56+
}
57+
}
58+
59+
Console.WriteLine($"PDF generated with date header: {outputPath}");
60+
}
61+
}

facades-fill-forms/agents.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
---
2+
name: facades-fill-forms
3+
description: C# examples for facades-fill-forms using Aspose.PDF for .NET
4+
language: csharp
5+
framework: net10.0
6+
parent: ../agents.md
7+
---
8+
9+
# AGENTS - facades-fill-forms
10+
11+
## Persona
12+
13+
You are a C# developer specializing in PDF processing using Aspose.PDF for .NET,
14+
working within the **facades-fill-forms** category.
15+
This folder contains standalone C# examples for facades-fill-forms operations.
16+
See the root [agents.md](../agents.md) for repository-wide conventions and boundaries.
17+
18+
## Scope
19+
- This folder contains examples for **facades-fill-forms**.
20+
- Files are standalone `.cs` examples stored directly in this folder.
21+
22+
## Required Namespaces
23+
24+
- `using Aspose.Pdf.Facades;` (27/28 files) ← category-specific
25+
- `using Aspose.Pdf;` (13/28 files)
26+
- `using Aspose.Pdf.Forms;` (3/28 files)
27+
- `using System;` (28/28 files)
28+
- `using System.IO;` (28/28 files)
29+
- `using System.Data;` (19/28 files) ← category-specific
30+
- `using System.Collections.Generic;` (5/28 files)
31+
- `using System.Threading;` (2/28 files)
32+
- `using System.Threading.Tasks;` (2/28 files)
33+
- `using System.Linq;` (1/28 files)
34+
- `using System.Text.Json;` (1/28 files)
35+
36+
## Common Code Pattern
37+
38+
Most files follow this pattern:
39+
40+
```csharp
41+
using (Document doc = new Document("input.pdf"))
42+
{
43+
// ... operations ...
44+
doc.Save("output.pdf");
45+
}
46+
```
47+
48+
## Files in this folder
49+
50+
| File | Title | Key APIs | Description |
51+
|------|-------|----------|-------------|
52+
| [add-current-date-header-to-pdf](./add-current-date-header-to-pdf.cs) | Add Current Date Header to PDF Using AutoFiller | `BindPdf`, `Save`, `BindPdf` | Shows how to generate a PDF from a template with AutoFiller and then add a header containing the ... |
53+
| [async-fill-pdf-form-from-xlsx](./async-fill-pdf-form-from-xlsx.cs) | Asynchronously Fill PDF Form from XLSX Data | `Document`, `Form`, `FillField` | The example reads an XLSX file asynchronously, extracts field values, fills a PDF form using Aspo... |
54+
| [batch-fill-pdf-template-with-multiple-datatables](./batch-fill-pdf-template-with-multiple-datatables.cs) | Batch Fill PDF Template with Multiple DataTables | `AutoFiller`, `BindPdf`, `ImportDataTable` | Shows how to repeatedly fill a single PDF form template using a list of DataTable objects, produc... |
55+
| [bind-pdf-form-to-autofiller](./bind-pdf-form-to-autofiller.cs) | Bind PDF Form to AutoFiller and Save | `AutoFiller`, `BindPdf`, `Save` | Demonstrates how to bind an existing PDF form to an Aspose.Pdf.Facades.AutoFiller instance and sa... |
56+
| [configure-datacolumn-properties-fill-pdf-form](./configure-datacolumn-properties-fill-pdf-form.cs) | Configure DataColumn Properties and Fill PDF Form with AutoF... | `Document`, `Page`, `Rectangle` | The example shows how to set DataColumn flags such as ReadOnly and Unique before importing a Data... |
57+
| [convert-filled-pdf-to-byte-array](./convert-filled-pdf-to-byte-array.cs) | Convert Filled PDF to Byte Array Using PdfViewer | `PdfViewer`, `BindPdf`, `Save` | Shows how to load a filled PDF with Aspose.Pdf.Facades.PdfViewer and return its content as a byte... |
58+
| [extract-pdf-pages-with-custom-names](./extract-pdf-pages-with-custom-names.cs) | Extract PDF Pages with Custom File Names from DataTable | `PdfFileEditor`, `Extract` | Shows how to split a PDF into individual pages using Aspose.Pdf.Facades.PdfFileEditor and name ea... |
59+
| [fill-pdf-form-async-timeout](./fill-pdf-form-async-timeout.cs) | Fill PDF Form Asynchronously with Timeout Cancellation | `Form`, `BindPdf`, `FillField` | Demonstrates how to fill a PDF form using Aspose.Pdf.Facades, then save the document asynchronous... |
60+
| [fill-pdf-form-from-csv](./fill-pdf-form-from-csv.cs) | Fill PDF Form from CSV Data | `Form`, `FillField`, `Save` | Shows how to read a CSV file into a DataTable, map column names to PDF form field names, and use ... |
61+
| [fill-pdf-form-per-datarow-merge-pages](./fill-pdf-form-per-datarow-merge-pages.cs) | Fill PDF Form per DataTable Row and Merge Pages with Logging | `Document`, `AutoFiller`, `BindPdf` | Demonstrates how to fill a PDF form for each DataTable row using AutoFiller, merge the generated ... |
62+
| [fill-pdf-form-using-autofiller-datatable](./fill-pdf-form-using-autofiller-datatable.cs) | Fill PDF Form Using AutoFiller and DataTable | `AutoFiller`, `BindPdf`, `ImportDataTable` | Demonstrates how to bind a PDF template, import a DataTable whose column names match AcroForm fie... |
63+
| [fill-pdf-form-validate-size](./fill-pdf-form-validate-size.cs) | Fill PDF Form and Validate Generated File Size | `AutoFiller`, `BindPdf`, `ImportDataTable` | Demonstrates using Aspose.Pdf.Facades.AutoFiller to merge a DataTable into a PDF form, save the r... |
64+
| [fill-pdf-form-with-autofiller](./fill-pdf-form-with-autofiller.cs) | Fill PDF Form Using AutoFiller and DataTable | `AutoFiller`, `BindPdf`, `ImportDataTable` | Shows how to bind a PDF template, import data from a DataTable (simulating an XLSX source), and g... |
65+
| [fill-pdf-form-with-autofiller__v2](./fill-pdf-form-with-autofiller__v2.cs) | Fill PDF Form Using AutoFiller and Dispose Resources | `AutoFiller`, `InputFileName`, `ImportDataTable` | Demonstrates how to populate a PDF form from a DataTable using Aspose.Pdf.Facades.AutoFiller and ... |
66+
| [fill-pdf-form-with-csv-dynamic-mapping](./fill-pdf-form-with-csv-dynamic-mapping.cs) | Fill PDF Form Using CSV Data with Dynamic Column Mapping | `AutoFiller`, `BindPdf`, `ImportDataTable` | Demonstrates loading CSV data into a DataTable, applying a JSON‑based column‑to‑PDF‑field mapping... |
67+
| [fill-pdf-form-with-data-table-exception-handling](./fill-pdf-form-with-data-table-exception-handling.cs) | Fill PDF Form with DataTable and Handle Exceptions | `AutoFiller`, `BindPdf`, `ImportDataTable` | The example shows how to use Aspose.Pdf.Facades.AutoFiller to bind a PDF template, import data fr... |
68+
| [fill-pdf-form-with-datatable-custom-columns](./fill-pdf-form-with-datatable-custom-columns.cs) | Fill PDF Form Using DataTable with Custom Columns | `Document`, `Page`, `TextBoxField` | Shows how to create a PDF form, add matching DataTable columns (including extra custom columns) v... |
69+
| [generate-pdf-page-summary-report](./generate-pdf-page-summary-report.cs) | Generate PDF Page‑to‑DataTable Summary Report | `Document`, `Save`, `Add` | Creates a PDF (or a blank one if missing), populates a DataTable with identifiers for each page, ... |
70+
| [generate-pdf-per-datarow](./generate-pdf-per-datarow.cs) | Generate One‑Page PDFs from DataTable Rows | `Document`, `Page`, `Rectangle` | Demonstrates loading a PDF form template, filling its fields with values from each DataTable row,... |
71+
| [generate-pdfs-from-multiple-csv-worksheets](./generate-pdfs-from-multiple-csv-worksheets.cs) | Generate PDFs from Multiple CSV Worksheets using AutoFiller | `AutoFiller`, `BindPdf`, `ImportDataTable` | The example reads each CSV file in a folder, converts it to a DataTable, and uses Aspose.Pdf.Faca... |
72+
| [map-datatable-columns-to-pdf-form-fields](./map-datatable-columns-to-pdf-form-fields.cs) | Map DataTable Columns to PDF Form Fields with AutoFiller | `AutoFiller`, `BindPdf`, `ImportDataTable` | Shows how to rename DataTable columns to match PDF form field identifiers and use Aspose.Pdf.Faca... |
73+
| [merge-filled-pdfs-from-datatables](./merge-filled-pdfs-from-datatables.cs) | Merge Multiple Filled PDFs Generated from DataTables | `BindPdf`, `ImportDataTable`, `Save` | The example fills a PDF form template with data from several DataTables, saves each filled docume... |
74+
| [password-protect-filled-pdf](./password-protect-filled-pdf.cs) | Password‑Protect a Filled PDF with Aspose.Pdf | `Document`, `Save`, `PdfFileSecurity` | Demonstrates loading an already filled PDF and applying user and owner passwords with specific pr... |
75+
| [process-pdf-with-temp-folder](./process-pdf-with-temp-folder.cs) | Process PDF with Temporary Folder and Disk Buffer | `Document`, `PdfSaveOptions`, `PdfFileEditor` | Demonstrates how to use a unique temporary folder and disk buffering while processing large PDFs ... |
76+
| [save-filled-pdf-preserve-layout](./save-filled-pdf-preserve-layout.cs) | Save Filled PDF While Preserving Layout | `Form`, `BindPdf`, `Save` | Shows how to bind a filled PDF using the Form facade and save it to a new file, keeping the origi... |
77+
| [split-filled-pdf-into-single-page-pdfs](./split-filled-pdf-into-single-page-pdfs.cs) | Split Filled PDF into Single-Page PDFs | `PdfFileEditor`, `SplitToPages` | Demonstrates loading a filled PDF and using Aspose.Pdf.Facades.PdfFileEditor to split it into ind... |
78+
| [stream-large-csv-fill-pdf-form](./stream-large-csv-fill-pdf-form.cs) | Stream Large CSV Data into PDF Form Using AutoFiller | `AutoFiller`, `BindPdf`, `ImportDataTable` | Demonstrates how to read a large CSV file row‑by‑row into a DataTable and use Aspose.Pdf.Facades.... |
79+
| [validate-required-pdf-form-fields](./validate-required-pdf-form-fields.cs) | Validate Required PDF Form Fields Against DataTable Columns | `Form`, `FieldNames`, `IsRequiredField` | Shows how to check that every required AcroForm field in a PDF has a matching column in a DataTab... |
80+
81+
## Category Statistics
82+
- Total examples: 28
83+
84+
## Category-Specific Tips
85+
86+
### Key API Surface
87+
- `Aspose.Pdf.Facades.FieldType`
88+
- `Aspose.Pdf.Facades.Form`
89+
- `Aspose.Pdf.Facades.Form.BindPdf`
90+
- `Aspose.Pdf.Facades.Form.BindPdf(string)`
91+
- `Aspose.Pdf.Facades.Form.ExportFdf`
92+
- `Aspose.Pdf.Facades.Form.FillField(string, string)`
93+
- `Aspose.Pdf.Facades.Form.GetField(string)`
94+
- `Aspose.Pdf.Facades.Form.ImportFdf`
95+
- `Aspose.Pdf.Facades.Form.Save`
96+
- `Aspose.Pdf.Facades.Form.Save(string)`
97+
- `Aspose.Pdf.Facades.FormEditor`
98+
- `Aspose.Pdf.Facades.FormEditor.BindPdf`
99+
- `Aspose.Pdf.Facades.FormEditor.CopyOuterField`
100+
- `Aspose.Pdf.Facades.FormEditor.Save`
101+
- `Aspose.Pdf.Facades.FormFieldFacade`
102+
103+
### Rules
104+
- Bind a PDF file to a Form facade with Form.BindPdf({input_pdf}).
105+
- Flatten every form field in the bound document by calling Form.FlattenAllFields().
106+
- Persist the flattened document using Form.Save({output_pdf}).
107+
- Use Form.BindPdf({input_pdf}) to open a PDF document for form manipulation.
108+
- Open an FDF file as a stream and call Form.ImportFdf({fdf_stream}) to populate the PDF form fields.
109+
110+
### Warnings
111+
- The Form class belongs to the Aspose.Pdf.Facades namespace, which may be deprecated in future releases; consider using the newer Document/FormField APIs.
112+
- The example manually manages the FileStream; ensure the stream is closed or disposed to avoid resource leaks.
113+
- The example assumes the target PDF already contains an AcroForm; otherwise AddField may have no effect.
114+
- Coordinate values are in points; callers must convert from other units if needed.
115+
- FormFieldFacade.Alignment expects one of the FormFieldFacade alignment constants (e.g., AlignCenter).
116+
117+
## General Tips
118+
- See parent [agents.md](../agents.md) for:
119+
- **Boundaries** — Always / Ask First / Never rules for all examples
120+
- **Common Mistakes** — verified anti-patterns that cause build failures
121+
- **Domain Knowledge** — cross-cutting API-specific gotchas
122+
- **Testing Guide** — build and run verification steps
123+
- Review code examples in this folder for facades-fill-forms patterns
124+
125+
<!-- AUTOGENERATED:START -->
126+
Updated: 2026-05-08 | Run: `20260508_144436_050a95`
127+
<!-- AUTOGENERATED:END -->
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Aspose.Pdf;
6+
using Aspose.Pdf.Facades;
7+
8+
class Program
9+
{
10+
// Async entry point (C# 7.1+)
11+
static async Task Main(string[] args)
12+
{
13+
// Paths to the source XLSX file, PDF template, and the output PDF.
14+
const string xlsxPath = "data.xlsx";
15+
const string pdfTemplatePath = "template.pdf";
16+
const string outputPdfPath = "filled.pdf";
17+
18+
// -----------------------------------------------------------------
19+
// 1. Read the XLSX source asynchronously.
20+
// This uses the built‑in .NET async file I/O API.
21+
// -----------------------------------------------------------------
22+
byte[] xlsxBytes;
23+
try
24+
{
25+
xlsxBytes = await File.ReadAllBytesAsync(xlsxPath);
26+
}
27+
catch (Exception ex)
28+
{
29+
Console.Error.WriteLine($"Failed to read XLSX file: {ex.Message}");
30+
return;
31+
}
32+
33+
// -----------------------------------------------------------------
34+
// 2. Extract data from the XLSX byte array.
35+
// For brevity, we mock the extraction with a simple dictionary.
36+
// In a real scenario you would parse the XLSX (e.g., using
37+
// a library such as ClosedXML or EPPlus) and populate the
38+
// dictionary with field names and values.
39+
// -----------------------------------------------------------------
40+
var fieldValues = new System.Collections.Generic.Dictionary<string, string>
41+
{
42+
{ "CustomerName", "John Doe" },
43+
{ "InvoiceNumber", "INV-1001" },
44+
{ "TotalAmount", "1234.56" }
45+
// Add more field/value pairs as needed.
46+
};
47+
48+
// -----------------------------------------------------------------
49+
// 3. Load the PDF template using Aspose.Pdf.Document.
50+
// The Document is wrapped in a using block for deterministic disposal.
51+
// -----------------------------------------------------------------
52+
if (!File.Exists(pdfTemplatePath))
53+
{
54+
Console.Error.WriteLine($"PDF template not found: {pdfTemplatePath}");
55+
return;
56+
}
57+
58+
using (Document pdfDoc = new Document(pdfTemplatePath))
59+
{
60+
// -----------------------------------------------------------------
61+
// 4. Fill the PDF form fields using the Aspose.Pdf.Facades.Form class.
62+
// The Form facade is also disposed via a using block.
63+
// -----------------------------------------------------------------
64+
using (Form form = new Form(pdfDoc))
65+
{
66+
foreach (var kvp in fieldValues)
67+
{
68+
// Fill each field with the corresponding value.
69+
// The FillField method accepts the field name and the value as a string.
70+
form.FillField(kvp.Key, kvp.Value);
71+
}
72+
}
73+
74+
// -----------------------------------------------------------------
75+
// 5. Save the filled PDF asynchronously.
76+
// SaveAsync(string, CancellationToken) writes the PDF to disk
77+
// without blocking the calling thread.
78+
// -----------------------------------------------------------------
79+
try
80+
{
81+
await pdfDoc.SaveAsync(outputPdfPath, CancellationToken.None);
82+
Console.WriteLine($"PDF successfully saved to '{outputPdfPath}'.");
83+
}
84+
catch (Exception ex)
85+
{
86+
Console.Error.WriteLine($"Failed to save PDF: {ex.Message}");
87+
}
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)