gpt4 book ai didi

azure - 在 Azure blob 上创建 PDF

转载 作者:行者123 更新时间:2023-12-01 09:22:53 24 4
gpt4 key购买 nike

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

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

最佳答案

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方法 上传该文件。

关于azure - 在 Azure blob 上创建 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31088166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com