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

Free Support Forum - aspose.com

Paragraph / Text concatenation with different formats

I am trying to figure out how I would do this. I have a manually generated PDF that I am creating from a list of records from a Database, and for example, I have a Title, Institution, and Text Field.

I want to be able to have the title be bold and say font size 14, and then on the same line right after and then have an optional reference to the Institutiion on the same line in font size 12 and normal. After that, have the main text body in 12 normal/. I am able to do this where title and institution are on separate lines, but I want them as part of the same line. I have tried in the past creating two text fragments applying different formats to these segments and then concatenating and adding them, but when I do so, I lose the formatting on the second text fragment (it is the same as the first) Instead of page.Paragraphs.Add I am looking for something like page.Paragraphs.Append which will continue to add the text right after the previous add. What I am looking for would produce

This is a Section Title (For DFCI only)
This is the body of the text blah blah blah blah

A snippet of the code which works for what i want but does 3 lines and I want two is

Aspose.Pdf.Text.TextFragment sectTitle = new Aspose.Pdf.Text.TextFragment(rowTitle.ToString());
sectTitle.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
page.Paragraphs.Add(sectTitle);
page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(" (For DFCI only) “));
page.Paragraphs.Add(new Aspose.Pdf.HtmlFragment(rowText.ToString()));
page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(”"));

Thanks

@dlaskey

You can use TextSegment in order to achieve your requirements. For example, please check below example:

Document doc = new Document();
// Add page to pages collection of Document object
Page page = doc.Pages.Add();
            
// Create text fragment
TextFragment fragment1 = new TextFragment("the quick brown fox jumps over the lazy dog");
fragment1.TextState.Font = FontRepository.FindFont("Times New Roman");
fragment1.TextState.FontSize = 12;

// Create text segment
TextSegment fragment2 = new TextSegment("the quick brown fox jumps over the lazy dog");
fragment2.TextState.Font = FontRepository.FindFont("Times New Roman");
fragment2.TextState.FontSize = 14;

fragment1.Segments.Add(fragment2);

page.Paragraphs.Add(fragment1);
            
// Save resulting PDF document.
doc.Save(dataDir + "output.pdf");

Is there a way to do that in the context of a Table Cell related to color?

When I do that to the fragment and then add to a cell, I lose the color formating. I am trying to do this when adding to the Header Table we have been working on earlier.

Example {BOLD, 24 Black} Document Title : {BOLD, 24, Blue} File Number

So the left and including the " : " is black and to the right Blue.

        Aspose.Pdf.Text.TextFragment frag3 = new Aspose.Pdf.Text.TextFragment("DF / HCC Protocol #: ");
        frag3.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Times New Roman");
        frag3.TextState.FontSize = 24;

        // Create text segment
        Aspose.Pdf.Text.TextSegment frag4 = new Aspose.Pdf.Text.TextSegment("[" + ViewState["passedProtNo"].ToString() + "]");
        frag4.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Times New Roman");
        frag4.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue);
        frag4.TextState.FontSize = 24;

        frag3.Segments.Add(frag4);

        Aspose.Pdf.Row hdrrow3 = hdrTable.Rows.Add();
        hdrrow3.Cells.Add(frag3.Text).Alignment = HorizontalAlignment.Center;

It looks like I have to break out the table into multiple cells for each row and then apply the formatting to the cells, so I may have answered my own question

@dlaskey

You can surely try to achieve your requirements using this approach. However, if any issue occurs, please share complete sample code snippet with us so that we can try to replicate the issue in our environment and address it accordingly.