PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 9:30 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Tue Apr 26, 2022 1:32 pm 
Offline

Joined: Tue Jan 04, 2022 1:15 pm
Posts: 5
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 1853 times ]
File comment: How should look
hello131.png
hello131.png [ 45.59 KiB | Viewed 1853 times ]
Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 26, 2022 5:10 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Tue Apr 26, 2022 6:59 pm 
Offline

Joined: Tue Jan 04, 2022 1:15 pm
Posts: 5
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 68 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group