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:

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

VB.NET
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:

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

VB.NET
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:

C#

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;
           
return;
         }
      }

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


 

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?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
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, 2017. InstantASP Ltd. All Rights Reserved