We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Adding a line break to a TextFragment

I am creating a PDF document and manually adding text using a TextFragment and then appending it to a TextBuilder. My question is how do you add a line break to a Text Fragment? I want to be able to break up my string into multiple lines for display.


Here is my code:

Aspose.Pdf.Document pdfApplicationDoc = new Aspose.Pdf.Document();
Aspose.Pdf.Page applicationFirstPage = (Aspose.Pdf.Page)pdfApplicationDoc.Pages.Add();

Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment();
string strTextFrag = “Applicant Name: Joe Smoe”;
textFragment.Text = strTextFrag;
textFragment.Position = new Aspose.Pdf.Text.Position(100, 600);
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(“TimesNewRoman”);
textFragment.TextState.BackgroundColor = System.Drawing.Color.LightGray;
textFragment.TextState.ForegroundColor = System.Drawing.Color.Red;
Aspose.Pdf.Text.TextBuilder textBuilder = new Aspose.Pdf.Text.TextBuilder(applicationFirstPage);
textBuilder.AppendText(textFragment);

Hi Tom,


Thanks for using our products.

I am afraid currently TextFragment class does not support any replaceable symbols (which can be used to add line break). For the sake of correction, , I have logged this requirement in our
issue tracking system under New Features list as PDFNEWNET-34444. We will further investigate this requirement
in details and will keep you updated on the status of a correction.

We
apologize for your inconvenience.

However as a workaround, you may consider creating a separate TextFragment object and specify different position attributes for it. Please take a look over following code snippet.

