Positioning shapes on a page

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I’m having trouble positioning shapes, such as lines and rectangles on a page.

I understand drawing “inside” the graph with origin (0,0) in the lower left cornor.

But where is the graph itself positioned on the page, by “just” adding it to the paragraphs collection?

OR is there another “smarter” way to draw shapes on a page where one could draw relative to the pages coordinates?

What I’m seeking is a solution to draw an X over the entire page (line from upper left corner to lower right + line from upper right corner to lower left)

When doing the first line with the following code I get the attached result:

Document pDoc = new Document();

Page pg = pDoc.Pages.Add();

float pw = (float)pg.Rect.Width;

float ph = (float)pg.Rect.Height;

Graph graph = new Graph(pw, ph);

Line line = new Line(new float[] { 0, 0, pw, ph });

graph.Shapes.Add(line);

pg.Paragraphs.Add(graph);

pDoc.Save(@"c:\test1.pdf");

Best regards,

Jørgen Dam

Hi Jørgen,


Thanks for contacting support.

The Graph object is instantiated while providing Width and Height information and you can pass page Width and Height information as arguments. However concerning to drawing lines, you can use following code snippet to draw a cross starting from Left-Bottom to Right-Upper corner and Left-Top corner to Bottom-Right corner. Please take a look over following code snippet.

[C#]

// create Document instance<o:p></o:p>

Document pDoc = new
Document();<o:p></o:p>

// add page to pages collection of PDF
document
<o:p></o:p>

Page pg = pDoc.Pages.Add();<o:p></o:p>

// set page margin on all sides as 0<o:p></o:p>

pg.PageInfo.Margin.Left = pg.PageInfo.Margin.Right
= pg.PageInfo.Margin.Bottom = pg.PageInfo.Margin.Top = 0;<o:p></o:p>

// create Graph object with Width and
Height equal to page dimensions
<o:p></o:p>

Aspose.Pdf.Drawing.Graph
graph = new Aspose.Pdf.Drawing.Graph((float)pg.PageInfo.Width
, (float)pg.PageInfo.Height);<o:p></o:p>

// create first line object starting
from Lower-Left to Top-Right corner of page
<o:p></o:p>

Aspose.Pdf.Drawing.Line
line = new Aspose.Pdf.Drawing.Line(new float[] { (float)pg.Rect.LLX,
0, (float)pg.PageInfo.Width, (float)pg.Rect.URY });<o:p></o:p>

// add line to shapes collection of
Graph object
<o:p></o:p>

graph.Shapes.Add(line);<o:p></o:p>

// draw line from Top-Left corner of
page to Bottom-Right corner of page
<o:p></o:p>

Aspose.Pdf.Drawing.Line
line2 = new Aspose.Pdf.Drawing.Line(new float[] { 0, (float)pg.Rect.URY,
(float)pg.PageInfo.Width, (float)pg.Rect.LLX });<o:p></o:p>

// add line to shapes collection of
Graph object
<o:p></o:p>

graph.Shapes.Add(line2);<o:p></o:p>

// add Graph object to paragraphs collection of page<o:p></o:p>

pg.Paragraphs.Add(graph);<o:p></o:p>

// save resultant file<o:p></o:p>

pDoc.Save(@“c:\pdftest\GraphLine_test1.pdf”);

Thanks, it seems to work, but I'm not quite sure why.

why not (from the your example):
Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { (0,, 0, (float)pg.PageInfo.Width, (float)pg.PageInfo.Height });

Is there some documentation for the page and margins coordinates in relation to eachother?

For exapmle:
What is the difference between pg.PageInfo.Width and pg.Rect.Width ?
What is the difference between pg.PageInfo.Width and pg.Rect.URX ?
When can pg.Rect.LLX and pg.Rect.LLY be other than zero?
When is pg.PageInfo.Width <> pg.Rect.URX and pg.PageInfo.Height <> pg.Rect.URY?



Best regards,
Joergen.

jdk-1:
Thanks, it seems to work, but I’m not quite sure why.

why not (from the your example):
Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { (0,, 0, (float)pg.PageInfo.Width, (float)pg.PageInfo.Height });<o:p></o:p>

Is there some documentation for the page and margins coordinates in relation to eachother?
Hi Joergen,

Thanks for contacting support.

A page has main body area and a margin information associated with it. However you may consider visiting following links for required information and they explain how to get page dimensions and related information for particular page in PDF file
jdk-1:
What is the difference between pg.PageInfo.Width and pg.Rect.Width ?
There is no difference when using both properties. In fact we prodived Width and Height properties for Rect object, so that when a customer is using Rect instance for Page object, he can get leverage of these properties and get page Width and Height information.
jdk-1:
What is the difference between pg.PageInfo.Width and pg.Rect.URX ?
Both properties return same values.
jdk-1:
When can pg.Rect.LLX and pg.Rect.LLY be other than zero?
The page dimensions are calculated from 0 origin. However while reading properties, you can set value other than 0 and further manipulation will be performed accordingly.
jdk-1:
When is pg.PageInfo.Width <> pg.Rect.URX and pg.PageInfo.Height <> pg.Rect.URY?
As explained above, both properties return same values.

Hello!

I’m trying the snippet provided above but the X is not quite centered on the document. And I’m seeing the same behavior for any kind of shape I add to the document.

It kind of leaves a space on the X,Y coordinates. I’m attaching an image of the result. Code is the same as the snippet provided.aspose-x-drawing-20210217.png (12.9 KB)

Is anything else missing to actually place the shapes on a 0,0 coordinate, for example?

Best regards,
Rolando

@rmed1na

It seems like you are not specifying the margins for PDF Page so the shape is being added with respect to default margins i.e. 72pt for all sides. Please specify the margins as zero for all sides in Page.PageInfo and add the shape again. Please feel free to let us know in case you still face any issue.

Yeap, I already specified the margins as well but that’s the way it looks. Below a snippet of the code:

        var page = document.Pages.Add();
        page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

        Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
        Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { (float)page.Rect.LLX,0, (float)page.PageInfo.Width, (float)page.Rect.URY });

        graph.Shapes.Add(line);

        Aspose.Pdf.Drawing.Line line2 = new Aspose.Pdf.Drawing.Line(new float[] { 0, (float)page.Rect.URY,(float)page.PageInfo.Width, (float)page.Rect.LLX });

        graph.Shapes.Add(line2);
        page.Paragraphs.Add(graph);

