如何在PDFsharp中定位和居中文本

本文关键字:中文 文本 定位 PDFsharp | 更新日期: 2023-09-27 18:22:04

我需要标题下居中的"作业设置表"。应该如何做到这一点?

以下是我尝试过的:

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

如何在PDFsharp中定位和居中文本

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

示例代码可以在这里找到:

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

我提出了这个解决方案

using PdfSharp.Drawing;
using PdfSharp.Pdf;
string text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit" +
              "sed do eiusmod tempor incididunt ut labore et dolore" +
              "magna aliqua. Ut enim ad minim veniam, quis nostrud" +
              "exercitation ullamco laboris nisi ut aliquip ex ea "; 
//new document
PdfDocument document = new PdfDocument();
// New Page
PdfPage page = document.AddPage();
// The class will provide drwaing related methods for specified page
XGraphics gfx = XGraphics.FromPdfPage(page);
PdfSharp.Drawing.Layout.XTextFormatter tf = new 
PdfSharp.Drawing.Layout.XTextFormatter(gfx);
XFont font = new XFont("Arial", 10);
XRect rect = new XRect(80, 80, 500, 500);
gfx.DrawRectangle(XBrushes.SeaShell, rect);
tf.Alignment = PdfSharp.Drawing.Layout.XParagraphAlignment.Justify;
tf.DrawString(text, fontcuerpo, XBrushes.Black, rect, XStringFormats.Center);

注意:不能使用/n分隔行中的文本。它必须是内联字符串。否则它就不会起作用。这将允许您使用几种类型的文本方向来更改XStringFormats,还可以使用不同的对齐方式来更改对齐方式。