Free Support Forum - aspose.com

How to find out paragraphs vertical position in auto layout mode?

I'm creating a document using Aspose.PDF

After I added some text I need to find out if there is enough space to fit a table, image or another large chunk of text.

Is there a way to find out paragraphs Bottom or Top position when in auto layout mode?

I tried to use Paragraph's Top property but always got -1.

Thank you

Hi,

Thank you for considering Aspose.

Yes, you can check it. Please refer to the following code.

Pdf pdf1 = new Pdf();

Aspose.Pdf.Section sec1 = pdf1.Sections.Add();

Aspose.Pdf.Text t1 = new Text("1st Paragraph Hello world");

t1.PositioningType = PositioningType.PageRelative;

t1.Top = 200;

t1.Left = 100;

t1.RotatingAngle = 30;

sec1.Paragraphs.Add(t1);

Aspose.Pdf.Text t2 = new Text("2ND Paragraph");

t2.PositioningType = PositioningType.PageRelative;

t2.Top = 300;

t2.Left = 100;

t2.RotatingAngle = 60;

sec1.Paragraphs.Add(t1);

foreach (Aspose.Pdf.Section sec in pdf1.Sections)

{

foreach (Aspose.Pdf.Paragraph para in sec.Paragraphs)

{

float i = para.Top;

float j = para.Left;

}

}

pdf1.Save("e:/test.pdf");

Thanks.

Thank you for prompt reply

In your example you used PageRelative positioning. I had a problem with default position type (auto layout). Here is my example, is there any way to make it work?

Pdf pdf = new Pdf();
Section sec = pdf.Sections.Add();
Text para1 = new Text(sec, "First line of text");
sec.Paragraphs.Add(para1);
float top = para1.Top; //Always -1
Text para2 = new Text(sec, "Second line of text");
sec.Paragraphs.Add(para2);
top = para1.Top; //Always -1
pdf.Save("test.pdf");

By the way I tried to calculate the height of a paragraph. GetTextHeight method of Text object requires the width of the paragraph. For some reason I always get -1 when call para1.TextWidth. I think this issue is related to the subject.

Best regards,

Steve

Hi,

I will have to discuss this with the developers. I will let you know soon.

Thanks.

Hi,

It is right to use GetTextHeight method to calculate the height of a paragraph. But it requires users specify the width of the paragraph. Would you please tell us what is your intention to get the text's height so that we can help you better. Thanks.

Best regards.

1. First of all, how can I use "GetTextHeight" if I can't determine paragraph's width. TextWidth property of Text object always returns -1 (in auto layout mode, which I use, I don't use relative positioning) ?

2. I'm writing complex document. After adding some text I need to decide if I have enough space to fit entire table (or image) or I have to split it manually. Unfortunately I can not rely on auto split row by row. The table contains rows which must be displayed together on one page. What I would like to do is to use paragraphs Top property, add to it it's Height to find Bottom edge. Having this number I can have an idea how much white space left on the page. Again I'm using auto layout (just add document objects one by one, without specifying RelativePosition).

Hi,

It is hard to determine paragraph’s width before pdf.Save() is called. You could calculate TextWidth with PageWidth, MarginRight,MarginLeft etc. Sorry for inconvenience.
I think you could set Table.IsBroken = false to resolve your problem. When Table.IsBroken is set to be false, the table will be placed on current page if there is enough space left. Otherwise,if there is not enough space to fit entire table,the entire table will be placed on next page. Please try it and be free to let us know if you have any other problems. Thanks.

Best regards.

Best regards.