I have a Preview button on my page. I want to display a .doc file in the browser when the user clicks on that button. I have written the code below, but it directly downloads the file instead of displaying it in the browser.
public FileResult ViewFile() {
Response.AddHeader("content-disposition", "inline;filename=;");
return File("~/Templates/chronoExp.doc", "application/msword");
}
You will need some sort of control or library that's capable of showing the file type within the browser. By default, browsers are not able to display an office document file (I believe PDF is the only document style that gets relatively decent support for embedded viewing across all browsers).
In order to do this, look for some 3rd party tools or controls that would allow for this to work. Such tools will involve passing the document to them and likely some server-side transformation and render to the control's browser display, so it's likely to be read-only.
This answer provides a decent example of using Google's document viewer within an iframe: How do I render a Word document (.doc, .docx) in the browser using JavaScript?
Related
I'm writing a document view and I'd like to use the capabilities of the WebBrowser control for some file formats (pdf, e-mail).
Now I have the problem, that for every file format that is unknown to the WebBrowser control, it offers to download the file.
So the question is: How to find out what file formats (file extensions) can be displayed by the browser, so that I can display a message to the user that the current fileformat is not supported for displaying instead of getting a "Do you want to download" dialog box.
If I was you I wouldn't use a WebBrowser control for following reasons:
WebBrowser is just a wrapper around IE engine. It uses old version of IE by default, so it can have troubles displaying correctly HTML documents with basic CSS styling. It can be forced to use newer versions of IE or even other engines, but it takes some effort.
WebBrowser control purpose is to display and navigate throught web pages. And navigating is essentially downloading. I believe it's gonna be hard if not impossible to prevent WebBrowser from downloading files. I believe one way you can do it is by modifying html files like this: Remove hyperlink but keep text? Also you would probably want to prevent context menu from openning.
WebBrowser control is capable of displaying PDF as described here Displaying a PDF in a WPF Application Not Working - WebBrowser or Adobe Control But I have no idea how would you prevent following the links placed in pdf
Instead of using WebBrowser I suggest you to use one of many royalty-free PDF viewing controls and convert your html documents to PDF. I believe suggested approach will take you less time and will be more robust
How to disable the pdf Toolbar because I don't want to give the download option to the pdf viewers.
I have used Iframe for showing Pdf File please find the following code
<iframe ID="iFrame2" runat="server" align="bottom" frameborder="2" name="iframe1" height="500" width="600" src="~/pdffiles/example.pdf"></iframe>
Please help me to disable the download option using c#.
Thanks
You cant. It is a browser feature which means you have no control over it.
You can use third party tools like Google Document Viewer or change the format of your document to an image.
Google Document Viewer
If you want to avoid that an end user saves a PDF document, you are asking something that is impossible. The only way to avoid that an end user doesn't have a copy of the PDF is by not sending him the PDF. A PDF can't be opened in Adobe Reader without having the actual bytes on the disk. Even if you would disable saving (for instance in the context of a web application), you'd always find the PDF somewhere in the temp files and people would be able to copy that file as many times as they want.
Trying to hide the toolbar (a viewer preference) doesn't make sense. Whether or not this viewer preference will be respected entirely depends on the PDF viewer. For instance: in Adobe Reader X and later, you have a special widget (HUD or Heads Up Display) that appears when you hover over the document. This widget allows users to save the document.
Let me quote Adobe:
the "Heads Up Display" (HUD) is not customizable. There are no APIs to
HUD. You can’t use JavaScript to enter Read Mode, exit Read Mode or
detect that the document is in Read Mode. Though it might seem like
it, this wasn’t an oversight. There are some very sound engineering
reasons why this is the case but I won’t go into those here.
Even with Adobe Reader 9, hiding the toolbar isn't sufficient: if the user chooses the appropriate menu item or hits the appropriate "hot key", the toolbar would appear and they could happily click the Save button. In addition, they could have right-clicked and chosen "Save" as well.
In short, you're asking the wrong question (and that probably explains the downvotes given by several people).
I am working with C# and MVC4, I am trying to open the print dialog when the pdf document is opened by clicking on print button by user.
I Google it, but I could not find any better solution for this.
One thing i can do is, I can create one view and embed the pdf document to the view then on open i can have jquery method to print the document. Is it a correct way?
Please suggest.
Calling print in the HTML document does not have to call the print in the PDF document displayed by the PDF plugin. Some browsers do seem to call the print command of the PDF plugin but this is not the standard behaviour. Also, there is no guarantee that a PDF plugin has been installed on all client computers.
You could create a auto-print pdf where the document is set to invoke its print command when it is opened in a PDF viewer application. This would work even if the document is saved and then opened outside the browser.
This is what I have done:
I have loaded a pdf file in web browser,
Now I want to select text from that file and paste into a text box.
Can anyone help me?
I'm pretty sure that this is going to be prohibitively difficult, if not impossible, to do.
The browser does not 'run' the PDF, it acts as a host for the PDF application, which ends up sharing it's main window. After that, control of the cursor etc passes to the PDF application and the browser is effectively no longer aware of what happens inside it. If the PDF application being used exposes COM interfaces for manipulating the cursor/text selection (doubtful), then it's possible to script against those interfaces from client script - but you won't be able to actually run any script in that window because the browser is showing a PDF, not a web page.
It might be possible if you hosted the web control on a windows forms application, but even so I wouldn't even know where to start on that one.
If your goal is to extract text from the PDF then you're probably better off pushing it through a .Net PDF library. A quick google on that one will yield you some suitable libraries.
if your pdf file has form elements then the file can be submitted to a url.
check this link.. it might help.
Can a PDF fillable form post itself to an HTTPS URL?
I am developing a C# WinForms application, and I would like to be able to "preview" various document types within it. That is, when a user selects a filename from a list, it shows below, within the same form, a preview of the selected file. It's a lot like they way Outlook allows you to preview a selected message without double-clicking.
Is there any way to take just a filename and create a control dynamically within my WinForms application that shows the contents of that file?
If you want to provide a preview/readonly version of the file types, an idea might be to implement PDF viewing first, this primarily involves converting PDF pages to images and then creating a view for those. Once you have that done (using GhostScript or other commercial components) you can then work on converting other formats to PDF, and use the PDF viewing option, you can probably do most office documents through word automation and text based files could be displayed directly (possibly using a rich text editor for formatted text)
HTH