【问题标题】:How to position and center text in PDFsharp?如何在 PDFsharp 中定位和居中文本?
【发布时间】:2014-09-17 03:29:55
【问题描述】:

我需要标题下方居中的“工作设置表”。应该怎么做?

这是我尝试过的:

// Create an empty page
PdfPage page = document.AddPage();
page.Size = PageSize.Letter;
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);

//XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode,
                                                PdfFontEmbedding.Always);

// Create a font
XFont HeadingFont = new XFont("Times New Roman", 20, XFontStyle.Bold);
XFont BodyFont = new XFont("Times New Roman", 12);
// Draw the text
gfx.DrawString("Texas Exterior Systems", HeadingFont, XBrushes.Black,
  new XRect(0, 0, page.Width, page.Height),
  XStringFormats.TopCenter);

gfx.DrawString("Job Setup Sheet", BodyFont, XBrushes.Black,
  new XRect(0, 0, page.Width, page.Height),
  XStringFormats.Center);

【问题讨论】:

    标签: c# pdfsharp


    【解决方案1】:

    你传递给 DrawString 的 XRect 总是覆盖整个页面。通过使用矩形提供正确的顶部和/或底部位置,可以在该位置绘制文本。

    示例代码可以是found here:

    void DrawText(XGraphics gfx, int number)
    {
        BeginBox(gfx, number, "Text Styles");
    
        const string facename = "Times New Roman";
    
        //XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
        XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.WinAnsi, PdfFontEmbedding.Default);
    
        XFont fontRegular = new XFont(facename, 20, XFontStyle.Regular, options);
        XFont fontBold = new XFont(facename, 20, XFontStyle.Bold, options);
        XFont fontItalic = new XFont(facename, 20, XFontStyle.Italic, options);
        XFont fontBoldItalic = new XFont(facename, 20, XFontStyle.BoldItalic, options);
    
        // The default alignment is baseline left (that differs from GDI+)
        gfx.DrawString("Times (regular)", fontRegular, XBrushes.DarkSlateGray, 0, 30);
        gfx.DrawString("Times (bold)", fontBold, XBrushes.DarkSlateGray, 0, 65);
        gfx.DrawString("Times (italic)", fontItalic, XBrushes.DarkSlateGray, 0, 100);
        gfx.DrawString("Times (bold italic)", fontBoldItalic, XBrushes.DarkSlateGray, 0, 135);
    
        EndBox(gfx);
    }
    

    【讨论】:

    • 好消息。另一个问题。每次我在 Visual Studio 中更改它时,我都需要清理和重建项目。有更快的方法吗?
    • 我按 F5,Visual Studio 编译更改并启动程序。您是否排除了配置管理器中的项目?
    • 如果这是 PDFsharp 的官方帐户,您可以在您的个人资料中注明吗?
    • 我不明白怎么把它放在中间?
    • @rogue39nin 查看问题中的XStringFormats.Center 参数值。计算每一行的XRect,不要在任何地方使用整个页面,如问题所示。
    猜你喜欢
    • 1970-01-01
    • 2016-09-20
    • 2017-05-03
    • 2019-10-02
    • 1970-01-01
    • 2014-06-28
    • 2020-01-11
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多