using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing.Core;
using Leadtools.Pdf;
// This example shows how to load only visible PowerPoint slides
public void CodecsPowerPointOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.pptx");
string dstFileName = Path.Combine(LEAD_VARS.ImagesDir, "Out.png");
// Set CodecsPowerPointLoadOptions.ShowHiddenSlides to false to load only visible slides
codecs.Options.PowerPoint.Load.ShowHiddenSlides = false; // CodecsPowerPointOptions
// Load the first visible slide
using (RasterImage image = codecs.Load(srcFileName, 1))
{
codecs.Save(image, dstFileName, RasterImageFormat.Png, 0);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsPowerPointOptions-->
/// <!--CodecsPtokaOptions-->
public void CodecsPtokaOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "BusinessForm.ica");
// Setting new options.
// CodecsPtokaOptions & CodecsPtokaLoadOptions reference
codecs.Options.Ptoka.Load.Resolution = 500;
RasterImage image = codecs.Load(srcFileName);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsPtokaOptions-->
/// <!--CodecsRawOptions-->
private struct RawData
{
public int Width; // Width of image
public int Height; // Height of image
public int BitsPerPixel; // Bits per pixel of image--if palettized, a gray palette is generated
public RasterViewPerspective ViewPerspective; // View perspective of raw data (TopLeft, BottomLeft, etc)
public RasterByteOrder Order; // Rgb or Bgr
public int XResolution; // Horizontal resolution in DPI
public int YResolution; // Vertical resolution in DPI
public int Offset; // Offset into file where raw data begins
public bool Padding; // true if each line of data is padded to four bytes
public bool ReverseBits; // true if the bits of each byte are reversed
}
private RawData myRawData;
private void codecs_LoadInformation(object sender, CodecsLoadInformationEventArgs e)
{
// Set the information
e.Format = RasterImageFormat.Raw;
e.Width = myRawData.Width;
e.Height = myRawData.Height;
e.BitsPerPixel = myRawData.BitsPerPixel;
e.XResolution = myRawData.XResolution;
e.YResolution = myRawData.YResolution;
e.Offset = myRawData.Offset;
if (myRawData.Padding)
e.Pad4 = true;
e.Order = myRawData.Order;
if (myRawData.ReverseBits)
e.LeastSignificantBitFirst = true;
e.ViewPerspective = myRawData.ViewPerspective;
// If image is palettized create a grayscale palette
if (e.BitsPerPixel <= 8)
{
int colors = 1 << e.BitsPerPixel;
RasterColor[] palette = new RasterColor[colors];
for (int i = 0; i < palette.Length; i++)
{
byte val = (byte)((i * 255) / (colors - 1));
palette[i] = new RasterColor(val, val, val);
}
e.SetPalette(palette);
}
}
public void CodecsRawOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
// First, load a JPEG or CMP file
RasterImage image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp"));
// Save this image as RAW data
// Set RAW options
// CodecsRawOptions & CodecsRawSaveOptions reference
codecs.Options.Raw.Save.Pad4 = true;
codecs.Options.Raw.Save.ReverseBits = true;
codecs.Options.Save.OptimizedPalette = true;
string rawFileName = Path.Combine(LEAD_VARS.ImagesDir, "Test.raw");
codecs.Save(image, rawFileName, RasterImageFormat.Raw, 0);
// Save information about this image so we can use it to load the RAW file
myRawData = new RawData();
myRawData.Width = image.Width;
myRawData.Height = image.Height;
myRawData.BitsPerPixel = image.BitsPerPixel;
myRawData.ViewPerspective = image.ViewPerspective;
myRawData.Order = image.Order;
myRawData.XResolution = image.XResolution;
myRawData.YResolution = image.YResolution;
myRawData.Offset = 0;
myRawData.Padding = true;
myRawData.ReverseBits = true;
// We dont need the image anymore
image.Dispose();
// Now load this RAW image back
// First subscribe to the LoadInformation event so we can set the image information
codecs.LoadInformation += new EventHandler<CodecsLoadInformationEventArgs>(codecs_LoadInformation);
// Load the image
image = codecs.Load(rawFileName);
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "raw.bmp"), RasterImageFormat.Bmp, image.BitsPerPixel);
// Unsubscribe from the event
codecs.LoadInformation -= new EventHandler<CodecsLoadInformationEventArgs>(codecs_LoadInformation);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsRawOptions-->
/// <!--CodecsRtfOptions-->
public void CodecsRtfOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.rtf");
// Setting new options.
codecs.Options.RasterizeDocument.Load.LeftMargin = 1.00;
codecs.Options.RasterizeDocument.Load.TopMargin = 1.25;
codecs.Options.RasterizeDocument.Load.PageWidth = 8.50;
codecs.Options.RasterizeDocument.Load.PageHeight = 11.00;
codecs.Options.RasterizeDocument.Load.BottomMargin = 1.00;
codecs.Options.RasterizeDocument.Load.RightMargin = 1.25;
codecs.Options.Rtf.Load.BackColor = RasterColor.White; // CodecsRtfOptions & CodecsRtfLoadOptions reference
RasterImage image = codecs.Load(srcFileName);
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "rtf.bmp"), RasterImageFormat.Bmp, image.BitsPerPixel);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsRtfOptions-->
/// <!--CodecsTiffOptions-->
public void CodecsTiffOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "OCR1.TIF");
// Get all Information about the Tiff file that you want to load.
CodecsImageInfo imageInfo = codecs.GetInformation(srcFileName, true);
if (imageInfo.Tiff.IsImageFileDirectoryOffsetValid)
{
codecs.Options.Tiff.Load.ImageFileDirectoryOffset = imageInfo.Tiff.ImageFileDirectoryOffset;
codecs.Options.Tiff.Save.ImageFileDirectoryOffset = imageInfo.Tiff.ImageFileDirectoryOffset;
}
// CodecsTiffOptions & CodecsTiffLoadOptions reference
codecs.Options.Tiff.Load.UseImageFileDirectoryOffset = imageInfo.Tiff.IsImageFileDirectoryOffsetValid;
codecs.Options.Tiff.Save.UseImageFileDirectoryOffset = imageInfo.Tiff.IsImageFileDirectoryOffsetValid;
codecs.Options.Tiff.Load.IgnorePhotometricInterpretation = true;
codecs.Options.Tiff.Load.IgnoreViewPerspective = false;
codecs.Options.Tiff.Load.UseFastConversion = false;
RasterImage image = codecs.Load(srcFileName);
codecs.Options.Tiff.Save.NoPalette = imageInfo.Tiff.HasNoPalette;
codecs.Options.Tiff.Save.PreservePalette = imageInfo.Tiff.HasNoPalette;
// Set the tiffSave options
// CodecsTiffSaveOptions reference
codecs.Options.Tiff.Save.NoPageNumber = false;
codecs.Options.Tiff.Save.NoSubFileType = false;
codecs.Options.Tiff.Save.UsePhotometricInterpretation = true;
codecs.Options.Tiff.Save.PhotometricInterpretation = CodecsTiffPhotometricInterpretation.YcbCr;
codecs.Options.Tiff.Save.UseTileSize = true;
codecs.Options.Tiff.Save.TileHeight = 200;
codecs.Options.Tiff.Save.TileWidth = 200;
codecs.Options.Tiff.Save.NoLzwAutoClear = false;
codecs.Options.Tiff.Save.SavePlanar = false;
codecs.Options.Tiff.Save.UsePredictor = false;
// make the output file have the same format as the input, so Tiff->Tiff, BigTiff->BigTiff
codecs.Options.Tiff.Save.BigTiff = imageInfo.Tiff.IsBigTiff;
//saving the image
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "testtiff1.TIF"), RasterImageFormat.Tif, image.BitsPerPixel, 1, image.PageCount, 1, CodecsSavePageMode.Overwrite);
//change some tiffsave options.
codecs.Options.Tiff.Save.NoPageNumber = true;
codecs.Options.Tiff.Save.NoSubFileType = true;
codecs.Options.Tiff.Save.UsePhotometricInterpretation = false;
codecs.Options.Tiff.Save.UseTileSize = false;
codecs.Options.Tiff.Save.NoPalette = false;
codecs.Options.Tiff.Save.PreservePalette = true;
codecs.Options.Tiff.Save.PhotometricInterpretation = CodecsTiffPhotometricInterpretation.Palette;
//save the image with different name.
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "testtiff2.TIF"), RasterImageFormat.CcittGroup4, image.BitsPerPixel, 1, image.PageCount, 1, CodecsSavePageMode.Overwrite);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsTiffOptions-->
/// <!--CodecsTxtOptions-->
public void CodecsTxtOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.txt");
// Setting new options.
codecs.Options.RasterizeDocument.Load.LeftMargin = 1.00;
codecs.Options.RasterizeDocument.Load.TopMargin = 1.25;
codecs.Options.RasterizeDocument.Load.PageWidth = 8.50;
codecs.Options.RasterizeDocument.Load.PageHeight = 11.00;
codecs.Options.RasterizeDocument.Load.BottomMargin = 1.00;
codecs.Options.RasterizeDocument.Load.RightMargin = 1.25;
// CodecsTxtOptions & CodecsTxtLoadOptions reference
codecs.Options.Txt.Load.BackColor = RasterColor.White;
codecs.Options.Txt.Load.FaceName = "Courier New";
codecs.Options.Txt.Load.FontColor = new RasterColor(255, 0, 0);
codecs.Options.Txt.Load.Highlight = RasterColor.White;
codecs.Options.Txt.Load.FontSize = 12;
codecs.Options.Txt.Load.Italic = false;
codecs.Options.Txt.Load.Strikethrough = false;
codecs.Options.Txt.Load.Underline = false;
codecs.Options.Txt.Load.Bold = true;
codecs.Options.Txt.Load.Enabled = true;
codecs.Options.Txt.Load.TabSize = 8;
RasterImage image = codecs.Load(srcFileName);
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "txt.bmp"), RasterImageFormat.Bmp, image.BitsPerPixel);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsTxtOptions-->
/// <!--CodecsWmfOptions-->
public void CodecsWmfOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
// Setting new options.
// CodecsWmfOptions & CodecsWmfLoadOptions reference
codecs.Options.Wmf.Load.XResolution = 500;
codecs.Options.Wmf.Load.YResolution = 500;
codecs.Options.Wmf.Load.Resolution = 0;
RasterImage image = codecs.Load(srcFileName);
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "WmfImage.wmf"), RasterImageFormat.Wmf, image.BitsPerPixel);
image = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "WmfImage.wmf"));
codecs.Save(image, Path.Combine(LEAD_VARS.ImagesDir, "wmf.bmp"), RasterImageFormat.Bmp, image.BitsPerPixel);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsWmfOptions-->
/// <!--CodecsXpsOptions-->
public void CodecsXpsOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.xps");
string pngDestFileName = Path.Combine(LEAD_VARS.ImagesDir, "PngCompressed.xps");
string jpegDestFileName = Path.Combine(LEAD_VARS.ImagesDir, "JpegCompressed.xps");
// Set the resolution for loading XPS files to 300 by 300 DPI (Dots per inch)
// CodecsXpsOptions & CodecsXpsLoadOptions reference
codecs.Options.RasterizeDocument.Load.XResolution = 300;
codecs.Options.RasterizeDocument.Load.YResolution = 300;
// Load the source file
RasterImage image = codecs.Load(srcFileName);
// Save this file as XPS with PNG compression and quality factor of 9 (highest compression)
// CodecsXpsSaveOptions reference
codecs.Options.Xps.Save.PngQualityFactor = 9;
codecs.Save(image, pngDestFileName, RasterImageFormat.Xps, 24);
// Save this file as XPS with JPEG 422 compression and quality factor of 255 (highest compression)
codecs.Options.Xps.Save.JpegQualityFactor = 255;
codecs.Save(image, jpegDestFileName, RasterImageFormat.Xps, 24);
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsXpsOptions-->
/// <!--CodecsXlsOptions-->
public void CodecsXlsOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.Xls");
// Enable using the RasterizeDocumentOptions
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;
// 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;
// Load each sheet in a separate page
// CodecsXlsOptions & CodecsXlsLoadOptions reference
codecs.Options.Xls.Load.MultiPageSheet = true;
codecs.Options.Xls.Load.MultiPageEnableMargins = false;
codecs.Options.Xls.Load.MultiPageUseSheetWidth = false;
codecs.Options.Xls.Load.PageOrderDownThenOver = false;
codecs.Options.Xls.Load.ShowHiddenSheet = false;
// Load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Show the image information
Debug.WriteLine("Image has {0} pages", image.PageCount);
Debug.WriteLine("Image size: {0} by {1} pixels at {2} by {3} DPI",
image.ImageWidth, image.ImageHeight, image.XResolution, image.YResolution);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsXlsOptions-->
/// <!--CodecsExcelOptions-->
public void CodecsExcelOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.Xls");
// Enable using the RasterizeDocumentOptions
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;
// 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;
// Load each sheet in a separate page - CodecsExcelOptions & CodecsExcelLoadOptions reference
codecs.Options.Excel.Load.MultiPageSheet = false;
codecs.Options.Excel.Load.MultiPageEnableMargins = false;
codecs.Options.Excel.Load.MultiPageUseSheetWidth = false;
codecs.Options.Excel.Load.PageOrderDownThenOver = false;
codecs.Options.Excel.Load.ShowHiddenSheets = false; // ShowHiddenSheet reference
// Load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Show the image information
Debug.WriteLine("Image has {0} pages", image.PageCount);
Debug.WriteLine("Image size: {0} by {1} pixels at {2} by {3} DPI",
image.ImageWidth, image.ImageHeight, image.XResolution, image.YResolution);
//To save as BMP, uncomment out this line:
//codecs.Save(image, srcFileName + ".bmp", RasterImageFormat.Bmp, 24);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsExcelOptions-->
/// <!--CodecsRasterizeDocumentLoadOptions-->
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)
{
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;
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;
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");
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
Debug.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
Debug.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:
Debug.WriteLine("Loading at original document page size");
// No checking
break;
}
}
/// <!--CodecsRasterizeDocumentLoadOptions-->
/// <!--CodecsDocLoadOptions-->
public void CodecsDocLoadOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "test.doc");
// Enable using the RasterizeDocumentOptions
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.Options.RasterizeDocument.Load;
// 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;
// Load each page at 1-bits/pixel
codecs.Options.Doc.Load.BitsPerPixel = 1; // CodecsDocLoadOptions & CodecsDocOptions references
// Load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Show the image information
Debug.WriteLine("Image has {0} pages", image.PageCount);
Debug.WriteLine("Image size: {0} by {1} pixels at {2} by {3} DPI",
image.ImageWidth, image.ImageHeight, image.XResolution, image.YResolution);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsDocLoadOptions-->
/// <!--CodecsAnzLoadOptions-->
public void CodecsAnzLoadOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.Anz");
string dstFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_AnzCoronal.bmp");
// Set the load options to use
// "view is from front" ANZ viewing options
codecs.Options.Anz.Load.View = CodecsAnzView.Coronal; // CodecsAnzOptions & CodecsAnzLoadOptions reference
// Load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Save the image as BMP
codecs.Save(image, dstFileName, RasterImageFormat.Bmp, 24);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsAnzLoadOptions-->
/// <!--CodecsVffLoadOptions-->
public void CodecsVffLoadOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.vff");
string dstFileName1 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_Vff_UpToDown.bmp");
string dstFileName2 = Path.Combine(LEAD_VARS.ImagesDir, "Image1_Vff_LeftToRight.bmp");
// Set the load options to use up to down along the X-Axis
// CodecsVffOptions & CodecsVffLoadOptions reference
codecs.Options.Vff.Load.View = CodecsVffView.UpToDown;
// Load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Save the image as BMP
codecs.Save(image, dstFileName1, RasterImageFormat.Bmp, 24);
}
// Now, set the load options to use left to right along the Y-Axis
codecs.Options.Vff.Load.View = CodecsVffView.LeftToRight;
// Re-load the source file
using (RasterImage image = codecs.Load(srcFileName))
{
// Save the image as BMP
codecs.Save(image, dstFileName2, RasterImageFormat.Bmp, 24);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsVffLoadOptions-->
/// <!--CodecsRtfOptions.LoadMetafile-->
[DllImport("Gdi32")]
private static extern int DeleteEnhMetaFile(IntPtr hemf);
public void LoadRtfAsMetafileExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.rtf");
// Load the source file as a metafile
IntPtr hemf = codecs.Options.Rtf.LoadMetafile(srcFileName, 1);
if (hemf != IntPtr.Zero)
{
// Use the metafile here
// Finally, delete it using the Windows API
DeleteEnhMetaFile(hemf);
}
// Clean up
codecs.Dispose();
}
/// <!--CodecsRtfOptions.LoadMetafile-->
/// <!--CodecsSaveOptions.RetrieveDataFromImage-->
private int myMode;
public void RetrieveDataFromImageExample()
{
using (RasterCodecs codecs = new RasterCodecs())
{
string srcFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1.cmp");
string blankFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Blank.bmp");
string defaultFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Default.bmp");
string invertedFile = Path.Combine(LEAD_VARS.ImagesDir, "Sample1_Inverted.bmp");
// Load the source image
using (RasterImage image = codecs.Load(srcFile))
{
// Subscribe to the SaveImage event
codecs.SaveImage += new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage);
// First, set RetrieveDataFromImage to false and save
// This should generate a blank image
myMode = 0;
codecs.Options.Save.RetrieveDataFromImage = false;
codecs.Save(image, blankFile, RasterImageFormat.Bmp, 24);
// Next, set RetrieveDataFromImage to true but do not
// change the data, this should generate the correct image and is the equivalant
// to calling Save without subscribing to SaveImage
myMode = 1;
codecs.Options.Save.RetrieveDataFromImage = true;
codecs.Save(image, defaultFile, RasterImageFormat.Bmp, 24);
// Finally, leave RetrieveDataFromImage as true but change the data by inverting
// the scanlines, this should generate an inverted image
myMode = 2;
codecs.Save(image, invertedFile, RasterImageFormat.Bmp, 24);
codecs.SaveImage -= new EventHandler<CodecsSaveImageEventArgs>(codecs_SaveImage);
}
}
}
private void codecs_SaveImage(object sender, CodecsSaveImageEventArgs e)
{
// This example works with 24 bpp images only
Debug.Assert(e.Image.BitsPerPixel == 24);
switch (myMode)
{
case 0:
// Do not do anything
break;
case 1:
// Do not do anything
break;
case 2:
// Invert the image data
{
int scanlineLength = e.Image.BytesPerLine;
byte[] scanline = new byte[scanlineLength];
// Loop through all the scanlines in the data
for (int y = 0; y < e.Lines; y++)
{
// Get this row
e.Buffer.GetData(y * scanlineLength, scanline, 0, scanlineLength);
// Process it by inverting every pixel data
for (int x = 0; x < scanlineLength; x++)
{
scanline[x] = (byte)(255 - scanline[x]);
}
// Copy it back to the event buffer
e.Buffer.SetData(y * scanlineLength, scanline, 0, scanlineLength);
}
}
break;
}
}
/// <!--CodecsSaveOptions.RetrieveDataFromImage-->
/// <!--CodecsVectorLoadOptions-->
public void CodecsVectorLoadOptionsExample()
{
RasterCodecs codecs = new RasterCodecs();
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "random.dxf");
//Check if it is a vector file
CodecsImageInfo info = codecs.GetInformation(srcFileName, false);
Debug.WriteLine($"Is {srcFileName} a vector file? : {info.Vector.IsVectorFile}");
Debug.WriteLine($"Units: {info.Vector.Unit}");
//Parallelogram data
CodecsVectorImageInfo codecsVectorImageInfo = info.Vector;
Debug.WriteLine($"Parallelogram Max X: {codecsVectorImageInfo.ParallelogramMaxX}");
Debug.WriteLine($"Parallelogram Max Y: {codecsVectorImageInfo.ParallelogramMaxY}");
Debug.WriteLine($"Parallelogram Max Z: {codecsVectorImageInfo.ParallelogramMaxZ}");
Debug.WriteLine($"Parallelogram Min X: {codecsVectorImageInfo.ParallelogramMinX}");
Debug.WriteLine($"Parallelogram Min Y: {codecsVectorImageInfo.ParallelogramMinY}");
Debug.WriteLine($"Parallelogram Min Z: {codecsVectorImageInfo.ParallelogramMinZ}");
// Set the vector load options
// CodecsVectorOptions & CodecsVectorLoadOptions reference
codecs.Options.Vector.Load.BackgroundColor = new RasterColor(255, 255, 255);
codecs.Options.Vector.Load.BitsPerPixel = 24;
codecs.Options.Vector.Load.ForceBackgroundColor = true;
codecs.Options.Vector.Load.ViewWidth = 800;
codecs.Options.Vector.Load.ViewHeight = 800;
codecs.Options.Vector.Load.ViewMode = CodecsVectorViewMode.UseBest;
// Load the image
RasterImage image = codecs.Load(srcFileName);
// do something with the image here
// Clean up
image.Dispose();
codecs.Dispose();
}
/// <!--CodecsVectorLoadOptions-->
/// <!--CodecsPstOptions-->
public void CodecsPstOptionsExample()
{
using (RasterCodecs codecs = new RasterCodecs())
{
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "file.pst");
int messageCount = 0;
// Get number of messages in the PST files
using (CodecsImageInfo imageInfo = codecs.GetInformation(srcFileName, true))
{
messageCount = imageInfo.Pst.MessageCount;
}
// Load all messages as pure text
for (int messageNumber = 1; messageNumber <= messageCount; messageNumber++)
{
// Load message number 10 as text only
// CodecsPstOptions & CodecsPstLoadOptions reference
codecs.Options.Pst.Load.MessageNumber = messageNumber;
codecs.Options.Pst.Load.PlainText = true;
// Load the image
using (RasterImage image = codecs.Load(srcFileName))
{
// do something with the image here
}
}
}
}
/// <!--CodecsPstOptions-->
/// <!--RasterImageTypeConverter-->
public void RasterImageTypeConverterExample()
{
string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif");
// Construct the source URL to use the source file and load the image
Uri uri = new Uri(srcFileName);
RasterCodecs codecs = new RasterCodecs();
// Create a new instance of the RasterImageTypeConverter class
RasterImageTypeConverter rasterImageConverter = new RasterImageTypeConverter(codecs);
// We should be able to convert from a URL
Debug.Assert(rasterImageConverter.CanConvertFrom(uri.GetType()));
// Convert the image
// The return value from RasterImageTypeConverter.ConvertFrom might be an image that is
// still loading. So, we must subscribe to the RasterCodecs.LoadAsyncCompleteted
// event to obtain the finished image
codecs.LoadAsyncCompleted += new EventHandler<CodecsLoadAsyncCompletedEventArgs>(rasterImageTypeConverterExample_LoadAsyncCompleted);
object rasterImageObject = rasterImageConverter.ConvertFrom(uri);
// Notice that the returned rasterImageObject is a RasterImage with IsLoading set to true at this point
// The IsLoading will be false (and hence, the object will be usable) when the LoadAsyncCompleteted
// fires.
}
private void rasterImageTypeConverterExample_LoadAsyncCompleted(object sender, CodecsLoadAsyncCompletedEventArgs e)
{
// Check if the user canceled or if we have errors
if (e.Cancelled)
{
Debug.WriteLine("Canceled by the user");
}
else if (e.Error != null)
{
Debug.WriteLine("Error: " + e.Error.Message);
}
else
{
// Everything is OK, get the image
RasterImage image = e.Image;
Debug.WriteLine("Image at {0} loaded OK, size: {1} by {2}", e.Uri, image.Width, image.Height);
image.Dispose();
}
// Unsubscribe to the event and dispose the RasterCodecs object
RasterCodecs codecs = sender as RasterCodecs;
codecs.LoadAsyncCompleted -= new EventHandler<CodecsLoadAsyncCompletedEventArgs>(rasterImageTypeConverterExample_LoadAsyncCompleted);
codecs.Dispose();
}
/// <!--RasterImageTypeConverter-->
/// <!--CodecsHtmlOptions-->
public void CodecsHtmlOptions_Example()
{
using (RasterCodecs codecs = new RasterCodecs())
{
// Use the default HTML rendering engine - CodecsHtmlOptions & CodecsHtmlLoadOptions reference
codecs.Options.Html.Load.HtmlEngine = CodecsHtmlEngine.Auto;
// Set the HTML options to only allow loading resources from the leadtools.com domain:
codecs.Options.Html.Load.DomainWhitelist = "leadtools.com";
// Disable all JavaScript embedded in the HTML file
codecs.Options.Html.Load.EnableJS = false;
// Load the image
using (RasterImage image = codecs.Load("file.html", 1))
{
// Do something with image
}
}
}
/// <!--CodecsHtmlOptions-->
/// <!--CodecsCsvOptions-->
public void CodecsCsvOptions_Example()
{
string csvFileName = Path.Combine(LEAD_VARS.ImagesDir, "sample.csv");
string pngFileName = Path.Combine(LEAD_VARS.ImagesDir, "sample-csv.png");
using (RasterCodecs codecs = new RasterCodecs())
{
// Change some CSV options
CodecsCsvLoadOptions csvOptions = codecs.Options.Csv.Load; // CodecsCsvOptions reference
csvOptions.HeaderFontItalic = true;
csvOptions.BodyFontName = "Arial";
csvOptions.TableBorderColor = RasterColor.FromKnownColor(RasterKnownColor.Red);
// Load the first page from a CSV file
using (RasterImage image = codecs.Load(csvFileName, 1))
{
// Save it as PNG
codecs.Save(image, pngFileName, RasterImageFormat.Png, 0);
}
}
}
/// <!--CodecsCsvOptions-->
/// <!--CodecsPdfLoadOptions.EnhanceThinLines-->
public void CodecsEnhanceThinLinesOption_Example()
{
/* The following example shows you how to disable the thin line enhancement property */
RasterCodecs codecs = new RasterCodecs();
codecs.Options.Pdf.Load.EnhanceThinLines = false;
string inputFile = @"MyFile.PDF";
RasterImage rawImage = codecs.Load(inputFile, 24, CodecsLoadByteOrder.RgbOrGray, 1, -1);
}
/// <!--CodecsPdfLoadOptions.EnhanceThinLines-->
/// <!--CodecsTxtLoadOptions.DefaultEncoding-->
// This C# example shows you how to load a file called "Ansi.txt" that contains ANSI text.
public void CodecsTxtLoadOptionsDefaultEncoding_Example()
{
using (RasterCodecs codecs = new RasterCodecs())
{
string srcFile = @"Ansi.txt";
using (CodecsImageInfo info = codecs.GetInformation(srcFile, false))
{
if (!info.HasBom)
codecs.Options.Txt.Load.DefaultEncoding = CodecsTxtEncoding.Ansi; /* Load as Ansi. Here you can bring up a message box asking the user to select the encoding */
codecs.Options.Load.AllPages = true;
using (RasterImage image = codecs.Load(srcFile))
{
codecs.Save(image, srcFile + ".tif", RasterImageFormat.TifLzw, 0);
}
}
}
}
/// <!--CodecsTxtLoadOptions.DefaultEncoding-->
/// <!--CodecsLoadOptions.Decrypt-->
public void CodecsLoadOptionsDecrypt_Example()
{
/* This sample loads Encrypted.docx, which is encrypted with the password 'MyPassword' */
using (RasterCodecs codecs = new RasterCodecs())
{
string srcFile = @"Encrypted.docx";
codecs.Options.Load.Decrypt.Password = "MyPassword";
codecs.Options.Load.AllPages = true;
using (RasterImage image = codecs.Load(srcFile))
{
codecs.Save(image, srcFile + ".tif", RasterImageFormat.TifLzw, 0);
}
}
}
/// <!--CodecsLoadOptions.Decrypt-->
///<!--CodecsHeicOptions-->
public void CodecsHeicOptions_Example()
{
RasterCodecs codecs = new RasterCodecs();
string fileName = "C:\temp\test.heic";
using (RasterImage image = codecs.Load(fileName))
{
/* Save a HEIC file using qFactor of 30 and no stamp */
string outFile30 = @"C:\temp\out30-NoStamp.heic";
codecs.Options.Heic.Load.Multithreaded = false; // CodecsHeicLoadOptions reference
codecs.Options.Heic.Save.QualityFactor = 30; // CodecsHeicOptions & CodecsHeicSaveOptions reference
codecs.Save(image, outFile30, RasterImageFormat.Heic, 0);
/* Save a HEIC file using qFactor of 40 and a stamp */
string outFile40 = @"C:\temp\out40-Stamp.heic";
codecs.Options.Heic.Save.QualityFactor = 40;
codecs.Options.Heic.Save.SaveWithStamp = true;
codecs.Options.Heic.Save.StampWidth = 320;
codecs.Options.Heic.Save.StampHeight = codecs.Options.Heic.Save.StampWidth * image.Height / image.Width;
codecs.Options.Heic.Save.StampBitsPerPixel = 24;
codecs.Save(image, outFile40, RasterImageFormat.Heic, 0);
}
// Clean up
codecs.Dispose();
}
///<!--CodecsHeicOptions-->
///<!--CodecsPowerPointOptions-->
public static void CodecsPowerPointOptions_Example()
{
RasterCodecs codecs = new RasterCodecs();
string filename = @"C:\temp\test.ppt";
string savefilename = @"C:\temp\test-save.ppt";
/* Specify PowerPointLoadOptions */
CodecsPowerPointLoadOptions loadOptions = codecs.Options.PowerPoint.Load;
loadOptions.ShowHiddenSlides = false;
/* Load and Save PowerPoint */
using (RasterImage image = codecs.Load(filename, 1))
{
codecs.Save(image, savefilename, RasterImageFormat.Ppt, 24);
}
// Clean up
codecs.Dispose();
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";
}
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import org.junit.*;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import static org.junit.Assert.*;
import leadtools.*;
import leadtools.codecs.*;
import leadtools.imageprocessing.core.MinMaxBitsCommand;
// This example shows how to load only visible PowerPoint slides
public void codecsPowerPointOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "test.pptx");
String dstFileName = combine(LEAD_VARS_IMAGES_DIR, "out.png");
// Set CodecsPowerPointLoadOptions.ShowHiddenSlides to false to load only
// visible slides
codecs.getOptions().getPowerPoint().getLoad().setShowHiddenSlides(false); // CodecsPowerPointOptions
// Load the first visible slide
RasterImage image = codecs.load(srcFileName, 1);
codecs.save(image, dstFileName, RasterImageFormat.PNG, 0);
assertTrue("File unsuccessfully saved to " + dstFileName, (new File(dstFileName)).exists());
System.out.printf("File successfully saved to %s%n", dstFileName);
image = null;
// Clean up
codecs.dispose();
}
// <!--CodecsPowerPointOptions-->
// <!--CodecsPtokaOptions-->
public void codecsPtokaOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "barcode1.tif");
// Setting new options.
// CodecsPtokaOptions & CodecsPtokaLoadOptions reference
codecs.getOptions().getPtoka().getLoad().setResolution(500);
RasterImage image = codecs.load(srcFileName);
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "Result.jpg");
codecs.save(image, outputFileName, RasterImageFormat.JPEG, 0);
assertTrue("File unsuccessfully saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
// Clean up
image.dispose();
codecs.dispose();
}
// <!--CodecsPtokaOptions-->
// <!--CodecsRawOptions-->
private class RawData {
public int Width; // Width of image
public int Height; // Height of image
public int BitsPerPixel; // Bits per pixel of image--if palettized, a gray palette is generated
public RasterViewPerspective ViewPerspective; // View perspective of raw data (TopLeft, BottomLeft, etc)
public RasterByteOrder Order; // Rgb or Bgr
public int XResolution; // Horizontal resolution in DPI
public int YResolution; // Vertical resolution in DPI
public int Offset; // Offset into file where raw data begins
public boolean Padding; // true if each line of data is padded to four bytes
public boolean ReverseBits; // true if the bits of each byte are reversed
}
private RawData myRawData;
CodecsLoadInformationListener infoListener = new CodecsLoadInformationListener() {
@Override
public void onLoadInformation(CodecsLoadInformationEvent e) {
// Set the information
e.setFormat(RasterImageFormat.RAW);
e.setWidth(myRawData.Width);
e.setHeight(myRawData.Height);
e.setBitsPerPixel(myRawData.BitsPerPixel);
e.setXResolution(myRawData.XResolution);
e.setYResolution(myRawData.YResolution);
e.setOffset(myRawData.Offset);
if (myRawData.Padding)
e.setPad4(true);
e.setOrder(myRawData.Order);
if (myRawData.ReverseBits)
e.setLeastSignificantBitFirst(true);
e.setViewPerspective(myRawData.ViewPerspective);
// If image is palettized create a grayscale palette
if (e.getBitsPerPixel() <= 8) {
int colors = 1 << e.getBitsPerPixel();
RasterColor[] palette = new RasterColor[colors];
for (int i = 0; i < palette.length; i++) {
byte val = (byte) ((i * 255) / (colors - 1));
palette[i] = new RasterColor(val, val, val);
}
e.setPalette(palette);
}
}
};
public void codecsRawOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
// First, load a JPEG or CMP file
RasterImage image = codecs.load(combine(LEAD_VARS_IMAGES_DIR, "barcode1.tif"));
// Save this image as RAW data
// Set RAW options
// CodecsRawOptions & CodecsRawSaveOptions reference
codecs.getOptions().getRaw().getSave().setPad4(true);
codecs.getOptions().getRaw().getSave().setReverseBits(true);
codecs.getOptions().getSave().setOptimizedPalette(true);
String rawFileName = combine(LEAD_VARS_IMAGES_DIR, "Test.raw");
codecs.save(image, rawFileName, RasterImageFormat.RAW, 0);
// Save information about this image so we can use it to load the RAW file
myRawData = new RawData();
myRawData.Width = image.getWidth();
myRawData.Height = image.getHeight();
myRawData.BitsPerPixel = image.getBitsPerPixel();
myRawData.ViewPerspective = image.getViewPerspective();
myRawData.Order = image.getOrder();
myRawData.XResolution = image.getXResolution();
myRawData.YResolution = image.getYResolution();
myRawData.Offset = 0;
myRawData.Padding = true;
myRawData.ReverseBits = true;
// We dont need the image anymore
image.dispose();
// Now load this RAW image back
// First subscribe to the LoadInformation event so we can set the image
// information
codecs.addLoadInformationListener(infoListener);
// Load the image
image = codecs.load(rawFileName);
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "raw.bmp");
codecs.save(image, outputFileName, RasterImageFormat.BMP, image.getBitsPerPixel());
assertTrue("File unsuccessfully saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
// Unsubscribe from the event
codecs.removeLoadInformationListener(infoListener);
// Clean up
image.dispose();
codecs.dispose();
}
// <!--CodecsRawOptions-->
// <!--CodecsRtfOptions-->
public void codecsRtfOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Examples\\Forms\\DotNet\\MasterFormsEditor\\src\\Resources";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "Info.rtf");
// Setting new options.
codecs.getOptions().getRasterizeDocument().getLoad().setLeftMargin(1.00);
codecs.getOptions().getRasterizeDocument().getLoad().setTopMargin(1.25);
codecs.getOptions().getRasterizeDocument().getLoad().setPageWidth(8.50);
codecs.getOptions().getRasterizeDocument().getLoad().setPageHeight(1.00);
codecs.getOptions().getRasterizeDocument().getLoad().setBottomMargin(1.00);
codecs.getOptions().getRasterizeDocument().getLoad().setRightMargin(1.25);
codecs.getOptions().getRtf().getLoad().setBackColor(RasterColor.WHITE); // CodecsRtfOptions & CodecsRtfLoadOptions
// reference
RasterImage image = codecs.load(srcFileName);
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "rtf.bmp");
codecs.save(image, outputFileName, RasterImageFormat.BMP, image.getBitsPerPixel());
assertTrue("File unsuccessfully saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
// Clean up
image.dispose();
codecs.dispose();
}
// <!--CodecsRtfOptions-->
// <!--CodecsTiffOptions-->
public void codecsTiffOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "ocr1.tif");
// Get all Information about the Tiff file that you want to load.
CodecsImageInfo imageInfo = codecs.getInformation(srcFileName, true);
if (imageInfo.getTiff().isImageFileDirectoryOffsetValid()) {
codecs.getOptions().getTiff().getLoad()
.setImageFileDirectoryOffset(imageInfo.getTiff().getImageFileDirectoryOffset());
codecs.getOptions().getTiff().getSave()
.setImageFileDirectoryOffset(imageInfo.getTiff().getImageFileDirectoryOffset());
}
// CodecsTiffOptions & CodecsTiffLoadOptions reference
codecs.getOptions().getTiff().getLoad()
.setUseImageFileDirectoryOffset(imageInfo.getTiff().isImageFileDirectoryOffsetValid());
codecs.getOptions().getTiff().getSave()
.setUseImageFileDirectoryOffset(imageInfo.getTiff().isImageFileDirectoryOffsetValid());
codecs.getOptions().getTiff().getLoad().setIgnorePhotometricInterpretation(true);
codecs.getOptions().getTiff().getLoad().setIgnoreViewPerspective(false);
codecs.getOptions().getTiff().getLoad().setUseFastConversion(false);
RasterImage image = codecs.load(srcFileName);
codecs.getOptions().getTiff().getSave().setNoPalette(imageInfo.getTiff().hasNoPalette());
codecs.getOptions().getTiff().getSave().setPreservePalette(imageInfo.getTiff().hasNoPalette());
// Set the tiffSave options
// CodecsTiffSaveOptions reference
codecs.getOptions().getTiff().getSave().setNoPageNumber(false);
codecs.getOptions().getTiff().getSave().setNoSubFileType(false);
codecs.getOptions().getTiff().getSave().setUsePhotometricInterpretation(true);
codecs.getOptions().getTiff().getSave().setPhotometricInterpretation(CodecsTiffPhotometricInterpretation.YCBCR);
codecs.getOptions().getTiff().getSave().setUseTileSize(true);
codecs.getOptions().getTiff().getSave().setTileHeight(200);
codecs.getOptions().getTiff().getSave().setTileWidth(200);
codecs.getOptions().getTiff().getSave().setNoLzwAutoClear(false);
codecs.getOptions().getTiff().getSave().setSavePlanar(false);
codecs.getOptions().getTiff().getSave().setUsePredictor(false);
// make the output file have the same format as the input, so Tiff->Tiff,
// BigTiff->BigTiff
codecs.getOptions().getTiff().getSave().setBigTiff(imageInfo.getTiff().isBigTiff());
// saving the image
codecs.save(image, combine(LEAD_VARS_IMAGES_DIR, "testtiff1.TIF"), RasterImageFormat.TIF, image.getBitsPerPixel(),
1, image.getPageCount(), 1, CodecsSavePageMode.OVERWRITE);
// change some tiffsave options.
codecs.getOptions().getTiff().getSave().setNoPageNumber(true);
codecs.getOptions().getTiff().getSave().setNoSubFileType(true);
codecs.getOptions().getTiff().getSave().setUsePhotometricInterpretation(false);
codecs.getOptions().getTiff().getSave().setUseTileSize(false);
codecs.getOptions().getTiff().getSave().setNoPalette(false);
codecs.getOptions().getTiff().getSave().setPreservePalette(true);
codecs.getOptions().getTiff().getSave().setPhotometricInterpretation(CodecsTiffPhotometricInterpretation.PALETTE);
// save the image with different name.
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "testtiff2.TIF");
codecs.save(image, outputFileName, RasterImageFormat.CCITT_GROUP4, image.getBitsPerPixel(), 1,
image.getPageCount(), 1, CodecsSavePageMode.OVERWRITE);
assertTrue("File unsuccessfully saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
// Clean up
image.dispose();
codecs.dispose();
}
// <!--CodecsTiffOptions-->
// <!--CodecsTxtOptions-->
public void codecsTxtOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "Example.txt");
// Setting new options.
codecs.getOptions().getRasterizeDocument().getLoad().setLeftMargin(1.00);
codecs.getOptions().getRasterizeDocument().getLoad().setTopMargin(1.25);
codecs.getOptions().getRasterizeDocument().getLoad().setPageWidth(8.50);
codecs.getOptions().getRasterizeDocument().getLoad().setPageHeight(1.00);
codecs.getOptions().getRasterizeDocument().getLoad().setBottomMargin(1.00);
codecs.getOptions().getRasterizeDocument().getLoad().setRightMargin(1.25);
// CodecsTxtOptions & CodecsTxtLoadOptions reference
codecs.getOptions().getTxt().getLoad().setBackColor(RasterColor.WHITE);
codecs.getOptions().getTxt().getLoad().setFaceName("Courier New");
codecs.getOptions().getTxt().getLoad().setFontColor(new RasterColor(255, 0, 0));
codecs.getOptions().getTxt().getLoad().setHighlight(RasterColor.WHITE);
codecs.getOptions().getTxt().getLoad().setFontSize(12);
codecs.getOptions().getTxt().getLoad().setItalic(false);
codecs.getOptions().getTxt().getLoad().setStrikethrough(false);
codecs.getOptions().getTxt().getLoad().setUnderline(false);
codecs.getOptions().getTxt().getLoad().setBold(true);
codecs.getOptions().getTxt().getLoad().setEnabled(true);
RasterImage image = codecs.load(srcFileName);
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "txt.bmp");
codecs.save(image, outputFileName, RasterImageFormat.BMP, image.getBitsPerPixel());
assertTrue("File unsuccessfully saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
// Clean up
image.dispose();
codecs.dispose();
}
// <!--CodecsTxtOptions-->
// <!--CodecsWmfOptions-->
public void codecsWmfOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "barcode1.tif");
// Setting new options.
// CodecsWmfOptions & CodecsWmfLoadOptions reference
codecs.getOptions().getWmf().getLoad().setXResolution(500);
codecs.getOptions().getWmf().getLoad().setYResolution(500);
codecs.getOptions().getWmf().getLoad().setResolution(0);
RasterImage image = codecs.load(srcFileName);
codecs.save(image, combine(LEAD_VARS_IMAGES_DIR, "WmfImage.wmf"), RasterImageFormat.WMF, 0);
image = codecs.load(combine(LEAD_VARS_IMAGES_DIR, "WmfImage.wmf"));
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "wmf.bmp");
codecs.save(image, outputFileName, RasterImageFormat.BMP, image.getBitsPerPixel());
assertTrue("File unsuccessfully saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
// Clean up
image.dispose();
codecs.dispose();
}
// <!--CodecsWmfOptions-->
// <!--CodecsXpsOptions-->
public void codecsXpsOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "Leadtools.xps");
String pngDestFileName = combine(LEAD_VARS_IMAGES_DIR, "PngCompressed.xps");
String jpegDestFileName = combine(LEAD_VARS_IMAGES_DIR, "JpegCompressed.xps");
// Set the resolution for loading XPS files to 300 by 300 DPI (Dots per inch)
// CodecsXpsOptions & CodecsXpsLoadOptions reference
codecs.getOptions().getRasterizeDocument().getLoad().setXResolution(300);
codecs.getOptions().getRasterizeDocument().getLoad().setYResolution(300);
// Load the source file
RasterImage image = codecs.load(srcFileName);
// Save this file as XPS with PNG compression and quality factor of 9 (highest compression)
// CodecsXpsSaveOptions reference
codecs.getOptions().getXps().getSave().setPngQualityFactor(9);
codecs.save(image, pngDestFileName, RasterImageFormat.XPS, 24);
assertTrue("File unsuccessfully saved to " + pngDestFileName, (new File(pngDestFileName)).exists());
System.out.printf("File successfully saved to %s%n", pngDestFileName);
// Save this file as XPS with JPEG 422 compression and quality factor of 255
// (highest compression)
codecs.getOptions().getXps().getSave().setJpegQualityFactor(255);
codecs.save(image, jpegDestFileName, RasterImageFormat.XPS, 24);
assertTrue("File unsuccessfully saved to " + jpegDestFileName, (new File(jpegDestFileName)).exists());
System.out.printf("File successfully saved to %s%n", jpegDestFileName);
// Clean up
image.dispose();
codecs.dispose();
}
// <!--CodecsXpsOptions-->
// <!--CodecsXlsOptions-->
public void codecsXlsOptionsExample(){
RasterCodecs codecs = new RasterCodecs();
String LEAD_VARS_IMAGES_DIR ="C:\\LEADTOOLS23\\Resources\\Images";
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "large_sheet_5k.xls");
// Enable using the RasterizeDocumentOptions
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions =
codecs.getOptions().getRasterizeDocument().getLoad();
// Fit the document at 8.5 by 11 inches at 96 DPI keeping the aspect ratio
rasterizeDocumentLoadOptions.setSizeMode(CodecsRasterizeDocumentSizeMode.FIT);
rasterizeDocumentLoadOptions.setPageWidth(8.5);
rasterizeDocumentLoadOptions.setPageHeight(11);
rasterizeDocumentLoadOptions.setUnit(CodecsRasterizeDocumentUnit.INCH);
rasterizeDocumentLoadOptions.setXResolution(300);
rasterizeDocumentLoadOptions.setYResolution(300);
// Load each sheet in a separate page
codecs.getOptions().getExcel().getLoad().setMultiPageSheet(true);
codecs.getOptions().getExcel().getLoad().setMultiPageEnableMargins(false);
codecs.getOptions().getExcel().getLoad().setMultiPageUseSheetWidth(false);
codecs.getOptions().getExcel().getLoad().setPageOrderDownThenOver(false);
codecs.getOptions().getExcel().getLoad().setShowHiddenSheets(false);
// Load the source file
RasterImage image = codecs.load(srcFileName);
// Show the image information
System.out.println("Image has " + image.getPageCount() + " pages");
System.out.printf("Image size: %1d by %2d pixels at %3d by %4d DPI%n",
image.getImageWidth(), image.getImageHeight(), image.getXResolution(),
image.getYResolution());
image = null;
// Clean up
codecs.dispose();
}
// <!--CodecsXlsOptions-->
// <!--CodecsExcelOptions-->
public void codecsExcelOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "barcode1.tif");
// Enable using the RasterizeDocumentOptions
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.getOptions().getRasterizeDocument()
.getLoad();
// Fit the document at 8.5 by 11 inches at 96 DPI keeping the aspect ratio
rasterizeDocumentLoadOptions.setSizeMode(CodecsRasterizeDocumentSizeMode.FIT);
rasterizeDocumentLoadOptions.setPageWidth(8.5);
rasterizeDocumentLoadOptions.setPageHeight(11);
rasterizeDocumentLoadOptions.setUnit(CodecsRasterizeDocumentUnit.INCH);
rasterizeDocumentLoadOptions.setXResolution(300);
rasterizeDocumentLoadOptions.setYResolution(300);
// Load each sheet in a separate page - CodecsExcelOptions &
// CodecsExcelLoadOptions reference
codecs.getOptions().getExcel().getLoad().setMultiPageSheet(false);
codecs.getOptions().getExcel().getLoad().setMultiPageEnableMargins(false);
codecs.getOptions().getExcel().getLoad().setMultiPageUseSheetWidth(false);
codecs.getOptions().getExcel().getLoad().setPageOrderDownThenOver(false);
codecs.getOptions().getExcel().getLoad().setShowHiddenSheets(false); // ShowHiddenSheet reference
// Load the source file
RasterImage image = codecs.load(srcFileName);
// Show the image information
System.out.printf("Image has %d pages%n", image.getPageCount());
System.out.printf("Image size: %d by %2d pixels at %3d by %4d DPI",
image.getImageWidth(), image.getImageHeight(), image.getXResolution(), image.getYResolution());
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "Result.jpg");
codecs.save(image, outputFileName, RasterImageFormat.JPEG, 0);
assertTrue("File unsuccessfully saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
// To save as BMP, uncomment out this line:
// codecs.save(image, srcFileName + ".bmp", RasterImageFormat.BMP, 24);
// Clean up
image.dispose();
codecs.dispose();
}
// <!--CodecsExcelOptions-->
// <!--CodecsRasterizeDocumentLoadOptions-->
public void codecsRasterizeDocumentExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
// Initialize the RasterCodecs object to use
RasterCodecs codecs = new RasterCodecs();
// Enable using the RasterizeDocumentOptions
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.getOptions().getRasterizeDocument()
.getLoad();
// No margins (only used with RTF and TXT files)
rasterizeDocumentLoadOptions.setLeftMargin(0);
rasterizeDocumentLoadOptions.setTopMargin(0);
rasterizeDocumentLoadOptions.setRightMargin(0);
rasterizeDocumentLoadOptions.setBottomMargin(0);
// The PDF file we are testing. This could be an XPS, RTF, TXT or XLS file as
// well
String imageFileName = combine(LEAD_VARS_IMAGES_DIR, "Leadtools.pdf");
// Show the file information
CodecsImageInfo imageInfo = codecs.getInformation(imageFileName, true);
CodecsDocumentImageInfo documentImageInfo = imageInfo.getDocument();
// If this is a document file, show the document information
if (documentImageInfo.isDocumentFile()) {
System.out.println("Document file");
System.out.println(" Original size is: " + documentImageInfo.getPageWidth() + " by "
+ documentImageInfo.getPageHeight() + " " + documentImageInfo.getUnit());
System.out.printf(" Using current rasterization, load size will be: %1d by %2d pixels at %3d by %4d",
imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getXResolution(), imageInfo.getYResolution());
} else {
// Regular raster image, show the image information
System.out.println("Raster file");
System.out.printf(" Original size is: %1d by %2d pixels at %3d by %4d",
imageInfo.getWidth(), imageInfo.getHeight(), imageInfo.getXResolution(), imageInfo.getYResolution());
}
image = null;
// Example 1. Load at original document size at 300 DPI
rasterizeDocumentLoadOptions.setSizeMode(CodecsRasterizeDocumentSizeMode.NONE);
rasterizeDocumentLoadOptions.setXResolution(300);
rasterizeDocumentLoadOptions.setYResolution(300);
RasterImage image = codecs.load(imageFileName, 0, CodecsLoadByteOrder.BGR_OR_GRAY, 1, 1);
showResult(codecs, image);
image = null;
// Example 2. Fit the document at 640 by 480 pixels at 96 DPI keeping the aspect
// ratio
rasterizeDocumentLoadOptions.setSizeMode(CodecsRasterizeDocumentSizeMode.FIT);
rasterizeDocumentLoadOptions.setPageWidth(640);
rasterizeDocumentLoadOptions.setPageHeight(480);
rasterizeDocumentLoadOptions.setUnit(CodecsRasterizeDocumentUnit.PIXEL);
rasterizeDocumentLoadOptions.setXResolution(96);
rasterizeDocumentLoadOptions.setYResolution(96);
image = codecs.load(imageFileName, 0, CodecsLoadByteOrder.BGR_OR_GRAY, 1, 1);
showResult(codecs, image);
// image = null;
// Example 3. Fit the document at 8.5 by 11 inches at 96 DPI keeping the aspect
// ratio
rasterizeDocumentLoadOptions.setSizeMode(CodecsRasterizeDocumentSizeMode.FIT);
rasterizeDocumentLoadOptions.setPageWidth(8.5);
rasterizeDocumentLoadOptions.setPageHeight(11);
rasterizeDocumentLoadOptions.setUnit(CodecsRasterizeDocumentUnit.INCH);
rasterizeDocumentLoadOptions.setXResolution(300);
rasterizeDocumentLoadOptions.setYResolution(300);
image = codecs.load(imageFileName, 0, CodecsLoadByteOrder.BGR_OR_GRAY, 1, 1);
showResult(codecs, image);
image = null;
// Example 4. Stretch the image to always be 4 by 4 inches at 150 DPI. Aspect
// ratio may differ
rasterizeDocumentLoadOptions.setSizeMode(CodecsRasterizeDocumentSizeMode.STRETCH);
rasterizeDocumentLoadOptions.setPageWidth(4);
rasterizeDocumentLoadOptions.setPageHeight(4);
rasterizeDocumentLoadOptions.setUnit(CodecsRasterizeDocumentUnit.INCH);
rasterizeDocumentLoadOptions.setXResolution(150);
rasterizeDocumentLoadOptions.setYResolution(150);
image = codecs.load(imageFileName, 0, CodecsLoadByteOrder.BGR_OR_GRAY, 1, 1);
showResult(codecs, image);
image = null;
codecs.dispose();
}
private static void showResult(RasterCodecs codecs, RasterImage image) {
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.getOptions().getRasterizeDocument()
.getLoad();
System.out.println("Showing result..");
System.out.println(" Current rasterization options:");
System.out.printf(" Size mode is: " + rasterizeDocumentLoadOptions.getSizeMode());
System.out.printf(" Page size is %f by %f " + rasterizeDocumentLoadOptions.getUnit().toString(),
rasterizeDocumentLoadOptions.getPageWidth(), rasterizeDocumentLoadOptions.getPageHeight());
System.out.printf(" Resolution is %1d by %2d",
rasterizeDocumentLoadOptions.getXResolution(), rasterizeDocumentLoadOptions.getYResolution());
System.out.printf(" Loaded raster image size is: %1d by %2d at %3d by %4d",
image.getImageWidth(), image.getImageHeight(), image.getXResolution(), image.getYResolution());
// Code to verify our results
// Get the suggested page width and height in pixels
double pageWidthInPixels;
double pageHeightInPixels;
switch (rasterizeDocumentLoadOptions.getUnit()) {
case INCH:
System.out.println("inch");
pageWidthInPixels = rasterizeDocumentLoadOptions.getPageWidth()
* rasterizeDocumentLoadOptions.getXResolution();
pageHeightInPixels = rasterizeDocumentLoadOptions.getPageHeight()
* rasterizeDocumentLoadOptions.getYResolution();
break;
case MILLIMETER:
System.out.println("mm");
pageWidthInPixels = rasterizeDocumentLoadOptions.getPageWidth()
* rasterizeDocumentLoadOptions.getXResolution() * 25.400000025908000026426160026955;
pageHeightInPixels = rasterizeDocumentLoadOptions.getPageHeight()
* rasterizeDocumentLoadOptions.getYResolution() * 25.400000025908000026426160026955;
break;
case PIXEL:
default:
System.out.println("px");
pageWidthInPixels = rasterizeDocumentLoadOptions.getPageWidth();
pageHeightInPixels = rasterizeDocumentLoadOptions.getPageHeight();
break;
}
// Verify
switch (rasterizeDocumentLoadOptions.getSizeMode()) {
case FIT:
case FIT_ALWAYS:
// The image width/height must not be greater than suggested page width/height
System.out.println("Image width/height must be less than or equal to page width/height");
assertTrue("Vertical screen size is incorrect", image.getImageWidth() <= pageWidthInPixels);
assertTrue("Horozontal screen size is incorrect", image.getImageHeight() <= pageHeightInPixels);
break;
case FIT_WIDTH:
// The image width must be equal to the suggested page width
System.out.println("Image width must be equal to page width");
assertEquals(image.getImageWidth(), pageWidthInPixels, 0.001);
break;
case STRETCH:
// The image width/height must be equal to the suggested page width/height
System.out.println("Image width/height must be equal to suggested page width/height");
assertEquals(image.getImageWidth(), pageWidthInPixels, 0.001);
assertEquals(image.getImageHeight(), pageHeightInPixels, 0.001);
break;
case NONE:
default:
System.out.println("Loading at original document page size");
// No checking
break;
}
}
// <!--CodecsRasterizeDocumentLoadOptions-->
// <!--CodecsDocLoadOptions-->
public void codecsDocLoadOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "Example.doc");
// Enable using the RasterizeDocumentOptions
CodecsRasterizeDocumentLoadOptions rasterizeDocumentLoadOptions = codecs.getOptions().getRasterizeDocument().getLoad();
// Fit the document at 8.5 by 11 inches at 96 DPI keeping the aspect ratio
rasterizeDocumentLoadOptions.setSizeMode(CodecsRasterizeDocumentSizeMode.FIT);
rasterizeDocumentLoadOptions.setPageWidth(8.5);
rasterizeDocumentLoadOptions.setPageHeight(11);
rasterizeDocumentLoadOptions.setUnit(CodecsRasterizeDocumentUnit.INCH);
rasterizeDocumentLoadOptions.setXResolution(300);
rasterizeDocumentLoadOptions.setYResolution(300);
// Load each page at 1-bits/pixel
codecs.getOptions().getDoc().getLoad().setBitsPerPixel(1); // CodecsDocLoadOptions & CodecsDocOptions references
// Load the source file
RasterImage image = codecs.load(srcFileName);
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "Result.jpg");
codecs.save(image, outputFileName, RasterImageFormat.JPEG, 0);
assertTrue("File unsuccessfully saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
// Show the image information
System.out.printf("Image has %1d pages", image.getPageCount());
System.out.printf("Image size: %1d by %2d pixels at %3d by %4d DPI",
image.getImageWidth(), image.getImageHeight(), image.getXResolution(), image.getYResolution());
image = null;
// Clean up
codecs.dispose();
}
// <!--CodecsDocLoadOptions-->
// <!--CodecsVffLoadOptions-->
public void CodecsVffLoadOptionsExample(){
final String LEAD_VARS_IMAGES_DIR ="C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "aortas_vehicle_crop.vff");
String dstFileName1 = combine(LEAD_VARS_IMAGES_DIR,
"Image1_Vff_UpToDown.bmp");
String dstFileName2 = combine(LEAD_VARS_IMAGES_DIR,
"Image1_Vff_LeftToRight.bmp");
// Set the load options to use up to down along the X-Axis
// CodecsVffOptions & CodecsVffLoadOptions reference
codecs.getOptions().getVff().getLoad().setView(CodecsVffView.VFF_VIEW_UPTODOWN);
// Load the source file
RasterImage image = codecs.load(srcFileName);
// Save the image as BMP
codecs.save(image, dstFileName1, RasterImageFormat.BMP, 24);
assertTrue("File unsuccessfully saved to " + dstFileName1, (new
File(dstFileName1)).exists());
System.out.printf("File successfully saved to %s%n", dstFileName1);
image = null;
// Now, set the load options to use left to right along the Y-Axis
codecs.getOptions().getVff().getLoad().setView(CodecsVffView.VFF_VIEW_LEFTTORIGHT);
// Re-load the source file
image = codecs.load(srcFileName);
// Save the image as BMP
codecs.save(image, dstFileName2, RasterImageFormat.BMP, 24);
assertTrue("File unsuccessfully saved to " + dstFileName2, (new
File(dstFileName2)).exists());
System.out.printf("File successfully saved to %s%n", dstFileName2);
// Clean up
image.dispose();;
codecs.dispose();
}
// <!--CodecsVffLoadOptions-->
// <!--CodecsSaveOptions.RetrieveDataFromImage-->
private int myMode;
public void codecsRetrieveDataFromImageExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFile = combine(LEAD_VARS_IMAGES_DIR, "Sample1.cmp");
String blankFile = combine(LEAD_VARS_IMAGES_DIR, "Sample1_Blank.bmp");
String defaultFile = combine(LEAD_VARS_IMAGES_DIR, "Sample1_Default.bmp");
String invertedFile = combine(LEAD_VARS_IMAGES_DIR, "Sample1_Inverted.bmp");
// Load the source image
RasterImage image = codecs.load(srcFile);
// Subscribe to the SaveImage event
codecs.addSaveImageListener(codecsSaveImage);
// First, set RetrieveDataFromImage to false and save
// This should generate a blank image
myMode = 0;
codecs.getOptions().getSave().setRetrieveDataFromImage(false);
codecs.save(image, blankFile, RasterImageFormat.BMP, 24);
// Next, set RetrieveDataFromImage to true but do not
// change the data, this should generate the correct image and is the equivalant
// to calling Save without subscribing to SaveImage
myMode = 1;
codecs.getOptions().getSave().setRetrieveDataFromImage(true);
codecs.save(image, defaultFile, RasterImageFormat.BMP, 24);
assertTrue("File saved to " + defaultFile, (new File(defaultFile)).exists());
System.out.printf("File successfully saved to %s%n", defaultFile);
// Finally, leave RetrieveDataFromImage as true but change the data by inverting
// the scanlines, this should generate an inverted image
myMode = 2;
codecs.save(image, invertedFile, RasterImageFormat.BMP, 24);
assertTrue("File unsuccessfully saved to " + invertedFile, (new File(invertedFile)).exists());
System.out.printf("File successfully saved to %s%n", invertedFile);
codecs.removeSaveImageListener(codecsSaveImage);
image = null;
codecs = null;
}
CodecsSaveImageListener codecsSaveImage = new CodecsSaveImageListener() {
@Override
public void onSaveImage(CodecsSaveImageEvent e) {
// This example works with 24 bpp images only
assertEquals(e.getImage().getBitsPerPixel(), 24);
switch (myMode) {
case 0:
// Do not do anything
break;
case 1:
// Do not do anything
break;
case 2:
// Invert the image data
{
int scanlineLength = e.getImage().getBytesPerLine();
byte[] scanline = new byte[scanlineLength];
// Loop through all the scanlines in the data
for (int y = 0; y < e.getLines(); y++) {
// Get this row
e.getBuffer().getData(y * scanlineLength, scanline, 0, scanlineLength);
// Process it by inverting every pixel data
for (int x = 0; x < scanlineLength; x++) {
scanline[x] = (byte) (255 - scanline[x]);
}
// Copy it back to the event buffer
e.getBuffer().setData(y * scanlineLength, scanline, 0, scanlineLength);
}
}
break;
}
}
};
// <!--CodecsSaveOptions.RetrieveDataFromImage-->
// <!--CodecsVectorLoadOptions-->
public void codecsVectorLoadOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "random.dxf");
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "Result.jpg");
// Check if it is a vector file
CodecsImageInfo info = codecs.getInformation(srcFileName, false);
System.out.println("Is {srcFileName} a vector file? : {info.Vector.IsVectorFile}");
System.out.println("Units: {info.Vector.Unit}");
// Parallelogram data
CodecsVectorImageInfo codecsVectorImageInfo = info.getVector();
System.out.println("Parallelogram Max X: " + codecsVectorImageInfo.getParallelogramMaxX());
System.out.println("Parallelogram Max Y: " + codecsVectorImageInfo.getParallelogramMaxY());
System.out.println("Parallelogram Max Z: " + codecsVectorImageInfo.getParallelogramMaxZ());
System.out.println("Parallelogram Min X: " + codecsVectorImageInfo.getParallelogramMinX());
System.out.println("Parallelogram Min Y: " + codecsVectorImageInfo.getParallelogramMinY());
System.out.println("Parallelogram Min Z: " + codecsVectorImageInfo.getParallelogramMinZ());
// Set the vector load options
// CodecsVectorOptions & CodecsVectorLoadOptions reference
codecs.getOptions().getVector().getLoad().setBackgroundColor(new RasterColor(255, 255, 255));
codecs.getOptions().getVector().getLoad().setBitsPerPixel(24);
codecs.getOptions().getVector().getLoad().setForceBackgroundColor(true);
codecs.getOptions().getVector().getLoad().setViewWidth(800);
codecs.getOptions().getVector().getLoad().setViewHeight(800);
codecs.getOptions().getVector().getLoad().setViewMode(CodecsVectorViewMode.UseBest);
// Load the image
RasterImage image = codecs.load(srcFileName);
codecs.save(image, outputFileName, RasterImageFormat.JPEG, 0);
// do something with the image here
assertTrue("File saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
// Clean up
image.dispose();
codecs.dispose();
}
// <!--CodecsVectorLoadOptions-->
// <!--CodecsPstOptions-->
public void codecsPstOptionsExample(){
final String LEAD_VARS_IMAGES_DIR ="C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "TestMessages.pst");
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "Result.jpg");
int messageCount = 0;
// Get number of messages in the PST files
CodecsImageInfo imageInfo = codecs.getInformation(srcFileName, true);
messageCount = imageInfo.getPst().getMessageCount();
imageInfo = null;
// Load all messages as pure text
for (int messageNumber = 1; messageNumber <= messageCount; messageNumber++) {
// Load message number 10 as text only
// CodecsPstOptions & CodecsPstLoadOptions reference
codecs.getOptions().getPst().getLoad().setMessageNumber(messageNumber);
codecs.getOptions().getPst().getLoad().setPlainText(true);
// Load the image
RasterImage image = codecs.load(srcFileName);
// do something with the image here
codecs.save(image, outputFileName, RasterImageFormat.JPEG, 0);
assertTrue("File saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
image.dispose();;
}
codecs.dispose();;
}
// <!--CodecsPstOptions-->
// <!--CodecsHtmlOptions-->
public void codecsHtmlOptionsExample() throws URISyntaxException {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
String srcFileName = combine(LEAD_VARS_IMAGES_DIR, "Example.html");
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "Result.jpg");
RasterCodecs codecs = new RasterCodecs();
// Use the default HTML rendering engine - CodecsHtmlOptions &
// CodecsHtmlLoadOptions reference
codecs.getOptions().getHtml().getLoad().setHtmlEngine(CodecsHtmlEngine.AUTO);
// Set the HTML options to only allow loading resources from the leadtools.com
// domain:
codecs.getOptions().getHtml().getLoad().setDomainWhitelist("leadtools.com");
// Disable all JavaScript embedded in the HTML file
codecs.getOptions().getHtml().getLoad().setEnableJS(false);
// Load the image
RasterImage image = codecs.load(srcFileName, 1);
// Do something with image
codecs.save(image, outputFileName, RasterImageFormat.JPEG, 0);
assertTrue("File unsuccessfully saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
image.dispose();;
codecs.dispose();
}
// <!--CodecsHtmlOptions-->
// <!--CodecsCsvOptions-->
public void codecsCsvOptionsExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
String csvFileName = combine(LEAD_VARS_IMAGES_DIR, "sample.csv");
String pngFileName = combine(LEAD_VARS_IMAGES_DIR, "sample-csv.png");
RasterCodecs codecs = new RasterCodecs();
// Change some CSV options
CodecsCsvLoadOptions csvOptions = codecs.getOptions().getCsv().getLoad(); // CodecsCsvOptions reference
csvOptions.setHeaderFontItalic(true);
csvOptions.setBodyFontName("Arial");
csvOptions.setTableBorderColor(RasterColor.fromKnownColor(RasterKnownColor.RED));
// Load the first page from a CSV file
RasterImage image = codecs.load(csvFileName, 1);
// Save it as PNG
codecs.save(image, pngFileName, RasterImageFormat.PNG, 0);
assertTrue("File unsuccessfully saved to " + pngFileName, (new File(pngFileName)).exists());
System.out.printf("File successfully saved to %s%n", pngFileName);
image = null;
codecs = null;
}
// <!--CodecsCsvOptions-->
// <!--CodecsPdfLoadOptions.EnhanceThinLines-->
public void codecsEnhanceThinLinesOptionExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
String outputFileName = combine(LEAD_VARS_IMAGES_DIR, "Result.jpg");
// The following example shows you how to disable the thin line enhancement
// property //
RasterCodecs codecs = new RasterCodecs();
codecs.getOptions().getPdf().getLoad().setEnhanceThinLines(false);
String inputFile = combine(LEAD_VARS_IMAGES_DIR, "leadtools.pdf");
RasterImage rawImage = codecs.load(inputFile, 24, CodecsLoadByteOrder.RGB_OR_GRAY, 1, -1);
codecs.save(rawImage, outputFileName, RasterImageFormat.JPEG, 0);
assertTrue("File unsuccessfully saved to " + outputFileName, (new File(outputFileName)).exists());
System.out.printf("File successfully saved to %s%n", outputFileName);
}
// <!--CodecsPdfLoadOptions.EnhanceThinLines-->
// <!--CodecsTxtLoadOptions.DefaultEncoding-->
// This C# example shows you how to load a file called "Ansi.txt" that contains
// ANSI text.
public void codecsTxtLoadOptionsDefaultEncodingExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String srcFile = combine(LEAD_VARS_IMAGES_DIR, "Example.txt");
CodecsImageInfo info = codecs.getInformation(srcFile, false);
if (!info.hasBom())
codecs.getOptions().getTxt().getLoad().setDefaultEncoding(CodecsTxtEncoding.ANSI); // Load as Ansi. Here you
// can bring up a message
// box asking the user to
// select the encoding //
codecs.getOptions().getLoad().setAllPages(true);
RasterImage image = codecs.load(srcFile);
codecs.save(image, srcFile + ".tif", RasterImageFormat.TIFLZW, 0);
assertTrue("File unsuccessfully saved to " + srcFile + ".tif", (new File(srcFile + ".tif")).exists());
System.out.printf("File successfully saved to %s%n", srcFile + ".tif");
image = null;
info = null;
codecs = null;
}
// <!--CodecsTxtLoadOptions.DefaultEncoding-->
// <!--CodecsLoadOptions.Decrypt-->
public void codecsLoadOptionsDecryptExample() {
final String LEAD_VARS_IMAGES_DIR = "C:\\LEADTOOLS23\\Resources\\Images";
String srcFile = combine(LEAD_VARS_IMAGES_DIR, "Encrypted.pdf");
RasterCodecs codecs = new RasterCodecs();
codecs.getOptions().getLoad().getDecrypt().setPassword("MyPassword");
codecs.getOptions().getLoad().setAllPages(true);
RasterImage image = codecs.load(srcFile);
codecs.save(image, srcFile + ".tif", RasterImageFormat.TIFLZW, 0);
assertTrue("File unsuccessfully saved to " + srcFile + ".tif", (new File(srcFile + ".tif")).exists());
System.out.printf("File successfully saved to %s%n", srcFile + ".tif");
image.dispose();;
codecs.dispose();;
}
// <!--CodecsLoadOptions.Decrypt-->
// <!--CodecsHeicOptions-->
public void codecsHeicOptionsExample(){
final String LEAD_VARS_IMAGES_DIR ="C:\\LEADTOOLS23\\Resources\\Images";
String fileName = combine(LEAD_VARS_IMAGES_DIR,"example.heic");
RasterCodecs codecs = new RasterCodecs();
RasterImage image = codecs.load(fileName);
// Save a HEIC file using qFactor of 30 and no stamp //
String outFile30 = combine(LEAD_VARS_IMAGES_DIR,"out30-NoStamp.heic");
codecs.getOptions().getHeic().getLoad().setMultithreaded(false); //
// CodecsHeicLoadOptions reference
codecs.getOptions().getHeic().getSave().setQualityFactor(30); //
// CodecsHeicOptions & CodecsHeicSaveOptions reference
codecs.save(image, outFile30, RasterImageFormat.HEIC,0);
assertTrue("File unsuccessfully saved to " + outFile30, (new File(outFile30)).exists());
System.out.printf("File successfully saved to %s%n", outFile30);
// Save a HEIC file using qFactor of 40 and a stamp //
String outFile40 = combine(LEAD_VARS_IMAGES_DIR,"out40-Stamp.heic");
codecs.getOptions().getHeic().getSave().setQualityFactor(40);
codecs.getOptions().getHeic().getSave().setSaveWithStamp(true);
codecs.getOptions().getHeic().getSave().setStampWidth(320);
codecs.getOptions().getHeic().getSave().setStampHeight(
(codecs.getOptions().getHeic().getSave().getStampWidth() * image.getHeight())
/ (image.getWidth())
);
codecs.getOptions().getHeic().getSave().setStampBitsPerPixel(24);
codecs.save(image, outFile40, RasterImageFormat.HEIC, 0);
assertTrue("File unsuccessfully saved to " + outFile40, (new File(outFile40)).exists());
System.out.printf("File successfully saved to %s%n", outFile40);
// Clean up
codecs.dispose();
}
// <!--CodecsHeicOptions-->
// <!--CodecsPowerPointOptions-->
public void codecsPowerPointOptionsExample2(){
final String LEAD_VARS_IMAGES_DIR ="C:\\LEADTOOLS23\\Resources\\Images";
RasterCodecs codecs = new RasterCodecs();
String filename = combine(LEAD_VARS_IMAGES_DIR, "test.ppt");
String savefilename = combine(LEAD_VARS_IMAGES_DIR, "copy.ppt");
// Specify PowerPointLoadOptions //
CodecsPowerPointLoadOptions loadOptions =
codecs.getOptions().getPowerPoint().getLoad();
loadOptions.setShowHiddenSlides(false);
// Load and Save PowerPoint //
RasterImage image = codecs.load(filename, 1);
codecs.save(image, savefilename, RasterImageFormat.PPT, 24);
image = null;
// Clean up
codecs.dispose();
}