PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

XGraphicsPath.AddString not match with XGraphics.DrawString
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4502
Page 1 of 1

Author:  poskotinov [ Sat Nov 04, 2023 4:26 pm ]
Post subject:  XGraphicsPath.AddString not match with XGraphics.DrawString

If use XGraphics.DrawString over XGraphicsPath.AddString for drawing text with spaces. First words match, but the following words move out.

Code:
    private static void Draw_Text_With_OUTER_line(ref PdfPage dst_page, string text, XFont xfont, XPen x_pen, XBrush x_brush, XStringFormat x_sf, XRect xr)
    {
        XGraphics gfx = XGraphics.FromPdfPage(dst_page);
        XGraphicsPath path = new XGraphicsPath();
        path.AddString(text, xfont.FontFamily, xfont.Style, xfont.Size, xr, x_sf);
        gfx.DrawPath(x_pen, x_brush, path);
        gfx.DrawString(text, xfont, x_brush, xr, x_sf);
        gfx.Dispose();
    }


Trying with "Arial" and "Times new roman" fonts.
Me need it for drawing text with outside outline.
May be tell me another way, for drawing innerside and outside text outline?

Attachments:
6666.jpg
6666.jpg [ 29.42 KiB | Viewed 4286 times ]

Author:  TH-Soft [ Mon Nov 06, 2023 12:15 pm ]
Post subject:  Re: XGraphicsPath.AddString not match with XGraphics.DrawStr

You can use DrawPath to draw text and outline at the same time.

See third method in this example:
https://pdfsharp.net/wiki/Watermark-sample.ashx

Code:
// Create a graphical path.
var path = new XGraphicsPath();
 
// Create a string format.
var format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
format.LineAlignment = XLineAlignment.Near;
 
// Add the text to the path.
// AddString is not implemented in PDFsharp Core.
path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150,
    new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
    format);
 
// Create a dimmed red pen and brush.
var pen = new XPen(XColor.FromArgb(50, 75, 0, 130), 3);
XBrush brush = new XSolidBrush(XColor.FromArgb(50, 106, 90, 205));
 
// Stroke the outline of the path.
gfx.DrawPath(pen, brush, path);

Author:  Thomas Hoevel [ Tue Nov 07, 2023 1:19 pm ]
Post subject:  Re: XGraphicsPath.AddString not match with XGraphics.DrawStr

poskotinov wrote:
If use XGraphics.DrawString over XGraphicsPath.AddString for drawing text with spaces. First words match, but the following words move out.
This is not a bug, it is a feature.
When using DrawString, the PDF file will contain the font and the Unicode string and the PDF viewer will render the text. Word spacing depends on the PDF viewer at the time of viewing the PDF file.
When using AddString, the text will be converted to drawing instructions by GDI+ or WPF. Word spacing depends on GDI+ or WPF at the time when the PDF file is being created.

Differences are to be expected when mixing these two ways of drawing text.

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