Click here to Skip to main content
13,798,308 members
Click here to Skip to main content
Add your own
alternative version


241 bookmarked
Posted 25 Jun 2009
Licenced GPL3

View PDF files in C# using the Xpdf and muPDF library, Print PostScript.

, 26 Nov 2010
Rate this:
Please Sign up or sign in to vote.
Wrapper C# class written in C++\CLI and a sample implementation in C# to render PDF files.



Xpdf is an Open Source library released under GPL license; they have an ActiveX with commercial license, but some time ago, before I knew about this commercial control, I wrote this wrapper library to render PDF files in C#. 


The basic idea is create a preview of PDF files in C#. After looking at many places in the internet, I found this wonderful library; the only problem is that the library uses XLib, and there is no XLib available for Windows. Fortunately, Xpdf can render the generated PDF into a Win32 DC.

Writting the wrapper

C++\CLI can mix managed and unmanaged code thanks to the IJW technology, so I was thinking that maybe I could to link the xpdf lib to the wrapper. The problem is that xpdf has some classes that are also declared in .Net, the solution was compile a C++ project with a class that includes only the necessary files to do the interop.
This library (AFPDFLib) contains a simple class that works like a proxy between C++ and C++\CLI, keeping the xpdf objects into the unmanaged Heap.
The C# wrapper is linked to AFPDFLib statically, and this only includes:
The classes:
AFPDFDoc -> Implement the methods that needs xpdf.
AFPDFDocInterop -> Write the methods to wrap into C#
PDFWrapper -> Wrapped methods

Marshal Strings:
IntPtr ptr = Marshal::StringToCoTaskMemAnsi(fileName);
char *singleByte= (char*)ptr.ToPointer();

For releasing resources is necessary implement IDisposable:


Using the code  

The file xpdfWin-Interop.sln includes all the necessary files, you can also download the last version from and recompile without the files that requires XLib.

The Build Project Order is as follows: freetype,xpdf,AFPDFLib, PDFLibNet. Once compiled PDFLibNet, it can be used in C# code:    

OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Portable Document Format (*.pdf)|*.pdf";
if (dlg.ShowDialog() == DialogResult.OK)
    _pdfDoc = new PDFLibNet.PDFWrapper();
    _pdfDoc.CurrentPage = 1;

   PictureBox pic =new PictureBox();
   pic.Height = _pdfDoc.PageHeight;
   Bitmap _backbuffer = new Bitmap(_pdfDoc.PageWidth, _pdfDoc.PageHeight);
   using (Graphics g = Graphics.FromImage(_backbuffer))
   pic.Image = _backbuffer;

It is necessary create a PictureBox because the class implements only a method that accepts an HWND, because in the first instance, I was trying to implement the scroll into the same control that the PDF is rendered. In the included sample, the scroll is controlled by a Panel container.

Xpdf can export the PDF to a PostScript file. For printing this is the best option if you have a PostScript Printer:       

PSOutputDev *psOut =new PSOutputDev((char *)fileName,m_PDFDoc->getXRef(),m_PDFDoc->getCatalog(),fromPage,toPage,psModePS);
delete psOut;  

The file must be sended in RAW format (

JPG Export 

For async export: 

1,        //From page
1,        //To page
150,      //Resolution in DPI
90,       //Jpg quality 
if you need a sync operation its posible especify a wait time:
1,        //From page
1,        //To page
150,      //Resolution in DPI
90,       //Jpg quality
-1);      //Time to wait, -1 to infinite.
 If the file name does not contains a %d token (for the page number), then the procedure replaces .jpg with -page%d.jpg.  PDFWrapper exposes two events ExportJpgProgress and ExportJpgFinished. Both events are called from the exporting Thread, so it is necesary to make a security call using Invoke, check frmExportJpg for a sample.  


  • Full deployed solution.
  • Updated to xpdf 3.0.2 version.
  • FreeType updated to 2.3.1
  • When click in a bookmark and search, the page scroll to the correct position.
  • PostScript implemented. 
  • Now gets the Title, Author.  
  • Some memory leaks corrected 
  • Prerender next page in new thread 
  • Cache of pages  
  • Mouse Scrolling 
  • Mouse Navigation 
  • Load links from page (LinkURI, LinkGoTo) 
  • Using DIB Sections,  fixes the problem with the zoom.
  • Added control PageViewer, now render only the viewable area
  • Open password protected files.
  • Export to txt
  • Export to jpg
  • Added support for Unicode in Bookmarks, title, subject, keywords...
  • Added support for named destinations
  • Fixed some bugs.
  • Added support for unicode search
  • Multithread jpg export
  • Fixed others bugs
  • Added MuPDF as second renderer. 
 26\NOV\2010 Know issues:     MuPDF has some problems with transparency, but is faster than xpdf.  A couple of memory leaks.


