【问题标题】:Create PDF on Azure blob在 Azure blob 上创建 PDF
【发布时间】:2015-09-14 07:00:54
【问题描述】:

我正在尝试在 Azure blob 上创建 PDF。我的代码如下。我在 blob 上创建 PDF 时遇到问题。我无法创建 PDF,因为不支持 Filestream。

FileStream fs = new FileStream("Chapter1_Example1.pdf", FileMode.Create, FileAccess.Write, FileShare.None);
            iTextSharp.text.Document doc = new iTextSharp.text.Document();

            PdfWriter writer = PdfWriter.GetInstance(doc, fs);
            doc.Open();
            doc.Add(new Paragraph("Hello World"));


            var permissions = container.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
            container.SetPermissions(permissions);
            string uniqueBlobName = string.Format("rewhizzdocuments/{0}", "helloworld.pdf");
            CloudBlockBlob blob = container.GetBlockBlobReference(uniqueBlobName);
            blob.Properties.ContentType = "application/pdf";
            blob.UploadFromStream(fs);


            doc.Close();

【问题讨论】:

    标签: azure azure-blob-storage


    【解决方案1】:

    FileStream主要用于处理文件,由于您对动态创建PDF文档感兴趣,我建议您改用MemoryStream

    看看下面使用MemoryStream的示例代码:

        private static void CreatePdf()
        {
            var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
            var blobClient = account.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference("pdftest");
            using (MemoryStream ms = new MemoryStream())
            {
                using (var doc = new iTextSharp.text.Document())
                {
                    PdfWriter writer = PdfWriter.GetInstance(doc, ms);
                    doc.Open();
                    doc.Add(new Paragraph("Hello World"));
                }
                var byteArray = ms.ToArray();
                var blobName = "hello-world.pdf";
                var blob = container.GetBlockBlobReference(blobName);
                blob.Properties.ContentType = "application/pdf";
                blob.UploadFromByteArray(byteArray, 0, byteArray.Length);
            }
        }
    

    如果您仍想使用FileStream,您可以做的另一件事是将文件保存在磁盘上,然后使用CloudBlockBlob 上的UploadFromFile 方法上传该文件。

    【讨论】:

    • 嗨 Gaurav,感谢您的回复及其对 itextsharp 版本 5.5.6.0 的工作,但我使用的是 4.1.2.0,所以我得到“'iTextSharp.text.Document': type used in a using statement must可以隐式转换为“System.IDisposable”错误。
    • 我什至在 Nuget 页面上都没有看到这个版本 :( - nuget.org/packages/iTextSharp。您可以尝试的一件事是摆脱 using 块并在 doc 之后立即调用 doc.Close() 方法.Add() 方法。
    • 感谢 Gaurav,它的工作!现在我想从我的一个 blob 添加添加图像,那么我该怎么做。找不到任何东西。
    • 您可能想将此作为另一个问题发布,恕我直言。
    【解决方案2】:

    请先调用 doc.Close(),再调用 blob.UploadFromFile("Chapter1_Example1.pdf")。

    另一种可能的解决方案是:先调用 doc.Close(),然后设置 fs.Position = 0L,最后调用 blob.UploadFromStream(fs)。 (如果 doc.Close() 会自动触发 FileStream 的处理,这可能不是一个有效的解决方案,我无法确定,因为我不熟悉 iText。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-12
      • 2011-02-24
      • 2016-12-31
      • 2022-01-13
      • 2014-01-06
      • 2019-07-03
      • 2011-02-06
      • 2018-07-26
      相关资源
      最近更新 更多