none
How do I generate pdf from a list? RRS feed

  • Question

  • I am trying to create an event receiver that would create a pdf file with data from a list and place the pdf in a document library. I am not having much luck unfortunately. Has anyone has any success doing this? I would really appreciate the help/pointers. Here is what my code looks like for now...

    public override void ItemAdded(SPItemEventProperties properties)
            {
                //base.ItemAdded(properties);
    
                try
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(properties.SiteId))
                        {
                            using (SPWeb currentWeb = site.OpenWeb(properties.Web.ID))
                            {
                                EventFiringEnabled = false;
                                //SPListItem item = properties.ListItem;
    
                                SPList PickupsList = currentWeb.Lists["Pickups"];  // The display name, ie. "Calendar"  
                                SPQuery query = new SPQuery();
    
                                query.Joins = @"
    						    <Joins>
    						      <Join Type='INNER' ListAlias='List2'>
    							    <Eq>
    							      <FieldRef Name='AgreementID' RefType='Id'/>
    							      <FieldRef List='Volunteer Applications' Name='ID'/>
    							    </Eq>
    						      </Join>
    						    </Joins>";
    
                                SPList volunteerApplications = currentWeb.Lists["Volunteer Applications"];
                                SPListItemCollection items = volunteerApplications.GetItems(query);
                                foreach (SPListItem item in items)
                                {
                                    groupName = item["GroupName"].ToString();
                                    address = item["GroupAddress"].ToString();
                                    city = item["GroupCity"].ToString();
                                    zip = item["GroupZip"].ToString();
                                    location = address + "-" + city + "-" + zip;
                                    agreementStartDate = item["AgreementStartDate"].ToString();
                                    agreementEndDate = item["AgreementEndDate"].ToString();
                                    lengthofAdoptedSection = item["LengthofAdoptedSection"].ToString();
                                    numberofPickups = item["NumberofPickups"].ToString();
                                    highwaySegmentContactName = item["HighwaySegmentContactName"].ToString();
                                    highwaySegmentContactPhone = item["HighwaySegmentContactPhone"].ToString();
                                    agreementID = item["AgreementID"].ToString();
    
                                    fileName = groupName.Replace(" ", "") + "_" + agreementID.Replace(" ", "") + ".pdf";
    
                                    PdfStamper stamper = default(PdfStamper);
                                    System.IO.MemoryStream m = new System.IO.MemoryStream();
                                    string formFile = HttpContext.Current.Server.MapPath("WorkOrder-ITS43-template.pdf");
                                    PdfReader reader = new PdfReader(formFile);
                                    stamper = new PdfStamper(reader, m);
                                    AcroFields fields = stamper.AcroFields;
    
                                    fields.SetField("ID#", agreementID);
                                    fields.SetField("Name", groupName);
                                    fields.SetField("Location", location);
                                    fields.SetField("AgreementStartDate", Strings.FormatDateTime(agreementStartDate));
                                    fields.SetField("AgreementEndDate", Strings.FormatDateTime(agreementEndDate));
                                    fields.SetField("Length of adopted section", lengthofAdoptedSection);
                                    fields.SetField("Cleaning cycle for adopted section", numberofPickups);
                                    fields.SetField("TxDOT Contact", highwaySegmentContactName);
                                    fields.SetField("Phone", highwaySegmentContactPhone);
    
                                    //Close stamper object and stream output to browser
                                    stamper.Close();
                                    HttpContext.Current.Response.Clear();
                                    HttpContext.Current.Response.ContentType = "application/pdf";
                                    //Response.AddHeader("content-disposition", "inline;filename=" + strFileName)
                                    HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + strFileName);
                                    //Response.AddHeader("Content-Length", objFileInfo.Length.ToString())
                                    HttpContext.Current.Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
                                    HttpContext.Current.Response.OutputStream.Close();
                                    HttpContext.Current.ApplicationInstance.CompleteRequest();
                                }
                            }
                        }
                    });
                }
                catch (Exception ex)
                {
    
                }
                finally
                {
                    this.EventFiringEnabled = true;
                }
            }
    I appreciate all the ideas. 

    Sunday, June 19, 2016 3:53 PM

All replies

  • Did you try to use itextsharp library. Here's a link 

    http://alispasset.blogspot.com/2012/08/export-sharepoint-listitem-to-pdf-file.html

    Thanks

    • Proposed as answer by Naveen K Karla Tuesday, June 21, 2016 4:00 PM
    • Unproposed as answer by Spawn10 Tuesday, June 21, 2016 8:59 PM
    Monday, June 20, 2016 12:31 AM
  • Hi

    could you provide some detail of the problem?


    顺其自然地勇往直前!—Justin Liu

    Monday, June 20, 2016 3:36 AM