LEADTOOLS Image File Support (Leadtools.Codecs assembly)

CodecsRasterizeDocumentLoadOptions Class

Show in webframe
Example 







Members 
Contains set options for loading a document file as a raster image (rasterization).
Object Model
Syntax
public class CodecsRasterizeDocumentLoadOptions 
'Declaration
 
Public Class CodecsRasterizeDocumentLoadOptions 
'Usage
 
Dim instance As CodecsRasterizeDocumentLoadOptions
public sealed class CodecsRasterizeDocumentLoadOptions 
@interface LTCodecsRasterizeDocumentLoadOptions : NSObject
public class CodecsRasterizeDocumentLoadOptions
function Leadtools.Codecs.CodecsRasterizeDocumentLoadOptions()
public ref class CodecsRasterizeDocumentLoadOptions 
Remarks

LEADTOOLS provides support for loading a document as a raster image. Document formats such as PDF, XPS, DOC/DOCX(2007/2010), PPT/PPTX(2007/2010), XLS/XLSX(2007/2010), RTF and Text do not contain physical width, height or resolution information. It is up to the loader (in this case, the RasterCodecs object) to specify the transformation from logical coordinates to physical pixels through a process called rasterization.

Rasterization is the process through which a document is converted into a raster image. To check whether a certain file on disk (or in a .NET stream) contains a document file rather than a regular raster image, call the RasterCodecs.GetInformationRasterCodecs.GetInformationAsync method and check the CodecsDocumentImageInfo.IsDocumentFile property. Here is a code snippet:


             CodecsImageInfo imageInfo = rasterCodecsInstance.GetInformation(fileName, true);
             if(imageInfo.Document.IsDocumentFile)
             {
                // A document file (PDF, XPS, DOC/DOCX(2007/2010), PPT/PPTX(2007/2010), XLS/XLSX(2007/2010), etc), show the original document size:
                Console.WriteLine("Document file, original size is {0} by {1} {2}",
                   imageInfo.Document.PageWidth, imageInfo.Document.PageHeight, imageInfo.Document.Unit);
            
                // Your code specific to rasterization based on the original document size goes here
             }
             

To rasterize a document file, set the properties of the CodecsRasterizeDocumentLoadOptions to the required values. For example, the following code snippet will instruct LEADTOOLS to fit any document files into 8.5 by 11 inches paper size at 300 dpi resolution. Every document file loaded with the RasterCodecs.Load or RasterCodecs.LoadAsync methods will have width/height of no more than 2550 by 3300 pixels with a resolution of 300 by 300:


             CodecsRasterizeDocumentLoadOptions rasterDocumentLoadOptions = rasterCodecsInstance.Options.RasterizeDocument.Load;
             // Set the size mode, we want to fit
             rasterDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit;
             // Set the page size
             rasterDocumentLoadOptions.PageWidth = 8.5;
             rasterDocumentLoadOptions.PageHeight = 11;
             rasterDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch;
             // And the resolution
             rasterDocumentLoadOptions.XResolution = 300;
             rasterDocumentLoadOptions.YResolution = 300;
            
             // Load the image
             RasterImage image = rasterCodecsInstance.Load(fileName);
            
             // Show its pixel size, it should be less than or equal to
             // 2550 by 3300 pixels (8.5 * 300 by 11 * 300)
             Console.WriteLine("Loaded image size: {0} by {1} pixels at {2} by {3}",
             image.ImageWidth, image.ImageHeight, image.XResolution, image.YResolution);
             

The CodecsRasterizeDocumentLoadOptions class contains the following properties:

Property Description
PageWidth and PageHeight

The resulting page width and height (in Unit). The value of the resulting raster image width and height in pixels depends on the current resolution and size mode values.

LeftMargin, TopMargin, RightMargin and BottomMargin

The size of the margins to leave on the left, top, right and bottom margins using Unit as the unit of measure. Currently, only RTF and TXT documents support margins.

Unit

The units to use for the PageWidth, PageHeight, LeftMargin, TopMargin, RightMargin and BottomMargin values.

XResolution and YResolution

The resolution to use when rasterizing the document files. A value of 0 means to use the current screen resolution (usually 96). The resolution controls the pixel density of the resulting raster image. For example, if you specify 8.5 by 11 inches page width and height and a resolution of 96, the resulting image will have a pixel width and height of (8.5 * 96 = 816) and (11 * 96 = 1056) pixels. This is suitable for viewing at a 100 percent zoom but when you start zooming in, the image will get pixelated. Pixelation may also occur if you send the raster image to a printer, since printers usually have much higher resolution than a monitor.

