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

Free Support Forum - aspose.com

TextParagraph set origin of rotation

Hi guys,

I wonder if it’s possible to set the point of origin when rotating a text fragment?

Here is my code:

                TextBuilder textBuilder = new TextBuilder(page);
                var list = new System.Collections.Generic.List<Field>();

                int[] angles = { 0, 90, 180, 270 };

                foreach (var i in angles)
                {
                    var field1 = new Field
                    {
                        Text = i + " degrees",
                        Rotation = i,
                        LocationX = 220,
                        LocationY = 150,
                        ActualFontSizeF = 12,
                        ActualHeight = 26,
                        ActualWidth = 120
                    };
                    list.Add(field1);
                }

                foreach (Field field in list)
                {
                    // Control font size is in pixel. Multiplier is an estimate to convert pixel to float. 
                    var fontSize = (float)(field.ActualFontSizeF * 0.755);

                    // Create text fragment
                    var textFragment = new TextFragment(field.Text);
                    textFragment.TextState.Font = FontRepository.FindFont("Verdana");
                    textFragment.TextState.FontSize = fontSize;
                    textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);

                    // create text paragraph
                    TextParagraph paragraph = new TextParagraph();
                    // specify the location to add TextParagraph
                    paragraph.Rectangle = new Aspose.Pdf.Rectangle(
                        Aspose.Words.ConvertUtil.PixelToPoint(field.LocationX + adjustment, b.HorizontalResolution),
                        (pageHeight - Aspose.Words.ConvertUtil.PixelToPoint(field.LocationY + field.ActualHeight, b.VerticalResolution)),
                        Aspose.Words.ConvertUtil.PixelToPoint(field.LocationX + field.ActualWidth - adjustment, b.HorizontalResolution),
                        (pageHeight - Aspose.Words.ConvertUtil.PixelToPoint(field.LocationY, b.VerticalResolution)));

                    paragraph.VerticalAlignment = VerticalAlignment.Top;
                    paragraph.Rotation = -(field.Rotation);

                    paragraph.AppendLine(textFragment);
                    textBuilder.AppendParagraph(paragraph);
                }

                document.Save(dataDir + "Sample_FontAngle_out.pdf");

I have also attached the output I got from the above code. Noticed that the rotated text are outside the boxes. The text having 0 angle looks fine.

Sample_FontAngle_out.pdf (57.5 KB)

Can you please advise on this?

Kind regards,
Byang

@vitro

Would you please share the complete code snippet as shared code snippet has missing object i.e. document, field, etc. Also, please share the respective source PDF document so that we can investigate the scenario accordingly.

Here is the complete code:

var document = new Document();
            var page = document.Pages.Add();
            page.PageInfo.Margin.Bottom = 0;
            page.PageInfo.Margin.Top = 0;
            page.PageInfo.Margin.Left = 0;
            page.PageInfo.Margin.Right = 0;

            var b = new System.Drawing.Bitmap(dataDir + "Sample_FontAngle.jpg");
            page.SetPageSize(Aspose.Words.ConvertUtil.PixelToPoint(b.Width), Aspose.Words.ConvertUtil.PixelToPoint(b.Height));
            page.AddImage(dataDir + "Sample_FontAngle.jpg", new Aspose.Pdf.Rectangle(0, 0, Aspose.Words.ConvertUtil.PixelToPoint(b.Width), Aspose.Words.ConvertUtil.PixelToPoint(b.Height)));

            // Create TextBuilder object
            TextBuilder textBuilder = new TextBuilder(page);
            var list = new System.Collections.Generic.List<Field>();

            int[] angles = { 0, 90, 180, 270 };

            foreach (var i in angles)
            {
                var field1 = new Field
                {
                    Text = i + " degrees",
                    Rotation = i,
                    LocationX = 220,
                    LocationY = 150,
                    ActualFontSizeF = 12,
                    ActualHeight = 26,
                    ActualWidth = 120
                };
                list.Add(field1);
            }

            foreach (Field field in list)
            {
                // Control font size is in pixel. Multiplier is an estimate to convert pixel to float. 
                var fontSize = (float)(field.ActualFontSizeF * 0.755);

                // Create text fragment
                var textFragment = new TextFragment(field.Text);
                textFragment.TextState.Font = FontRepository.FindFont("Verdana");
                textFragment.TextState.FontSize = fontSize;
                textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);

                var adjustment = 5;
                var pageHeight = Aspose.Words.ConvertUtil.PixelToPoint(b.Height - adjustment, b.VerticalResolution);

                // create text paragraph
                TextParagraph paragraph = new TextParagraph();
                // specify the location to add TextParagraph
                paragraph.Rectangle = new Aspose.Pdf.Rectangle(
                    Aspose.Words.ConvertUtil.PixelToPoint(field.LocationX + adjustment, b.HorizontalResolution),
                    (pageHeight - Aspose.Words.ConvertUtil.PixelToPoint(field.LocationY + field.ActualHeight, b.VerticalResolution)),
                    Aspose.Words.ConvertUtil.PixelToPoint(field.LocationX + field.ActualWidth - adjustment, b.HorizontalResolution),
                    (pageHeight - Aspose.Words.ConvertUtil.PixelToPoint(field.LocationY, b.VerticalResolution)));

                paragraph.VerticalAlignment = VerticalAlignment.Top;
                paragraph.Rotation = -(field.Rotation);

                paragraph.AppendLine(textFragment);
                textBuilder.AppendParagraph(paragraph);
            }

            document.Save(dataDir + "Sample_FontAngle_out.pdf");
        }

The following class for the Field:

class Field
    {
        public int LocationX;
        public int LocationY;
        public int ActualWidth;
        public int ActualHeight;
        public float ActualFontSizeF;
        public double Rotation;
        public string Text;
    }

Also attached is the image used as background.

Sample_FontAngle.jpg (35.1 KB)

Kind regards,
Byang

@vitro

We were able to reproduce the issue in our environment and we need to further investigate whether it is possible to specify and determine the origin of rotation for text fragments or not. For the purpose, we have logged an investigation ticket as PDFNET-48761 in our issue tracking system. We will further look into its details and keep you informed about the status of ticket resolution. Please be patient and spare us some time.

We apologize for the inconvenience.