Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

itextsharp convert points to cm

Tags:

c#

itextsharp

I am using iTextsharp - a java pdf lib - to generate pdfs dynamically. Now as I understood it, measurements are specified in points. I know where to place what on the place in cm. so I need the conversion: points <-> cm

like image 676
Mina N Avatar asked Jan 29 '13 23:01

Mina N


1 Answers

iText(Sharp) has a Utilities class (package com.itextpdf.text / namespace iTextSharp.text) which contains several static conversion methods, in particular:

// iText
public static final float millimetersToPoints(final float value);
public static final float millimetersToInches(final float value);
public static final float pointsToMillimeters(final float value);
public static final float pointsToInches(final float value);
public static final float inchesToMillimeters(final float value);
public static final float inchesToPoints(final float value);

// iTextSharp
public static float MillimetersToPoints(float value);
public static float MillimetersToInches(float value);
public static float PointsToMillimeters(float value);
public static float PointsToInches(float value);
public static float InchesToMillimeters(float value);
public static float InchesToPoints(float value);

Your assumption that measurements are specified in points is only partially correct but in your use case to generate pdfs dynamically that's good enough.

In general, though, measurements are specified in user space units, and the default user space unit (i.e. before any transformations are in place) can be configured on a per-page basis to be any float multiple (subject to implementation specific limitations) of 1/72 inch:

UserUnit number (Optional; PDF 1.6) A positive number that shall give the size of default user space units, in multiples of 1⁄72 inch. The range of supported values shall be implementation-dependent.

Default value: 1.0 (user space unit is 1⁄72 inch).

(Table 30, section 7.7.3.3, ISO 32000-1)

Due to that default your assumption holds if nothing else is selected.

like image 195
mkl Avatar answered Sep 19 '22 10:09

mkl