[C#]

Aspose.Pdf.Document
pdfApplicationDoc = new Aspose.Pdf.Document();<o:p></o:p>

Aspose.Pdf.Page applicationFirstPage = (Aspose.Pdf.Page)pdfApplicationDoc.Pages.Add();

Aspose.Pdf.Text.TextFragment textFragment = new Aspose.Pdf.Text.TextFragment();

string strTextFrag = "Applicant Name:";

// create a new TextFragment object

Aspose.Pdf.Text.TextFragment fragment2 = new TextFragment();

// specify its contents

fragment2.Text= "Joe Smoe";

// set the position/origon of new TextFragment object

fragment2.Position = new Position(100,590);

textFragment.Text = strTextFrag;

textFragment.Position = new Aspose.Pdf.Text.Position(100, 600);

textFragment.TextState.FontSize = 12;

textFragment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman");

textFragment.TextState.BackgroundColor = System.Drawing.Color.LightGray;

textFragment.TextState.ForegroundColor = System.Drawing.Color.Red;

Aspose.Pdf.Text.TextBuilder textBuilder = new Aspose.Pdf.Text.TextBuilder(applicationFirstPage);

textBuilder.AppendText(textFragment);

textBuilder.AppendText(fragment2);

//save document

pdfApplicationDoc.Save("d:/pdftest/MultiLine_Text_output.pdf");


Please take a look over attached PDF document.

Thanks Nayyer! I have a followup question. I am trying to understand the best practice, using the Aspose software, for creating a PDF document on the fly by pulling data from a database in an ASP.NET web application.


Currently, I have created an aspx page to generate the PDF document. I have a method in that page that points to another aspx page on the server, that pulls the data out of the database and generates a web page. I then convert this web page to a PDF and display it in the browser. I have discovered that this is a consumptive process on both the web server and the database when I have multiple web pages being converted into PDF files by many users at once.

My question is - instead of creating a dynamically generated web page and then converting it to a PDF, is there another way to write directly to the PDF from the database which is less consumptive and doesn’t take the extra step of generating the .aspx page then converting that to a PDF?

Thanks,
Tom

Hi Tom,


Yes you are can directly read the contents from database and place them inside PDF document, instead of creating an aspx page and then converting it to PDF format. Please visit the following link for further information on Integrate Table with Database

In case of any further query, please feel free to contact.

Hi Tom,


Thanks for your patience.

We have further investigated the issue reported earlier and as per our observations, TextFragment itself doesn’t support line feed inside the text.

However in order to add text with line feed, please use TextFragment with TextParagraph:

  • use “\r\n” or Environment.NewLine in TextFragment instead of single “\n”;
  • create TextParagraph object. It will add text with line splitting;
  • add the TextFragment with TextParagraph.AppendLine;
  • add the TextParagraph with TextBuilder.AppendParagraph.

Please use complete code snippet:

[C#]

Aspose.Pdf.Document pdfApplicationDoc =
new Aspose.Pdf.Document();<o:p></o:p>

Aspose.Pdf.Page applicationFirstPage =
(Aspose.Pdf.
Page)pdfApplicationDoc.Pages.Add();<o:p></o:p>

<o:p> </o:p>

//initialize
new TextFragment with text containing required newline markers
<o:p></o:p>

Aspose.Pdf.Text.TextFragment
textFragment =
new Aspose.Pdf.Text.TextFragment(“Applicant Name:
"
+ Environment.NewLine + " Joe
Smoe”
);<o:p></o:p>

<o:p> </o:p>

//set
text fragment properties if necessary
<o:p></o:p>

textFragment.TextState.FontSize
= 12;<o:p></o:p>

textFragment.TextState.Font
= Aspose.Pdf.Text.
FontRepository.FindFont(“TimesNewRoman”);<o:p></o:p>

textFragment.TextState.BackgroundColor
= Aspose.Pdf.
Color.LightGray;<o:p></o:p>

textFragment.TextState.ForegroundColor
= Aspose.Pdf.
Color.Red;<o:p></o:p>

<o:p> </o:p>

//create
TextParagraph object
<o:p></o:p>

TextParagraph par = new TextParagraph();<o:p></o:p>

<o:p> </o:p>

//add
new TextFragment to paragraph
<o:p></o:p>

par.AppendLine(textFragment);<o:p></o:p>

<o:p> </o:p>

//set
paragraph position
<o:p></o:p>

par.Position
=
new
Aspose.Pdf.Text.
Position(100, 600);<o:p></o:p>

<o:p> </o:p>

//create
TextBuilder object
<o:p></o:p>

TextBuilder textBuilder = new TextBuilder(applicationFirstPage);<o:p></o:p>

//add
the TextParagraph using TextBuilder
<o:p></o:p>

textBuilder.AppendParagraph(par);<o:p></o:p>

<o:p> </o:p>

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

pdfApplicationDoc.Save(outFile);

I would like to add this multiline text in Table.Cell. How to do that?
Currently, the paragraph requires position but how to determine position inside of Cell? I want to keep the Cell’s ability to position itself, but I want the text inside it to be as in the example above.

More info: I’m building new document, and inside of it I use Table class to display some data. I’m adding text in the sells, and that text could have newlines inside of it. I want to properly see the text with the new lines.

I reproduced the example, and that’s what I want to see, but it needs to be in the cell not on a place predefined with Position.

@niktv

Thanks for your inquiry.

You may add multiple text fragments in a table cell, so that each text fragment will be shown in new line, inside the cell. Following code snippet demonstrate a basic example where I have added two lines text in a header cell of the table. For your reference, an output PDF is also attached.

Document doc = new Document();
var page = doc.Pages.Add();
Table table = new Table();

Row Header = new Row();
Header.Cells.Add("header");
Header.Cells.Add("header");
TextFragment tf = new TextFragment("New Line");
Header.Cells[1].Paragraphs.Add(tf);
table.Rows.Add(Header);
            
Row r = new Row();
r.Cells.Add("Cell");
r.Cells.Add("Cell");
int i = 0;
while (i < 80)
{
 table.Rows.Add(r);
 i++;
}
table.RepeatingRowsCount = 1;
page.Paragraphs.Add(table); 

Table (2).pdf (3.3 KB)

In case of any further assistance, please feel free to contact us.