Displaying PDF on WebBrowser Control not working - c#

I Have a test.pdf and I want to display it inside in my form.
My code is very simple:
public Form1()
{
InitializeComponent();
this.wbPdf.Navigate(#"file:///<fullpath>\test.pdf#toolbar=0");
}
and it's not working. It's showing me a white page with an "X".
But if I do instead this:
this.wbPdf.Navigate(#"file:///<fullpath>\test.pdf#toolbar=0", true);
the IE opened and it showed my pdf. Of course, I tried to use false in the second parameter and it's not working.
Also, if I tried to do something like
this.wbPdf.Navigate(#"http://www.google.com");
it showed me google, so I think there isn't any problem of configuration of Web Browser Control
Any ideas? I'm not will be able to have Acrobat Reader installed, so using ActiveX components is not an option (also, this project is in x64 and I've read that this component is not working very well in x64).
I want to display the pdf only for viewing inside the form, not in another window.

AFAIK, the web browser control in WinForms relies on the default PDF reader (usually Acrobat Reader) for displaying PDF files. If you need to display PDF files without requiring any other piece of software to be installed, then you will probably need to use a PDF rendering library in your application. Some examples of PDF rendering libraries:
MuPDF A GPL/Commercial viewer with a .net wrapper, you need a commercial license for use on commercial closed-source applications.
Amyuni PDF Creator .Net Commercial library for editing or displaying PDF files. Disclaimer: I currently work as a developer of the library.
Another option would be to create a local HTML5 page that renders the PDF file using the project pdf.js, then embeed that page on your web browser control. But this will only work on Windows systems with IE 9.0 or above.

Related

Print doc files in asp.net mvc project

I am working on an asp.net mvc project. How can I open doc.file to print it from page? The only thing I can do now is downloading, however, I'd like to open it as print friendly file in sort of window.print()
Normally you just supply correct content type. Then user can view the file inside the browser and print it.
However, if the browser doesn't have appropriate plugin, then user can only download the file.
public ActionResult Download()
{
return File("~/MyDocument.doc", "application/vnd.ms-word", "MyDocument.doc");
}
.doc files are designed to be opened with MS Word (and at one point there was a Word Viewer presumably designed for this sort of thing). There are also other apps (e.g. OpenOffice / LibreOffice) which will allow you to read & write MS Word documents with varying degrees of success.
So one option -- as #Win points out -- is to require that users have some compatible application installed. You frequently see this with PDF files, the site will typically contain a link to download Adobe (Acrobat) Reader. The external app will be used to display the file; if the app can run as a plugin the content will be displayed "inside" the browser.
If the app is primarily intended for users internal to your business, and they all have Office installed, then that's a perfectly acceptable solution.
If you want the content to be available people who don't have Office (and/or don't run Windows) it gets much more difficult. You'd need to either:
write an MVC view that will parse/render .doc files (not for the faint of heart)
convert the content to some other format (PDF or HTML) that's more easily "consumed".

Opening Word (.docx) files on a Windows Form C#

I'm trying to make my program have the ability to display a Microsoft Word file on a form but not having any luck in doing so. I want to be able to open the file and display it on the form as a Read-Only. So basically just display it's contents. Various users on the web have recommended displaying files in the WebBrowser control (under toolbox). I have tried this but failed to get it working. My end goal is to be able to annotate on top of the web browser (or something of similar manner) and subsequently save the annotations along with the opened file.
I'm not that experienced in the C# language too so any help on how to achieve my problem would be greatly appreciated.
You need a DOCX viewer control. My company recently gave away our multi-format WinForms viewer control as a free product - XtremeDocumentStudio .NET Free. It can display DOCX and is available on NuGet. It does not require a Web Browser control for embedding an online viewer or Word software installed locally.
http://www.nuget.org/packages/XtremeDocumentStudio.NETFree/
There are some good links that might help you.
First, you will find a closely related or similar question here
And in that conversation someone posted the following link which is about how to build a user control for displaying Word documents in a webbrowser control.
I also found another article here about how to integrate Excel in a Windows Form application using WebBrowser. But Excel is used as an example and you may be able to adapt it to Word.

Open PDF in C# as view only without adobe

im needing to create a form in my C# project that just allows the user to view the pdf.
i have a way to open the pdf and read it but i need to disable features like printing, saving, highlighting, copy/pasting while maintaining the ability to search in the document
they should really just be able to open the document, read it,search for words in the document, close it
any help would be great
thanks in advanced
You could use Ghostscript to convert PDF to images and then show the images on your form or you could rasterize your PDF directly to the screen.
To use Ghostscript from .NET you can take a look at the Ghostscript.NET library (managed wrapper around the Ghostscript library).
Ghostscript Viewer C# sample that rasterizes PDF directly to the screen can be found here: https://github.com/jhabjan/Ghostscript.NET/tree/master/Ghostscript.NET.Viewer
To search for the text inside the pdf you can use iTextSharp
(Disclaimer I worked on this component at Software Siglo XXI)
If you don't want to mess with Ghostscript API and need a quick working solution to visualise the documents, you could use ImageZoom Viewer .NET. It's available for both 32 and 64 bit and is very cheap and effective. I'd recommend you to try it since it's a very fancy and fast. You can browse, scroll and print the pages from the viewer.
You can take a look here: http://softwaresigloxxi.com/ImageZoom.html
This is for quick browsing and reading. Then, when you want to use text operations, you could let the user to use Adobe Reader, launching the PDF from there.

hosting a PDF in a c# application

I am creating an ebook for a friend. He has the content in word (docx) so I have a few questions. I created a thread earlier on how to approach this and the best response was to create PDF's out of it. The main point of this was so we can have insertable textboxes, links, and table of contents.
Now I was wondering if I can host the pdfs in my application. I want to make an "adobe reader" basically. Is there a library out there that does this? Can I use installed COM/dlls to do this?
The reason for this is that I was to create like a "quiz" section on my application, or a "report card" or even a "bookmark" feature.
Thanks guys.
Making a custom PDF browser control could be an extremely challenging task. You could instead use the real Adobe Reader in your application as it is provided as a COM control which could be hosted in any Windows application. Here are the steps:
Start a new WinForms application
Make the Acrobat control available in the toolbox (Tools -> Add/Remove Toolbox Items: turn on "Adobe Acrobat 7.0 Browser Document" in COM Components tab). Don't forget to grab a cup of coffee before clicking on the COM tab.
Drag the control to the form
Manipulate it:
axAcroPDF1.LoadFile("mypdf.pdf");
axAcroPDF1.Show();
I know this has been asked before, but I can't remember where. The accepted answer was something along these lines:
It would be a very bad idea to create your own PDF reader, because:
you are not in control of when Adobe
changes specs
Adobe provides a
reader free of charge, and most
users already have it installed.
Given the previous two statements, why would you want to spend that much time on reinventing the wheel? It makes sense to reinvent the wheel when you can improve upon it, but not when you are just copying functionality that is freely available.
It would be far better to find a way to use the Acrobat reader that they have already designed, and distribute free of charge.
This is very easy to do. All you need to do us use the WebBrowser control (assuming this is WinForms) and set the DocumentSource to the PDF file. The WebBrowser control will just automatically use whatever pdf reader is already installed on the client PC, just as would Internet Explorer.
I've had very good experiences working the the Aspose libraries. They're affordable and will save you a lot of time.
You can embed Adobe Reader in your apps via the ActiveX component that is installed on your machine along with the reader. You can add it to your VS Toolbox from the COM Components tab. Anyone who has Abobe Reader installed will then be able to use your app to view PDF documents.
Note that if there is not Adobe Reader installed your program will not work, or you need somehow to provide the lib with the application.
If you want something that does not require Adobe Reader then you may like to check out the iText#.
A potential problem with using a Adobe ActiveX/com component is that the user must have Adobe Reader installed for this to work (I don't think you're able and/or allowed to redistribute the activeX control only and wouldn't want to force my users to install Adobe Reader). So for more serious work I would try to use a Pdf library that you can distribute with your code such as writing a (presumably very small) P/Invoke wrapper for the open source library http://ccxvii.net/mupdf/ or if you can/want afford it use some commercial library.
An problem with using a webbrowser control is that you have to be careful to test what happens if a target user as embedding of pdf disabled in IE. It can happen that instead of embedding you get a external adobe reader (or whatever is registered as pdf handler) pop up! There might be a workaround, but I've been bitten by this before (resulting in one of those "works on my machine" problems)

Printing a PDF from a Windows service

With C#, how can I print a PDF document (without any dialog boxes in the background) to an indicated printer?
I have used the following PDF library in the past to perform batch printing of PDF documents from a C# Windows Service.
http://www.quickpdflibrary.com/
It's a pretty decent library and if I remember correctly it only took a few lines of code to print the document. It worked very well and saved me a consider amount of time.
Also, the recommended link that discusses using DDE to launch a desktop application on a server to perform the printing is a bad idea. Adobe Reader probably doesn't support this and since it is a desktop application running in a windowless session you may run into problems with message boxes and dialog boxes. That solution just doesn't scale in my humble opinion.
here we go
Disclaimer: I work for Atalasoft
If you don't want to depend on Acrobat being on the server, you need to rasterize the PDF pages yourself and send them to the printer using the normal .NET printing API. We have a product that can rasterize PDF that uses the Foxit engine underneath. It deploys as a normal .NET assembly and doesn't require any other software to be installed.

Categories