【问题标题】:Save generated PDF file in azure将生成的 PDF 文件保存在 azure 中
【发布时间】:2016-09-07 05:19:08
【问题描述】:

我在 ASP.NET 中有一个表单,当我在最后一步填写表单时,它会生成一个 PDF 文件。我使用了 jsPDF。 我想要的是,生成的 pdf 文件要发送(保存)在 Azure 存储中,有人可以帮助我吗?

谢谢

更新:这是我正在尝试的代码,它正在工作,但它只提取文本,它不会按原样保存 pdf:

var account = new CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("storageaccount", 
                    "accesskey"), true);
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("folderpath");

StringBuilder text = new StringBuilder();
string filePath = "C:\\Users\\username\\Desktop\\toPDF\\testing PDFs\\test.pdf";

if (File.Exists(filePath))
{
     PdfReader pdfReader = new PdfReader(filePath);

     for (int page = 1; page <= pdfReader.NumberOfPages; page++)
     {
         ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
         string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
         currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
         text.Append(currentText);
       }
       pdfReader.Close();

       using (MemoryStream ms = new MemoryStream())
       {
       using (var doc = new iTextSharp.text.Document())
       {
             PdfWriter writer = PdfWriter.GetInstance(doc, ms);
             doc.Open();
             doc.Add(new Paragraph(text.ToString()));
       }
       var byteArray = ms.ToArray();
       var blobName = "test.pdf";
       var blob = container.GetBlockBlobReference(blobName);
       blob.Properties.ContentType = "application/pdf";
       blob.UploadFromByteArray(byteArray, 0, byteArray.Length);
       }
    } 

【问题讨论】:

  • 上面添加的代码,它只是保存提取的文本,我该如何保存pdf文件,包括图像、颜色等?

标签: c# asp.net azure jspdf


【解决方案1】:

我找到了一个简单的解决方案,代码就是这样做的:

string filePath = "C:\\Users\\username\\Desktop\\toPDF\\testing PDFs\\rpa.pdf";
var credentials = new StorageCredentials("storageaccount","accesskey");
var client = new CloudBlobClient(new Uri("https://jpllanatest.blob.core.windows.net/"), credentials);

// Retrieve a reference to a container. (You need to create one using the mangement portal, or call container.CreateIfNotExists())
var container = client.GetContainerReference("folderpath");
// Retrieve reference to a blob named "myfile.pdf".
var blockBlob = container.GetBlockBlobReference("myfile.pdf");

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(filePath))
{
     blockBlob.UploadFromStream(fileStream);
} 

【讨论】:

    【解决方案2】:

    在 Visual Studio 中单击您的解决方案,然后添加 => 添加连接的服务 => 选择 Azure 存储,然后通过向导(如果需要,创建存储帐户 - 向导具有该选项),然后,您的解决方案将配置所有需要的设置(包括连接字符串),VS 将打开页面,其中包含有关如何在浏览器中使用 Azure 存储的详细教程。由于它包含信息和所需的代码片段,因此我不会在此处包含它(可能会在将来更改,以避免不推荐使用的信息)。

    Tutorial about Add Connected Service => Azure Storage functionality.

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2013-05-15
      • 2012-02-17
      • 2021-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多