PDFsharp自动换行

May*_*lor 9 c# pdfsharp

我似乎无法理解如何使用PDFsharp在我创建的矩形中包装文本.我已经看过文章解释如何,但是当我尝试它时,我的文本仍然延伸到PDF的页面.任何帮助都会很棒(这是我第一次使用PDFsharp).

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);
Run Code Online (Sandbox Code Playgroud)

这就是它正在做的事情> 在此输入图像描述

Je *_*not 10

从你的代码片段我假设它tf是XTextFormatter类的一个对象,而是gfx一个XGraphics对象.

XGraphics.DrawString 不支持换行符.

XTextFormatter.DrawString 支持换行符.

代码中的错误:您正在呼叫gfx.DrawString您打算呼叫的位置tf.DrawString.


小智 7

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

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

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

  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);
Run Code Online (Sandbox Code Playgroud)

如何保存和运行示例:

string pdfFilename = "firstpage.pdf";
pdf.Save(pdfFilename);
Process.Start(pdfFilename);
Run Code Online (Sandbox Code Playgroud)