In one of our
requirement, we have to save WFFM form fields data in PDF file so that WFFM
fields data showed in tabular format.
I created WFFM form
and created a new Save action under “/sitecore/system/Modules/Web
Forms for Marketers/Settings/Actions/Save Actions”
My WFFM form looks as
Sample WFFM Form |
I bind the newly
created Save action to this WFFM form.
I am using
iTextSharp to generate PDF file.
Here is the code
for this Save Action.
using System;
using System.Linq;
using Sitecore.Data;
using Sitecore.WFFM.Abstractions.Actions;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
namespace SitecorePOCinMVC.Web
{
public class MyCustomAction : ISaveAction
{
public ID ActionID { get; set; }
public string UniqueKey { get; set; }
public ActionType ActionType { get; private set; }
public ActionState QueryState(ActionQueryContext queryContext)
{
return ActionState.Enabled;
}
public void Execute(ID formId, AdaptedResultList adaptedFields, ActionCallContext actionCallContext = null, params object[] data)
{
try
{
GeneratePDFFile(adaptedFields);
}
catch (Exception ex)
{
System.IO.File.AppendAllText("d:\\sitecoretext.txt", "PDF Error : -" + ex.Message);
}
}
private void GeneratePDFFile(AdaptedResultList adaptedFields)
{
//Create
document
Document doc = new Document();
//Create
PDF Table
PdfPTable tableLayout = new PdfPTable(4);
//Create
a PDF file in specific path
PdfWriter.GetInstance(doc, new FileStream(@"D:\Sample-PDF-File.pdf", FileMode.Create));
//Open
the PDF document
doc.Open();
//Add
Content to PDF
doc.Add(AddContentToPDF(tableLayout, adaptedFields));
//
Closing the document
doc.Close();
}
private PdfPTable AddContentToPDF(PdfPTable tableLayout, AdaptedResultList adaptedFields)
{
float[] headers = { 40, 40, 0, 0 }; //Header
Widths
tableLayout.SetWidths(headers);
//Set the pdf headers
tableLayout.WidthPercentage =
80; //Set
the PDF File witdh percentage
//Add
Title to the PDF file at the top
tableLayout.AddCell(new PdfPCell(new Phrase("WFFM Generated Document", new Font(Font.NORMAL, 13, 1, new iTextSharp.text.BaseColor(153, 51, 0)))) { Colspan = 4, Border = 0, PaddingBottom =
20, HorizontalAlignment = Element.ALIGN_CENTER });
//Add
header
AddCellToHeader(tableLayout, "Fields Name");
AddCellToHeader(tableLayout, "User entered data");
tableLayout.CompleteRow();
foreach (var i in adaptedFields.ToList())
{
AddCellToBody(tableLayout,
i.FieldName);
AddCellToBody(tableLayout,
i.Value);
tableLayout.CompleteRow();
}
return tableLayout;
}
/// <summary>
/// Method to add single cell to the header
/// </summary>
/// <param
name="tableLayout"></param>
/// <param name="cellText"></param>
private static void AddCellToHeader(PdfPTable tableLayout, string cellText)
{
tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.NORMAL, 8, 1,
iTextSharp.text.BaseColor.WHITE))) { HorizontalAlignment = Element.ALIGN_CENTER,
Padding = 5, BackgroundColor = new iTextSharp.text.BaseColor(0, 51, 102) });
}
/// <summary>
/// Method to add single cell to the body
/// </summary>
/// <param name="tableLayout"></param>
/// <param
name="cellText"></param>
private static void AddCellToBody(PdfPTable tableLayout, string cellText)
{
tableLayout.AddCell(new PdfPCell(new Phrase(cellText, new Font(Font.NORMAL, 8, 1, iTextSharp.text.BaseColor.BLACK))) {
HorizontalAlignment = Element.ALIGN_CENTER, Padding = 5, BackgroundColor =
iTextSharp.text.BaseColor.WHITE });
}
}
}
After executing
this code, I can generate PDF file and able to save WFFM fields data in tabular
format as
WFFM Generated PDF |
I hope you like this Sitecore tip. Stay tuned for more Sitecore
related articles.
Till that happy Sitecoring
:)
No comments:
Post a Comment