Free Support Forum - aspose.com

Append multiple text objects on the same line

Hi support,

As I’ve concluded with “codewarior” : “appending text objects on same line is not supported in Aspose.Pdf JAVA version” .

My question is if there is any workaround. It will be very helpful.

Thank you,
Milan Cutlac

Hello Milan,

During our conversation session I mentioned that Segment.InLineParagraph property is only in .Net version not in JAVA. In order to add text objects over the same line, simply create segments and add them to the Segments collection of Text. Please try the following code snippet.

//Instantiate Pdf instance by calling its empty constructor
Pdf pdf1 = new Pdf();
//Create a new section in the Pdf object
Section sec1 = pdf1.getSections().add();
//Add 1st paragraph (inheriting the text format settings from the section)
sec1.getParagraphs().add(new Text(sec1, "paragraph 1 "));

//Create 2nd paragraph
Text t3 = new Text(sec1);
//Create a segment "seg1" in the paragraph "t3"
Segment seg1 = new Segment(t3);
//Assign some content to the segment
seg1.setContent("paragraph 2 (segment 1)");
//Add segment (with red text color) to the paragraph
t3.getSegments().add(seg1);

//Create a new segment "seg2" in the paragraph "t3"
Segment seg2 = new Segment(t3);
//Assign some content to the segment
seg2.setContent(" paragraph 2 (segment 2)");
//Add segment (with green text color) to the paragraph
t3.getSegments().add(seg2);

//Add 2nd text paragraph to the section
sec1.getParagraphs().add(t3);

pdf1.Save("TextObjectsOverSameLine.pdf");

The output generated through this code is in attachment, kindly take a review. Incase of any further query please feel free to contact.

Hi Codewarior,

We can use your example to clarify my issue : in my scenario, when seg2 is created, t3 is already added to the paragraph collection. Still, I want that seg2 content to be on the same line with seg1 content. But the API doesn’t allow me to extract the text object from current paragraph. I don’t want to use the same t3 object, but a new text object that is the result of extracting text from current paragraph. If this would be possible, I could easily add seg2 to my text object and the problem would be solved, because “my text object” wouldn’t have to be added to paragraph collection, because is already there.

I attach some code to be more explicit :

Text txt = new Text(sec1);
Segment seg1 = new Segment(txt, “txt 1”);
seg1.getTextInfo().setTextColor(Color.Red);
txt.getSegments().add(seg1);
Segment seg2 = new Segment(txt, “txt 2”);
txt.getSegments().add(seg2);
txt.getTextInfo().setFontName(“Algerian”);

sec1.getParagraphs().add(txt);

//here I don’t want to use the “txt” object; the command in the quote marks is what I need to solve my issue, but couldn’t find

Text myTxt = “sec1.getLastParagraph().getTextObject()”;
Segment seg3 = new Segment(myTxt, “txt 3”);
myTxt.getSegments().add(seg3);
seg3.getTextInfo().setFontName(“Cambria”);

//…So I know that the last part could be easily replaced by:
Segment seg3 = new Segment(txt, “txt 3”);
txt.getSegments().add(seg3);
seg3.getTextInfo().setFontName(“Cambria”);

Regards,
Milan

Hello milan,

If I don’t misunderstand what you mean, what you want is find a way to get
a Text object from the Section. In your code:

Text myTxt = “sec1.getLastParagraph().getTextObject()”;

I think you can use the follow code to get the effect you want:

int size = sec1.getParagraphs().size();
Text t = (Text)sec1.getParagraphs().get(size -1);
t.getSegments().add(“segment you want”);

You can also use the similar method( Text.getSegments() ) the retrieve the segment you want in a Text object.

Hopefully this will help, Thank you for using our product.