If zooming in or high quality printing is a requirement in your code, then a higher resolution value must be specified (for example, 300 by 300). For an 8.5 by 11 inch document, this results in a raster image size of (8.5 * 300 = 2550) and (11 * 300 = 3300) pixels, which is more than suitable for printing or zooming in. Keep in mind that increasing the resolution will increase the memory used to hold the image data. Finding the right balance between pixel density and memory consumption depends upon your application needs.

SizeMode

Controls the transformation used when converting the logical size specified in page width and height to the physical raster image size. Refer to CodecsRasterizeDocumentSizeMode for more information.

Refer to the C# and VB Rasterize Document Demo that ships with your version of LEADTOOLS for an interactive demonstration of CodecsRasterizeDocumentLoadOptions.

Example
For an example, refer to CodecsPdfOptions.
Copy Code  
Imports Leadtools
Imports Leadtools.Codecs

Public Sub RasterizeDocumentExample()
   ' Initialize the RasterCodecs object to use
   Dim codecs As New RasterCodecs()
   ' Enable using the RasterizeDocumentOptions
   Dim rasterizeDocumentLoadOptions As CodecsRasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load

   ' No margins (only used with RTF and TXT files)
   rasterizeDocumentLoadOptions.LeftMargin = 0
   rasterizeDocumentLoadOptions.TopMargin = 0
   rasterizeDocumentLoadOptions.RightMargin = 0
   rasterizeDocumentLoadOptions.BottomMargin = 0

   ' The PDF file we are testing. This could be an XPS, RTF, TXT or XLS file as well
   Dim imageFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf")

   ' Show the file information
   Using imageInfo As CodecsImageInfo = codecs.GetInformation(imageFileName, True)
      Dim documentImageInfo As CodecsDocumentImageInfo = imageInfo.Document

      ' If this is a document file, show the document information
      If documentImageInfo.IsDocumentFile Then
         Console.WriteLine("Document file")
         Console.WriteLine("  Original size is: {0} by {1} {2}", _
            documentImageInfo.PageWidth, documentImageInfo.PageHeight, documentImageInfo.Unit)

         Console.WriteLine("  Using current rasterization, load size will be: {0} by {1} pixels at {2} by {3}", _
            imageInfo.Width, imageInfo.Height, imageInfo.XResolution, imageInfo.YResolution)
      Else
         ' Regular raster image, show the image information
         Console.WriteLine("Raster file")
         Console.WriteLine("  Original size is: {0} by {1} pixels at {2} by {3}", _
            imageInfo.Width, imageInfo.Height, imageInfo.XResolution, imageInfo.YResolution)
      End If
   End Using

   ' Example 1. Load at original document size at 300 DPI
   rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.None
   rasterizeDocumentLoadOptions.XResolution = 300
   rasterizeDocumentLoadOptions.YResolution = 300

   Using image As RasterImage = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)
      ShowResult(codecs, image)
   End Using

   ' Example 2. Fit the document at 640 by 480 pixels at 96 DPI keeping the aspect ratio
   rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit
   rasterizeDocumentLoadOptions.PageWidth = 640
   rasterizeDocumentLoadOptions.PageHeight = 480
   rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Pixel
   rasterizeDocumentLoadOptions.XResolution = 96
   rasterizeDocumentLoadOptions.YResolution = 96

   Using image As RasterImage = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)
      ShowResult(codecs, image)
   End Using

   ' Example 3. Fit the document at 8.5 by 11 inches at 96 DPI keeping the aspect ratio
   rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit
   rasterizeDocumentLoadOptions.PageWidth = 8.5
   rasterizeDocumentLoadOptions.PageHeight = 11
   rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch
   rasterizeDocumentLoadOptions.XResolution = 300
   rasterizeDocumentLoadOptions.YResolution = 300

   Using image As RasterImage = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)
      ShowResult(codecs, image)
   End Using

   ' Example 4. Stretch the image to always be 4 by 4 inches at 150 DPI. Aspect ratio may differ
   rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Stretch
   rasterizeDocumentLoadOptions.PageWidth = 4
   rasterizeDocumentLoadOptions.PageHeight = 4
   rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch
   rasterizeDocumentLoadOptions.XResolution = 150
   rasterizeDocumentLoadOptions.YResolution = 150

   Using image As RasterImage = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1)
      ShowResult(codecs, image)
   End Using

   codecs.Dispose()
