PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Apr 24, 2024 6:04 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Sun Jun 17, 2012 11:54 pm 
Offline

Joined: Sun Jun 17, 2012 11:26 pm
Posts: 7
I am currently using PDFSharp v1.32 in combination with PDFSharp.XPS from PDFSharp v1.31 to do some conversion of XPS documents to PDF. Now I noticed that the images are converted correctly but that the right and bottom side of the images will display the first line and column of the image again. I looked at the code and see that image tiling is used but I cannot figure out how to fix this issue. Hope someone can tell me how to prevent this.

I also noticed that the saved PDF file size is growing rapidly when adding images. We used a HTML to PDF converter before and our reports were about 300KB. The same report created with PDFSharp is now about 2MB (but 400 KB without images). Is there some way to set the image compression?


Attachments:
File comment: The correct image
correct.png
correct.png [ 13.38 KiB | Viewed 13356 times ]
File comment: Incorrect. See right and bottom side of the image.
wrong.png
wrong.png [ 14.36 KiB | Viewed 13356 times ]
Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 18, 2012 11:32 am 
Offline

Joined: Sun Jun 17, 2012 11:26 pm
Posts: 7
I figured out how to fix the tiling for the ImageBrush. In TilingPatternBuilder.BuildPattern I incremented the xStep and yStep with 1. This will fix the problem in this case however I do not know if this is the correct fix or that I have to do this somewhere else. But it looks like it does the job. There is also a BuildPattern for VisualBrush objects that probably will have the same issue and can be fixed the same way.

The PDF size issue is still remaining so it would be great if someone can help me out with this.


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 18, 2012 12:42 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
Igor Markovic wrote:
The PDF size issue is still remaining so it would be great if someone can help me out with this.
Do not look at the filesize unless you use a Release build (files created with a Debug build will be much bigger).

PDFsharp does not alter images. JPEG images are added to PDF as they are, other images use a lossless compression in PDF.
Maybe your HTML to PDF tool did some optimizations with images (reduced resolution or lower JPEG quality).
With PDFsharp it's up to you to reduce/recompress images before adding them to PDF (automatic filesize optimization is on the wishlist, but not with high priority).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 18, 2012 12:49 pm 
Offline

Joined: Sun Jun 17, 2012 11:26 pm
Posts: 7
Yeah I just figured out that the release build will be smaller, however now I'm having a problem with converting the XPS document to PDF using the release build. It doensn't work out of the box. The PDF will only contain the top part of the first page. I'm still trying to figure out what is going wrong here.


Top
 Profile  
Reply with quote  
 Post subject: Graphics.DrawImage
PostPosted: Mon Jun 18, 2012 3:06 pm 
Offline

Joined: Sun Jun 17, 2012 11:26 pm
Posts: 7
It took me a while to figure it out, but I found the problem that caused the corrupted PDF file in release mode. In PdfContentWriter-Writer.cs the WriteIndent method is flagged with the Conditional attribute ([Conditional("DEBUG")]). I gues that some values are appended without a trailing \n. By removing the ConditionalAttribute from the indent it will work correctly. Maybe the ident can be just a space in release mode to reduce the file size?


Top
 Profile  
Reply with quote  
PostPosted: Sun Jun 24, 2012 3:29 pm 
Offline

Joined: Sun Jun 17, 2012 11:26 pm
Posts: 7
So I figured out that I am saving the XPS document incorrectly. The pagination of the FrameworkElement was done by just clipping the content so that the page looks correct. This however will result in that the each page will contain all content only the visible area will be different. I have currently no idea how to fix this but thats a problem for later.

However, what does happen is that PDFSharp will save all resources on a per page basis. So all images in the document will be saved for each page. So when I have a 3 page document where each page will contain 1 image. Then PDFSharp will save 9 images in total. So you can imagine that this can explode the PDF size when using a lot of images. I'm still trying to understand the code, but it also looks like this will happen for all other resources such as fonts.

As I'm still trying to understand all code, PDF file structure, etc. it takes a lot of time for me to fix this issue. Is there anyone who can help me with this? Is there a way to save the images on a per document basis instead of per page?


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 12, 2012 4:05 pm 
Offline

Joined: Wed Sep 12, 2012 3:52 pm
Posts: 1
As Igor, I'm also using a combination of PdfSharp 1.31 and 1.32 to do xps2pdf conversion. I'm also facing the first problem with image tiling that he described. The suggested solution (modifying xStep/yStep in the BuildPattern method) is really the way to go? I've tried that but, depending on the zoom aplied to the pdf document the repeated edges still appear. I tried incrementing xStep/yStep by more than 1 unit (5) and it gave me better results. However, the drawing process seems fairly complex and I don't know how the modification will affect it. Igor, your solution worked well for you? Have you managed to discover anything new?