Result: aspose-x-drawing-20210218.png (9.3 KB)

@rmed1na

Please specify the Margins for Graph as well:

Document document = new Document();
var page = document.Pages.Add();
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
graph.Margin = new MarginInfo(0, 0, 0, 0);
Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { (float)page.Rect.LLX, 0, (float)page.PageInfo.Width, (float)page.Rect.URY });

graph.Shapes.Add(line);

Aspose.Pdf.Drawing.Line line2 = new Aspose.Pdf.Drawing.Line(new float[] { 0, (float)page.Rect.URY, (float)page.PageInfo.Width, (float)page.Rect.LLX });

graph.Shapes.Add(line2);
page.Paragraphs.Add(graph);
document.Save(dataDir + "Line.pdf");

Line.pdf (1.6 KB)

1 Like

It works for me on new documents or documents in which I’m creating a new page to put the graph. However for existing documents where I need to add a graph in an existing page it doesn’t position the elements correctly.

Here’s my sample code:

var document = new Document(pdfPath);
    var page = document.Pages[1];
                page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

                Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)page.PageInfo.Width, (float)page.PageInfo.Height);
                graph.Margin = new MarginInfo(0, 0, 0, 0);
                Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { (float)page.Rect.LLX, 0, (float)page.PageInfo.Width, (float)page.Rect.URY });

                graph.Shapes.Add(line);

                Aspose.Pdf.Drawing.Line line2 = new Aspose.Pdf.Drawing.Line(new float[] { 0, (float)page.Rect.URY, (float)page.PageInfo.Width, (float)page.Rect.LLX });

                graph.Shapes.Add(line2);
                page.Paragraphs.Add(graph);
                document.Save(@"C:\Users\rolan\source\repos\Personal\Demos\AsposePdfDemo\AsposePdfDemo\" + "Line.pdf");

I’m attaching also the source document (aspose-prev-doc) used for the operation. It’s a simple document made from Microsoft Word as a PDF. And also the result (Line.pdf)

aspose-prev-doc.pdf (38.0 KB)

Line.pdf (70.4 KB)
aspose-line-result.png (11.4 KB)

@rmed1na

In order to get correct page height/width in existing PDF, please use Page.Rect.Height and Page.Rect.Width instead of Page.PageInfo.Height/Width. The Page.PageInfo Class is used for PDF generation purpose only. Please check following code snippet to achieve correct output:

Document document = new Document(dataDir + "aspose-prev-doc.pdf");
var page = document.Pages[1];
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

Aspose.Pdf.Drawing.Graph graph = new Aspose.Pdf.Drawing.Graph((float)page.Rect.Width, (float)page.Rect.Height);
graph.Margin = new MarginInfo(0, 0, 0, 0);
Aspose.Pdf.Drawing.Line line = new Aspose.Pdf.Drawing.Line(new float[] { (float)page.Rect.LLX, 0, (float)page.Rect.Width, (float)page.Rect.URY });

graph.Shapes.Add(line);

Aspose.Pdf.Drawing.Line line2 = new Aspose.Pdf.Drawing.Line(new float[] { 0, (float)page.Rect.URY, (float)page.Rect.Width, (float)page.Rect.LLX });

graph.Shapes.Add(line2);
page.Paragraphs.Add(graph);
document.Save(dataDir + "Line.pdf");

Line.pdf (37.0 KB)