End Sub

Private Shared Sub ShowResult(ByVal codecs As RasterCodecs, ByVal image As RasterImage)
   Dim rasterizeDocumentLoadOptions As CodecsRasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load

   Console.WriteLine("Showing result..")
   Console.WriteLine("  Current rasterization options:")
   Console.WriteLine("    Size mode is: {0}", rasterizeDocumentLoadOptions.SizeMode)
   Console.WriteLine("    Page size is {0} by {1} {2}", _
      rasterizeDocumentLoadOptions.PageWidth, rasterizeDocumentLoadOptions.PageHeight, rasterizeDocumentLoadOptions.Unit)
   Console.WriteLine("    Resolution is {0} by {1}", _
      rasterizeDocumentLoadOptions.XResolution, rasterizeDocumentLoadOptions.YResolution)

   Console.WriteLine("  Loaded raster image size is: {0} by {1} at {2} by {3}", _
      image.ImageWidth, image.ImageHeight, image.XResolution, image.YResolution)

   ' Code to verify our results

   ' Get the suggested page width and height in pixels
   Dim pageWidthInPixels As Double
   Dim pageHeightInPixels As Double

   Select Case rasterizeDocumentLoadOptions.Unit
      Case CodecsRasterizeDocumentUnit.Inch
         pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth * rasterizeDocumentLoadOptions.XResolution
         pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight * rasterizeDocumentLoadOptions.YResolution

      Case CodecsRasterizeDocumentUnit.Millimeter
         pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth * rasterizeDocumentLoadOptions.XResolution * 25.400000025908
         pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight * rasterizeDocumentLoadOptions.YResolution * 25.400000025908

      Case CodecsRasterizeDocumentUnit.Pixel
         pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth
         pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight
   End Select

   ' Verify
   Select Case rasterizeDocumentLoadOptions.SizeMode
      Case CodecsRasterizeDocumentSizeMode.Fit, CodecsRasterizeDocumentSizeMode.FitAlways
         ' The image width/height must not be greater than suggested page width/height
         Console.WriteLine("Image width/height must be less than or equal to page width/height")
         System.Diagnostics.Debug.Assert(image.ImageWidth <= pageWidthInPixels)
         System.Diagnostics.Debug.Assert(image.ImageHeight <= pageHeightInPixels)

      Case CodecsRasterizeDocumentSizeMode.FitWidth
         ' The image width must be equal to the suggested page width
         Console.WriteLine("Image width must be equal to page width")
         System.Diagnostics.Debug.Assert(image.ImageWidth = pageWidthInPixels)

      Case CodecsRasterizeDocumentSizeMode.Stretch
         ' The image width/height must be equal to the suggested page width/height
         Console.WriteLine("Image width/height must be equal to suggested page width/height")
         System.Diagnostics.Debug.Assert(image.ImageWidth = pageWidthInPixels)
         System.Diagnostics.Debug.Assert(image.ImageHeight = pageHeightInPixels)

      Case CodecsRasterizeDocumentSizeMode.None
         Console.WriteLine("Loading at original document page size")
         ' No checking
   End Select
End Sub

Public NotInheritable Class LEAD_VARS
Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
using Leadtools;
using Leadtools.Codecs;

