public class CodecsRasterizeDocumentLoadOptions
LEADTOOLS provides support for loading a document as a raster image. Document formats such as PDF, XPS, DOCX/DOC, PPTX/PPT, XLSS/XLS, 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.GetInformation or RasterCodecs.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, DOCX/DOC, PPTX/PPT, XLSS/XLS, 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, TXT and documents support margins. |
Unit |
The units to use for the PageWidth, PageHeight, LeftMargin, TopMargin, RightMargin and BottomMargin values. |
XResolution, YResolution and Resolution |
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# Rasterize Document Demo that ships with your version of LEADTOOLS for an interactive demonstration of CodecsRasterizeDocumentLoadOptions.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing.Core;
using Leadtools.Pdf;
public void CodecsPdfOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "PDFSegmentation.pdf");
string destFileName1 = Path.Combine(LEAD_VARS.ImagesDir, "PdfOptions.pdf");
string destFileName2 = Path.Combine(LEAD_VARS.ImagesDir, "PdfOptions.bmp");
CodecsImageInfo info = codecs.GetInformation(srcFileName, true);
codecs.Options.Pdf.InitialPath = @"C:\MyApp\Bin";
info = codecs.GetInformation(srcFileName, true);
Debug.WriteLine("Information for: {0}", srcFileName);
Debug.WriteLine(string.Format("Document: {0}", info.Document)); // CodecsDocumentImageInfo reference
Debug.WriteLine(string.Format("Document: {0}", info.Document.IsDocumentFile));
Debug.WriteLine(string.Format("Document: {0}", info.Document.PageHeight));
Debug.WriteLine(string.Format("Document: {0}", info.Document.PageWidth));
Debug.WriteLine(string.Format("Document: {0}", info.Document.Unit));
Debug.WriteLine(string.Format("Document: {0}", info.IsGray8Alpha));
Debug.WriteLine(string.Format("Palette: {0}", info.GetPalette()));
// Check if the PDF engine is installed then get the load and save options of the PDF files.
if (codecs.Options.Pdf.IsEngineInstalled)
{
// Resulting image pixel depth.
// CodecsPdfOptions & CodecsPdfLoadOptions reference
codecs.Options.Pdf.Load.DisplayDepth = 24;
codecs.Options.Pdf.Load.GraphicsAlpha = 4;
codecs.Options.Pdf.Load.DisableCieColors = false;
codecs.Options.Pdf.Load.DisableCropping = false;
codecs.Options.Pdf.Load.EnableInterpolate = false;
codecs.Options.Pdf.Load.Password = "";
// Type of font anti-aliasing to use.
codecs.Options.Pdf.Load.TextAlpha = 1;
codecs.Options.Pdf.Load.UseLibFonts = true;
// Horizontal,vertical display resolution in dots per inch.
codecs.Options.RasterizeDocument.Load.XResolution = 150;
codecs.Options.RasterizeDocument.Load.YResolution = 150;
// CodecsRasterizeDocumentOptions & CodecsRasterizeDocumentLoadOptions reference
codecs.Options.RasterizeDocument.Load.BottomMargin = 0.1;
codecs.Options.RasterizeDocument.Load.LeftMargin = 0.1;
codecs.Options.RasterizeDocument.Load.PageHeight = 11;
codecs.Options.RasterizeDocument.Load.PageWidth = 8.5;
codecs.Options.RasterizeDocument.Load.Resolution = 150;
codecs.Options.RasterizeDocument.Load.RightMargin = 1.25;
codecs.Options.RasterizeDocument.Load.SizeMode = CodecsRasterizeDocumentSizeMode.None; // CodecsRasterizeDocumentSizeMode Enumeration reference
codecs.Options.RasterizeDocument.Load.TopMargin = 1.0;
codecs.Options.RasterizeDocument.Load.Unit = CodecsRasterizeDocumentUnit.Pixel; // CodecsRasterizeDocumentUnit Enumeration reference
using (RasterImage image = codecs.Load(srcFileName, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
{
// Set access rights for the user when he\she opens the file we create
// CodecsPdfSaveOptions reference
codecs.Options.Pdf.Save.AssembleDocument = true;
codecs.Options.Pdf.Save.ExtractText = true;
codecs.Options.Pdf.Save.ExtractTextGraphics = true;
codecs.Options.Pdf.Save.PrintDocument = false;
codecs.Options.Pdf.Save.FillForm = true;
codecs.Options.Pdf.Save.ModifyAnnotation = true;
codecs.Options.Pdf.Save.ModifyDocument = true;
codecs.Options.Pdf.Save.OwnerPassword = "LEAD Technologies";
codecs.Options.Pdf.Save.PrintFaithful = false;
codecs.Options.Pdf.Save.TextEncoding = CodecsPdfTextEncoding.Hex;
codecs.Options.Pdf.Save.Use128BitEncryption = true;
codecs.Options.Pdf.Save.UserPassword = "LEAD";
// Set the PDF version to be v1.4
codecs.Options.Pdf.Save.Version = CodecsRasterPdfVersion.V14;
// Save it as linearized (optimized for web view)
codecs.Options.Pdf.Save.Linearized = true;
codecs.Options.Pdf.Save.LowMemoryUsage = false;
codecs.Options.Pdf.Save.SavePdfA = false;
codecs.Options.Pdf.Save.SavePdfv13 = false;
codecs.Options.Pdf.Save.SavePdfv14 = false;
codecs.Options.Pdf.Save.SavePdfv15 = false;
codecs.Options.Pdf.Save.SavePdfv16 = false;
codecs.Options.Pdf.Save.SavePdfv17 = false;
codecs.Options.Pdf.Save.UseImageResolution = true;
// Save the image back as PDF
codecs.Save(image, destFileName1, RasterImageFormat.RasPdf, 24);
}
// And load it back before saving it as BMP
using (RasterImage image = codecs.Load(destFileName1))
{
codecs.Save(image, destFileName2, RasterImageFormat.Bmp, image.BitsPerPixel);
}
}
else
{
Debug.WriteLine("PDF Engine is not found!");
}
// Clean up
codecs.Dispose();
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";
}