Introduction
The task to convert from PDF to TIFF format can sometimes be tricky and quite challenging if you don’t have the right tool for that. Our Apitron PDF Rasterizer for .NET component has been offering this feature for years and now we’ve tweaked it a bit to fit into more demanding workflows. From now on, you’ll be able to control the conversion of the individual page by changing its DPI, rotation, crop and other parameters.
The complete code sample can be downloaded from our github repo.
The code
We’ll start with the following PDF document having one rotated page:
Pic. 1 Source PDF file before conversion to TIFF |
Using the custom page conversion handling code in the handler of BeforeRenderPage event, we’ll correct the rotation for the second page, set the crop and remove the 3d page.
internal class Program
{
const int DPI = 144;
private static void Main(string[] args)
{
// open and load the file
using (FileStream fs = new FileStream(@"..\..\map.pdf", FileMode.Open),
fsOut = File.Create("out.tiff"))
{
// this objects represents a PDF document
using (Document document = new Document(fs))
{
// save to tiff using CCIT4 compression, black and white tiff.
// set the DPI to 144.0 for this sample, so twice more than default PDF dpi setting.
TiffRenderingSettings tiffRenderingSettings =
new TiffRenderingSettings(TiffCompressionMethod.LZW, DPI, DPI);
tiffRenderingSettings.RenderMode = RenderMode.HighQuality;
tiffRenderingSettings.ScaleMode = ScaleMode.PreserveAspectRatio;
tiffRenderingSettings.Compression = TiffCompressionMethod.LZW;
// subscribe to before rendering event and adjust rendering settings
// for each page as you want
tiffRenderingSettings.BeforeRenderPage += TiffRenderingSettings_BeforeRenderPage;
document.SaveToTiff(fsOut, tiffRenderingSettings);
}
System.Diagnostics.Process.Start("out.tiff");
}
}
private static void TiffRenderingSettings_BeforeRenderPage(BeforeRenderPageEventArgs args)
{
// skip all pages expect first two
if (args.PageIndex > 1)
{
args.IsPageSkipped = true;
return;
}
// force the portrait mode for all pages if needed
if (args.DesiredHeight < args.DesiredWidth)
{
args.RenderingSettings.RotationAngle = RotationAngle.Rotate270;
}
// crop from the all sides by cutting out existing margins
double margin = 40;
args.CropBox = new Rectangle(margin, margin, args.DesiredWidth - margin,
args.DesiredHeight - margin);
}
}
Pic. 2 Cropped first page of the resulting TIFF file
(gray area outlines its original dimensions)
|
Produced tiff file will have only two cropped pages left as we ignored everything starting from the second one, and additionally the second page will be rotated for a better viewing.
Summary
The Apitron PDF Rasterizer for .NET component is a reliable and highly effective solution for those who need the ultimate quality and control over the PDF to image conversion. It can also be used for creation of cross-platform solutions targeting many platforms at once being available (including plain vanilla .NET) for Xamarin, Mono and Windows store based applications.
No comments:
Post a Comment