// ........ using var loadedDocument = new PdfLoadedDocument(sourceStream); var loadedForm = loadedDocument.Form; loadedForm.EnableXfaFormFill = true; loadedForm.DisableAutoFormat = false; // Or true also do not affect
// ........
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net5.0</TargetFramework> <LangVersion>latest</LangVersion> <AnalysisLevel>latest</AnalysisLevel> <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> </PropertyGroup> <PropertyGroup> <OutputPath>..\..\output</OutputPath> </PropertyGroup> <ItemGroup> <PackageReference Include="Syncfusion.Pdf.Net.Core" Version="18.4.0.44" /> <!--<PackageReference Include="Syncfusion.Pdf.WinForms" Version="18.4.0.44" />--> <PackageReference Include="System.Drawing.Common" Version="5.0.1" /> </ItemGroup> </Project>
using System; using System.Globalization; using System.IO; using System.Reflection; using Syncfusion.Pdf.Parsing; namespace SyncfusionPdf { internal static class Program { private static readonly CultureInfo _cultureInfo = new CultureInfo("nl-NL"); private static void Main() { var startupPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var sourceSjabloonPath = Path.Join(startupPath, "Netto-inkomen (A).pdf"); using var sourceSjablonenStream = File.Open(sourceSjabloonPath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read); var loadedDocument = new PdfLoadedDocument(sourceSjablonenStream); var loadedForm = loadedDocument.Form; loadedForm.EnableXfaFormFill = true; loadedForm.DisableAutoFormat = false; // Or true also do not affect loadedForm.SetDefaultAppearance(false); foreach (PdfLoadedField field in loadedForm.Fields) { Console.WriteLine($"{{Type: {field.GetType().Name}, Name: {field.Name.Trim()}, ObjectID: {field.ObjectID}}}"); switch (field) { case PdfLoadedTextBoxField pdfLoadedTextBoxField when pdfLoadedTextBoxField.Name != "A": pdfLoadedTextBoxField.Text = 5.22M.ToString("C2", _cultureInfo); break; } } var targetSjabloonPath = Path.Join(startupPath, $"{Guid.NewGuid()}.pdf"); using var targetSjablonenStream = File.Open(targetSjabloonPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); loadedDocument.Save(targetSjablonenStream); loadedDocument.Close(true); sourceSjablonenStream.Close(); targetSjablonenStream.Flush(); targetSjablonenStream.Close(); } } }
|
//Load PDF document
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
var loadedForm = loadedDocument.Form;
float total = 0;
loadedForm.EnableXfaFormFill = true;
loadedForm.DisableAutoFormat = false;
loadedForm.SetDefaultAppearance(true);
foreach (PdfLoadedField field in loadedForm.Fields)
{
Console.WriteLine($"{{Type: {field.GetType().Name}, Name: {field.Name.Trim()}, ObjectID:{field.ObjectID}}}");
switch (field)
{
case PdfLoadedTextBoxField pdfLoadedTextBoxField whenpdfLoadedTextBoxField.Name != "A":
pdfLoadedTextBoxField.Text = 5.22M.ToString("C2", _cultureInfo);
total += 5.22f;
break;
case PdfLoadedTextBoxField pdfLoadedTextBoxField whenpdfLoadedTextBoxField.Name == "A":
pdfLoadedTextBoxField.Text = total.ToString("C2", _cultureInfo);
break;
}
}
//Save and close the document
MemoryStream stream = new MemoryStream();
loadedDocument.Save(stream);
loadedDocument.Close(true);
|