PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 7:43 pm

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: Sat Nov 04, 2023 4:26 pm 
Offline

Joined: Sat Feb 25, 2023 4:34 pm
Posts: 3
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 4067 times ]
Top
 Profile  
Reply with quote  
PostPosted: Mon Nov 06, 2023 12:15 pm 
Offline
PDFsharp Expert
User avatar

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

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


Top
 Profile  
Reply with quote  
PostPosted: Tue Nov 07, 2023 1:19 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


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: Google [Bot] and 347 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