Top
 Profile  
Reply with quote  
PostPosted: Fri Mar 29, 2013 1:39 pm 
Offline

Joined: Fri Mar 29, 2013 10:31 am
Posts: 1
Facing the same problem of image tiling dealing with png images.
The problem seems to be related to transparent pixels on the images borders.
I modified my images with an editor (gimp/"zealous crop") which suppress these transparent pixels and the tiling problem seems to be solved.
Nevertheless, this bypass works only with transparent pixels on the borders. If there are inside the image, the tiling is still noticeable.
I do the same process to produce my pdf creating first an xps. And comparing the xps (and the original png) and the resulting pdf, the problematic images seem more pixelized in the pdf so maybe there something to investigate in the way images are compressed in PDFSharp (LZ compression as i understand in another post).
Hope this helps and thanks for this well done project.


Top
 Profile  
Reply with quote  
PostPosted: Tue May 17, 2016 11:55 am 
Offline

Joined: Tue Jul 15, 2014 8:56 pm
Posts: 13
I saw the same issue too, but also the viewbox was not being interpreted correctly so any images that had been cropped in the xps were coming out incorrectly.. (try cropping an image in word, printing to xps and then converting)

my fix was to set the bounding box to the viewbox
and turn the matrix transform into the translate below.

Code:
// 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);

//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 but it works and I can't seem to fix it elsewhere
            matrix.TranslatePrepend((brush.Viewport.X - brush.Viewbox.X)+2, (brush.Viewport.Y - brush.Viewbox.Y)+2);



I think the issue is really coming from the xps output in windows itself, I noticed with my cropped image that it looks perfect in word or saved to other formats but when it goes through the xps print I can see the antialiased pixels from the left are cropped off. that's viewing it in the xps viewer.

I think therefore the issue is around the crop not taking into account the pixels added when anti-aliasing is applied.


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 01, 2016 4:19 pm 
Offline

Joined: Tue Jul 15, 2014 8:56 pm
Posts: 13
I tested more and found I hadn't fixed things with that.

I think I might have now, I've put a fork of the best fixed code I've found and added my fixes which feel a bit of a hack but seem to work.

https://github.com/ken-sands/PdfSharp.XPS


Top
 Profile  
Reply with quote  
PostPosted: Tue Jul 26, 2016 10:40 pm 
Offline

Joined: Tue Jul 26, 2016 10:37 pm
Posts: 1
I've been working on wonky cropping/placing of images for a bit, and here is the solution that seems to be working for me. I haven't had a chance yet to test it against the repo that Kensands linked, although that looks like a good spot to start. Thought I'd post here as this thread was really helpful in getting me pointed in the right direction:

Code:
...
PdfTilingPattern BuildPattern(ImageBrush brush, XMatrix transform)
        {
            // Bounding box lays always at (0,0)
            XRect bbox = new XRect(0, 0, brush.Viewport.Width, brush.Viewport.Height);
#if true
            XMatrix matrix = transform;

            //adjust pixel width dimensions from viewBox to match the units of the viewport
            var translateOffsetX = brush.Viewport.Width * brush.Viewbox.X / brush.Viewbox.Width * -1;
            var translateOffsetY = brush.Viewport.Height * brush.Viewbox.Y / brush.Viewbox.Height * -1;
            matrix.Prepend(new XMatrix(1, 0, 0, 1, brush.Viewport.X + translateOffsetX, brush.Viewport.Y + translateOffsetY));

            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));
            }
#else
      double c = 1;
      XMatrix matrix = new XMatrix(1 * c, 0, 0, 1 * c, brush.Viewport.X * c, brush.Viewport.Y * c); // HACK: 480
      XMatrix t = transform;
      //t.Invert();
      t.Prepend(matrix);
      //t.TranslateAppend(brush.Viewport.X , brush.Viewport.Y);
      //matrix.Append(t);
      matrix = t;
#endif
            double xStep = brush.Viewport.Width + 1;
            double yStep = brush.Viewport.Height + 1;

            // PdfTilingPattern
...



EDIT: on doing more testing this appears to work for portrait or landscape pictures that should be shrunk down to fit in a square viewport (letterboxed or pillarboxed) but there is still a problem when they should get clipped and fill the viewport (I'm converting from Xaml to XPS and then from XPS to PDF as described here: https://nathanpjones.com/wp/2013/03/output-to-pdf-in-wpf-for-free/, and if I use the Stretch="Uniform" property on my imagebrush it letterboxes as I would expect, but if I use Stretch="UniformToFill" it does not behave as I would expect). I'm investigating more hoping to find a fix that works in both cases.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 215 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