Saving WFFM form result as PDF

In the Web Forms For Marketers module I wanted to be able to save the responses as PDF, to then be able to send that PDF with the responses, save the PDF in the Media Library or whatever else.

So there are 2 parts to this:

  1. Create a new custom WFFM Save Action
  2. Create a PDF using some kind of library. In my case I used PDFSharp, which is open source and easy to use but I haven’t done any research into PDF libraries so I couldn’t say whether there’s better ones out there

First up is creating the new WFFM Save Action, which is fairly simple. We’ll need a Sitecore definition item and a bit of code as usual. The WFFM Save Actions can be found in /sitecore/system/Modules/Web Forms for Marketers/Settings/Actions/Save Actions. I’ve created a ‘Save PDF’ item there based on the ‘Submit Action’ template.


Don’t forget to set the Assembly and Class fields.

Then the code. When we’re creating a WFFM save action we need to make sure our class inherits from the WffmSaveAction class. We’ll also have to reference Sitecore.WFFM.Abstractions and Sitecore.WFFM.Actions. For our PDF creation functionality we’ll also need to add a reference to the PdfSharp assembly.

In my case, this is how my class looks:

namespace Sandbox.WFFM.Actions
    using Sitecore.WFFM.Abstractions.Actions;
    using Sitecore.WFFM.Actions.Base;
    using Sitecore.Data;
    using PdfSharp.Pdf;
    using PdfSharp.Drawing;

    public class SavePDF : WffmSaveAction
        public override void Execute(ID formId, AdaptedResultList adaptedFields, ActionCallContext actionCallContext = null, params object[] data)
            // Our code goes here.

Now in the Execute method we’ll start by creating a new PDF document, adding a new page and writing some content there.

PdfDocument pdf = new PdfDocument();
PdfPage page = pdf.AddPage();
XGraphics graph = XGraphics.FromPdfPage(page);
XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
var i = 0; // Used for positioning the text
foreach (AdaptedControlResult field in adaptedFields) // Loop through all fields in the form
    graph.DrawString(string.Format("{0}: {1}", field.FieldName, field.Value), font, XBrushes.Black,
new XRect(40, i, page.Width.Point, page.Height.Point), XStringFormats.TopLeft);

    i = i + 40; // Make sure to increment i so we don't write on the same line over and over again

pdf.Save(@"C:\temp\sandboxtest.pdf"); // Probably shouldn't write this here... If we do we need something smart for the naming

Then to test. I’ve added a form on one of our news articles:
And this is the resulting PDF after filling in the form:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s