Reputation: 1
I'm a beginner programmer and attempting to write a vb.net code to fill pdf forms using Sautinsoft or ASPOSE. My goal is to populate the fields within a pdf template using the data from the data set. For example, assume creation of a student report card using unique identifiers from source data. Each student report card should be created as a separate pdf file using the pdf template. Is anyone able to share a sample code?
Note: I'm looking for either Sautinsoft or ASPOSE code in particular (and not itextsharp).
Thanks in advance.
Upvotes: 0
Views: 264
Reputation: 131
To fill a PDF form using Sautinsoft.Document library in VB.Net, you can follow the example below. This code demonstrates how to populate fields within a PDF template.
Option Infer On
Imports Microsoft.VisualBasic
Imports System
Imports System.IO
Imports SautinSoft
Imports SautinSoft.Pdf
Imports SautinSoft.Pdf.Content
Namespace Sample
Friend Class Sample
''' <summary>
''' Fill in PDF interactive forms.
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/fill-in-pdf-interactive-forms.php
''' </remarks>
Shared Sub Main(ByVal args() As String)
' Before starting this example, please get a free 100-day trial key:
' https://sautinsoft.com/start-for-free/
' Apply the key here:
' PdfDocument.SetLicense("...");
Dim pdfFile As String = Path.GetFullPath("..\..\..\Form.pdf")
'Load PDF Document with forms.
Using document = PdfDocument.Load(pdfFile)
' Fill the form fields.
document.Form.Fields("FullName").Value = "Jane Doe"
document.Form.Fields("ID").Value = "0123456789"
document.Form.Fields("Gender").Value = "Female"
document.Form.Fields("Married").Value = "Yes"
document.Form.Fields("City").Value = "Berlin"
document.Form.Fields("Language").Value = New String() { "German", "Italian" }
document.Form.Fields("Notes").Value = "Notes first line" & vbCr & "Notes second line" & vbCr & "Notes third line"
' Save PDF Document.
document.Save("FormFilled.pdf")
End Using
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("FormFilled.pdf") With {.UseShellExecute = True})
End Sub
End Class
End Namespace
If you are interested in the code sample for C#, you can follow this link.
Upvotes: 0
Reputation: 11
Aspose is a cool component, but expensive. SautinSoft - not very famous, but the functionality is the same and cheaper at times.
For your task, you can do this:
Code:
static void CustomDataSource()
{
// Populate some data that we will use in the mail merge.
List<Actor> actors = new List<Actor>();
actors.Add(new Actor("Arnold Schwarzenegger", "12989 Chalon Road, Los Angeles, CA 90049"));
actors.Add(new Actor("Sylvester Stallone", "30 Beverly Park Terrace, Beverly Hills, CA 90210"));
// Populate some data for nesting in the mail merge.
actors[0].Orders.Add(new Order("Bowflex SelectTech 1090 Adjustable Dumbbell", 2));
actors[0].Orders.Add(new Order("Gold's Gym Kettlebell Kit, 5-15 Lbs.", 1));
actors[1].Orders.Add(new Order("Weider Cast Iron Olympic Hammertone Weight Set, 300 Lb.", 1));
// Load the template document.
DocumentCore dc = DocumentCore.Load(@"..\..\..\Template.docx");
// To be able to mail merge from your own data source, it must be wrapped into an object that implements the IMailMergeDataSource interface.
CustomMailMergeDataSource customDataSource = new CustomMailMergeDataSource(actors);
// Execute the mail merge.
dc.MailMerge.Execute(customDataSource);
string resultPath = "StudentReportCard.pdf";
// Save the output to file.
dc.Save(resultPath);
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
}
/// <summary>
/// An example of a class that contain actor's data.
/// </summary>
public class Actor
{
private string _fullName;
private string _address;
private List<Order> _orders;
public string FullName
{
get { return _fullName; }
set { _fullName = value; }
}
public string Address
{
get { return _address; }
set { _address = value; }
}
public List<Order> Orders
{
get { return _orders; }
set { _orders = value; }
}
public Actor(string fullName, string address)
{
_fullName = fullName;
_address = address;
_orders = new List<Order>();
}
}
/// <summary>
/// An example of a class that contain order's data.
/// </summary>
public class Order
{
private string _name;
private int _quantity;
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Quantity
{
get { return _quantity; }
set { _quantity = value; }
}
public Order(string name, int quantity)
{
_name = name;
_quantity = quantity;
}
}
/// <summary>
/// A custom mail merge data source that allows SautinSoft.Document to retrieve data from Actor objects.
/// </summary>
public class CustomMailMergeDataSource : IMailMergeDataSource
{
private readonly List<Actor> _actors;
private int _recordIndex;
/// <summary>
/// The name of the data source.
/// </summary>
public string Name
{
get { return "Actor"; }
}
/// <summary>
/// SautinSoft.Document calls this method to get a value for every data field.
/// </summary>
public bool TryGetValue(string valueName, out object value)
{
switch (valueName)
{
case "FullName":
value = _actors[_recordIndex].FullName;
return true;
case "Address":
value = _actors[_recordIndex].Address;
return true;
default:
// A field with this name was not found
value = null;
return false;
}
}
/// <summary>
/// A standard implementation for moving to a next record in a collection.
/// </summary>
public bool MoveNext()
{
return (++_recordIndex < _actors.Count);
}
public IMailMergeDataSource GetChildDataSource(string sourceName)
{
switch (sourceName)
{
case "Order":
return new OrderMailMergeDataSource(_actors[_recordIndex].Orders);
default:
return null;
}
}
public CustomMailMergeDataSource(List<Actor> actors)
{
_actors = actors;
// When the data source is initialized, it must be positioned before the first record.
_recordIndex = -1;
}
}
public class OrderMailMergeDataSource : IMailMergeDataSource
{
private readonly List<Order> _orders;
private int _recordIndex;
/// <summary>
/// The name of the data source.
/// </summary>
public string Name
{
get { return "Order"; }
}
/// <summary>
/// SautinSoft.Document calls this method to get a value for every data field.
/// </summary>
public bool TryGetValue(string valueName, out object value)
{
switch (valueName)
{
case "Name":
value = _orders[_recordIndex].Name;
return true;
case "Quantity":
value = _orders[_recordIndex].Quantity;
return true;
default:
// A field with this name was not found
value = null;
return false;
}
}
/// <summary>
/// A standard implementation for moving to a next record in a collection.
/// </summary>
public bool MoveNext()
{
return (++_recordIndex < _orders.Count);
}
// Return null because Order haven't any child elements.
public IMailMergeDataSource GetChildDataSource(string tableName)
{
return null;
}
public OrderMailMergeDataSource(List<Order> orders)
{
_orders = orders;
// When the data source is initialized, it must be positioned before the first record.
_recordIndex = -1;
}
}
Upvotes: 0