2015-06-29 104 views
2

我在AZURE BLOB存儲中創建了一個動態PDF。所有的內容都寫得很完美。現在我想從blob存儲之一的PDF內容頂部添加Image。對於創建PDF我的代碼如下:將圖像插入AZURE BLOB存儲中動態創建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); 

      } 

請讓我知道如何從蔚藍的BLOB PDF的頂部添加圖像。

回答

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