PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Difference RotateAtTransform() in PdfSharp 1.31 and 1.51
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4338
Page 1 of 1

Author:  ivans [ Tue Apr 26, 2022 1:32 pm ]
Post subject:  Difference RotateAtTransform() in PdfSharp 1.31 and 1.51

Hi

We are using g.RotateAtTransform(90, new XPoint(0, 0)); to position text verticaly. It worked with pdfsharp 1.31 but in 1.51 text is positioned outside of page. What are changes in RotateAtTransform() , why Xpoint(0,0) doesn't work anymore? What should we use instead g.RotateAtTransform(90, new XPoint(0, 0))? Sample code:

Code:
        public Stream RenderToPdfHello()
        {
            Color backgroundColor = ColorTranslator.FromHtml("#D3CCFF");
            Color textColor = ColorTranslator.FromHtml("#000000");
            var margin = XUnit.FromInch(0.25f);
            var location = new XRect(margin, 0, 0, 0);
            var fontSize = 8;
            var text = "Hello";

            using (var doc = new PdfSharp.Pdf.PdfDocument())
            {
                var pdfPage = new PdfPage(doc);

                pdfPage.Width = XUnit.FromInch(0.18d);
                pdfPage.Height = XUnit.FromInch(12d);

                using (XGraphics g = XGraphics.FromPdfPage(pdfPage))
                {
                    g.DrawRectangle(new XSolidBrush(XColor.FromArgb(backgroundColor)), 0, 0, pdfPage.Width, pdfPage.Height);

                    {
                        var textBrush = new XSolidBrush(XColor.FromArgb(textColor));
                        var xf = new XFont(FontFamily.GenericSansSerif, new XUnit(fontSize), XFontStyle.Bold,
                            new XPdfFontOptions(PdfFontEncoding.Unicode));
                        var xsf = new XStringFormat { Alignment = XStringAlignment.Center };

                        var state = g.Save();

                        [b]var size = g.MeasureString(text ?? "", xf, xsf);
                        g.RotateAtTransform(90, new XPoint(0, 0));
                        g.TranslateTransform(size.Height, size.Width / 2.0, XMatrixOrder.Append);
                        g.TranslateTransform((pdfPage.Width.Point - size.Height) / 2.0, 0, XMatrixOrder.Append);

                        g.DrawString(text, xf, textBrush, location, xsf);
[/b]
                        g.Restore(state);
                    }
                }
                doc.AddPage(pdfPage);
                var m = new MemoryStream();
                doc.Save(m, false);
                m.Position = 0;
                return m;
            }
        }


Attachments:
File comment: Text not showing in 1.51
hello151.png
hello151.png [ 45.35 KiB | Viewed 2516 times ]
File comment: How should look
hello131.png
hello131.png [ 45.59 KiB | Viewed 2516 times ]

Author:  TH-Soft [ Tue Apr 26, 2022 5:10 pm ]
Post subject:  Re: Difference RotateAtTransform() in PdfSharp 1.31 and 1.51

Hi!
Maybe the problem is not with rotate, but with your "xsf" parameter. Your "location" is a zero-sized rectangle.
Maybe play with different values for xsf and location.

Author:  ivans [ Tue Apr 26, 2022 6:59 pm ]
Post subject:  Re: Difference RotateAtTransform() in PdfSharp 1.31 and 1.51

I tried:
Code:
                        g.DrawString("OOGGG", xf, textBrush, new XPoint(margin, 0), xsf);
                        g.DrawString("GGGOO", xf, textBrush, new XRect(margin, 0, size.Width, size.Height), xsf);
                        g.DrawString("OOGGG", xf, textBrush, new XPoint(margin, 0));

                        g.DrawString("GOGOG", xf, textBrush, new XPoint(margin, 0), XStringFormats.TopLeft);
                        g.DrawString("GGGOO", xf, textBrush, new XRect(margin, 0, size.Width, size.Height), XStringFormats.TopLeft);
                        g.DrawString("GOGOG", xf, textBrush, new XPoint(margin, 0), XStringFormats.Center);
                        g.DrawString("GGGOO", xf, textBrush, new XRect(margin, 0, size.Width, size.Height), XStringFormats.Center);


Nothing works

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/