PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Mar 29, 2024 12:53 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
 Post subject: Optimize PDF file size
PostPosted: Tue Aug 07, 2018 11:14 am 
Offline

Joined: Mon Nov 06, 2017 3:53 pm
Posts: 18
Hello,

at first I want to thank you for the great library you provide.

Now I'm a bit in a tricky situation and I hope you can help.

I'm creating a PDF containing three tables. One in the header, one in the body and one in the footer.
This PDF can be rather long (70 DIN-A4 pages each page has about 40 rows in their table). Each page as a bookmark, so the user can jump to them easily. The fonts are not embedded in the pdf, I use the fonts coming from windows. The size of the file is about 10MB. Which is way to big.

I guess the cause of this is that each line of the main body table contains two pictures which I create in runtime. These are only Icons (36*36px) and pictures which have a maximum size of 200x50px. Resolution is 72 in horizontal and vertical.
I put the images in the chart by converting them into binary and add them to to cell.

Code:
string strRedRectangle = "base64:" + Convert.ToBase64String(byRedRectangle);
cell.AddImage(strRedRectangle).Width = "5pt";


Each picture is about 2kb.

How can optimize the size of the pdf file? Because I've read that the 'released' dll creates much smaller files than the 'debug' version of pdfsharp, I'm using the release version of pdfsharp.

I cannot make the icons smaller :(

Has someone a suggestion?
A third party console application which can be called after creating the pdf to make it smaller is also welcome.

Thank you!


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 07, 2018 11:39 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
First find out what contributes to the size of the PDF file.
With PDFsharp there will be embedded fonts in the PDF file. Using different fonts (or maybe a different build of PDFsharp) could reduce the file size.
Check if there are duplicated copies of your images in the PDF - anything else is just speculation.

Have you seen this post:
viewtopic.php?f=8&t=3748

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 07, 2018 12:10 pm 
Offline

Joined: Mon Nov 06, 2017 3:53 pm
Posts: 18
Thank you for your answer.

Yes I've seen the linked post and I'm running it in BestCompression and I'm using the release build of pdfsharp.

For finding the source of the file size I removed the images and the size dropped drastically to under one megabyte. So the images are causing this.

As said I create the bitmaps during runtime and add them when created to the cell in the table. I cannot get the size down of the images (36*36) and 200*50px because they will get unreadable.

Any idea?


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 07, 2018 12:37 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
Save the images to files - just one file per unique image.
Then reference the files in calls to "AddImage()". Only a single copy of each file should now be added to the final PDF.

It could be that the current implementation of the "BASE64" magic prevents this file size optimization and includes multiple copies of identical images.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Aug 08, 2018 12:51 pm 
Offline

Joined: Mon Nov 06, 2017 3:53 pm
Posts: 18
To give you a quick feedback.

It really seems that the "BASE64" magic prevents the file size optimization.

To solve this I came up with the following.

1) Create an empty list containing images and a name.
2) Check if an entry with an unique name exists.
2.1) If the name was not found. An image will be created, added to the list with its name and saved to a temporary directory.
2.2) If the name was found don't create a bitmap
3) Clone found/created bitmap and add it to the cell.

Now I'm down to about 1.5MB (former 10MB!) which is OK.

Sadly, I cannot do this with all bitmaps created during runtime because some do differ in details to each other and it would not make a difference if I save them and put insert them into the cell, or directly inserting them into the cell via "BASE64".

Thank you again!


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: No registered users and 154 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