目录
引入dll
本次程序中引入的是spire.pdf.dll,引入方法如下:
【方法1】通过安装。
可以在visual studio中打开“kb88凯时官网登录的解决方案资源管理器”,鼠标右键点击“引用”,“管理nuget包”,然后搜索“free spire.pdf”,点击“安装”。
也可以将以下内容复制到pm控制台安装:
install-package freespire.pdf -version 7.8.9
【方法2】手动安装。
可通过手动下载包,然后解压,找到bin文件夹下的spire.pdf.dll。在visual studio中打开“kb88凯时官网登录的解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径bin文件夹下的dll文件添加引用至程序。
应用字体
c#
using spire.pdf; using spire.pdf.graphics; using system.drawing; namespace applyfonts { class program { static void main(string[] args) { //创建pdfdocument对象 pdfdocument pdf = new pdfdocument(); //添加一页 pdfpagebase page = pdf.pages.add(); //初始化y坐标 float y = 30; //使用standard字体绘制文字 pdffont standardfont = new pdffont(pdffontfamily.helvetica, 14f); page.canvas.drawstring("standard font - helvetica", standardfont, pdfbrushes.black, 0, y); standardfont = new pdffont(pdffontfamily.timesroman, 14f); page.canvas.drawstring("standard font - times_roman", standardfont, pdfbrushes.black, 0, (y = y 16)); standardfont = new pdffont(pdffontfamily.courier, 14f); page.canvas.drawstring("standard font - courier", standardfont, pdfbrushes.black, 0, (y = y 16)); //使用true type字体绘制文字 pdftruetypefont truetypefont = new pdftruetypefont(new font("arial", 12f), true); page.canvas.drawstring("truetype font - arial", truetypefont, pdfbrushes.blue, 0, (y = y 30f)); /*//使用私有字体绘制文字 string fontfilename = "c:\\users\\administrator\\desktop\\fontfile.ttf"; truetypefont = new pdftruetypefont(fontfilename, 14f); page.canvas.drawstring("private font: 私有字体", truetypefont, pdfbrushes.darkgreen, 0, (y = y 30f)); */ //使用cjk字体绘制文字 pdfcjkstandardfont cjkfont = new pdfcjkstandardfont(pdfcjkfontfamily.monotypeheimedium, 14f); page.canvas.drawstring("你 好", cjkfont, pdfbrushes.deeppink, 0, (y = y 30f)); cjkfont = new pdfcjkstandardfont(pdfcjkfontfamily.hanyangsystemsgothicmedium, 14f); page.canvas.drawstring("こんにちは", cjkfont, pdfbrushes.orangered, 0, (y = y 16f)); cjkfont = new pdfcjkstandardfont(pdfcjkfontfamily.hanyangsystemsshinmyeongjomedium, 14f); page.canvas.drawstring("안녕하세요", cjkfont, pdfbrushes.purple, 0, (y = y 16f)); //保存文档 pdf.savetofile("applyfonts.pdf",fileformat.pdf); system.diagnostics.process.start("applyfonts.pdf"); } } }
vb.net
imports spire.pdf imports spire.pdf.graphics imports system.drawing namespace applyfonts class program private shared sub main(args as string()) '创建pdfdocument对象 dim pdf as new pdfdocument() '添加一页 dim page as pdfpagebase = pdf.pages.add() '初始化y坐标 dim y as single = 30 '使用standard字体绘制文字 dim standardfont as new pdffont(pdffontfamily.helvetica, 14f) page.canvas.drawstring("standard font - helvetica", standardfont, pdfbrushes.black, 0, y) standardfont = new pdffont(pdffontfamily.timesroman, 14f) page.canvas.drawstring("standard font - times_roman", standardfont, pdfbrushes.black, 0, (inlineassignhelper(y, y 16))) standardfont = new pdffont(pdffontfamily.courier, 14f) page.canvas.drawstring("standard font - courier", standardfont, pdfbrushes.black, 0, (inlineassignhelper(y, y 16))) '使用true type字体绘制文字 dim truetypefont as new pdftruetypefont(new font("arial", 12f), true) page.canvas.drawstring("truetype font - arial", truetypefont, pdfbrushes.blue, 0, (inlineassignhelper(y, y 30f))) '//使用私有字体绘制文字 ' string fontfilename = "c:\\users\\administrator\\desktop\\fontfile.ttf"; ' truetypefont = new pdftruetypefont(fontfilename, 14f); ' page.canvas.drawstring("private font: 私有字体", truetypefont, pdfbrushes.darkgreen, 0, (y = y 30f)); ' '使用cjk字体绘制文字 dim cjkfont as new pdfcjkstandardfont(pdfcjkfontfamily.monotypeheimedium, 14f) page.canvas.drawstring("你 好", cjkfont, pdfbrushes.deeppink, 0, (inlineassignhelper(y, y 30f))) cjkfont = new pdfcjkstandardfont(pdfcjkfontfamily.hanyangsystemsgothicmedium, 14f) page.canvas.drawstring("こんにちは", cjkfont, pdfbrushes.orangered, 0, (inlineassignhelper(y, y 16f))) cjkfont = new pdfcjkstandardfont(pdfcjkfontfamily.hanyangsystemsshinmyeongjomedium, 14f) page.canvas.drawstring("안녕하세요", cjkfont, pdfbrushes.purple, 0, (inlineassignhelper(y, y 16f))) '保存文档 pdf.savetofile("applyfonts.pdf", fileformat.pdf) system.diagnostics.process.start("applyfonts.pdf") end sub private shared function inlineassignhelper(of t)(byref target as t, value as t) as t target = value return value end function end class end namespace
绘制效果
以上就是c#实现在pdf文档中应用多种不同字体的详细内容,更多关于c# pdf应用不同字体的资料请关注其它相关文章!