Q:

Insert Image into Dynamically create PDF in AZURE BLOB Storage

I created one dynamic PDF in AZURE BLOB Storage. All content is write perfectly. Now I want to add Image at top of the PDF content from one of blob storage. For create PDF my code is as below : 
//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);

            }

Please let me know how to add image at top of PDF from azure blob. 

A:

Try the code below. Basically the trick is to read the blob's data as a stream and create an iTextSharp.text.Image from that stream. Once you have that object, you can insert it in your PDF document.
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);
    }
}

