Reputation: 49
I have a form in ASP.NET and in when I fill up the form in the last step it generates a PDF file. I used jsPDF. What I want is that, the generated pdf file to be send (saved) in Azure storage, does anyone can help me?
Thank you
UPDATE: This is the code that I'm trying, it's working but it's extracting only the text, it doesn't save the pdf as it is:
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);
}
}
Upvotes: 0
Views: 5358
Reputation: 49
I found a simple solution, this is what the code does:
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);
}
Upvotes: 1
Reputation: 4062
Click on your solution in Visual Studio, then Add => Add Connected Service => Select Azure Storage, then go through the wizard (if you need, create the storage account - the wizard has that option) and, after that, your solution will be configured with all needed settings (connection string included) and VS will open the page with the detailed tutorial on how to use Azure Storage in your browser. As it has the information and needed code pieces, i willnot include it here (likely, it will change in a future, to avoid the deprecated information).
Tutorial about Add Connected Service => Azure Storage functionality.
Upvotes: 0