插入图像以在AZURE BLOB存储中动态创建PDF

时间:2015-06-29 20:39:01

标签: azure itextsharp blob azure-storage-blobs

我在AZURE BLOB Storage中创建了一个动态PDF。所有内容都写得很完美。现在我想在一个blob存储器的PDF内容顶部添加Image。对于创建PDF,我的代码如下:

//Parse the connection string and return a reference to the storage account.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("connectionstring"));

            //Create the blob client object.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            //Get a reference to a container to use for the sample code, and create it if it does not exist.
            CloudBlobContainer container = blobClient.GetContainerReference("containername");
            container.CreateIfNotExists();

MemoryStream ms1 = new MemoryStream();
            ms1.Position = 0;
            using (ms1)
            {
                var doc1 = new iTextSharp.text.Document();
                PdfWriter writer = PdfWriter.GetInstance(doc1, ms1);
                doc1.Open();
                doc1.Add(new Paragraph("itextSharp DLL"));
                doc1.Close();
                var byteArray = ms1.ToArray();
                var blobName = "testingCREATEPDF.pdf";
                var blob = container.GetBlockBlobReference(blobName);
                blob.Properties.ContentType = "application/pdf";
                blob.UploadFromByteArray(byteArray, 0, byteArray.Length);

            }

请告诉我如何在azure blob的PDF顶部添加图片。

1 个答案:

答案 0 :(得分:1)

尝试以下代码。基本上诀窍是将blob的数据作为流读取并从该流创建iTextSharp.text.Image。拥有该对象后,可以将其插入PDF文档中。

private static void CreatePdf()
{
    var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
    var blobClient = account.CreateCloudBlobClient();
    var container = blobClient.GetContainerReference("pdftest");
    container.CreateIfNotExists();
    var imagesContainer = blobClient.GetContainerReference("images");
    var imageName = "Storage-blob.png";
    using (MemoryStream ms = new MemoryStream())
    {
        var doc = new iTextSharp.text.Document();
        PdfWriter writer = PdfWriter.GetInstance(doc, ms);
        doc.Open();
        doc.Add(new Paragraph("Hello World"));
        var imageBlockBlob = imagesContainer.GetBlockBlobReference(imageName);
        using (var stream = new MemoryStream())
        {
            imageBlockBlob.DownloadToStream(stream);//Read blob contents in stream.
            stream.Position = 0;//Reset the stream's position to top.
            Image img = Image.GetInstance(stream);//Create am instance of iTextSharp.text.image.
            doc.Add(img);//Add that image to the document.
        }
        doc.Close();
        var byteArray = ms.ToArray();
        var blobName = (DateTime.MaxValue.Ticks - DateTime.Now.Ticks).ToString() + ".pdf";
        var blob = container.GetBlockBlobReference(blobName);
        blob.Properties.ContentType = "application/pdf";
        blob.UploadFromByteArray(byteArray, 0, byteArray.Length);
    }
}