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:
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>
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(newfloat[] { (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
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.
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?
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.
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)
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");