Export form entries to Pdf format

This blog post comes as a solution to the desired ability to export form entries to PDF format. See Sitefinity's feature request here.

Find the full code in GitHub repository here

Video demo

Click on the image below to watch a video demonstration:

Revision history settings for pages in Sitefinity

Solution plan

In order to create an export to PDF format, the iTextSharp third party library have been used. It provides an API for .NET to create and manipulate pdf documents.

In order to create new pdf exporter helper class, you must dig deeper into how the Excel export has been implemented. You will need to create a custom class that implement the DataItemExporterBase class. This will help you overriding only a few methods keeping the logic to configue the response to the base class. Currently Excel exporter uses the FormEntriesExportHttpHandler to put several query string paramaters as ExportingItemsTypeName and *StylesheetName. You can benefit from this logic without touching it. What you need to do in this case is to only create new dependency injection for a class that implelements the IDataItemExporter interface. That way benefitting from the IoC pattern, you can "theft" the logic that the current "Export as CSV" button is doing and force it to use your exporter.

Solution

1.Create dependency injection to register the pdf exporter:

You can either use the Global.asax Application_Start method or be smarter. Create a self-installing assembly via the PreApplicationStartMethodAttribute:

public class Installer
{
    /// <summary>
    /// Method that is called by ASP.NET before application start to register the custom Pdf exporter 
    /// </summary>
    public static void PreApplicationStart()
    {
        SystemManager.ApplicationStart += SystemManager_ApplicationStart;
    }

    private static void SystemManager_ApplicationStart(object sender, EventArgs e)
    {
        ObjectFactory.Container.RegisterType<IDataItemExporter, PdfExporter>(new InjectionConstructor());
    }
}

2.Implement the export logic

The export logic is done in a specific class called PdfExporter that is using the iTextSharp API. The code in the class is done in a similar fashion to the ExcelExporter class used for the default export to excel functionality. The important method that does the export is ExportToStream. Here's the code:

 public override void ExportToStream(System.IO.Stream streamToExportTo, IEnumerable<Telerik.Sitefinity.Model.IDataItem> itemsToExport, System.Text.Encoding encoding)
    {
        if (streamToExportTo == null)
        {
            throw new ArgumentNullException("streamToExportTo");
        }
        if (itemsToExport == null)
        {
            throw new ArgumentNullException("itemsToExport");
        }
        if (encoding == null)
        {
            throw new ArgumentNullException("encoding");
        }

        // Sets the document to A4 size and rotates it so that the orientation of the page is Landscape.
        using (Document pdfDocument = new Document(PageSize.A4.Rotate(), 0, 0, 15, 5))
        {
            //// Gets the instance of the document created and writes it to the output stream of the Response object.
            PdfWriter.GetInstance(pdfDocument, streamToExportTo);

            pdfDocument.Open();

            pdfDocument.AddTitle("Form entries");

            PropertyDescriptorCollection itemProperties = null;
            IEnumerator<IDataItem> enumerator = itemsToExport.GetEnumerator();
            if (!enumerator.MoveNext())
            {
                throw new ArgumentException("Can't export empty collection");
            }
            IDataItem current = enumerator.Current;
            itemProperties = this.GetItemProperties(current);

            int count = 1;
            this.AppendDataItemContent(pdfDocument, current, itemProperties, count);
            while (enumerator.MoveNext())
            {
                count++;
                pdfDocument.NewPage();
                this.AppendDataItemContent(pdfDocument, enumerator.Current, itemProperties, count);
            }

            pdfDocument.Close();
        }
    }

The code above uses the iTextSharp to create new pdf document and tables and fill the cells with form entry field values.

Voila!

Veronica Milcheva

About Veronica Milcheva

I am a passionate Sitefinity blogger, developer and consultant. In my spare time I enjoy running and listening to music. My personal quote: There's no tough problem, just not enough coffee :)

View Comments

comments powered by Disqus