Hi,
I was trying to display vertical text by setting Text.RotatingAngle property to 90. Great, I got exactly what I wanted.
Then I tried to do the same thing but with a text that is made of multiple segments (I need to apply different fonts on different parts of the text) and ouch! The result is,… suprising.
It seems like my text is cut is multiple fragments and that the rotating angle is applied to each fragment. This results, for vertical text, to having multiple columns. In addition, number of fragments is higher than the number of segments.
Let me show you with a little example :
//Create Pdf
Pdf pdf1 = new Pdf();
//Create section
Section sec1 = pdf1.Sections.Add();
//Create text
Text text1 = new Text(sec1, “Vertical text test”);
//Add a segment
Segment seg2 = new Segment(text1);
seg2.Content = “with a second segment”;
text1.Segments.Add(seg2);
//Set rotating angle to vertical
text1.RotatingAngle = 90;
sec1.Paragraphs.Add(text1);
return (pdf1);
Here we have 2 segments but the output pdf document show four columns of text (see atached pdf file).
I tried creating an empty text and adding segments one by one, or set RotatingAngle before adding any segment, same result.
Of course I could use a floating box with VerticalTextRotationAngle which works well but this makes positioning tricky as I need to fix the bottom position regardless of the text length.
Anyway, I use this as a workaround but I would love to see it work without this trick.
Thanks,
Erwan
ps: off topic : can sdy tell me how to post formatted code with colors and everything ? Thanks.
Hi Erwan,
Thanks for considering Aspose products.
I strongly recommend you to use floating box with VerticalTextRotationAngle to meet your requirement. At present, vertical box with multi-segments is not supported well. However, I think this could be supported in about 1-2 weeks. And it allows you to customize the floating box to control how the text looks (like position, height, width, alignment type etc).
And would you please provide us with the sample code with which we can reproduce the positioning tricky when you use floating box?
PS: I will ask our customer supporter Nayyer to help you post formatted code.
Thanks.
Best regards.
Hello Erwan,
In order for the text to preserver the text formatting, please copy the code snippet from VisualStudio IDE and paste it into Text area of the forum post or you can use the option the option present over this text area to insert code snippet (notice the VisualStudio .Net icon present over the text area when writing a forum post).
You may also use the formatting options specified over the text area to accomplish your requirement, as shown in the image below.
Hans,
Thanks for this quick reply, when I said using floating box made positioning tricky I refered to the fact that when I set the top position of a Text object using page relative positioning, the bottom of my text stood still no mater how long the text was. This was convenient since the length of this text is not fixed and has the botom of it has to remain at the same spot.
I understand this is because the Top attribute refers to the top position of the Text object when no rotating angle is applied. That makes sense since the vertical position of an horizontal text does not change according to its length (except for line breaks).
When I use a floating box and set the Top attribute, I need to set it (as well as the box's height)according to the length of my text so I can keep the same baseline, no matter how long it is.
//Define and instantiate a Floating box
FloatingBox box1 = new FloatingBox(1, verticalText.GetLength(currentPdf));
//Set the Positioning and Alignment of Floating Box
box1.BoxHorizontalPositioning = BoxHorizontalPositioningType.Page;
box1.BoxHorizontalAlignment = BoxHorizontalAlignmentType.None;
box1.BoxVerticalPositioning = BoxVerticalPositioningType.Page;
box1.BoxVerticalAlignment = BoxVerticalAlignmentType.None;
box1.VerticalTextRotationAngle = VerticalTextRotationType.AntiClockWise;
box1.Paragraphs.Add(verticalText);
box1.SetAbsolutePosition(575f, 800f - (verticalText.GetLength(currentPdf)));
It's as simple as that, but adding a floating box plus this positioning trick adds complexity that I though may be useless if using a simple text object did it.
Nayyer, concerning code formatting in this forum, for some reason I don't have the same taskbar (see attached image), anyway, copy paste from VS did it, I don't know why it did not before.
Hi,
Thanks for the detail info. Here is a sample code for your reference. Please try it and feel free to ask further questions if you have any other problems.
Pdf pdf1 = new Pdf();
//Create section
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();
//Create text
Text text1 = new Text(sec1, "Vertical text test");
Segment seg2 = new Segment(text1);
seg2.Content = "with a second segment";
text1.Segments.Add(seg2);
sec1.Paragraphs.Add(text1);
Text text2 = new Text("verticalText yo be added and rotated");
Text text3 = new Text("text3: verticalText yo be added and rotated");
text3.TextInfo.FontSize = 15F;
//Define and instantiate a Floating box
FloatingBox box1 = new FloatingBox(100, 200); //Initial Width and Height
//Set the Positioning and Alignment of Floating Box
box1.BoxHorizontalPositioning = BoxHorizontalPositioningType.Page;
box1.Left = 100; //Horizontal distance apart from Left page margin
box1.BoxVerticalPositioning = BoxVerticalPositioningType.Page;
box1.Top = 200; //Vertical distance apart from Top page margin
box1.BackgroundColor = new Aspose.Pdf.Color(255, 0, 0); //Box BackgroundColor
box1.Border = new BorderInfo((int)BorderSide.All, 1.5F, new Aspose.Pdf.Color("Blue")); //Box border
box1.VerticalTextRotationAngle = VerticalTextRotationType.AntiClockWise; //Text rotationAngle(+-90)
//Add texts to box
box1.Paragraphs.Add(text1);
box1.Paragraphs.Add(text2);
box1.Paragraphs.Add(text3);
//Set text alignmentType. As contents in box are rotated. The meaning of Horizontal and Vertical Alignment are reversed.
text1.TextInfo.Alignment = AlignmentType.Right; //Vertical alignmentType,Here we consider adding a new attribute: FloatingBox.TextHorizontalAlignment for convenience
text2.TextInfo.Alignment = AlignmentType.Right;
text3.TextInfo.Alignment = AlignmentType.Right;
box1.TextVerticalAlignment = VerticalAlignmentType.Top; //Horizontaol alignmentType
sec1.Paragraphs.Add(box1);
pdf1.Save(@"E:/temp/tests.pdf");
PS: The resultant pdf file is attached.