Home : PDF : Safely Change / Set Resolution of PdfDecoder
Q10433 - HOWTO: Safely Change / Set Resolution of PdfDecoder

When you add a PdfDecoder to your RegisteredDecoders.Decoders collection, you are modifying the static collection where all of your DotImage components look when they want to know how to render a given image type. If you simply add a default PdfDecoder like this:

Atalasoft.Imaging.Codec.RegisteredDecoders.Decoders.Add(new Atalasoft.Imaging.Codec.Pdf.PdfDecoder());

Global.Atalasoft.Imaging.Codec.RegisteredDecoders.Decoders.Add(new Global.Atalasoft.Imaging.Codec.Pdf.PdfDecoder())

There are two things you should be aware of:

1) The default resolution is that of your system - which is usually 96 DPI - this is generally far too low quality for printing and editing.

2) You're "blindly" adding a decoder to the collection without checking if there's already one there. If you add multiple PdfDecoder objects, the "extra" ones wont get picked up because the way our decoders work, it stops once it finds the first instance of a decoder that works.

So, what we're left with is that if you want to set the resolution when you add (issue 1) you can do it easily:

Atalasoft.Imaging.Codec.RegisteredDecoders.Decoders.Add(new Atalasoft.Imaging.Codec.Pdf.PdfDecoder() {Resolution = 200 } );

Global.Atalasoft.Imaging.Codec.RegisteredDecoders.Decoders.Add(new Global.Atalasoft.Imaging.Codec.Pdf.PdfDecoder() With { .Resolution = 200 } )

This is great, and it gets you a desired higher resolution (suggested values are 200 or 300... any higher and you significantly increase memory and/or file size without really gaining much in quality, any lower and the quality is generally noticeably poor) .. however, it doesn't address item 2

So, to properly, safely add a PdfDecoder (only if needed) or modify the already present PdfDecoder to use a new resolution, you can use this approach:


static readonly object pdfLock = new object();

private static void SafelySetPdfDecoderResolution(int newRes)
   lock (pdfLock)
foreach (ImageDecoder rawDecoder in RegisteredDecoders.Decoders)
if (rawDecoder is PdfDecoder)
((PdfDecoder)rawDecoder).Resolution = newRes;

RegisteredDecoders.Decoders.Add(new PdfDecoder() { Resolution = newRes });

NOTE: the return statement inside the loop properly exists the whole safelySetPdfDecoderResolution method after having updated the decoder.. it does not ever hit the final RegisteredDecoders.Add statement unless no PdfDecoder was found and modified.

This is the reason we suggest this method.. because it safely adds ONLY IF needed otherwise it modifies the existing decoder

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?


Tell us why you rated the content this way. (optional)
Approved Comments...
No user comments available for this article.

Powered By InstantKB.NET v1.3
Copyright © 2002, 2019. InstantASP Ltd. All Rights Reserved