public void RasterizeDocumentExample()
{
    // Initialize the RasterCodecs object to use
    RasterCodecs codecs = new RasterCodecs();
    // Enable using the RasterizeDocumentOptions
    CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;

    // No margins (only used with RTF and TXT files)
    rasterizeDocumentLoadOptions.LeftMargin = 0;
    rasterizeDocumentLoadOptions.TopMargin = 0;
    rasterizeDocumentLoadOptions.RightMargin = 0;
    rasterizeDocumentLoadOptions.BottomMargin = 0;

    // The PDF file we are testing. This could be an XPS, RTF, TXT or XLS file as well
    string imageFileName = Path.Combine(LEAD_VARS.ImagesDir, "Leadtools.pdf");

    // Show the file information
    using (CodecsImageInfo imageInfo = codecs.GetInformation(imageFileName, true))
    {
        CodecsDocumentImageInfo documentImageInfo = imageInfo.Document;

        // If this is a document file, show the document information
        if (documentImageInfo.IsDocumentFile)
        {
            Console.WriteLine("Document file");
            Console.WriteLine("  Original size is: {0} by {1} {2}",
               documentImageInfo.PageWidth, documentImageInfo.PageHeight, documentImageInfo.Unit);

            Console.WriteLine("  Using current rasterization, load size will be: {0} by {1} pixels at {2} by {3}",
               imageInfo.Width, imageInfo.Height, imageInfo.XResolution, imageInfo.YResolution);
        }
        else
        {
            // Regular raster image, show the image information
            Console.WriteLine("Raster file");
            Console.WriteLine("  Original size is: {0} by {1} pixels at {2} by {3}",
               imageInfo.Width, imageInfo.Height, imageInfo.XResolution, imageInfo.YResolution);
        }
    }

    // Example 1. Load at original document size at 300 DPI
    rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.None;
    rasterizeDocumentLoadOptions.XResolution = 300;
    rasterizeDocumentLoadOptions.YResolution = 300;

    using (RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
    {
        ShowResult(codecs, image);
    }

    // Example 2. Fit the document at 640 by 480 pixels at 96 DPI keeping the aspect ratio
    rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit;
    rasterizeDocumentLoadOptions.PageWidth = 640;
    rasterizeDocumentLoadOptions.PageHeight = 480;
    rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Pixel;
    rasterizeDocumentLoadOptions.XResolution = 96;
    rasterizeDocumentLoadOptions.YResolution = 96;

    using (RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
    {
        ShowResult(codecs, image);
    }

    // Example 3. Fit the document at 8.5 by 11 inches at 96 DPI keeping the aspect ratio
    rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit;
    rasterizeDocumentLoadOptions.PageWidth = 8.5;
    rasterizeDocumentLoadOptions.PageHeight = 11;
    rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch;
    rasterizeDocumentLoadOptions.XResolution = 300;
    rasterizeDocumentLoadOptions.YResolution = 300;

    using (RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
    {
        ShowResult(codecs, image);
    }

    // Example 4. Stretch the image to always be 4 by 4 inches at 150 DPI. Aspect ratio may differ
    rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Stretch;
    rasterizeDocumentLoadOptions.PageWidth = 4;
    rasterizeDocumentLoadOptions.PageHeight = 4;
    rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch;
    rasterizeDocumentLoadOptions.XResolution = 150;
    rasterizeDocumentLoadOptions.YResolution = 150;

    using (RasterImage image = codecs.Load(imageFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
    {
        ShowResult(codecs, image);
    }

    codecs.Dispose();
}

private static void ShowResult(RasterCodecs codecs, RasterImage image)
{
    CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;

    Console.WriteLine("Showing result..");
    Console.WriteLine("  Current rasterization options:");
    Console.WriteLine("    Size mode is: {0}", rasterizeDocumentLoadOptions.SizeMode);
    Console.WriteLine("    Page size is {0} by {1} {2}",
       rasterizeDocumentLoadOptions.PageWidth, rasterizeDocumentLoadOptions.PageHeight, rasterizeDocumentLoadOptions.Unit);
    Console.WriteLine("    Resolution is {0} by {1}",
       rasterizeDocumentLoadOptions.XResolution, rasterizeDocumentLoadOptions.YResolution);

    Console.WriteLine("  Loaded raster image size is: {0} by {1} at {2} by {3}",
       image.ImageWidth, image.ImageHeight, image.XResolution, image.YResolution);

    // Code to verify our results

    // Get the suggested page width and height in pixels
    double pageWidthInPixels;
    double pageHeightInPixels;

    switch (rasterizeDocumentLoadOptions.Unit)
    {
        case CodecsRasterizeDocumentUnit.Inch:
            pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth * rasterizeDocumentLoadOptions.XResolution;
            pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight * rasterizeDocumentLoadOptions.YResolution;
            break;

        case CodecsRasterizeDocumentUnit.Millimeter:
            pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth * rasterizeDocumentLoadOptions.XResolution * 25.400000025908000026426160026955;
            pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight * rasterizeDocumentLoadOptions.YResolution * 25.400000025908000026426160026955;
            break;

        case CodecsRasterizeDocumentUnit.Pixel:
        default:
            pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth;
            pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight;
            break;
    }

    // Verify
    switch (rasterizeDocumentLoadOptions.SizeMode)
    {
        case CodecsRasterizeDocumentSizeMode.Fit:
        case CodecsRasterizeDocumentSizeMode.FitAlways:
            // The image width/height must not be greater than suggested page width/height
            Console.WriteLine("Image width/height must be less than or equal to page width/height");
            System.Diagnostics.Debug.Assert(image.ImageWidth <= pageWidthInPixels);
            System.Diagnostics.Debug.Assert(image.ImageHeight <= pageHeightInPixels);
            break;

        case CodecsRasterizeDocumentSizeMode.FitWidth:
            // The image width must be equal to the suggested page width
            Console.WriteLine("Image width must be equal to page width");
            System.Diagnostics.Debug.Assert(image.ImageWidth == pageWidthInPixels);
            break;

        case CodecsRasterizeDocumentSizeMode.Stretch:
            // The image width/height must be equal to the suggested page width/height
            Console.WriteLine("Image width/height must be equal to suggested page width/height");
            System.Diagnostics.Debug.Assert(image.ImageWidth == pageWidthInPixels);
            System.Diagnostics.Debug.Assert(image.ImageHeight == pageHeightInPixels);
            break;

        case CodecsRasterizeDocumentSizeMode.None:
        default:
            Console.WriteLine("Loading at original document page size");
            // No checking
            break;
    }
}

static class LEAD_VARS
{
public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
using Leadtools;
using Leadtools.Codecs;

public async Task RasterizeDocumentExample()
{
   // Initialize the RasterCodecs object to use
   RasterCodecs codecs = new RasterCodecs();
   // Enable using the RasterizeDocumentOptions
   CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;

   // No margins (only used with RTF and TXT files)
   rasterizeDocumentLoadOptions.LeftMargin = 0;
   rasterizeDocumentLoadOptions.TopMargin = 0;
   rasterizeDocumentLoadOptions.RightMargin = 0;
   rasterizeDocumentLoadOptions.BottomMargin = 0;

   // The PDF file we are testing. This could be an XPS, RTF, TXT or XLS file as well
   string srcFileName = @"Assets\Leadtools.pdf";

   // Show the file information
   StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   using (CodecsImageInfo imageInfo = await codecs.GetInformationAsync(LeadStreamFactory.Create(loadFile), true, 1))
   {
      CodecsDocumentImageInfo documentImageInfo = imageInfo.Document;

      // If this is a document file, show the document information
      if (documentImageInfo.IsDocumentFile)
      {
         Debug.WriteLine("Document file");
         Debug.WriteLine("  Original size is: {0} by {1} {2}",
            documentImageInfo.PageWidth, documentImageInfo.PageHeight, documentImageInfo.Unit);

         Debug.WriteLine("  Using current rasterization, load size will be: {0} by {1} pixels at {2} by {3}",
            imageInfo.Width, imageInfo.Height, imageInfo.XResolution, imageInfo.YResolution);
      }
      else
      {
         // Regular raster image, show the image information
         Debug.WriteLine("Raster file");
         Debug.WriteLine("  Original size is: {0} by {1} pixels at {2} by {3}",
            imageInfo.Width, imageInfo.Height, imageInfo.XResolution, imageInfo.YResolution);
      }
   }

   // Example 1. Load at original document size at 300 DPI
   rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.None;
   rasterizeDocumentLoadOptions.XResolution = 300;
   rasterizeDocumentLoadOptions.YResolution = 300;

   loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   using (RasterImage srcImage = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 0, CodecsLoadByteOrder.Bgr, 1, 1))
   {
      ShowResult(codecs, srcImage);
   }

   // Example 2. Fit the document at 640 by 480 pixels at 96 DPI keeping the aspect ratio
   rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit;
   rasterizeDocumentLoadOptions.PageWidth = 640;
   rasterizeDocumentLoadOptions.PageHeight = 480;
   rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Pixel;
   rasterizeDocumentLoadOptions.XResolution = 96;
   rasterizeDocumentLoadOptions.YResolution = 96;

   loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   using (RasterImage srcImage = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 0, CodecsLoadByteOrder.Bgr, 1, 1))
   {
      ShowResult(codecs, srcImage);
   }

   // Example 3. Fit the document at 8.5 by 11 inches at 96 DPI keeping the aspect ratio
   rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Fit;
   rasterizeDocumentLoadOptions.PageWidth = 8.5;
   rasterizeDocumentLoadOptions.PageHeight = 11;
   rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch;
   rasterizeDocumentLoadOptions.XResolution = 300;
   rasterizeDocumentLoadOptions.YResolution = 300;

   loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   using (RasterImage srcImage = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 0, CodecsLoadByteOrder.Bgr, 1, 1))
   {
      ShowResult(codecs, srcImage);
   }

   // Example 4. Stretch the image to always be 4 by 4 inches at 150 DPI. Aspect ratio may differ
   rasterizeDocumentLoadOptions.SizeMode = CodecsRasterizeDocumentSizeMode.Stretch;
   rasterizeDocumentLoadOptions.PageWidth = 4;
   rasterizeDocumentLoadOptions.PageHeight = 4;
   rasterizeDocumentLoadOptions.Unit = CodecsRasterizeDocumentUnit.Inch;
   rasterizeDocumentLoadOptions.XResolution = 150;
   rasterizeDocumentLoadOptions.YResolution = 150;

   loadFile = await Tools.AppInstallFolder.GetFileAsync(srcFileName);
   using (RasterImage srcImage = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile), 0, CodecsLoadByteOrder.Bgr, 1, 1))
   {
      ShowResult(codecs, srcImage);
   }

   codecs.Dispose();
}

private static void ShowResult(RasterCodecs codecs, RasterImage image)
{
   CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;

   Debug.WriteLine("Showing result..");
   Debug.WriteLine("  Current rasterization options:");
   Debug.WriteLine("    Size mode is: {0}", rasterizeDocumentLoadOptions.SizeMode);
   Debug.WriteLine("    Page size is {0} by {1} {2}",
      rasterizeDocumentLoadOptions.PageWidth, rasterizeDocumentLoadOptions.PageHeight, rasterizeDocumentLoadOptions.Unit);
   Debug.WriteLine("    Resolution is {0} by {1}",
      rasterizeDocumentLoadOptions.XResolution, rasterizeDocumentLoadOptions.YResolution);

   Debug.WriteLine("  Loaded raster image size is: {0} by {1} at {2} by {3}",
      image.ImageWidth, image.ImageHeight, image.XResolution, image.YResolution);

   // Code to verify our results

   // Get the suggested page width and height in pixels
   double pageWidthInPixels;
   double pageHeightInPixels;

   switch (rasterizeDocumentLoadOptions.Unit)
   {
      case CodecsRasterizeDocumentUnit.Inch:
         pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth * rasterizeDocumentLoadOptions.XResolution;
         pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight * rasterizeDocumentLoadOptions.YResolution;
         break;

      case CodecsRasterizeDocumentUnit.Millimeter:
         pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth * rasterizeDocumentLoadOptions.XResolution * 25.400000025908000026426160026955;
         pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight * rasterizeDocumentLoadOptions.YResolution * 25.400000025908000026426160026955;
         break;

      case CodecsRasterizeDocumentUnit.Pixel:
      default:
         pageWidthInPixels = rasterizeDocumentLoadOptions.PageWidth;
         pageHeightInPixels = rasterizeDocumentLoadOptions.PageHeight;
         break;
   }

   // Verify
   switch (rasterizeDocumentLoadOptions.SizeMode)
   {
      case CodecsRasterizeDocumentSizeMode.Fit:
      case CodecsRasterizeDocumentSizeMode.FitAlways:
         // The image width/height must not be greater than suggested page width/height
         Debug.WriteLine("Image width/height must be less than or equal to page width/height");
         Assert.IsTrue(image.ImageWidth <= pageWidthInPixels);
         Assert.IsTrue(image.ImageHeight <= pageHeightInPixels);
         break;

      case CodecsRasterizeDocumentSizeMode.FitWidth:
         // The image width must be equal to the suggested page width
         Debug.WriteLine("Image width must be equal to page width");
         Assert.IsTrue(image.ImageWidth == pageWidthInPixels);
         break;

      case CodecsRasterizeDocumentSizeMode.Stretch:
         // The image width/height must be equal to the suggested page width/height
         Debug.WriteLine("Image width/height must be equal to suggested page width/height");
         Assert.IsTrue(image.ImageWidth == pageWidthInPixels);
         Assert.IsTrue(image.ImageHeight == pageHeightInPixels);
         break;

      case CodecsRasterizeDocumentSizeMode.None:
      default:
         Debug.WriteLine("Loading at original document page size");
         // No checking
         break;
   }
}
Requirements

Target Platforms

See Also

Reference

CodecsRasterizeDocumentLoadOptions Members
Leadtools.Codecs Namespace
Implementing PDF Plug in Features
CodecsDocumentImageInfo Class

 

 


Products | Support | Contact Us | Copyright Notices
© 2006-2014 All Rights Reserved. LEAD Technologies, Inc.