Acrobat Viewer in your WinForms App

by digvijay 12. June 2009 23:56

I am working on an application where we needed the user to be able to view a PDF and be able to add some annotations/comments and save it back. One choice was to just launch Acrobat Professional and let the user do what they wanted and other was to integrate Acrobat in our application.

Acrobat SDK (which is now free) does provide several samples and shows how to do it however, it is never easy to do it when you have a specific task in mind. I managed to show the PDF but adding the comment etc is not easy via the API and i was wondering how to enable comments and annotations when the Acrobat window is opened in my application.

I managed to do it via the API but I am not sure why it works sometimes and why it does not at other times.

Anyway, here is a snippet:

/// <summary>
/// Opens the document.
/// </summary>
/// <param name="fileName">Name of the file.</param>
public void OpenDocument(string fileName)
{
if (this._avDoc == null)
{
this._avDoc = new AcroAVDocClass();
}

if (string.IsNullOrEmpty(fileName))
{
throw new ArgumentNullException("The filename specified is either empty or null.");
}

if (!File.Exists(fileName))
{
throw new FileNotFoundException("The filename specified does not represent a file on disk or could not be found.");
}

this._currentFileName = fileName;

try
{
this._avDoc.Close(1);
bool b = this._avDoc.OpenInWindowEx(this._currentFileName,this.Handle.ToInt32(),
(int)AVOpenParams.AV_DOC_VIEW, 0, 0, (short)PDViewMode.PDUseNone,
(short)AVZoomType.AVZoomFitPage, 0, 0, 0);

Debug.WriteLine("OpenInWindowEx returned: " + b.ToString());
}
catch
{
throw;
}
}

This allowed me to render the acrobat in a Control derived window.

using Acrobat;
/// <summary>
/// The PDF Component.
/// </summary>
[ToolboxBitmap(typeof(PDFEditor))]
public class PDFEditor : Control
{
#region Fields
/// <summary>
/// The name of the currently open file.
/// </summary>
private string _currentFileName = string.Empty;

/// <summary>
/// The AcroBat AVDoc Interop object.
/// </summary>
private CAcroAVDoc _avDoc = null;

#endregion Fields

#region Properties

/// <summary>
/// Gets the name of the currently open file.
/// </summary>
/// <value>The name of the current file.</value>
public string CurrentFileName
{
get
{
return this._currentFileName;
}
}

.....
}

And yes, you need to have Acrobat Pro installed and a reference to the acrobat.tlb from COM tab added to your project.

Hope someone finds this Tip useful.

Tags: ,

.NET | Acrobat

 

About Digvijay

  Digvijay Chauhan
I am a developer living in Stockholm, Sweden and I love to program and work on cutting edge technologies in the Microsoft Technology space.

LinkedIn Twitter StackOverflow

Certifications

Digvijay Chauhan Microsoft Certification Logo

Other Pages

RecentPosts

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

Most comments

Live Traffic

Live Traffic Feed
  Västra Frölunda, Vastra Gotaland arrived from digvijay.eu on "blank_page"

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Translate