Editing PDF Forms within Silverlight
2012
PDF form or AcroForms allow users to interactively edit specific portions of a PDF document in supporting viewer applications. A PDF form is composed by one or more AcroForm fields that provide a name-value association once they have been edited.
Amyuni PDF for Silverlight is an online PDF viewer based on Amyuni PDF Creator as a server side component and a Silverlight viewer that runs within a browser. Amyuni PDF Creator takes care of converting a PDF file into an equivalent XAML-based package, and sends it to the Silverlight client. A full description of Amyuni PDF for Silverlight can be found in this page: http://www.amyuni.com/en/developer/pdfsilverlight.
This document will focus on how to provide AcroForm editing capabilities to your Silverlight application. |
Reviewing our goals, we want to be able to:
- Show form fields as editable components in Silverlight
- Submit the values of those fields to the server (so that we can store them in a database for example)
- Put the values back in the PDF file.
- Send back the filled-out PDF to the client either as an editable form or as a flattened PDF.
Showing each form field as an editable component in Silverlight
When a PDF file containing form fields is converted into a XAML package by Amyuni PDF Creator, each text field will be converted into a TextBox XAML tag. This tag will be loaded as the corresponding editable component in Silverlight.
Example 1:
<TextBox Canvas.Left = "233.60" Canvas.Top = "242.69" Width = "215.27" Height = "37.13" FontSize="15.91" BorderThickness="0" Background="{x:Null}" Name="acField1">
<TextBox.Foreground>
<SolidColorBrush Color = "#000000" Opacity="1.00"/>
</TextBox.Foreground>
</TextBox>
Let’s first review the internal architecture of our Silverlight viewer sample, we have a library with a Silverlight control called PDFSilverlightControl where our XAML based packages are loaded from a URL provided, and we have a Silverlight sample application that is hosting this control.
In our PDFSilverlightControl sample class we have the property:
Example 2:
/// <summary>
/// Returns editable text fields from the PDF file.
/// </summary>
public List<TextBox> FormFields { get;}
This property can be used to retrieve the form fields in the Silverlight application.
Submitting the values of all fields to the server
We know that every PDF form field has a corresponding name which is unique for all pages in a PDF file. From Example 1 we can see that this name is added to the TextBox tag in our XAML package:
Example 2:
<TextBox ... Name="acField1">
...
</TextBox>
The name attribute in the corresponding TextBox class can be used for creating a key-value collection that we can submit the server from our hosting Silverlight application. There are several possible approaches for this, just to mention 2 of them:
1- We can submit the key-value pairs as a query string to an asp.net web page:
Example 4:
string pdfFormUrl = "./GetFilledForm.aspx?PDFFile="+ PDFFileName;
foreach (TextBox tb in PDFSilverlightControl1.FormFields)
{
pdfFormUrl += "&";
pdfFormUrl += tb.Name + "=" + tb.Text;
}
HtmlPage.Window.Navigate(new Uri(pdfFormUrl, UriKind.Relative), "_blank");
2- We can submit the key-value pairs as part of a web from request using HttpWebRequest class.
Putting the values back in the PDF file
Once we have submitted the key-value pairs from Silverlight to the server, we can use Amyuni PDF Creator for putting back those values into out PDF file. The Amyuni PDF Creator control can be obtained either through the Silverlight package or through the following link:
http://www.amyuni.com/en/developer/pdfcreator
Example 5:
NameValueCollection formFieldValues = Request.QueryString;
// Using the PDF Creator ActiveX object
// This will throw an exception if the control is not installed and registered
var acpdf = Activator.CreateInstance<ACPDFCREACTIVEX.PDFCreactiveXClass>();
acpdf.SetLicenseKey("Silverlight Evaluation", "07EFCDAB...8E7E085619");
// This will throw an exception if the document is not found
acpdf.OpenEx(inFilePath, string.Empty);
acpdf.CurrentPage = 1;
string[] keys = formFieldValues.AllKeys;
foreach (string key in keys)
{
if (key != "PDFFile")
{
acObject field = acpdf.GetObjectByName(key);
field["TextFont"] = "Times,12";
if (flattenForm)
{
field["Value"] = formFieldValues[key];
field["Annotation"] = false;
}
else
{
field["Text"] = formFieldValues[key];
field["Value"] = formFieldValues[key];
}
}
}
//Saving the file
acpdf.Save(outFilepath, FileSaveOptionConstants.acFileSaveDefault);
Sending back the filled-out PDF to the client
After filling out our PDF file with the field values we can send it back to the client by properly configuring the response headers:
Example 6:
protected void Page_Load(object sender, EventArgs e)
{
Response.ClearContent();
Response.AddHeader("content-disposition",
"attachment;filename=FilledForm.pdf");
Response.ContentType = "application/pdf";
byte[] bytes = PDFFormProcess.GetPDFBytes(Server, Request, false);
Response.BinaryWrite(bytes);
Response.End();
}
public class PDFFormProcess
{
public static byte[] GetPDFBytes(
HttpServerUtility Server,
HttpRequest Request, bool flattenForm);
} //Full sample code is provided with the product
How does it look?
Try our Silverlight – based PDF forms editor at http://www.amyuni.ca/Silverlight4/.
Other features that could be combined with this sample:
Amyuni PDF Creator also provides the following features:
- Convert the PDF file into raster images for visualization on browsers where Silverlight control is not installed or for showing PDF thumbnails on your Silverlight application.
- Protect your output PDF file by using 128bit encryption (or 256bit if its use is legal in your country).
- Edit existing PDF files and forms or creating new ones either programmatically or by using our PDF editor application.
- Add PDF Annotations, watermarks, bookmarks, layers or file attachments to your PDF files.
- Resave PDF files into PDF 1.4, 1.5, or PDF/A formats (PDF/A aims at long-term storage of electronic documents).
- Add custom metadata to the XMP metadata stream of a PDF.
And more
By:Yoisel Melis Santana – Amyuni Technologies
Comment