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.
try
{
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;
//barcode
Barcode128 bc = new Barcode128();

//Giving heading to the Word Document with style
StringBuilder strHTMLContent = new StringBuilder();
strHTMLContent.Append(SPContext.Current.Item["Title"].ToString());
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));
document.Add(jpg);

document.Close();
writer.Close();
ms.Close();
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.

5 comments:

  1. The explanation is really good. It will be beneficial for those who are using its new version and don’t have knowledge about shortcuts and other information which are present in the post.
    Adilib Software

    ReplyDelete
  2. How to put Export to PDF/ webpart inside View Page

    ReplyDelete
  3. how do you add the button to your sharepoint form?

    ReplyDelete
    Replies
    1. You can edit /Lists/customlist/DispForm.aspx and insert custom webpart.

      Delete
    2. You can insert the custom webpart by editing the DispForm.aspx page.

      Delete