【问题标题】:How to convert web form to pdf and upload to the sharepoint document library?如何将 web 表单转换为 pdf 并上传到 sharepoint 文档库?
【发布时间】:2014-04-27 20:03:48
【问题描述】:

我有一个 Web 部件,用户可以在上面写下他们的信息。我需要做的是在用户单击“保存”按钮后将这些信息以 PDF 格式保存到共享点文档库。

我正在使用 itextsharp,到目前为止我所做的是在 SP 服务器 c:\ 驱动器中创建 PDF 并将其上传到 sarepoint 文档库。但我想做的是不在 c:\ 驱动器中创建文档。谢谢你

//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();
                }
            }

【问题讨论】:

    标签: c# asp.net sharepoint


    【解决方案1】:

    所以使用 MemoryStream 而不是写入文件

    using(MemoryStream ms = new MemoryStream())
    {
        PdfWriter.GetInstance(document, ms, FileMode.Create));
    
        ...
        ms.Position = 0;
        SPFile spfile = myLibrary.Files.Add(fileName, ms, replaceExistingFiles);
    ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多