csharp PdfSharp / MigraDoc到AzureStorage

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp PdfSharp / MigraDoc到AzureStorage相关的知识,希望对你有一定的参考价值。

using System.IO;
using System.Threading.Tasks;
using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using PdfSharp.Pdf;

namespace MyApp.Providers
{
    public class ReportProvider
    {
        protected AzureProvider _azureProvider;

        public ReportProvider()
        {
            _azureProvider = new AzureProvider();
        }

        public async Task<string> CreateReport()
        {
            var document = CreateDocument();
            var containerName = "democontainer";
            var fileName = "demodocument.pdf";
            var stream = ConvertMemDocToMemoryStream(document);

            return await _azureProvider.UploadStreamToAzure(containerName, fileName, stream);
        }

        private Document CreateDocument()
        {
            Document document = new Document();
            var section = document.AddSection();
            var paragraph = section.AddParagraph("Report Demo");

            // Add other document elements here

            return document;
        }

        protected static MemoryStream ConvertMemDocToMemoryStream(Document document)
        {
            PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false, PdfFontEmbedding.Always);
            pdfRenderer.Document = document;
            pdfRenderer.RenderDocument();
            MemoryStream stream = new MemoryStream();
            pdfRenderer.PdfDocument.Save(stream, false);
            return stream;
        }
    }
}
using System;
using System.Configuration;
using System.IO;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

namespace MyApp.Providers
{
    public class AzureProvider
    {
        public async Task<string> UploadStreamToAzure(string containerName, string fileName, MemoryStream stream)
        {
            var blockBlob = ResolveCloudBlockBlob(containerName, fileName);
            await blockBlob.UploadFromStreamAsync(stream);
            return fileName;
        }

        public CloudBlockBlob ResolveCloudBlockBlob(string containerName, string fileName)
        {
            var container = ResolveCloudBlobContainer(containerName);
            var blockBlob = container.GetBlockBlobReference(fileName);
            return blockBlob;
        }

        public CloudBlobContainer ResolveCloudBlobContainer(string containerName)
        {
            var storageAccount = GetCloudStorageAccount();
            var blobClient = storageAccount.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference(containerName);
            return container;
        }

        private CloudStorageAccount GetCloudStorageAccount()
        {
            return CloudStorageAccount.Parse(ResolveAzureStorageConnectionString());
        }

        public string ResolveAzureStorageConnectionString()
        {
            var accountName = ConfigurationManager.AppSettings["Azure.Storage.AccountName"]; // Get account name from web.config
            var accessKey = ConfigurationManager.AppSettings["Azure.Storage.PrimaryAccessKey"]; // Get primary access key from web.config
            return String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", accountName, accessKey);
        }
    }
}

以上是关于csharp PdfSharp / MigraDoc到AzureStorage的主要内容,如果未能解决你的问题,请参考以下文章

PDFsharp 保存到 MemoryStream

无法加载文件或程序集 PDFsharp

使用 PdfSharp 组合可填写的 Pdf 表单

如何在保留书签的同时向现有 pdf 添加附加页面? (PDFSharp等)

如何使用 PDFsharp .NET 库将 PDF 页面导出为图像?

使用 TheArtOfDev.HtmlRenderer.PdfSharp 从 HTML 创建 PDF