如何在PDFsharp中定位文本?

时间:2014-09-17 03:29:55

标签: c# pdfsharp

我对jsPdf有一些经验,而且我没有接受PDFsharp。我想知道是否有人可以给我一个快速的样品。 我需要"工作设置表"以标题为中心。如果我想在它们之间应用空间

// 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);

1 个答案:

答案 0 :(得分:3)

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

示例代码可以是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);
}