views:

60

answers:

2

I've got a PDF I'd like to show in a Book Control. Since this control can't use PDFs I have to work around it by either using images or converting the PDF to XAML.

What is the most elegant way to do this?

+2  A: 

I have written two PDF to XAML conversion programs, based on the PDFRenderer library (open source). I need this in order to convert my mathematical documents and/or drawings to XAML so that I can paste them into a shared whiteboard (of my own design) when tutoring over the internet, see http://www.mathcoach.ch/blog/?p=30.

The main problem is the conversion of font references ("glyphs"). If you use PDFRenderer, which includes the conversion of font references to outlines, it is easy to generate XAML code that contains Path elements only. Since XAML does not, as far as I know, allow you to directly insert images, my two conversion programs do not convert raster images but just silently drop them (converting raster images it's just something that I do not consider worth my time at the moment).

To convert PDFs to XAML using Path elements only, you can simply implement a Graphics2D object (it makes things easier if you inherit from org.apache.batik.ext.awt.g2d.AbstractGraphics2D). The main methods to implement in that case are fill() and stroke(), which is easy to do because Java2's GeneralPath objects map very neatly to XAML's Path elements (line segments, quadratic and cubic Bézier segments).

Note, however, that if the original PDF contains lots of text (glyph references) the resulting XAML code is going to be large. About 2MB per A4 page of text. However, it works great with mathematical drawings that just contain relatively few glyph references.

For my own PDFs I can keep the glyph references, i.e. do not have to convert them to outlines and, therefore, to Path elements, because I am using fonts only that are sure to be already installed on the machine where the document is to be displayed. Also, this requires a relatively fragile process of transforming the glyph indices so that they are just right if used as indices into installed font files.

Christian Stapfer
Can I download and try PDF2XAML somehow?
Hedge
I hesitate to make it generally available because of the question of legal issues. I don't even want to read all the various ifs and buts of the license. It works great for me. However, these programs are not documented for others to understand, and to make the conversion more efficient I had to modify the source of the PDFRenderer library. So, at the moment, I would like to keep these two converters for my private (non-profit) use. - But maybe you can mail me an example PDF to convert with my tools? You can find my mail address here http://www.mathcoach.ch/Frame/English/Contact.html
Christian Stapfer
+1  A: 

I would say that for the purpose of reading in a book control, converting the document to images is a better solution, as that would avoid creating massive XAML documents and a huge amount of processing.

There are a number of free and commercial libraries that can convert a PDF document to images page-by-page. Depending on performance requirements you could either create a list of pages on opening the document or get and convert each page as it is required, possibly with some caching of previous pages. The sky's the limit really.

IanGilham
I went with that way because the documents were really too big.I'm showing them in one size only, because there are some problems with including a BookControl in a ScatterViewItem (The flipping won't work as it should).
Hedge