Free Support Forum - aspose.com

Fill Text in TextParagraph-Rectangle and put overlapping part into a new TextParagraph

Hi,

I have a string of text which need to be filled in a TextParagraph element with a particular size (Rectangle). However, it can occur, that the text delivered is too long and the piece of text which is too long should be added in a new TextParagraph right of the first paragraph (see Image).

Code for creating the too long element

//Define the page
Document doc = new Document();
Page page = doc.Pages.Add();
page.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

TextBuilder builder = new TextBuilder(page);

float
    x1 = 40,
    x1End = 40 + 253,
    x2 = (float)page.PageInfo.Width - (40 + 40 + 253),
    x2End = x2 + 253,
    y = 500,
    height1 = 45f * 72f / 25.4f;

TextParagraph paragraph = new TextParagraph
{
    Rectangle = new Aspose.Pdf.Rectangle(x1, y, x1End, y - height1)
};

// Specify word wraping mode
paragraph.FormattingOptions.WrapMode = TextFormattingOptions.WordWrapMode.ByWords;
paragraph.FormattingOptions.LineSpacing = TextFormattingOptions.LineSpacingMode.FontSize;
paragraph.VerticalAlignment = VerticalAlignment.Top;
paragraph.HorizontalAlignment = HorizontalAlignment.Justify;
paragraph.Margin = new MarginInfo(2, 0, 2, 3);

string
    FundDescription = @"Reichmuth Voralpin ist ein aktiv verwalteter defensiver Anlagestrategiefonds mit Referenzwährung CHF, der in traditionelle und nicht-traditionelle Anlagen wie Anleihen, Aktien, Immobilien, Alternativanlagen oder Edelmetall-Anlagen investiert. Ziel ist ein robuster Portfolioaufbau mit ausgewogenenRisikobeiträgen und einer langfristigen Zielrendite von LIBOR + 2% pro Jahr.Der Investment Manager setzt auf einen szenariobasierten, benchmarkunabhängigen Investmentansatz. Gestützt auf eigenes Research und Investment Know-how investiert Reichmuth Voralpin primär in Direktanlagen, opportunistisch ergänzt mit ETFs und Fonds sowie Derivaten zu Absicherungszwecken. Eine dynamische Portfolioallokation und ein überwiegend qualitätsorientierter Selektionsansatz zeichnen Reichmuth Voralpin aus.";

// Create text fragment
TextFragment tf = new TextFragment(FundDescription);
tf.TextState.Font = FontRepository.FindFont("Arial"); ;
tf.TextState.FontSize = 9;
tf.TextState.LineSpacing = 2f;

// Add fragment to paragraph
paragraph.AppendLine(tf);

// Add paragraph
builder.AppendParagraph(paragraph);

doc.Save(dataDir + "TextParagraph.pdf");

Is there a way how I can get the information, that the right margin at the bottom of the rectangle (in this case it’s the word “opportunistisch”) is reached in order to add the remaining text to another paragraph?

Many thanks for you help.

TextParagraph.pdf (44.7 KB)
ThisIsHowTheTextShouldLookLike.PNG (79.3 KB)

@sporty21

Thank you for contacting support.

We are afraid that the API may not detect when right margin at bottom of the rectangle is reached while adding text. Therefore, overflowed text may not be added to another paragraph based on this criteria. However, you can avoid overflow of the text by adding a FloatingBox and a Table wrapper, as in the code snippet below. The text does not overflow in this case, thus not needing any margin detection.

Document document = new Document();
Aspose.Pdf.Page page = document.Pages.Add();
Table table = new Table();
// Specify position of table
table.Left = 40;
table.Top = 500;
table.ColumnWidths = ("253");
string FundDescription = @"Reichmuth Voralpin ist ein aktiv verwalteter defensiver Anlagestrategiefonds mit Referenzwährung CHF, der in traditionelle und nicht-traditionelle Anlagen wie Anleihen, Aktien, Immobilien, Alternativanlagen oder Edelmetall-Anlagen investiert. Ziel ist ein robuster Portfolioaufbau mit ausgewogenenRisikobeiträgen und einer langfristigen Zielrendite von LIBOR + 2% pro Jahr.Der Investment Manager setzt auf einen szenariobasierten, benchmarkunabhängigen Investmentansatz. Gestützt auf eigenes Research und Investment Know-how investiert Reichmuth Voralpin primär in Direktanlagen, opportunistisch ergänzt mit ETFs und Fonds sowie Derivaten zu Absicherungszwecken. Eine dynamische Portfolioallokation und ein überwiegend qualitätsorientierter Selektionsansatz zeichnen Reichmuth Voralpin aus.";
// Create text fragment
TextFragment tf = new TextFragment(FundDescription);
tf.TextState.Font = FontRepository.FindFont("Arial"); ;
tf.TextState.FontSize = 9;
tf.TextState.LineSpacing = 2f;
tf.TextState.HorizontalAlignment = HorizontalAlignment.Justify;

FloatingBox floatingBox = new FloatingBox();
Row row = table.Rows.Add();
Cell cell = row.Cells.Add();
page.Paragraphs.Add(floatingBox);
cell.Paragraphs.Add(tf);
floatingBox.Paragraphs.Add(table);
document.Save(dataDir + "_table_one_cell_width253.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

@Farhan.Raza

Thanks for you answer. The text does not overflow anymore but if the text is longer than usual the tables is getting too big overalapping with other elements. Is there a way how I can procedd automatically in new second colum / cell if a particular number of lines in the current TextFragment is reached?

@sporty21

Thank you for your kind feedback.

We are glad to know that suggested workaround has resolved the problem of text overflow. About overlapping of table, we are afraid that the dimensions may not be checked while adding the text. So, new column or cell may not be added for that case.

However, we have logged an investigation ticket with ID PDFNET-45555 in our issue management system for further investigations. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

@Farhan.Raza

Thank you for opening an investigation ticket.

The API has a page method call GetNotifications() (it’s one of the examples in the Documentation section) in case if there are linebreaks. Is there way how I can get the number of linebreaks in a TextFraggment / TextParagraph?

@sporty21

Would you please share the link for GetNotifications method which you are referring to, so that we may investigate further to help you out.

@Farhan.Raza

Here it is.

https://docs.aspose.com/display/pdfnet/Determine+Line+Break

@sporty21

Thank you for sharing requested information.

We are afraid GetNotifications method may not help in this scenario because it can be used once the text is added on page and document is saved. So the number of lines or line breaks can not be specified during text addition. We will let you know as soon as some significant updates will be available about logged ticket. Please be patient and spare us little time.