【问题标题】:PDFsharp word wrapPDFsharp 自动换行
【发布时间】:2014-03-28 23:00:52
【问题描述】:

如何使用 PDFsharp 在矩形内换行?

在我的尝试中,文本仍然延伸到 PDF 的页面之外。这是尝试过的:

rect = new XRect(20, 300, 400, 100);
tf.Alignment = XParagraphAlignment.Left;
gfx.DrawString("\t • Please call the borrower prior to closing and confirm your arrival time and the closing location. \n", font, XBrushes.Black, rect, XStringFormats.TopLeft);
rect = new XRect(20, 320, 100, 100);
gfx.DrawString("\t • If the borrower needs to change the closing appointment time or date for any reason please have them call McDonnell Law Firm at 866-931-8793 \n", font, XBrushes.Black, rect, XStringFormats.TopLeft);
rect = new XRect(20, 340, 100, 100);
gfx.DrawString("\t • Completed documents must be received same day. Fax back to 888-612-4912 or email ClosingDocs@appliedtechres.com \n", font, XBrushes.Black, rect, XStringFormats.TopLeft);
rect = new XRect(20, 360, 100, 100);
gfx.DrawString("\t • Documents are to be returned via Fedex or UPS with shipping label provided. Documents must be dropped same day. \n", font, XBrushes.Black, rect, XStringFormats.TopLeft);

这就是它正在做的事情>

【问题讨论】:

  • 什么是问题?请更具体。
  • 我添加了一张图片。文本离开页面。我已经改变了矩形的宽度,但没有区别。
  • 我相信他的意思是“似乎无法理解......”

标签: c# pdfsharp


【解决方案1】:

根据您的代码 sn-p,我假设 tf 是 XTextFormatter 类的对象,而 gfx 是 XGraphics 对象。

XGraphics.DrawString 不支持换行。

XTextFormatter.DrawString 支持换行。

您的代码中的错误:您正在调用 gfx.DrawString,而您的本意是调用 tf.DrawString

【讨论】:

    【解决方案2】:

    这是我的示例,它执行以下操作:

    1. 定义一个矩形并画一个细长的框来显示轮廓

    2. 将文本放置在框中,处理小逻辑以添加边距(向 X 坐标添加 5 px 并减去与文本宽度区域相同的 5 px。

    3. XTextFormatter 将用于将文本放置在定义的矩形内。

    示例

     PdfDocument pdf = new PdfDocument();
     PdfPage pdfPage = pdf.AddPage();
     XGraphics graph = XGraphics.FromPdfPage(pdfPage);
    
     var tf = new XTextFormatter(graph);
     var rect = new XRect(25, 50, 200, 34);
    
     XPen xpen = new XPen(XColors.Navy, 0.4);
    
     graph.DrawRectangle(xpen, rect);
     XStringFormat format = new XStringFormat();
     format.LineAlignment = XLineAlignment.Near;
     format.Alignment = XStringAlignment.Near;
    
     XBrush brush = XBrushes.Purple;
     tf.DrawString("This is some text written in a textbox over three lines bla bla bla bla bla ffffffffffffffffffffdsdsdsd", 
                   new XFont("Helvetica", 8), 
                   brush, 
                   new XRect(rect.X + 5, rect.Y, rect.Width - 5, 34), format);
    

    如何保存并运行示例:

    string pdfFilename = "firstpage.pdf";
    pdf.Save(pdfFilename);
    Process.Start(pdfFilename);
    

    【讨论】:

    • 不会写超过 3 行,但会自动换行 +1
    猜你喜欢
    • 2011-07-16
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多