PDF: Working with PDF page
In This Topic
PdfPage class represents a page of PDF document and allows to:
Here is C#/VB.NET code that demonstrates how to create new PDF page and add it to a PDF document:
// page size
System.Drawing.RectangleF pageSize = new System.Drawing.RectangleF(0, 0, 600, 800);
// create new page of specified size
Vintasoft.Imaging.Pdf.Tree.PdfPage page =
new Vintasoft.Imaging.Pdf.Tree.PdfPage(document, pageSize);
// add page to a PDF document
document.Pages.Add(page);
' page size
Dim pageSize As New System.Drawing.RectangleF(0, 0, 600, 800)
' create new page of specified size
Dim page As New Vintasoft.Imaging.Pdf.Tree.PdfPage(document, pageSize)
' add page to a PDF document
document.Pages.Add(page)
Here is C#/VB.NET code that demonstrates how to obtain information about image resources of all pages of PDF document:
/// <summary>
/// Gets and prints information about PDF image resources.
/// </summary>
/// <param name="pdfFileName">The filename of PDF document.</param>
public static void GetImageResourcesInfo(string pdfFileName)
{
// open PDF document
using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFileName))
{
// for each PDF page
for (int pageIndex = 0; pageIndex < document.Pages.Count; pageIndex++)
{
// get PDF page
Vintasoft.Imaging.Pdf.Tree.PdfPage page = document.Pages[pageIndex];
// get a collection of PDF image resources of page
Vintasoft.Imaging.Pdf.Tree.PdfImageResource[] imageResources = page.GetImages();
// print the page index and count of image resources
System.Console.WriteLine("Page {0} Image resources count: {1}", pageIndex + 1, imageResources.Length);
// for each image resource
foreach (Vintasoft.Imaging.Pdf.Tree.PdfImageResource imageResource in imageResources)
{
// print information about image resource
System.Console.WriteLine("\t {0}x{1} {2,5}bpp {3}",
imageResource.Width, imageResource.Height,
imageResource.BitsPerPixel, imageResource.Compression);
}
}
}
}
/* This code example produces the following output:
Page 1 Image resources count: 9
157x139 32bpp Zip
157x139 8bpp Jpeg
700x596 24bpp Jpeg
686x585 24bpp Jpeg
459x317 32bpp Jpeg
459x317 8bpp Zip, Predictor
484x487 32bpp Jpeg
484x487 8bpp Zip, Predictor
824x537 24bpp Zip
Page 2 Image resources count: 0
*/
''' <summary>
''' Gets and prints information about PDF image resources.
''' </summary>
''' <param name="pdfFileName">The filename of PDF document.</param>
Public Shared Sub GetImageResourcesInfo(pdfFileName As String)
' open PDF document
Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFileName)
' for each PDF page
For pageIndex As Integer = 0 To document.Pages.Count - 1
' get PDF page
Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages(pageIndex)
' get a collection of PDF image resources of page
Dim imageResources As Vintasoft.Imaging.Pdf.Tree.PdfImageResource() = page.GetImages()
' print the page index and count of image resources
System.Console.WriteLine("Page {0} Image resources count: {1}", pageIndex + 1, imageResources.Length)
' for each image resource
For Each imageResource As Vintasoft.Imaging.Pdf.Tree.PdfImageResource In imageResources
' print information about image resource
System.Console.WriteLine(vbTab & " {0}x{1} {2,5}bpp {3}", imageResource.Width, imageResource.Height, imageResource.BitsPerPixel, imageResource.Compression)
Next
Next
End Using
End Sub
' This code example produces the following output:
'
' Page 1 Image resources count: 9
' 157x139 32bpp Zip
' 157x139 8bpp Jpeg
' 700x596 24bpp Jpeg
' 686x585 24bpp Jpeg
' 459x317 32bpp Jpeg
' 459x317 8bpp Zip, Predictor
' 484x487 32bpp Jpeg
' 484x487 8bpp Zip, Predictor
' 824x537 24bpp Zip
' Page 2 Image resources count: 0
'
Coordinate system and measurement units of PDF page
PDF page uses Cartesian coordinates system, i.e. the center of coordinates system is located in the left-lower corner of PDF page, X axis is directed left to right, Y axis is directed bottom to top.
PDF document is a document in vector format and it does not contain information about the document resolution, in other words page of PDF document can be rendered in any resolution. By default, PDF page uses the user space units as default units of measure. 1 user space units equals 1/72 inch.
Image has a coordinate system that differs from a coordinate system of PDF page. The center in image coordinates system is located in the left-upper corner of the image, X axis is directed left to right, Y axis is directed top to bottom.
The SDK provides methods which simplify the process of translating the points from image coordinate system to PDF page coordinate system and vi-e-versa:
Here is a list of methods of
PdfPage class, which are used for coordinates translation:
- PdfPage.PointsFromImageSpaceToPageSpace - translates points from image coordinate system to a PDF page coordinate system taking into account the reflection of coordinates system by Y axis, crop box and rotation of page
- PdfPage.PointsFromPageSpaceToImageSpace - translates points from PDF page coordinate system to an image coordinate system taking into account the reflection of coordinates system by Y axis, crop box and rotation of page
- PdfPage.PointsToUnits - ranslates points from image coordinate system to a PDF page coordinate system taking into account the reflection of coordinates system by Y axis
- PdfPage.UnitsToPixels - translates points from PDF page coordinate system to an image coordinate system taking into account the reflection of coordinates system by Y axis
- PdfPage.PointToPixel - translates a value from PDF page coordinate system to an image coordinate system
- PdfPage.PointToUnit - translates a value from image coordinate system to a PDF page coordinate system
Here is C#/VB.NET code that demonstrates how to translate a rectangle, which is specified in image coordinate system, to a rectangle, which is specified in PDF page coordinate system:
/// <summary>
/// Converts the rectangle from image space to page space.
/// </summary>
/// <param name="rect">Rectangle in image space.</param>
/// <param name="imageResolution">The image resolution.</param>
/// <param name="page">The PDF page.</param>
/// <returns><see cref="System.Drawing.RectangleF"/> structure in PDF page space.</returns>
public static System.Drawing.RectangleF ConvertRectangleFromImageSpaceToPageSpace(
System.Drawing.RectangleF rect,
Vintasoft.Imaging.Resolution imageResolution,
Vintasoft.Imaging.Pdf.Tree.PdfPage page)
{
// Rectangle -> PointF[]
System.Drawing.PointF[] points = new System.Drawing.PointF[] {
rect.Location,
new System.Drawing.PointF(rect.X + rect.Width, rect.Y + rect.Height) };
// ImageSpace -> PageSpace
page.PointsFromImageSpaceToPageSpace(points, imageResolution);
// Points -> RectangleF
float x0 = System.Math.Min(points[0].X, points[1].X);
float y0 = System.Math.Min(points[0].Y, points[1].Y);
float x1 = System.Math.Max(points[0].X, points[1].X);
float y1 = System.Math.Max(points[0].Y, points[1].Y);
return new System.Drawing.RectangleF(x0, y0, x1 - x0, y1 - y0);
}
''' <summary>
''' Converts the rectangle from image space to page space.
''' </summary>
''' <param name="rect">Rectangle in image space.</param>
''' <param name="imageResolution">The image resolution.</param>
''' <param name="page">The PDF page.</param>
''' <returns><see cref="System.Drawing.RectangleF"/> structure in PDF page space.</returns>
Public Shared Function ConvertRectangleFromImageSpaceToPageSpace(rect As System.Drawing.RectangleF, imageResolution As Vintasoft.Imaging.Resolution, page As Vintasoft.Imaging.Pdf.Tree.PdfPage) As System.Drawing.RectangleF
' Rectangle -> PointF[]
Dim points As System.Drawing.PointF() = New System.Drawing.PointF() {rect.Location, New System.Drawing.PointF(rect.X + rect.Width, rect.Y + rect.Height)}
' ImageSpace -> PageSpace
page.PointsFromImageSpaceToPageSpace(points, imageResolution)
' Points -> RectangleF
Dim x0 As Single = System.Math.Min(points(0).X, points(1).X)
Dim y0 As Single = System.Math.Min(points(0).Y, points(1).Y)
Dim x1 As Single = System.Math.Max(points(0).X, points(1).X)
Dim y1 As Single = System.Math.Max(points(0).Y, points(1).Y)
Return New System.Drawing.RectangleF(x0, y0, x1 - x0, y1 - y0)
End Function