com.aspose.words

Class ConvertUtil

  • java.lang.Object
    • com.aspose.words.ConvertUtil
public abstract class ConvertUtil 
extends java.lang.Object

Provides helper functions to convert between various measurement units.

Example:

Shows how to specify page properties in inches.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

PageSetup pageSetup = builder.getPageSetup();
pageSetup.setTopMargin(ConvertUtil.inchToPoint(1.0));
pageSetup.setBottomMargin(ConvertUtil.inchToPoint(1.0));
pageSetup.setLeftMargin(ConvertUtil.inchToPoint(1.5));
pageSetup.setRightMargin(ConvertUtil.inchToPoint(1.5));
pageSetup.setHeaderDistance(ConvertUtil.inchToPoint(0.2));
pageSetup.setFooterDistance(ConvertUtil.inchToPoint(0.2));

Example:

Specifies paper size, orientation, margins and other settings for a section.
DocumentBuilder builder = new DocumentBuilder();

PageSetup ps = builder.getPageSetup();
ps.setPaperSize(PaperSize.LEGAL);
ps.setOrientation(Orientation.LANDSCAPE);
ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
ps.setLeftMargin(ConvertUtil.inchToPoint(1.5));
ps.setRightMargin(ConvertUtil.inchToPoint(1.5));
ps.setHeaderDistance(ConvertUtil.inchToPoint(0.2));
ps.setFooterDistance(ConvertUtil.inchToPoint(0.2));

builder.writeln("Hello world.");

builder.getDocument().save(getMyDir() + "\\Artifacts\\PageSetup.PageMargins.doc");

