as per my requirement I created a PDF using iTextsharp and i want upload it to a specific document set (dynamic) in a document library when an event receiver is triggered from a list.
Can anyone please help me with this ?
as per my requirement I created a PDF using iTextsharp and i want upload it to a specific document set (dynamic) in a document library when an event receiver is triggered from a list.
Can anyone please help me with this ?
My sample test code for your reference.
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
using (SPWeb web = properties.Web)
{
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
Document document = new Document(PageSize.A4, 10, 10, 10, 10);
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
Paragraph para = new Paragraph(properties.ListItem["Title"].ToString());
document.Add(para);
string text = @"you are successfully created pdf file.";
Paragraph paragraph = new Paragraph();
paragraph.SpacingBefore = 10;
paragraph.SpacingAfter = 10;
paragraph.Alignment = Element.ALIGN_LEFT;
paragraph.Font = FontFactory.GetFont(FontFactory.HELVETICA, 12f, BaseColor.GREEN);
paragraph.Add(text);
document.Add(paragraph);
document.Close();
byte[] bytes = memoryStream.ToArray();
string libraryName = "doc";
SPList lib = web.Lists.TryGetList(libraryName);
SPFolder folder = web.GetFolder(lib.RootFolder.ServerRelativeUrl + "/testDocumentSet");
string dsturl = folder.ServerRelativeUrl + "/test.pdf";
SPFile myfile = folder.Files.Add(dsturl, bytes, true);
lib.Update();
memoryStream.Close();
}
}
}
Update:
Don't use encoded url in c# code.
I tested in my local SharePoint 2013 by creating a document set called [test, aaads], the code works.
SPFolder folder = web.GetFolder(lib.RootFolder.ServerRelativeUrl + "/test, aaads");
string fullurl= web.Url+folder.Url + "/test.pdf";
Debug Screenshot: