I want to generate a PDF file on server side, and then in response want to send that file (buffer,fileName- whatever may work) and show a print dialog to ask user to print the generated PDF file.
I tried something like below. But it does not trigger window.print() dialog.
public static void ForcedPrint(HttpResponse response, byte[] buffer, string fileName, string fileExtension) {
response.Clear();
response.Buffer=true;
response.Write("<script>window.print();</script>");
response.Charset="";
response.Cache.SetCacheability(HttpCacheability.NoCache);
response.ContentType="application/pdf";
response.BinaryWrite(buffer);
response.Flush();
response.End();
}
Can someone please help me with this?
The feature i am looking for is that i should be able to create PDF file on server, and in response user should get a dialog to print the generated file.
Thanks in advance.
As far as I'm aware you cannot generate a print command to the browser on the server. The most you can do is generate the javascript which will make it pop up with a print dialog (window.print()) but that wouldnt help you with what you're trying to do.
Just speculating but you may try generating a page with an iframe that points to the PDF file, and in the base page have the javascript that tells the iframe to print?
Hope this helps,
Darko
You need to embed the PDF in the HTML document, say in a div called thePDF and in JavaScript code in the document, you need to invoke
thePDF.printWithDialog()
The dialog that appears will be the Adobe Reader plugin's print dialog rather than the browser's print dialog; this will allow selection of pages etc. before printing.
Related
i have a problem that i can restrict user to download pdf,i only view the pdf document to user
in a browser my code is below:
string filepath = Server.MapPath(dt.Rows[0]["FilePath"].ToString());
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(filepath);
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline;filename=data.pdf#toolbar=0&navpanes=0");
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
but the above code can't hide the toolbar in browser..please help
What you ask cannot be achieved on every browser with every PDF reader plugin. You do not have control over which PDF reader your users may use, for example.
If the user can view the PDF, they can download it. There is nothing you can do to prevent this. The only way to prevent them from saving it locally is not to let them view it in the first place.
And even if you were able to prevent the system from allowing the file to be saved, you cannot protect against the analog hole. The user could simply take a photo of his monitor, for example, or retype the contents into a new document.
If the user can view it, then they can print it.
Hiding the Save or Print button does not secure the PDF file, if you need protection in PDF, consider securing the PDF.
Abobe.com : PDF File Protection
I have a gridview in which I have provided an option for the user to download the pdf files. When they click on the pdf icon sometimes it open the pdf file in a new tab and sometimes it starts downloading. How can i make it download always?
You need to add a button (image button, linknbutton or button) and handle the RowCommand event of GridView. In RowCommand handler you may write code to download a file.
You may use Response object's method.
string filepath=MapPath("~/files/file.pdf");
byte []bytes=System.IO.File.ReadAllBytes(filepath);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition","attachment; filename=file.pdf");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
In order to always force a download you need to add the Content-Disposition header as AVD showed; however, I find this totally unnecessary; I think it would suffice to have the link to the PDF open in a new window. In other words, have target="_blank" defined. Example:
invoice
Then, is up to the user whether he wants to save the file locally or just see it on the screen. I think the important thing is that this won't interfere with the current page the user is looking at.
I have a file that is in .pdf format for my website.
If you left click on the link, it will open the Adobe Reader and open the file.
What I want to do is when you left click, it opens a dialogue box that asks you where you want
to save the file.
I know you can do this by right clicking and choosing save as,
but is there any way by just
one simple left click to downlaod it?
Im sure there is some sort of hack to accomplish this but this is the feature that is installed with the browser when Adobe Reader is installed on the machine. Keeping this consistent for all users is the best approach. Do not bother with trying to make the left click open a save dialog. Users are accustomed to this behavior and will know how to save the PDF to thier local hard disk
Consistency is the key here.
two parts, you have to convert the pdf to a byte stream and add a content-disposition header to the html response.
first get the file and stream to a byte array
public byte[] GetDocument(string filePath)
{
return File.ReadAllBytes(filePath);
}
Then send it - as the very first thing in the Page_Load event - you'll need to pass the path to the file through session or query string to a new page, as you can't change the content type on an existing page you have already viewed in the browser.
Byte[] fileData = GetDocument(filePath);
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "application/pdf";
// prompt to download
Response.AppendHeader("content-disposition", "attachment; filename=declarations.pdf");
Response.AppendHeader("content-length", fileData.Length.ToString());
Response.BinaryWrite(fileData);
Response.Flush();
Response.Close();
Add the pdf to a zip folder and add a link to the zip folder
Remove the pdf location from the href part of your link, and code an onClick method instead.
<a href="#" onClick="$:openSaveDialog">
<script>
function openSaveDialog() {
//Write a JavaScript function to prompt the user with a Save Dialog Box.
}
</script>
I have one c# control and I am creating an image at runtime. The problem here is how do i open the file dialog when a user creates a submit button.
I have tried content-disposition but it is not working :(
Here is the scenario:
- I have one aspx file in that i have one ascx control file.
In that control file's cs file i am doing some rendering and creating one image unique to that user only. After creating that image i want to display it and download it
I have one button 'download' declared in control's html file and specified one method in the onclick event of that button
The function specified in onclick is written in cs file of that control. it is as follows:
public void DownloadPNG(object sender, EventArgs e)
{
Response.Clear();
Response.ContentType = "text/png";
Response.AddHeader("content-disposition", "attachment; filename=hello.png");
Response.Write("...");
Response.End();
}
Am I doing right?. Will this call this function? Is this function right?
Try using Response.WriteFile or Response.TransmitFile or even Response.BinaryWrite.
You've really given no information to figure out what you are doing.
However, if you want to make a content download as a file from an ASP.NET web page, here's an article I wrote that shows how to do just that.
We have a page that opens in a new browser window where there's an automatic redirect to an ASHX handler that produces some kind of an XLS file. It's done using a javascript redirect, i.e. setting window.location.href to the URL of the ASHX.
Although it works and presents the download dialog for the file, setting window.location.href also clears the content of the window so that it stays blank. It somehow makes sense but still it would be nice to keep the content of the previous page there while opening the download dialog in the foreground. Is it possible somehow (by defering the execution of the redirect or using a different technique to call the ASHX handler) ?
Another nice to have thing would be if we could close the parent page after the download dialog is presented, could this work in any way ?
Dynamically creating (in javascript) an hidden iframe that points to the download ashx location and adding it to the DOM would do the trick.
By using the content-disposition header in your response from your handler you can display the save dialog without having to open a new window. You won't need to use javascript to open a new window or create an iframe.
response.Clear();
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("content-disposition", "attachment; filename=sample.xls");
response.AddHeader("content-legth", content.Length.ToString());
response.Write(content.ToString());
response.End();
See this question for the possible excel mime types ( contentType )
Setting mime type for excel document