PDF: Working with image resources of PDF document
In This Topic
Image resource stores an image, which has to be drawn on PDF page.
Each image resource can be used several times, i.e. it can be drawn on several pages at once.
PdfImageResource class represents an image resource, which is stored in PDF document, and allows to:
- create a new image resource based on image
- obtain information about image, which is stored in the resource, using following properties: PdfImageResource.Width, PdfImageResource.Height, PdfImageResource.BitsPerPixel, PdfImageResource.PixelFormat, etc.
- get the compression algorithm of image resource using PdfImageResource.Compression property
- get image, which is stored in the resource, using PdfImageResource.GetImage method
- change image, which is stored in the resource, using PdfImageResource.SetImage method
- change the compression algorithm of image resource using PdfImageResource.Compress method
All image resources of PDF document can be gathered using
PdfDocument.GetImages method. All image resources of PDF page can be gathered using
PdfPage.GetImages.
Here is an example that demonstrates how to obtain information about all image resources of PDF page:
/// <summary>
/// Prints information about image-resources of PDF document.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
public static void GetPdfImagesInfo(string pdfFilename)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document =
new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
{
// get collection of image-resources of PDF document
Vintasoft.Imaging.Pdf.Tree.PdfImageResource[] images = document.GetImages();
// print count of image-resource
System.Console.WriteLine("Images count: {0}", images.Length);
// for each image-resource
foreach (Vintasoft.Imaging.Pdf.Tree.PdfImageResource image in images)
{
// print information about image-resource
System.Console.WriteLine("\t{0,4}x{1,-4} {2,5}bpp {3}", image.Width, image.Height,
image.BitsPerPixel, image.Compression);
}
}
}
/* This code example produces the following output:
Images count: 11
189x41 24bpp Jpeg
120x50 24bpp Jpeg
122x56 24bpp Jpeg
143x41 24bpp Jpeg
216x21 24bpp Jpeg
106x49 8bpp Zip
209x30 24bpp Jpeg
189x24 24bpp Jpeg
122x42 24bpp Jpeg
5x7 24bpp Zip
817x292 24bpp Jpeg
*/
''' <summary>
''' Prints information about image-resources of PDF document.
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
Public Shared Sub GetPdfImagesInfo(pdfFilename As String)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
' get collection of image-resources of PDF document
Dim images As Vintasoft.Imaging.Pdf.Tree.PdfImageResource() = document.GetImages()
' print count of image-resource
System.Console.WriteLine("Images count: {0}", images.Length)
' for each image-resource
For Each image As Vintasoft.Imaging.Pdf.Tree.PdfImageResource In images
' print information about image-resource
System.Console.WriteLine(vbTab & "{0,4}x{1,-4} {2,5}bpp {3}", image.Width, image.Height, image.BitsPerPixel, image.Compression)
Next
End Using
End Sub
' This code example produces the following output:
' Images count: 11
' 189x41 24bpp Jpeg
' 120x50 24bpp Jpeg
' 122x56 24bpp Jpeg
' 143x41 24bpp Jpeg
' 216x21 24bpp Jpeg
' 106x49 8bpp Zip
' 209x30 24bpp Jpeg
' 189x24 24bpp Jpeg
' 122x42 24bpp Jpeg
' 5x7 24bpp Zip
' 817x292 24bpp Jpeg
'
To add a new image resource to PDF document it is necessary to:
- create an image resource based on image
- draw the image resource on PDF page
Here is an example that demonstrates how to draw a new image on PDF page:
/// <summary>
/// Draws an image on PDF page.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
/// <param name="imageFilename">The filename of image.</param>
public static void DrawImageOnPdfPage(string pdfFilename, string imageFilename)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document =
new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
{
// open image
using (Vintasoft.Imaging.VintasoftImage image =
new Vintasoft.Imaging.VintasoftImage(imageFilename))
{
// get the first page
Vintasoft.Imaging.Pdf.Tree.PdfPage page = document.Pages[0];
// get PDF graphics of PDF page
using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics graphics = page.GetGraphics())
{
// create image-resource based on image
Vintasoft.Imaging.Pdf.Tree.PdfImageResource imageResource =
new Vintasoft.Imaging.Pdf.Tree.PdfImageResource(
document, image, Vintasoft.Imaging.Pdf.PdfCompression.Auto);
// draw image-resource on PDF page
graphics.DrawImage(imageResource, new System.Drawing.RectangleF(100, 100, 200, 300));
}
}
// save changes to a file
document.SaveChanges();
}
}
''' <summary>
''' Draws an image on PDF page.
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
''' <param name="imageFilename">The filename of image.</param>
Public Shared Sub DrawImageOnPdfPage(pdfFilename As String, imageFilename As String)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
' open image
Using image As New Vintasoft.Imaging.VintasoftImage(imageFilename)
' get the first page
Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages(0)
' get PDF graphics of PDF page
Using graphics As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = page.GetGraphics()
' create image-resource based on image
Dim imageResource As New Vintasoft.Imaging.Pdf.Tree.PdfImageResource(document, image, Vintasoft.Imaging.Pdf.PdfCompression.Auto)
' draw image-resource on PDF page
graphics.DrawImage(imageResource, New System.Drawing.RectangleF(100, 100, 200, 300))
End Using
End Using
' save changes to a file
document.SaveChanges()
End Using
End Sub
To change the compression algorithm of image resource it is necessary to do the following:
Here is an example that demonstrates how to change the compression algorithm of all black-white image resources:
/// <summary>
/// Changes compression of all black-white image-resources of PDF document.
/// </summary>
/// <param name="pdfFilename">The filename of PDF document.</param>
public static void ChangeCompressionBlackWhiteImages(string pdfFilename)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document =
new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
{
// get collection of image-resources
Vintasoft.Imaging.Pdf.Tree.PdfImageResource[] images = document.GetImages();
// create compression settings
Vintasoft.Imaging.Pdf.PdfCompressionSettings compressionSettings =
new Vintasoft.Imaging.Pdf.PdfCompressionSettings();
// for each image-resource
foreach (Vintasoft.Imaging.Pdf.Tree.PdfImageResource image in images)
{
// if image is black-white
if (image.PixelFormat == Vintasoft.Imaging.PixelFormat.BlackWhite)
// change compression of image-resource
image.Compress(Vintasoft.Imaging.Pdf.PdfCompression.CcittFax, compressionSettings);
}
// save changes to a file
document.SaveChanges();
}
}
''' <summary>
''' Changes compression of all black-white image-resources of PDF document.
''' </summary>
''' <param name="pdfFilename">The filename of PDF document.</param>
Public Shared Sub ChangeCompressionBlackWhiteImages(pdfFilename As String)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
' get collection of image-resources
Dim images As Vintasoft.Imaging.Pdf.Tree.PdfImageResource() = document.GetImages()
' create compression settings
Dim compressionSettings As New Vintasoft.Imaging.Pdf.PdfCompressionSettings()
' for each image-resource
For Each image As Vintasoft.Imaging.Pdf.Tree.PdfImageResource In images
' if image is black-white
If image.PixelFormat = Vintasoft.Imaging.PixelFormat.BlackWhite Then
' change compression of image-resource
image.Compress(Vintasoft.Imaging.Pdf.PdfCompression.CcittFax, compressionSettings)
End If
Next
' save changes to a file
document.SaveChanges()
End Using
End Sub