首页 > 解决方案 > 在 C# 中创建 PDF 文件 - 文本不适合页面

问题描述

我有一个按钮,可以创建一个带有一些文本的 pdf,到目前为止,我有这个:

    private void button_Click(object sender, EventArgs e)
    {
        PdfDocument pdf = new PdfDocument();
        pdf.Info.Title = "My First PDF";
        PdfPage pdfPage = pdf.AddPage();
        XGraphics graph = XGraphics.FromPdfPage(pdfPage);
        XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
        graph.DrawString("Example long text for stack overflow to see that it doesn't fit.",
            font, XBrushes.Black, new XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center);
        string pdfFilename = "example.pdf";
        pdf.Save(pdfFilename);
        Process.Start(pdfFilename);
   }

它工作得很好,但是我插入的字符串没有被格式化以适合页面,所以单词会被删掉。像这样的东西:https ://prnt.sc/1xdcsqi ,我希望它像普通文本一样适合页面。我试过 \n 但它对我不起作用。

提前感谢那些提供帮助的人。

标签: c#pdfbuttonformat

解决方案


推荐阅读