PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Wed Jun 25, 2025 6:58 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Mon Feb 22, 2010 12:28 pm 
Offline

Joined: Mon Feb 22, 2010 12:04 pm
Posts: 1
Hi there

I'm hoping someone could help with a problem I am experiencing when converting an XPS file to a PDF file using PDFsharp.

I have a very simple XPS document which contains only an image inside a canvas. When I view the XPS file I can see the image. When I used PDFsharp to convert the XPS file to a PDF file, the image is no longer displayed.

I have looked inside the PDF file which PDFsharp produces and the image data is there. It is a bit hard to follow the PDF command chain at a glance (I am in the process of having a closer look) but I’m fairly sure it is part of the document.
Interestingly, if I print the XPS file to a PDF file using PDFCreator I get a PDF file with the image in it.

Any ideas why a single image in an XPS file would convert to PDF but not display correctly?

My questions at the moment are:
-Could it be something like a render or layout transform (which I remove)?
-Could it be tiling (I notice the PDFsharp PDF file uses a pattern)?
-Is the PDF command to display the image being used?

Here is a more information into what I am doing roughly speaking:

1. First, I render a complex custom control to a PNG file as follows:
Code:
  // Create a render bitmap and push the surface to it
  RenderTargetBitmap renderBitmap =
    new RenderTargetBitmap(
      (int)size.Width,
      (int)size.Height,
      96d,
      96d,
      PixelFormats.Pbgra32);
  renderBitmap.Render(surface);


I’ve already tried saving the control directly to XPS and this doesn’t work either, hence I’m trying the rendering to an image.

3. Create a very simple WPF Canvas control:
Code:
<Canvas>
<Image Source=”renderBitmap.png”
... no scaling, no stretching/>
</Canvas>


4. Save Canvas to XPS:
Code:
  canvas.LayoutTransform = null;
 
  Package package = Package.Open(filename, FileMode.Create);
  XpsDocument doc = new XpsDocument(package);
  XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
  writer.Write(canvas);
  doc.Close();
  package.Close();


5. Convert XPS file to PDF using PDFsharp:

Code:
   PdfSharp.Xps.XpsConverter.Convert(filename)




Any help would be much appreciated!!

Mark


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 26, 2010 9:26 am 
Offline

Joined: Fri Mar 26, 2010 9:17 am
Posts: 4
There appear to be two bugs in the code for the XPS to PDF converter when including images.

The first is that when the image brush is created from the page resources, the opacity defaults to 0.0. If the image brush has no explicit opacity defined it ends up transparent. I've fixed this by a quick and dirty hack! In the method PdfSharp.Xps.Parsing.XpsParser.ParseImageBrush immediately after the brush is created add the line:
brush.Opacity = 1;

The second is that the boundary condition for drawing the brush is incorrect. In method PdfSharp.Xps.Rendering.PdfContentWriter find the section where the ImageBrush case is handled. It sets the opacity by multiplying the opacity of the path by the opacity of the brush. It then performs a bounds check - if (opacity < 1). However, this means that if both the opacity of the path and the brush are 1 the Alpha values on the state aren't set. The bounds check should be if (opacity <= 1).

These two changes make it work for me. Hope it sorts you out too.


Top
 Profile  
Reply with quote  
PostPosted: Thu Dec 01, 2011 11:33 am 
Offline

Joined: Thu Dec 01, 2011 11:28 am
Posts: 1
JEllenden wrote:
There appear to be two bugs in the code for the XPS to PDF converter when including images [...]


Thanks so much for your tips! I was having the same problem and I was going through the road of inspecting the source code, until I saw your answer. I wonder why those two changes are not yet implemented in the source code 9 months later...

Again, thanks a lot!


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 03, 2012 1:14 pm 
Offline

Joined: Fri Mar 30, 2012 1:06 pm
Posts: 3
Hi,

thanks for this great fix!!
Now it's working with images.


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 8 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:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group