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.
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 specifying different position attributes for it.
code snippet
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:";
Aspose.Pdf.Text.TextFragment fragment2 = new TextFragment();
// specify its contents
fragment2.Text = "Joe Smoe";
// set the position/origin 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.
Hi Tom,
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"
orEnvironment.NewLine
inTextFragment
instead of single"\n"
; - create
TextParagraph
object. It will add text with line splitting; - add the
TextFragment
withTextParagraph.AppendLine
; - add the
TextParagraph
withTextBuilder.AppendParagraph
.
Please use complete code snippet:
[C#]
Aspose.Pdf.Document pdfApplicationDoc = new Aspose.Pdf.Document();
Aspose.Pdf.Page applicationFirstPage =
(Aspose.Pdf.Page)pdfApplicationDoc.Pages.Add();
//initialize new TextFragment with text containing required newline markers
Aspose.Pdf.Text.TextFragment
textFragment = new Aspose.Pdf.Text.TextFragment(
"Applicant Name: \r\nJoe \r\nSmoe");
//set text fragment properties if necessary
textFragment.TextState.FontSize = 12;
textFragment.TextState.Font =
Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman");
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.LightGray;
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.Red;
//create TextParagraph object
TextParagraph par = new TextParagraph();
//add new TextFragment to paragraph
par.AppendLine(textFragment);
//set paragraph position
par.Position = new Aspose.Pdf.Text.Position(100, 600);
//create TextBuilder object
TextBuilder textBuilder = new TextBuilder(applicationFirstPage);
//add the TextParagraph using TextBuilder
textBuilder.AppendParagraph(par);
//save output file
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.
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.