Tuesday, May 19, 2020

Displaying a PDF File in a VB.NET Form

PDF files have an internal document format that requires a software object that understands the format. Since many of you might have used the functions of Office in your VB code, lets look briefly at Microsoft Word as an example of processing a formatted document to make sure we understand the concept. If you want to work with a Word document, you have to add a Reference to the Microsoft Word 12.0 Object Library (for Word 2007) and then instantiate the Word Application object in your code. Dim myWord As Microsoft.Office.Interop.Word.ApplicationClass Start Word and open the document. myWord CreateObject(Word.Application) myWord.Visible True myWord.Documents.Open(C:\myWordDocument.docx) ( must be replaced with the actual path to the document to make this code work on your PC.) Microsoft uses the Word Object Library to provide other methods and properties for your use. Read the article COM -.NET Interoperability in Visual Basic to understand more about Office COM interop. But PDF files arent a Microsoft technology. PDF - Portable Document Format - is a file format created by Adobe Systems for document exchange. For years, it was totally proprietary and you had to get software that could process a PDF file from Adobe. On July 1, 2008, PDF was finalized as a published international standard. Now, anyone is permitted to create applications that can read and write PDF files without having to pay royalties to Adobe Systems. If you plan on selling your software, you still may be required to get a license, but Adobe provides them royalty-free. (Microsoft created a different format called XPS that is based on XML. Adobes PDF format is based on Postscript. XPS became a published international standard on June 16, 2009.) The Uses of PDF Since the PDF format is a competitor to Microsofts technology, they dont provide a lot of support and you have to get a software object that understands the PDF format from someone other than Microsoft right now. Adobe returns the favor. They dont support Microsoft technology all that well either. Quoting from the latest (October 2009) Adobe Acrobat 9.1 documentation, There is currently no support for the development of plug-ins using managed languages such as C# or VB.NET. (A plug-in is an on-demand software component. Adobes plug-in is used to display PDFs in a browser.) Since PDF is a standard, several companies have developed software for sale that you can add to your project that will do the job, including Adobe. There are also a number of open-source systems available. You could also use the Word (or Visio) object libraries to read and write PDF files but using these large systems for just this one thing will require extra programming, also has license issues, and will make your program bigger than it has to be. Just as you need to buy Office before you can take advantage of Word, you also have to buy the full version of Acrobat before you can take advantage of more than just the Reader. You would use the full Acrobat product in about the same way that other object libraries, like Word 2007 above, are used. I dont happen to have the full Acrobat product installed so I couldnt provide any tested examples here. How To But if you only need to display PDF files in your program, Adobe provides an ActiveX COM control that you can add to the VB.NET Toolbox. It will do the job for free. Its the same one you probably use to display PDF files anyway: the free Adobe Acrobat PDF Reader. To use the Reader control, first make sure that you have downloaded and installed the free Acrobat Reader from Adobe. Step 2 is to add the control to the VB.NET Toolbox. Open VB.NET and start a standard Windows application. (Microsofts next generation of presentation, WPF, doesnt work with this control yet. Sorry!) To do that, right-click on any tab (such as Common Controls) and select Choose Items ... from the context menu that pops up. Select the COM Components tab and click the checkbox beside Adobe PDF Reader and click OK. You should be able to scroll down to the Controls tab in the Toolbox and see the Adobe PDF Reader there. Now just drag the control to your Windows Form in the design window and size it appropriately. For this quick example, Im not going to add any other logic, but the control has lots of flexibility that Ill tell you how to find out about later. For this example, Im just going to load a simple PDF that I created in Word 2007. To do that, add this code to the form Load event procedure: Console.WriteLine(AxAcroPDF1.LoadFile( _   Ã‚  Ã‚  C:\Users\Temp\SamplePDF.pdf)) Substitute the path and file name of a PDF file on your own computer to run this code. I displayed the result of the call in the Output windows only to show how that works. Heres the result: --------Click Here to display the illustrationClick the Back button on your browser to return-------- If you want to control the Reader, there are methods and properties for that in the control too. But the good folks at Adobe have done a better job than I could. Download the Adobe Acrobat SDK from their developer center (http://www.adobe.com/devnet/acrobat/). The AcrobatActiveXVB program in the VBSamples directory of the SDK shows you how to navigate in a document, get the version numbers of the Adobe software you are using, and much more. If you dont have the full Acrobat system installed - which must be purchased from Adobe - you wont be able to run other examples.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.