Monday, 2 July 2012

Export SharePoint ListItem to PDF file

Before one month this one is my major task, i did much research about this area. But i didn't found any links about to Print the SharePoint ListItems based on conditions. Finally got the output using with the iTextSharp library. It is a free downloadable library that is use for pdf generation with ListItems or any data.
Here is the steps i followed,
  • Here i wrote the code for below requirement :
    "I need to export the SharePoint List Item, Picture & its Barcode image with Barcode value.
    My intention is when user click on the item link the pop up window of DispForm.aspx page will appear with 'Export to PDF' button in top left. So user will click the button then PDF file will be genrate."

  • First you should download iTextSharp dll files from here
  • you must create a SharePoint project in VS 2010, and add this(itextsharp.dll) reference to the project.
  • Then create a visual webpart with any name, here i am giving ExportItemtoPDF and add a button in design page.
Make sure use the following namespaces in your code
using Microsoft.SharePoint;
using System.IO;using Microsoft.SharePoint.WebControls;using iTextSharp.text;using iTextSharp.text.pdf;using System.Text;namespaces will genrate like as below

Please add the below code in button click event.
using (MemoryStream ms = new MemoryStream()) {
//Opening the current site
SPSite oSiteCollection = SPContext.Current.Site;
//Fetching the current list
SPList oList = oSiteCollection.OpenWeb().Lists["PrintListItem"];
// Creae the document object, assigning the page margins
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
PdfWriter writer = PdfWriter.GetInstance(document, ms);

// Open the document, enabeling writing to the document
document.Open();PdfContentByte cb = writer.DirectContent;
Barcode128 bc = new Barcode128();

//Giving heading to the Word Document with style
StringBuilder strHTMLContent = new StringBuilder();
strHTMLContent.Append("\r\n" + SPContext.Current.Item["body"].ToString()); strHTMLContent.Append("\r\n" + SPContext.Current.Item["value"].ToString());
imagefilepath = SPContext.Current.Item["Picture"].ToString().Replace(",", "").Trim();
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imagefilepath);jpg.ScaleToFit(120, 80);

string code = SPContext.Current.Item["Barcode Value"].ToString();
bc.ChecksumText = true;
bc.GenerateChecksum = true;
bc.StartStopText =true;
bc.Code = code;

document.Add(new Paragraph(System.Text.RegularExpressions.Regex.Replace(strHTMLContent.ToString(), @"<(.|\n)*?>", string.Empty)));
document.Add(bc.CreateImageWithBarcode(cb, null, null));

Response.ContentType = "pdf/application";
string strFileName = SPContext.Current.Item["Title"].ToString() + ".pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);

catch(Exception ex){

This is the output

I tried with Barcode 39 type but i am getting special characters in the end of barcode value. I need to find more informatin about using Barcode 39 type in SharePoint.

Please check this link to Export SharePoint List items to Word Document.
I used above link, but i am not able Export the Barcode Image. Getting the Image Path and Value only.

For complete discussion about my issue please have a look  in MSDN forums.