c00*_*0ke 16 .net formatting pdfsharp
我需要知道根据使用的字体和可用宽度计算文本区域高度的最佳方法.
我需要知道高度,以便我可以在需要时处理分页符.
Wil*_*ler 20
PdfSharp.Drawing.XGraphics对象有一个MeasureString方法,可以返回您需要的内容.
var pdfDoc = new PdfSharp.Pdf.PdfDocument();
var pdfPage = pdfDoc.AddPage();
var pdfGfx = PdfSharp.Drawing.XGraphics.FromPdfPage(pdfPage);
var pdfFont = new PdfSharp.Drawing.XFont("Helvetica", 20);
while (pdfGfx.MeasureString("Hello World!").Width > pdfPage.Width)
--pdfFont.Size;
pdfGfx.DrawString("Hello World!", pdfFont
, PdfSharp.Drawing.XBrushes.Black
, new PdfSharp.Drawing.XPoint(100, 100));
Run Code Online (Sandbox Code Playgroud)
这应该对你有帮助.请考虑我没有测试此代码,因为我是动态编写的,以便提供帮助.它可能包含一些编译时错误,但您可能会得到这个想法.
Je *_*not 10
在.NET中,您可以调用Graphics.MeasureString来查找绘制文本的大小.
是的,但在使用PDFsharp时,您可以调用XGraphics.MeasureString.
小智 6
我有一个类似的问题所以我实现了这个扩展方法:
public static double MeasureHeight(this PdfSharp.Drawing.XGraphics gfx, string text, PdfSharp.Drawing.XFont font, int width)
{
var lines = text.Split('\n');
double totalHeight = 0;
foreach (string line in lines)
{
var size = gfx.MeasureString(line, font);
double height = size.Height + (size.Height * Math.Floor(size.Width / width));
totalHeight += height;
}
return totalHeight;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24507 次 |
| 最近记录: |