Hi, I’m using Aspose.PDF for .Net to both add images and graphics to my pdfs and I am having some issues.
-
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; }
-
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.