Add images and graphs to PDF using Aspose.PDF for .NET - Calculate coordinates and size

Hi, I’m using Aspose.PDF for .Net to both add images and graphics to my pdfs and I am having some issues.

  1. I have an image with an X and Y coordinate as Centimeters. Now I want to add this image to lets say the 1 page of my PDF document that will always be an A4 size. Now I am having trouble positioning this image because I don’t understand how to convert from centimeters to the correct format for the pdf. Below is an example of what I have tried. It always seems to be a little off. I am also having issues setting the width and height of the image? How to translate Pixels to the correct format?

     private Document AddImage(SomeObject someObject, Document pdfDocument)
             {
                 if (!string.IsNullOrWhiteSpace(someObject.ImageType) &&
                     someObject.ImagePage != null &&
                     someObject.ImageX != null &&
                     someObject.ImageY != null
                 )
                 {
                     string imageFilePath = GetImageFilePath(someObject.ImageType);
                     double imageXPoint = CmToDotsForPdf(someObject.ImageX.Value);
                     double imageYPoint = CmToDotsForPdf(someObject.ImageY.Value);
                     double pageHeight = pdfDocument.Pages[someObject.ImagePage.Value].PageInfo.Height;
    
                     int imageHeightPixels = 300;
                     int imageWidthPixels = 583;
    
                     ImageStamp imageStamp = new ImageStamp(imageFilePath)
                     {
                         Background = false,
                         XIndent = imageXPoint,
                         YIndent = pageHeight - imageYPoint,
                         Height = imageHeightPixels,
                         Width = imageWidthPixels
                     };
    
                     pdfDocument.Pages[someObject.ImagePage.Value].AddStamp(imageStamp);
                 }
                 else
                 {
                     throw new Exception("Could not add image");
                 }
                 return pdfDocument;
             }
    
      private double CmToDotsForPdf(double cm)
             {
                 return cm / 0.3527 * 10;
             }
    
  2. Somewhat related to my first question is about adding graohics to “white out” certain elements on the pdf. In this case, I have the x and y point of the left top corner as millimeter and also the width and height of the box in millimeter. I don’t understand how to convert millimeter to dots and also what should the size of the graph be? I have been testing a little and it seems that the graph has to be bigger than the rectangle in order for it to “fit”?

             float boxTopLeftXmm = float.Parse(boxProperties[0]);
             float boxTopLeftYmm = float.Parse(boxProperties[1]);
             float boxWidthmm = float.Parse(boxProperties[2]);
             float boxHeightmm = float.Parse(boxProperties[3]);
    
             //Divide mm with 10 to get it as cm.
             boxWidthmm = (float)CmToDotsForPdf((double) boxWidthmm/10);
             boxHeightmm = (float) CmToDotsForPdf((double) boxHeightmm/10);
             boxTopLeftXmm = (float)CmToDotsForPdf((double) boxTopLeftXmm/10);
             boxTopLeftYmm = (float) CmToDotsForPdf((double) boxTopLeftYmm/10);
    
             Page pdfPage = pdfDocument.Pages[1];
             // Create Graph instance
             Graph graph = new Graph(boxWidthmm +10, boxHeightmm +10); //Not sure what this actually does.. but it needs to be big enough for the rectangle to fit?
             // Add graph object to paragraphs collection of page instance
             pdfPage.Paragraphs.Add(graph);
             // Create Rectangle instance
             float left = (float)CmToDotsForPdf(boxTopLeftYmm / 10); //This is from the left, higher means more to the left
             float bottom = (float)CmToDotsForPdf(boxTopLeftXmm / 10);//This is Y, higher means the box comes further up
             float width = (float)CmToDotsForPdf(boxWidthmm / 10); //How wide the box should be
             float height = (float)CmToDotsForPdf(boxHeightmm / 10);//How tall the box should be
             Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(boxHeightmm, boxWidthmm, boxWidthmm, boxHeightmm)
             {
                 GraphInfo = { FillColor = Color.Yellow }
             };
             // Specify fill color for Graph object
             // Add rectangle object to shapes collection of Graph object
             graph.Shapes.Add(rect);
    
             pdfDocument.Save(Path.Combine(dir, "test.pdf"));
    

I’m not sure if its DPI related? My pdfs are always 200 DPI.

@Kranton

The basic unit of measurement in PDF is Point where 72 points = 1 inch = 2.54 cm = 25.4 mm. Furthermore, the PDF document follows the coordinate system where (0,0) means lower-left corner. Please take margins into account as well. By default, API created a PDF with A4 size and 72pts margins for each side.

It is suitable to create a graph with dimensions of PDF Page so that every object/item would get fit into it. We hope that the above explanation would help you achieve what you require. In case you face any issue, please share your sample PDF document along with other source files and expected output. We will test the scenario in our environment and address it accordingly.

Hi and thanks for your reply. Yeah I think there is an issue with our margins.

We also have a another issue. First we add our rectangle boxes as yellow. Then we want to add this image. However the white boxes always seem to come on top of the image when we actually want the image on top. We have tried setting the graph for the boxes z index to -1000 but it doesn’t seem to have any effect.

Should you create a new graph for every box we want to draw or is one enough per page? Lets say I want to draw 5 boxes on the first page. Would that be 5 graphs or 1?

@Kranton

Would you kindly share your sample code snippet along with source files? We will test the scenario in our environment and address it accordingly.

Only one graph would be sufficient to add multiple objects on single page. For example, please check this code snippet that adds zig-zag lines in PDF. It is in Java, but you can notice in the code snippet that multiple line fragments are being added in a single graph.