MuPDF uses recursion for analyze the tree document, so is necessary increment the Stack Size to at least 4mb to avoid problems with some complex files (editbin for C#, VB.Net exe's). Soon or later the recursion causes an stack overflow if the tree is so big, so while it is fixed that is the most important issue.

To Do:        

- Apply last xpdf patche - Show multiple pages in the viewer. - Improve user interface. - Implement LoadFromStream for MuPDF. There is missing some functionality that can be extracted from xpdf: - Enable selection, image extraction and instant snapshot. - Print in non PostScript printers.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Antonio Sandoval
Engineer HidraQuim SA de CV
Mexico Mexico
I'm a Chemical Engineering that loves the programming.
2003 - Graduated from Technical Programmer UNIVA México.
2009 - Graduated from Chemical Engineering Universidad de Guadalajara, Mexico.
Programmer by Hobby since 6 years ago.

You may also be interested in...


Comments and Discussions

AnswerRe: Loading PDF from Memory stream Pin
Antonio Sandoval21-Aug-09 4:56
memberAntonio Sandoval21-Aug-09 4:56 
GeneralRe: Loading PDF from Memory stream Pin
Antonio Sandoval21-Aug-09 22:22
memberAntonio Sandoval21-Aug-09 22:22 
GeneralRe: Loading PDF from Memory stream Pin
softboy9911-Mar-10 5:45
membersoftboy9911-Mar-10 5:45 
GeneralOutlineItem.Destination.Page property not available - corrupt memory error Pin
Ron Schuler16-Aug-09 6:08
memberRon Schuler16-Aug-09 6:08 
GeneralRe: OutlineItem.Destination.Page property not available - corrupt memory error Pin
Antonio Sandoval17-Aug-09 5:49
memberAntonio Sandoval17-Aug-09 5:49 
GeneralRe: OutlineItem.Destination.Page property not available - corrupt memory error Pin
Antonio Sandoval21-Aug-09 10:25
memberAntonio Sandoval21-Aug-09 10:25 
GeneralRe: OutlineItem.Destination.Page property not available - corrupt memory error Pin
Ron Schuler1-Sep-09 10:44
memberRon Schuler1-Sep-09 10:44 
GeneralRegarding PDFLibNet.dll Pin
xaglez10-Aug-09 6:41
memberxaglez10-Aug-09 6:41 
I found your code very useful specifically the PDFLibNet.dll

I need to know if I can use PDFLibNet.dll in my project the only thing I need from that dll is the ExportToJpeg functionality. My point is I have to pay for a commercial license for using that dll in my project (website closed code for commercial purposes).

I will appreciate a fast response on this.


AnswerRe: Regarding PDFLibNet.dll Pin
Antonio Sandoval10-Aug-09 13:31
memberAntonio Sandoval10-Aug-09 13:31 
GeneralRe: Regarding PDFLibNet.dll Pin
Ron Schuler15-Aug-09 16:23
memberRon Schuler15-Aug-09 16:23 
GeneralRe: Regarding PDFLibNet.dll Pin
softboy9911-Mar-10 5:46
membersoftboy9911-Mar-10 5:46 
GeneralRe: Regarding PDFLibNet.dll Pin
cancel0077-Oct-10 21:28
membercancel0077-Oct-10 21:28 
Questionwhy I can't search chinese words Pin
billjone7210-Aug-09 0:56
memberbilljone7210-Aug-09 0:56 
QuestionRe: why I can't search chinese words Pin
Antonio Sandoval10-Aug-09 5:32
memberAntonio Sandoval10-Aug-09 5:32 
AnswerRe: why I can't search chinese words [modified] Pin
billjone7210-Aug-09 7:12
memberbilljone7210-Aug-09 7:12 
QuestionRe: why I can't search chinese words Pin
Antonio Sandoval21-Aug-09 10:52
memberAntonio Sandoval21-Aug-09 10:52 
AnswerRe: why I can't search chinese words Pin
softboy9911-Mar-10 5:49
membersoftboy9911-Mar-10 5:49 
GeneralIntermittent rendering corruption Pin
Ron Schuler7-Aug-09 13:36
memberRon Schuler7-Aug-09 13:36 
QuestionRe: Intermittent rendering corruption Pin
Antonio Sandoval7-Aug-09 16:46
memberAntonio Sandoval7-Aug-09 16:46 
AnswerRe: Intermittent rendering corruption Pin
Ron Schuler8-Aug-09 12:35
memberRon Schuler8-Aug-09 12:35 
GeneralRe: Intermittent rendering corruption Pin
Antonio Sandoval8-Aug-09 13:08
memberAntonio Sandoval8-Aug-09 13:08 
GeneralRe: Intermittent rendering corruption Pin
Ron Schuler9-Aug-09 14:09
memberRon Schuler9-Aug-09 14:09 
GeneralDoes not work on a non-dev machine... Pin
pvatsa4-Aug-09 6:42
memberpvatsa4-Aug-09 6:42 
GeneralRe: Does not work on a non-dev machine... Pin
yasirkamal6-Aug-09 2:43
memberyasirkamal6-Aug-09 2:43 
GeneralYou're code Pin
egpuyos3-Aug-09 0:02
memberegpuyos3-Aug-09 0:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.181207.3 | Last Updated 26 Nov 2010
Article Copyright 2009 by Antonio Sandoval
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid