Adding Objects to a Report Using Inch Coordinates

Tom Puglisi / Wednesday, September 09, 2009

The Infragistics Document Engine has a Site class that allows you add various objects to it by specifying point coordinates. During your application development engagements you may find yourself having to add items using other units such as Inches or Millimeters. This blog post shows you how you would go about adding an Image to a Report's Site object by specifying Inch coordinates.

The Document Engine library has a Utils namespace which contains a Converter class. The converter class has static methods that allow you to convert values to and from various units of measure. The Document Engine ultimately requires that you provide Points when specifying coordinates for your object placement. However, there is no specific method to convert Inches to Points; not a problem, we will use the MillimetersToPoints method. We will just have to convert our Inch values into Millimeters and then pass them to the method.

In C#:

...
using Infragistics.Documents.Report;
using Infragistics.Documents.Report.Section;
using Infragistics.Documents.Utils;
...
            Report theReport = new Report();

            ISection theReportSection = theReport.AddSection();
           
            //These represent inches
            int xInches = 0;
            int yInches = 1;

            //Create an Image object
            Infragistics.Documents.Graphics.Image theImage =
                new Infragistics.Documents.Graphics.Image(
                    Image.FromFile(Application.StartupPath + @"\dunbine.jpg"));

            Infragistics.Documents.Report.ISite theSite = theReportSection.AddSite();

            //used to convert Inches to Millimeters
            const float INCH_2_MIL_FACTOR = 0.0393700787f;

            float xMil = xInches / INCH_2_MIL_FACTOR;
            float yMil = yInches / INCH_2_MIL_FACTOR;


            //Add an image object to a specific location on our Site object.
            theSite.AddImage(
                theImage,
                Converter.MillimetersToPoints(xMil),
                Converter.MillimetersToPoints(yMil));
           
            string theFile = Application.StartupPath + @"\MyDocument.pdf";
           
            //Generate and create the report
            theReport.Publish(theFile, FileFormat.PDF);

            //and launch it
            System.Diagnostics.Process.Start(theFile);