Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I am doing code in C#, and i have created pdf using itextsharp,
now i want to open my pdf directly in Acrobat Reader 9 (without giving any path to filestream)and then ask to client , wheather he want to save that pdf file.
C#
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(pDoc, ms); 

var file = System.IO.Path.GetTempFileName();
           using (var fileStream = File.OpenWrite(file))
           {
               var buffer = ms.GetBuffer();
               fileStream.Write(buffer, 0, (int)buffer.Length);
           }


I have used above code, but getting error SecurityException was unhandled by user code -- File operation not permitted. Access to path '' is denied

when i have used below code den pdf is getting created , means Admin settings have not any problems.
C#
writer = PdfWriter.GetInstance(doc, new FileStream(filpath, FileMode.Create));


so plz help me , so that i will open direct pdf from memorystream without saving it in a disk.
Posted
Updated 11-Oct-11 23:15pm
v2

1 solution

I cannot see where you got this exception. Writing to the file with the name obtained System.IO.Path.GetTempFileName() is perfectly legal. If the file name was empty (how your error message suggests), the error message would be different ("Empty path name is not legal"). So, I suspect your exception report is not 100% accurate. Not to worry — you don't need to do this.

First, please understand that the memory stream is quite useless. Yes, you can read and write to it, but how can you make Acrobat Reader 9 doing it? This application probably has nothing to do with .NET.

So, unfortunately, you're bound to use a temporary file. If your last code sample works, use it, only assign your variable filpath to temporary file name:
C#
string filpath = System.IO.Path.GetTempFileName();
//...


—SA
 
Share this answer
 
Comments
dev22 18-Oct-11 1:36am    
Hi SAKryukov,

I am doing code in silverlight application using C#, and above code i have wrote in silverlight page i.e in .xaml.cs file.
I have passed byte[] from WCF service, and calling service in my silverlight page as below.

void svc_CreatePdf1Completed(object sender, ServiceReference1.CreatePdf1CompletedEventArgs e)
{
byte[] pdfDone = e.Result;
using (var fileStream = File.OpenWrite(file))
{
var buffer = pdfDone;//ms.GetBuffer();
fileStream.Write(buffer, 0, (int)buffer.Length);
}

}

so how can i diplay direct pdf in browser without storing in disk and also as per client wish, he can save the pdf in disk.
Macro Man 18-Oct-11 7:58am    
I think SA is correct. Acrobat expects a file, not a memory stream. Even when viewing documents in a browser such as Firefox or Internet Explorer, the file exists on a disk, on a server.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900