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,
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.
apologize for your inconvenience.
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.
Hi Tom,
Hi Tom,
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.
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.