VintaSoft Imaging .NET SDK and Plug-ins Discussions
Questions, comments and suggestions concerning VintaSoft Imaging .NET SDK.
Board index < VintaSoft Imaging < VintaSoft Imaging .NET SDK and Plug-ins Discussions
/// <summary>
/// Loads image(s) from source TIFF file,
/// rotates the first image,
/// saves image(s) to a new TIFF file with encoding settings from source TIFF file.
/// </summary>
/// <param name="inFilePath">Path to a source TIFF file.</param>
/// <param name="outFilePath">Path to a destination TIFF file.</param>
static void ChangeAndSaveTiffFile(string inFilePath, string outFilePath)
{
// create image collection
using (ImageCollection images = new ImageCollection())
{
// add images from TIFF file to the image collection
images.Add(inFilePath, false);
// rotate the first image
images[0].Rotate(90);
// create TIFF encoder
using (TiffEncoder encoder = new TiffEncoder())
{
// if source and destination paths are the same
if (inFilePath == outFilePath)
// specify that image collection must change source after saving
encoder.SaveAndSwitchSource = true;
// subscribe to the image saving event
encoder.ImageSaving += new EventHandler<ImageSavingEventArgs>(EncoderImageSaving);
// save images synchronously
images.SaveSync(outFilePath, encoder);
// unsubscribe from the image saving event
encoder.ImageSaving -= EncoderImageSaving;
}
// clear image collection and dispose images
images.ClearAndDisposeItems();
}
}
/// <summary>
/// Handler of the image saving event.
/// </summary>
static void EncoderImageSaving(object sender, ImageSavingEventArgs e)
{
// get TIFF encoder
TiffEncoder encoder = (TiffEncoder)sender;
// get TIFF encoder settings from source TIFF image
encoder.Settings = GetTiffEncoderSettings(e.Image.Metadata);
}
/// <summary>
/// Gets the TIFF encoder settings from metadata of TIFF image.
/// </summary>
/// <param name="metadata">The metadata.</param>
static TiffEncoderSettings GetTiffEncoderSettings(VintasoftImageMetadata metadata)
{
// create TIFF encoder settings
TiffEncoderSettings settings = new TiffEncoderSettings();
// get page metadata of TIFF file
TiffPageMetadata tree = metadata.MetadataTree as TiffPageMetadata;
// if metadata is TIFF image metadata
if (tree != null)
{
// get compression of TIFF file
settings.Compression = tree.Compression;
settings.UseTiles = false;
settings.UseStrips = false;
// find RowsPerStrip tag of TIFF file
TiffTagMetadata rowsPerStripTag = tree.FindChildNode<TiffTagMetadata>("RowsPerStrip");
// if tag exists (TIFF image data are stored in strips)
if (rowsPerStripTag != null)
{
// specify that encoder must use strips
settings.UseStrips = true;
// set row count in a strip of TIFF file
settings.RowsPerStrip = Convert.ToInt32(rowsPerStripTag.Value);
}
// if tag does NOT exist (TIFF image data are stored in tiles)
else
{
// find TileWidth tag of TIFF file
TiffTagMetadata tileWidthTag = tree.FindChildNode<TiffTagMetadata>("TileWidth");
// if tag exists
if (tileWidthTag != null)
{
// find TileLength tag of TIFF file
TiffTagMetadata tileLengthTag = tree.FindChildNode<TiffTagMetadata>("TileLength");
// get tile width and height of TIFF file
int tileWidth = Convert.ToInt32(tileWidthTag.Value);
int tileHeigth = Convert.ToInt32(tileLengthTag.Value);
// specify that encoder must use tiles
settings.UseTiles = true;
// set size of tile of TIFF file
settings.TileSize = new Size(tileWidth, tileHeigth);
}
}
}
return settings;
}
Best regards, Alexander
can you please give me a hint, how this can be accomplished for pdf files? The PdfPageMetaData-class does not contain properties to retrieve the image compression or something like that.Do you want just rotate image (90, 180 or 270 degrees) and save OR apply custom processing to image and save?
/// <summary>
/// Renders page of surce PDF document,
/// inverts the first image,
/// saves image(s) to a new PDF file with encoding settings from source PDF document.
/// </summary>
/// <param name="inFilePath">Path to a source PDF file.</param>
/// <param name="outFilePath">Path to a destination PDF file.</param>
public static void ChangeAndSavePdfFile(string inFilePath, string outFilePath)
{
// create image collection
using (ImageCollection images = new ImageCollection())
{
// add images from file to the image collection
images.Add(inFilePath, false);
// invert the first image
images[0].Invert();
// create PDF encoder
using (PdfEncoder encoder = new PdfEncoder())
{
// if source and destination paths are the same
if (inFilePath == outFilePath)
// specify that image collection must change source after saving
encoder.SaveAndSwitchSource = true;
// subscribe to the image saving event
encoder.ImageSaving += new EventHandler<ImageSavingEventArgs>(EncoderImageSaving);
// save images synchronously
images.SaveSync(outFilePath, encoder);
// unsubscribe from the image saving event
encoder.ImageSaving -= EncoderImageSaving;
}
// clear image collection and dispose images
images.ClearAndDisposeItems();
}
}
/// <summary>
/// Handler of the image saving event.
/// </summary>
static void EncoderImageSaving(object sender, ImageSavingEventArgs e)
{
// get PDF encoder
PdfEncoder encoder = (PdfEncoder)sender;
// get TIFF encoder settings from source TIFF image
encoder.Settings = GetPdfEncoderSettings(e.Image);
}
/// <summary>
/// Gets the PDF encoder settings from PDF image.
/// </summary>
/// <param name="image">The image.</param>
static PdfEncoderSettings GetPdfEncoderSettings(VintasoftImage image)
{
// create PDF encoder settings
PdfEncoderSettings settings = new PdfEncoderSettings();
settings.Compression = PdfImageCompression.Auto;
// set document updating mode to "Incremental"
settings.UpdateMode = PdfDocumentUpdateMode.Incremental;
//// set document updating mode to "Pack"
//settings.UpdateMode = PdfDocumentUpdateMode.Pack;
// get PDF page associated with image
PdfPage page = PdfDocumentController.GetPageAssociatedWithImage(image);
// if PDF page is image only
if (page.IsImageOnly)
{
// select image compression
if (page.BackgroundImage.Compression == PdfCompression.None)
settings.Compression = PdfImageCompression.None;
else if ((page.BackgroundImage.Compression & PdfCompression.CcittFax) != 0)
settings.Compression = PdfImageCompression.CcittFax;
else if ((page.BackgroundImage.Compression & PdfCompression.Jpeg) != 0)
settings.Compression = PdfImageCompression.Jpeg;
else if ((page.BackgroundImage.Compression & PdfCompression.Zip) != 0)
settings.Compression = PdfImageCompression.Zip;
else if ((page.BackgroundImage.Compression & PdfCompression.Jpeg2000) != 0)
settings.Compression = PdfImageCompression.Jpeg2000;
else if ((page.BackgroundImage.Compression & PdfCompression.Lzw) != 0)
settings.Compression = PdfImageCompression.Lzw;
else if ((page.BackgroundImage.Compression & PdfCompression.Jbig2) != 0)
settings.Compression = PdfImageCompression.Jbig2;
if ((page.BackgroundImage.Compression & PdfCompression.Zip) != 0)
settings.Compression |= PdfImageCompression.Zip;
}
return settings;
}
Best regards, Alexander