user2956229
user2956229

Reputation: 51

How to convert web form to pdf and upload to the sharepoint document library?

I have a web part, which users writing their informations on it. What i need to do is saving those informations to sharepoint document library as PDF after user clicks "save" button.

i'm using itextsharp and what i've done so far is creating PDFs in SP server c:\ drive and uploading it to the sarepoint document library. But i want to do is without creating document in c:\ drive. Thank you

//creates document in c:// drive
Document document = new Document();
            PdfWriter.GetInstance(document, new FileStream(Page.Request.PhysicalApplicationPath + "\\MySamplePDF.pdf", FileMode.Create));
            document.Open();
            iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
            hw.Parse(new StringReader(content));
            document.Close();

//then uploads to sharepoint librart
            String fileToUpload = Page.Request.PhysicalApplicationPath +"\\MySamplePDF.pdf";
            String sharePointSite = "http://testportal/";
            String documentLibraryName = "mydefdoclibi";

            using (SPSite oSite = new SPSite(sharePointSite))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    if (!System.IO.File.Exists(fileToUpload))
                        throw new FileNotFoundException("File not found.", fileToUpload);

                    SPFolder myLibrary = oWeb.Folders[documentLibraryName];

                    // Prepare to upload
                    Boolean replaceExistingFiles = true;
                    String fileName = System.IO.Path.GetFileName(fileToUpload);
                    FileStream fileStream = File.OpenRead(fileToUpload);

                    // Upload document
                    SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

                    // Commit 
                    myLibrary.Update();
                }
            }

Upvotes: 0

Views: 1863

Answers (1)

trigras
trigras

Reputation: 515

So use MemoryStream instead of writing to file

using(MemoryStream ms = new MemoryStream())
{
    PdfWriter.GetInstance(document, ms, FileMode.Create));

    ...
    ms.Position = 0;
    SPFile spfile = myLibrary.Files.Add(fileName, ms, replaceExistingFiles);
...
}

Upvotes: 2

Related Questions