PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Apr 19, 2024 10:05 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Sep 19, 2012 2:14 pm 
Offline

Joined: Wed Sep 19, 2012 1:05 pm
Posts: 2
I have implemented an XPS to PDF conversion using PDFsharp 1.32 (WPF version) and the PdfSharp.Xps from PDFsharp 1.31. Initially, I had problems with all images in any XPS, they were not visible in Acrobat Reader after the conversion. However, in some other post on this forum, I found the two bug-fixes needed:

XpsParser.ImageBrush.cs (ln 22, added): brush.Opacity = 1;

PdfContentWriter.cs (ln 526, changed): if (opacity <= 1)

This fixes the image problem - almost: Images are now displayed after conversion of most XPS types. However, if I create a document in MS Word 2010 and produce an XPS using "Save As", I will get an XPS that is slightly different from the original, for example the shadow effect on text is lost. Still, it looks allright, everything is readable and the image (which btw is a transparent png) is intact. When I convert this XPS to a PDF with my converter, the image is "lost" and instead I can see a couple of overlayed squares where the image should have been.

Now comes the really strange part: If I produce the XPS from the same document in Word 2010 but now using "Print" and print to the "Microsoft XPS Document Writer" printer instead, I get an XPS that looks exactly like the original. And, strangely, this XPS converts perfectly with my converter (almost, the mirroring effect on the heading leaves some extra "dust" underneath).

I will try to attach some files to the post to illustrate. (But I just got an error message doing this, so bear with me if they don't show up as expected.)

I know the PdfSharp.Xps is in beta, but it just seems so extremely close to a final solution! So, if anybody has been working on similar projects and could give some hints on how to fix or get around this problem, it would be fantastic!

Otherwise, kudos to the authors/developers of PDFsharp, this a really, really useful contribution to the .NET world!

Best regards,
Bendik Engebretsen
Norway


Attachments:
File comment: PDF converted from XPS file produced with "Print" to "Microsoft XPS Document Writer" from MS Word 2010
XPStoPDF OK.jpg
XPStoPDF OK.jpg [ 75.92 KiB | Viewed 18545 times ]
File comment: PDF converted from XPS file produced with "Save As" from MS Word 2010
XPStoPDF Error.jpg
XPStoPDF Error.jpg [ 75.73 KiB | Viewed 18545 times ]
Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 19, 2012 6:31 pm 
Offline

Joined: Wed Sep 19, 2012 1:05 pm
Posts: 2
I am stubborn son of a b...., so I couldn't just sit and wait for anyone to help me... and it turns out my stubbornness was rewarded:

1. I started by investigating thoroughly the difference between the two XPS files.

(For those of you that are interested in x-raying XPS files, here's a quick receipe: An XPS file is actually a zip'ped package of many files in a directory structure. Make a copy of your XPS file, rename it to ".zip" and then unzip it ("Extract all..."). Now dig into the folder structure: At the root level you'll probably find 'FixedDocSeq.fdseq' - rename this to 'FixedDocSeq.xml' and you can view the overall page sequence of the document in xml format. If you dig further into 'Resources' (might be on a different level, depending on the type of XPS) you'll find the fonts and images used in the document. Under 'Documents\1\Pages' you'll probably find '1.fpage', '2.fpage' etc. one for each page. Rename to '1.xaml' etc. and you can now open and view the xaml structured content of each page.)

Anyway, I found an interesting difference in the <Path.Fill><ImageBrush> elements which is how images are rendered in XPS xaml: In the XPS that came from "Save As" in Word, there is an extra <ImageBrush.Transform><MatrixTransform> inside it - and the viewport is just 0,0,1,1.

2. I debugged the PDF rendering code of PdfSharp.Xps and quickly drilled down to the rendering of the ImageBrush (it starts in PdfContentWriter.cs line 522). Drilling even deeper into the TilingPatternBuilder.BuildFromImageBrush->BuildPattern I quickly understood that I was now at the heart of the PDF rendering of ImageBrush. What I also quickly understood is that the code in BuildPattern() does pay attention to the viewport by prepending a maxtrix transform like this:

matrix.Prepend(new XMatrix(1, 0, 0, 1, brush.Viewport.X, brush.Viewport.Y));

What is does NOT pay attention to is if the brush itself also has a transform attached to it - which is the case with ImageBrushes in those "Save As" XPS files. So what I did was to simply to prepend another transform just after the one mentioned above, like this:

if (brush.Transform != null)
{
matrix.Prepend(new XMatrix(brush.Transform.Matrix.m11, brush.Transform.Matrix.m12, brush.Transform.Matrix.m21,
brush.Transform.Matrix.m22, brush.Transform.Matrix.offsetX, brush.Transform.Matrix.offsetY));
}

And, voila! It worked, the images are now rendered correctly in the PDF.

If the developers/authors read this, and find this interesting, feel free to contact me.

Ciao!
Bendik


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 02, 2013 9:24 am 
Offline

Joined: Sat Mar 30, 2013 2:51 am
Posts: 4
GOOD


Top
 Profile  
Reply with quote  
PostPosted: Wed Mar 30, 2016 11:25 pm 
Offline

Joined: Mon Mar 21, 2016 5:16 pm
Posts: 1
We have been struggle for a while and trying to find the solution for converting from XPS to PDF and images lost.
After applying your solution, images kept after converting from XPS to PDF.
Thanks for providing this great solution! -:)


Top
 Profile  
Reply with quote  
PostPosted: Fri May 13, 2016 3:21 pm 
Offline

Joined: Tue Jul 15, 2014 8:56 pm
Posts: 13
That doesn't quite cover it though.

the brush in xps also has a viewbox which is the area of the source image to pass through to the viewport.

if you crop an image in word and then print the document to xps the cropped image will be there in full with a viewbox that starts at a non 0,0 point.

I found changing the bbox and first transform could cope with that, however there was always a pixel or so wrapping around. I hardcoded a fix for now but it suggests I'm doing this wrong somehow. changing the code to the following works with an xps with an offset viewbox, but anyone know how this should be done?

Code:
       PdfTilingPattern BuildPattern(ImageBrush brush, XMatrix transform)
        {
            // Bounding box lays always at (0,0) <-- Nope! it follows the viewbox
            XRect bbox = new XRect(brush.Viewbox.X, brush.Viewbox.Y, brush.Viewbox.Width, brush.Viewbox.Height);

#if true
            XMatrix matrix = transform;
           
//change the transform to a translate since that's all this did, and add in the viewbox offset, however add 2 to avoid the dodgy pixel... I guess I'm doing this wrong
            matrix.TranslatePrepend((brush.Viewport.X - brush.Viewbox.X)+2, (brush.Viewport.Y - brush.Viewbox.Y)+2);
         
            if (brush.Transform != null)
            {
...
...


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 216 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group