Method Summary
static doubleinchToPoint(double inches)
Converts inches to points.
static doublemillimeterToPoint(double millimeters)
Converts millimeters to points.
static intpixelToNewDpi(double pixels, double oldDpi, double newDpi)
Converts pixels from one resolution to another.
static doublepixelToPoint(double pixels)
Converts pixels to points at 96 dpi.
static doublepixelToPoint(double pixels, double resolution)
Converts pixels to points at the specified pixel resolution.
static doublepointToInch(double points)
Converts points to inches.
static doublepointToPixel(double points)
Converts points to pixels at 96 dpi.
static doublepointToPixel(double points, double resolution)
Converts points to pixels at the specified pixel resolution.
 

    • Method Detail

      • inchToPoint

        public static double inchToPoint(double inches)
        Converts inches to points. 1 inch equals 72 points.
        Parameters:
        inches - The value to convert.

        Example:

        Specifies paper size, orientation, margins and other settings for a section.
        DocumentBuilder builder = new DocumentBuilder();
        
        PageSetup ps = builder.getPageSetup();
        ps.setPaperSize(PaperSize.LEGAL);
        ps.setOrientation(Orientation.LANDSCAPE);
        ps.setTopMargin(ConvertUtil.inchToPoint(1.0));
        ps.setBottomMargin(ConvertUtil.inchToPoint(1.0));
        ps.setLeftMargin(ConvertUtil.inchToPoint(1.5));
        ps.setRightMargin(ConvertUtil.inchToPoint(1.5));
        ps.setHeaderDistance(ConvertUtil.inchToPoint(0.2));
        ps.setFooterDistance(ConvertUtil.inchToPoint(0.2));
        
        builder.writeln("Hello world.");
        
        builder.getDocument().save(getMyDir() + "\\Artifacts\\PageSetup.PageMargins.doc");
      • millimeterToPoint

        public static double millimeterToPoint(double millimeters)
        Converts millimeters to points. 1 inch equals 25.4 millimeters. 1 inch equals 72 points.
        Parameters:
        millimeters - The value to convert.

        Example:

        Shows how to specify page properties in millimeters.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        PageSetup pageSetup = builder.getPageSetup();
        pageSetup.setTopMargin(ConvertUtil.millimeterToPoint(25.0));
        pageSetup.setBottomMargin(ConvertUtil.millimeterToPoint(25.0));
        pageSetup.setLeftMargin(ConvertUtil.millimeterToPoint(37.5));
        pageSetup.setRightMargin(ConvertUtil.millimeterToPoint(37.5));
        pageSetup.setHeaderDistance(ConvertUtil.millimeterToPoint(5.0));
        pageSetup.setFooterDistance(ConvertUtil.millimeterToPoint(5.0));
        
        builder.writeln("Hello world.");
        builder.getDocument().save(getMyDir() + "\\Artifacts\\PageSetup.PageMargins.doc");
      • pixelToNewDpi

        public static int pixelToNewDpi(double pixels, double oldDpi, double newDpi)
        Converts pixels from one resolution to another.
        Parameters:
        pixels - The value to convert.
        oldDpi - The current dpi (dots per inch) resolution.
        newDpi - The new dpi (dots per inch) resolution.

        Example:

        Shows how to check how an amount of pixels changes when the dpi is changed.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        PageSetup pageSetup = builder.getPageSetup();
        pageSetup.setTopMargin(72);
        double oldDpi = 92.0;
        double newDpi = 192.0;
        
        System.out.println(MessageFormat.format("{0} pixels at {1} dpi becomes {2} pixels at {3} dpi.", pageSetup.getTopMargin(), oldDpi, ConvertUtil.pixelToNewDpi(pageSetup.getTopMargin(), oldDpi, newDpi), newDpi));
      • pixelToPoint

        public static double pixelToPoint(double pixels)
        Converts pixels to points at 96 dpi. 1 inch equals 72 points.
        Parameters:
        pixels - The value to convert.

        Example:

        Shows how to specify page properties in pixels with default and custom resolution.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        PageSetup pageSetupNoDpi = builder.getPageSetup();
        pageSetupNoDpi.setTopMargin(ConvertUtil.pixelToPoint(100.0));
        pageSetupNoDpi.setBottomMargin(ConvertUtil.pixelToPoint(100.0));
        pageSetupNoDpi.setLeftMargin(ConvertUtil.pixelToPoint(150.0));
        pageSetupNoDpi.setRightMargin(ConvertUtil.pixelToPoint(150.0));
        pageSetupNoDpi.setHeaderDistance(ConvertUtil.pixelToPoint(20.0));
        pageSetupNoDpi.setFooterDistance(ConvertUtil.pixelToPoint(20.0));
        
        builder.writeln("Hello world.");
        builder.getDocument().save(getMyDir() + "\\Artifacts\\PageSetup.PageMargins.DefaultResolution.doc");
        
        double myDpi = 150.0;
        
        PageSetup pageSetupWithDpi = builder.getPageSetup();
        pageSetupWithDpi.setTopMargin(ConvertUtil.pixelToPoint(100.0, myDpi));
        pageSetupWithDpi.setBottomMargin(ConvertUtil.pixelToPoint(100.0, myDpi));
        pageSetupWithDpi.setLeftMargin(ConvertUtil.pixelToPoint(150.0, myDpi));
        pageSetupWithDpi.setRightMargin(ConvertUtil.pixelToPoint(150.0, myDpi));
        pageSetupWithDpi.setHeaderDistance(ConvertUtil.pixelToPoint(20.0, myDpi));
        pageSetupWithDpi.setFooterDistance(ConvertUtil.pixelToPoint(20.0, myDpi));
        
        builder.getDocument().save(getMyDir() + "\\Artifacts\\PageSetup.PageMargins.CustomResolution.doc");
      • pixelToPoint

        public static double pixelToPoint(double pixels, double resolution)
        Converts pixels to points at the specified pixel resolution. 1 inch equals 72 points.
        Parameters:
        pixels - The value to convert.
        resolution - The dpi (dots per inch) resolution.

        Example:

        Shows how to specify page properties in pixels with default and custom resolution.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        PageSetup pageSetupNoDpi = builder.getPageSetup();
        pageSetupNoDpi.setTopMargin(ConvertUtil.pixelToPoint(100.0));
        pageSetupNoDpi.setBottomMargin(ConvertUtil.pixelToPoint(100.0));
        pageSetupNoDpi.setLeftMargin(ConvertUtil.pixelToPoint(150.0));
        pageSetupNoDpi.setRightMargin(ConvertUtil.pixelToPoint(150.0));
        pageSetupNoDpi.setHeaderDistance(ConvertUtil.pixelToPoint(20.0));
        pageSetupNoDpi.setFooterDistance(ConvertUtil.pixelToPoint(20.0));
        
        builder.writeln("Hello world.");
        builder.getDocument().save(getMyDir() + "\\Artifacts\\PageSetup.PageMargins.DefaultResolution.doc");
        
        double myDpi = 150.0;
        
        PageSetup pageSetupWithDpi = builder.getPageSetup();
        pageSetupWithDpi.setTopMargin(ConvertUtil.pixelToPoint(100.0, myDpi));
        pageSetupWithDpi.setBottomMargin(ConvertUtil.pixelToPoint(100.0, myDpi));
        pageSetupWithDpi.setLeftMargin(ConvertUtil.pixelToPoint(150.0, myDpi));
        pageSetupWithDpi.setRightMargin(ConvertUtil.pixelToPoint(150.0, myDpi));
        pageSetupWithDpi.setHeaderDistance(ConvertUtil.pixelToPoint(20.0, myDpi));
        pageSetupWithDpi.setFooterDistance(ConvertUtil.pixelToPoint(20.0, myDpi));
        
        builder.getDocument().save(getMyDir() + "\\Artifacts\\PageSetup.PageMargins.CustomResolution.doc");
      • pointToInch

        public static double pointToInch(double points)
        Converts points to inches. 1 inch equals 72 points.
        Parameters:
        points - The value to convert.

        Example:

        Shows how to convert points to inches.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        PageSetup pageSetup = builder.getPageSetup();
        pageSetup.setTopMargin(ConvertUtil.inchToPoint(2.0));
        
        System.out.println(MessageFormat.format("The size of my top margin is {0} points, or {1} inches.", pageSetup.getTopMargin(), ConvertUtil.pointToInch(pageSetup.getTopMargin())));
      • pointToPixel

        public static double pointToPixel(double points)
        Converts points to pixels at 96 dpi. 1 inch equals 72 points.
        Parameters:
        points - The value to convert.

        Example:

        Shows how to use convert points to pixels with default and custom resolution.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        PageSetup pageSetup = builder.getPageSetup();
        pageSetup.setTopMargin(ConvertUtil.inchToPoint(2.0));
        
        double myDpi = 192.0;
        
        System.out.println(MessageFormat.format("The size of my top margin is {0} points, or {1} pixels with default resolution.", pageSetup.getTopMargin(), ConvertUtil.pointToPixel(pageSetup.getTopMargin())));
        
        System.out.println(MessageFormat.format("The size of my top margin is {0} points, or {1} pixels with custom resolution.", pageSetup.getTopMargin(), ConvertUtil.pointToPixel(pageSetup.getTopMargin(), myDpi)));
      • pointToPixel

        public static double pointToPixel(double points, double resolution)
        Converts points to pixels at the specified pixel resolution. 1 inch equals 72 points.
        Parameters:
        points - The value to convert.
        resolution - The dpi (dots per inch) resolution.

        Example:

        Shows how to use convert points to pixels with default and custom resolution.
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);
        
        PageSetup pageSetup = builder.getPageSetup();
        pageSetup.setTopMargin(ConvertUtil.inchToPoint(2.0));
        
        double myDpi = 192.0;
        
        System.out.println(MessageFormat.format("The size of my top margin is {0} points, or {1} pixels with default resolution.", pageSetup.getTopMargin(), ConvertUtil.pointToPixel(pageSetup.getTopMargin())));
        
        System.out.println(MessageFormat.format("The size of my top margin is {0} points, or {1} pixels with custom resolution.", pageSetup.getTopMargin(), ConvertUtil.pointToPixel(pageSetup.getTopMargin(), myDpi)));