使用模板

假定你的PDF文档中经常用一些数学公式,例如下面的勾股定律公式:

32 + 42 = 52

如果你想用PDFCreator绘制,不是完全不可以,但需要时间和脑力堆砌。

我们可以学习Word报表或Excel报表的思路,用RTF写出这个公式:

[A]2 + [B]2 = [C]2

然后在绘制前,用实际的数值替换[A]、[B]、[C]即可。

这段公式的RTF源码是:

{\pard[A]\super 2\nosupersub + [B]\super 2\nosupersub = [C]\super 2\par}

你可以保存在一个文本文件中,或者复制到自己的代码中直接使用。

提醒:你可以在word、wps或Windows自带的写字板中写出这个公式,然后保存为RTF格式,最后用记事本打开这个文件找到对应的源码复制。

示例

Dim file As String = "c:\temp\test.pdf"
Dim
pdc As New PDFCreator()
pdc.ConformanceLevel = Pdf.PdfAConformanceLevel.PdfA1b
'
使用PDF/A格式提高兼容性
Dim
rect As RectangleF = pdc.PageRectangle()
rect.Inflate( - 72, - 72)

Dim
formula As String = "{\pard [A]\super 2\nosupersub + [B]\super 2\nosupersub = [C]\super 2\par}" 'RTF
模板
Dim
fnt As New Font("
微软雅黑", 12)
pdc.DrawString(
"RTF
模板示例", fnt, Brushes.Black, rect)
rect.Offset(0, 25)

Dim
x As Integer = 3, y As Integer = 4 , z As Integer = 5
Dim
tmp As String = formula.Replace("[A]", x).Replace("[B]", y).Replace("[C]", z) '
内容替换
pdc.DrawStringRtf(tmp, fnt, Brushes.red, rect)
'
DrawStringRtf绘制

pdc.Save(file)
Process.Start(file)

在命令窗口执行后,生成的文档为:

提示:

PDFCreator本就是一个小工具,难以完美渲染RTF文档,所以建议只有确实用PDFCReator难以实现的格式问题,才考虑用模板的思路解决。


本页地址:http://www.foxtable.com/webhelp/topics/6080.htm