Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Editing PDF Forms (AcroForms) within a Silverlight Application

7 May 2012 1  
PDF forms or AcroForms allow users to interactively edit specific portions of a PDF document. A PDF form is composed by AcroForm fields that provide a name-value association. This paper will focus on how to provide client and server AcroForm editing capabilities to your Silverlight application.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

PDF forms 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:

  1. Show form fields as editable components in Silverlight
  2. Submit the values of those fields to the server (so that we can store them in a database for example)
  3. Put the values back in the PDF file.
  4. 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.

<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>
Example 1

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:

/// <summary>
/// Returns editable text fields from the PDF file.
/// </summary>
public List<TextBox> FormFields { get;}
Example 2

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:

<TextBox  Name="acField1">
...
</TextBox>M
Example 3

The name attribute in the corresponding TextBox class can be used for creating a key-value collection that we can submit to the server from our Silverlight client 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:
    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");
    Example 4
  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 our 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

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);
Example 5

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:

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
Example 6

How does it look?

Try our Silverlight – based PDF forms editor at http://www.amyuni.ca/Silverlight4/.

Screen capture of the Silverlight viewer sample with a PDF form

Screen capture of Adobe Acrobat Reader with the resulting filled-out PDF form

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here