Class PdfUnitConvertor
Class allowing to convert different unit metrics. Converting is based on Graphics object DPI settings that is why for differ graphics settings must be created new instance. For example: printers often has 300 and greater dpi resolution, for compare default display screen dpi is 96.
Inheritance
Inherited Members
Namespace: Syncfusion.Pdf.Graphics
Assembly: Syncfusion.Pdf.Base.dll
Syntax
[SecurityCritical]
public class PdfUnitConvertor
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create PDF bitmap instance.
PdfBitmap image = new PdfBitmap("Autumn Leaves.jpg");
//Create a PDF unit converter instance.
PdfUnitConvertor converter = new PdfUnitConvertor();
//Convert to point.
float width = converter.ConvertFromPixels(image.Width, PdfGraphicsUnit.Point);
float height = converter.ConvertFromPixels(image.Height, PdfGraphicsUnit.Point);
//Draw image to PDF page.
page.Graphics.DrawImage(image, new RectangleF(0, 0, width, height));
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create PDF bitmap instance.
Dim image As New PdfBitmap("Autumn Leaves.jpg")
'Create a PDF unit converter instance.
Dim converter As New PdfUnitConvertor()
'Convert to point.
Dim width As Single = converter.ConvertFromPixels(image.Width, PdfGraphicsUnit.Point)
Dim height As Single = converter.ConvertFromPixels(image.Height, PdfGraphicsUnit.Point)
'Draw image to PDF page.
page.Graphics.DrawImage(image, New RectangleF(0, 0, width, height))
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
Constructors
PdfUnitConvertor()
Initializes a new instance of the PdfUnitConvertor class with default DPI value of 96.
Declaration
public PdfUnitConvertor()
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create PDF bitmap instance.
PdfBitmap image = new PdfBitmap("Autumn Leaves.jpg");
//Create a PDF unit converter instance.
PdfUnitConvertor converter = new PdfUnitConvertor();
//Convert to point.
float width = converter.ConvertFromPixels(image.Width, PdfGraphicsUnit.Point);
float height = converter.ConvertFromPixels(image.Height, PdfGraphicsUnit.Point);
//Draw image to PDF page.
page.Graphics.DrawImage(image, new RectangleF(0, 0, width, height));
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create PDF bitmap instance.
Dim image As New PdfBitmap("Autumn Leaves.jpg")
'Create a PDF unit converter instance.
Dim converter As New PdfUnitConvertor()
'Convert to point.
Dim width As Single = converter.ConvertFromPixels(image.Width, PdfGraphicsUnit.Point)
Dim height As Single = converter.ConvertFromPixels(image.Height, PdfGraphicsUnit.Point)
'Draw image to PDF page.
page.Graphics.DrawImage(image, New RectangleF(0, 0, width, height))
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
PdfUnitConvertor(Graphics)
Initializes a new instance of the UnitConvertor class with System.Drawing.Graphics.
Declaration
public PdfUnitConvertor(Graphics g)
Parameters
Type | Name | Description |
---|---|---|
System.Drawing.Graphics | g | Graphics for measuring the DPI value |
Remarks
This constructor is not supported in WinRT, Windows Phone, Xamarin, Universal Windows Platform and Silverlight.
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
Image systemImage = Image.FromFile("Autumn Leaves.jpg");
//Create PDF bitmap instance.
PdfBitmap image = new PdfBitmap(systemImage);
//Create system graphics.
Graphics graphics = Graphics.FromImage(systemImage);
//Create a PDF unit converter instance.
PdfUnitConvertor converter = new PdfUnitConvertor(graphics);
//Convert to point.
float width = converter.ConvertFromPixels(image.Width, PdfGraphicsUnit.Point);
float height = converter.ConvertFromPixels(image.Height, PdfGraphicsUnit.Point);
//Draw image to PDF page.
page.Graphics.DrawImage(image, new RectangleF(0, 0, width, height));
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
Dim systemImage As Image = Image.FromFile("Autumn Leaves.jpg")
'Create PDF bitmap instance.
Dim image As New PdfBitmap(systemImage)
'Create system graphics.
Dim graphics As Graphics = Graphics.FromImage(systemImage)
'Create a PDF unit converter instance.
Dim converter As New PdfUnitConvertor(graphics)
'Convert to point.
Dim width As Single = converter.ConvertFromPixels(image.Width, PdfGraphicsUnit.Point)
Dim height As Single = converter.ConvertFromPixels(image.Height, PdfGraphicsUnit.Point)
'Draw image to PDF page.
page.Graphics.DrawImage(image, New RectangleF(0, 0, width, height))
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
PdfUnitConvertor(Single)
Initializes a new instance of the UnitConvertor class with DPI value
Declaration
public PdfUnitConvertor(float dpi)
Parameters
Type | Name | Description |
---|---|---|
System.Single | dpi | The dpi. |
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create PDF bitmap instance.
PdfBitmap image = new PdfBitmap("Autumn Leaves.jpg");
//Create a PDF unit converter instance.
PdfUnitConvertor converter = new PdfUnitConvertor(360);
//Convert to point.
float width = converter.ConvertFromPixels(image.Width, PdfGraphicsUnit.Point);
float height = converter.ConvertFromPixels(image.Height, PdfGraphicsUnit.Point);
//Draw image to PDF page.
page.Graphics.DrawImage(image, new RectangleF(0, 0, width, height));
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create PDF bitmap instance.
Dim image As New PdfBitmap("Autumn Leaves.jpg")
'Create a PDF unit converter instance.
Dim converter As New PdfUnitConvertor(360)
'Convert to point.
Dim width As Single = converter.ConvertFromPixels(image.Width, PdfGraphicsUnit.Point)
Dim height As Single = converter.ConvertFromPixels(image.Height, PdfGraphicsUnit.Point)
'Draw image to PDF page.
page.Graphics.DrawImage(image, New RectangleF(0, 0, width, height))
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
Methods
ConvertFromPixels(PointF, PdfGraphicsUnit)
Converts rectangle from pixels to specified units
Declaration
public PointF ConvertFromPixels(PointF point, PdfGraphicsUnit to)
Parameters
Type | Name | Description |
---|---|---|
System.Drawing.PointF | point | point in pixels units |
PdfGraphicsUnit | to | convert to units |
Returns
Type | Description |
---|---|
System.Drawing.PointF | output Point in specified units |
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create PDF bitmap instance.
PdfBitmap image = new PdfBitmap("Autumn Leaves.jpg");
//Create a PDF unit converter instance.
PdfUnitConvertor converter = new PdfUnitConvertor();
//Convert to point.
PointF location = converter.ConvertFromPixels(new PointF(100,100), PdfGraphicsUnit.Point);
//Draw image to PDF page.
page.Graphics.DrawImage(image, new RectangleF(location, image.GetBounds().Size));
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create PDF bitmap instance.
Dim image As New PdfBitmap("Autumn Leaves.jpg")
'Create a PDF unit converter instance.
Dim converter As New PdfUnitConvertor()
'Convert to point.
Dim location As PointF = converter.ConvertFromPixels(New PointF(100,100), PdfGraphicsUnit.Point)
'Draw image to PDF page.
page.Graphics.DrawImage(image, New RectangleF(location, image.GetBounds().Size))
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
ConvertFromPixels(RectangleF, PdfGraphicsUnit)
Converts rectangle in Pixels into rectangle with specified measure units
Declaration
public RectangleF ConvertFromPixels(RectangleF rect, PdfGraphicsUnit to)
Parameters
Type | Name | Description |
---|---|---|
System.Drawing.RectangleF | rect | source rectangle in pixels units |
PdfGraphicsUnit | to | convert to units |
Returns
Type | Description |
---|---|
System.Drawing.RectangleF | output Rectangle in specified units |
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create PDF bitmap instance.
PdfBitmap image = new PdfBitmap("Autumn Leaves.jpg");
//Create a PDF unit converter instance.
PdfUnitConvertor converter = new PdfUnitConvertor();
//Convert to point.
RectangleF rect = converter.ConvertFromPixels(new RectangleF(100,100,500, 700), PdfGraphicsUnit.Point);
//Draw image to PDF page.
page.Graphics.DrawImage(image, rect);
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create PDF bitmap instance.
Dim image As New PdfBitmap("Autumn Leaves.jpg")
'Create a PDF unit converter instance.
Dim converter As New PdfUnitConvertor()
'Convert to point.
Dim rect As RectangleF = converter.ConvertFromPixels(New RectangleF(100,100,500, 700), PdfGraphicsUnit.Point)
'Draw image to PDF page.
page.Graphics.DrawImage(image, rect)
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
ConvertFromPixels(SizeF, PdfGraphicsUnit)
Converts Size in pixels to size in specified measure units
Declaration
public SizeF ConvertFromPixels(SizeF size, PdfGraphicsUnit to)
Parameters
Type | Name | Description |
---|---|---|
System.Drawing.SizeF | size | source size |
PdfGraphicsUnit | to | convert to units |
Returns
Type | Description |
---|---|
System.Drawing.SizeF | output size in specified measure units |
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create PDF bitmap instance.
PdfBitmap image = new PdfBitmap("Autumn Leaves.jpg");
//Create a PDF unit converter instance.
PdfUnitConvertor converter = new PdfUnitConvertor();
//Convert to point.
SizeF size = converter.ConvertFromPixels(image.GetBounds().Size, PdfGraphicsUnit.Point);
//Draw image to PDF page.
page.Graphics.DrawImage(image, new RectangleF( new PointF(0, 0), size));
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create PDF bitmap instance.
Dim image As New PdfBitmap("Autumn Leaves.jpg")
'Create a PDF unit converter instance.
Dim converter As New PdfUnitConvertor()
'Convert to point.
Dim size As SizeF = converter.ConvertFromPixels(image.GetBounds().Size, PdfGraphicsUnit.Point)
'Draw image to PDF page.
page.Graphics.DrawImage(image, New RectangleF(New PointF(0, 0), size))
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
ConvertFromPixels(Single, PdfGraphicsUnit)
Converts value, to specified graphics unit from Pixel.
Declaration
public float ConvertFromPixels(float value, PdfGraphicsUnit to)
Parameters
Type | Name | Description |
---|---|---|
System.Single | value | Value to convert |
PdfGraphicsUnit | to | Indicates units to convert to |
Returns
Type | Description |
---|---|
System.Single | Value stored in "to" units |
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create PDF bitmap instance.
PdfBitmap image = new PdfBitmap("Autumn Leaves.jpg");
//Create a PDF unit converter instance.
PdfUnitConvertor converter = new PdfUnitConvertor();
//Convert to point.
float width = converter.ConvertFromPixels(image.Width, PdfGraphicsUnit.Point);
float height = converter.ConvertFromPixels(image.Height, PdfGraphicsUnit.Point);
//Draw image to PDF page.
page.Graphics.DrawImage(image, new RectangleF(0, 0, width, height));
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create PDF bitmap instance.
Dim image As New PdfBitmap("Autumn Leaves.jpg")
'Create a PDF unit converter instance.
Dim converter As New PdfUnitConvertor()
'Convert to point.
Dim width As Single = converter.ConvertFromPixels(image.Width, PdfGraphicsUnit.Point)
Dim height As Single = converter.ConvertFromPixels(image.Height, PdfGraphicsUnit.Point)
'Draw image to PDF page.
page.Graphics.DrawImage(image, New RectangleF(0, 0, width, height))
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
ConvertToPixels(PointF, PdfGraphicsUnit)
Converts point from specified graphics units to pixels
Declaration
public PointF ConvertToPixels(PointF point, PdfGraphicsUnit from)
Parameters
Type | Name | Description |
---|---|---|
System.Drawing.PointF | point | source point for convert |
PdfGraphicsUnit | from | measure units |
Returns
Type | Description |
---|---|
System.Drawing.PointF | point in pixels coordinates |
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create new PDF unitconverter.
PdfUnitConvertor converter = new PdfUnitConvertor();
//Convert to pixels.
PointF location = converter.ConvertToPixels(new PointF(100, 100), PdfGraphicsUnit.Point);
page.Graphics.DrawRectangle(PdfPens.Red, new RectangleF(location, new SizeF(200, 50));
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create new PDF unitconverter.
Dim converter As New PdfUnitConvertor()
'Convert to pixels.
Dim location As PointF = converter.ConvertToPixels(New PointF(100, 100), PdfGraphicsUnit.Point)
page.Graphics.DrawRectangle(PdfPens.Red, new RectangleF(location, new SizeF(200, 50))
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
ConvertToPixels(RectangleF, PdfGraphicsUnit)
Converts the rectangle location and size to Pixels from specified measure units
Declaration
public RectangleF ConvertToPixels(RectangleF rect, PdfGraphicsUnit from)
Parameters
Type | Name | Description |
---|---|---|
System.Drawing.RectangleF | rect | source rectangle |
PdfGraphicsUnit | from | source rectangle measure units |
Returns
Type | Description |
---|---|
System.Drawing.RectangleF | Rectangle with Pixels |
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create new PDF unitconverter.
PdfUnitConvertor converter = new PdfUnitConvertor();
//Convert to pixels.
RectangleF rect = converter.ConvertToPixels(new RectangleF(100, 100, 200, 100), PdfGraphicsUnit.Point);
page.Graphics.DrawRectangle(PdfPens.Red, rect);
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create new PDF unitconverter.
Dim converter As New PdfUnitConvertor()
'Convert to pixels.
Dim rect As RectangleF = converter.ConvertToPixels(New RectangleF(100, 100, 200, 100), PdfGraphicsUnit.Point)
page.Graphics.DrawRectangle(PdfPens.Red, rect)
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
ConvertToPixels(SizeF, PdfGraphicsUnit)
Converts size from specified graphics units to pixels
Declaration
public SizeF ConvertToPixels(SizeF size, PdfGraphicsUnit from)
Parameters
Type | Name | Description |
---|---|---|
System.Drawing.SizeF | size | source size |
PdfGraphicsUnit | from | measure units |
Returns
Type | Description |
---|---|
System.Drawing.SizeF | size in pixels |
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create new PDF unitconverter.
PdfUnitConvertor converter = new PdfUnitConvertor();
//Convert to pixels.
SizeF pageSize = converter.ConvertToPixels(doc.PageSettings.Size, PdfGraphicsUnit.Point);
page.Graphics.DrawString("PDF page size is " + pageSize + " pixel", new PdfStandardFont(PdfFontFamily.Helvetica,12),PdfBrushes.Red, PointF.Empty);
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create new PDF unitconverter.
Dim converter As New PdfUnitConvertor()
'Convert to pixels.
Dim pageSize As SizeF = converter.ConvertToPixels(doc.PageSettings.Size, PdfGraphicsUnit.Point)
page.Graphics.DrawString("PDF page size is " + pageSize + " pixel", New PdfStandardFont(PdfFontFamily.Helvetica, 12), PdfBrushes.Red, PointF.Empty)
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
ConvertToPixels(Single, PdfGraphicsUnit)
Converts the value to pixel from specified graphics unit.
Declaration
public float ConvertToPixels(float value, PdfGraphicsUnit from)
Parameters
Type | Name | Description |
---|---|---|
System.Single | value | Value to convert |
PdfGraphicsUnit | from | Indicates units to convert from |
Returns
Type | Description |
---|---|
System.Single | Value stored in pixels |
Examples
//Create a new PDF document.
PdfDocument doc = new PdfDocument();
//Add a page to the document.
PdfPage page = doc.Pages.Add();
//Create new PDF unitconverter.
PdfUnitConvertor converter = new PdfUnitConvertor();
//Convert to pixels.
float pageWidth = converter.ConvertToPixels(doc.PageSettings.Size.Width, PdfGraphicsUnit.Point);
page.Graphics.DrawString("PDF page width is " + pageWidth + " pixel", new PdfStandardFont(PdfFontFamily.Helvetica,12),PdfBrushes.Red, PointF.Empty);
//Save the document.
doc.Save("Output.pdf");
//Close the document.
doc.Close(true);
'Create a new PDF document.
Dim doc As New PdfDocument()
'Add a page to the document.
Dim page As PdfPage = doc.Pages.Add()
'Create new PDF unitconverter.
Dim converter As New PdfUnitConvertor()
'Convert to pixels.
Dim pageWidth As Single = converter.ConvertToPixels(doc.PageSettings.Size.Width, PdfGraphicsUnit.Point)
page.Graphics.DrawString("PDF page width is " + pageWidth + " pixel", New PdfStandardFont(PdfFontFamily.Helvetica, 12), PdfBrushes.Red, PointF.Empty)
'Save the document.
doc.Save("Output.pdf")
'Close the document.
doc.Close(True)
ConvertUnits(Single, PdfGraphicsUnit, PdfGraphicsUnit)
Converts the value, from one graphics unit to another graphics unit.
Declaration
public float ConvertUnits(float value, PdfGraphicsUnit from, PdfGraphicsUnit to)
Parameters
Type | Name | Description |
---|---|---|
System.Single | value | Value to convert |
PdfGraphicsUnit | from | Indicates units to convert from |
PdfGraphicsUnit | to | Indicates units to convert to |
Returns
Type | Description |
---|---|
System.Single | Value stored in "to" units |