这篇文章将介绍如何使用spire.pdf在pdf文档中绘制上标和下标文本。
绘制上标
c#
//创建pdfdocument实例
pdfdocument pdf = new pdfdocument();
//添加一个页面
pdfpagebase page = pdf.pages.add();
//设置x,y坐标初始值
float x = 0;
float y = 50;
//设置字体
pdftruetypefont font = new pdftruetypefont(new font("arial unicode ms", 11f), true);
//绘制文本
string text = "示例文本";
page.canvas.drawstring(text, font, pdfbrushes.black, new pointf(x,y));
//计算文本的尺寸
sizef size = font.measurestring(text);
//设置上标的x坐标
x = size.width;
//创建pdfstringformat实例
pdfstringformat format = new pdfstringformat();
//设置格式为上标
format.subsuperscript = pdfsubsuperscript.superscript;
//绘制上标
text = "上标";
page.canvas.drawstring(text, font, pdfbrushes.black, new pointf(x,y), format);
//保存文档
pdf.savetofile("superscript.pdf");
vb.net
'创建pdfdocument实例
dim pdf as new pdfdocument()
'添加一个页面
dim page as pdfpagebase = pdf.pages.add()
'设置x,y坐标初始值
dim x as single = 0
dim y as single = 50
'设置字体
dim font as new pdftruetypefont(new font("arial unicode ms", 11f), true)
'绘制文本
dim text as string = "示例文本"
page.canvas.drawstring(text, font, pdfbrushes.black, new pointf(x, y))
'计算文本的尺寸
dim size as sizef = font.measurestring(text)
'设置上标的x坐标
x = size.width
'创建pdfstringformat实例
dim format as new pdfstringformat()
'设置格式为上标
format.subsuperscript = pdfsubsuperscript.superscript
'绘制上标
text = "上标"
page.canvas.drawstring(text, font, pdfbrushes.black, new pointf(x, y), format)
'保存文档
pdf.savetofile("superscript.pdf")
绘制下标
c#
//创建pdfdocument实例
pdfdocument pdf = new pdfdocument();
//添加一个页面
pdfpagebase page = pdf.pages.add();
//设置x,y坐标初始值
float x = 0;
float y = 50;
//设置字体
pdftruetypefont font = new pdftruetypefont(new font("arial unicode ms", 11f), true);
//绘制文本
string text = "示例文本";
page.canvas.drawstring(text, font, pdfbrushes.black, new pointf(x, y));
//计算文本的尺寸
sizef size = font.measurestring(text);
//设置下标的x坐标
x = size.width;
//创建pdfstringformat实例
pdfstringformat format = new pdfstringformat();
//设置格式为下标
format.subsuperscript = pdfsubsuperscript.subscript;
//绘制下标
text = "下标";
page.canvas.drawstring(text, font, pdfbrushes.black, new pointf(x, y), format);
//保存文档
pdf.savetofile("subscript.pdf");
vb.net
'创建pdfdocument实例
dim pdf as new pdfdocument()
'添加一个页面
dim page as pdfpagebase = pdf.pages.add()
'设置x,y坐标初始值
dim x as single = 0
dim y as single = 50
'设置字体
dim font as new pdftruetypefont(new font("arial unicode ms", 11f), true)
'绘制文本
dim text as string = "示例文本"
page.canvas.drawstring(text, font, pdfbrushes.black, new pointf(x, y))
'计算文本的尺寸
dim size as sizef = font.measurestring(text)
'设置下标的x坐标
x = size.width
'创建pdfstringformat实例
dim format as new pdfstringformat()
'设置格式为下标
format.subsuperscript = pdfsubsuperscript.subscript
'绘制下标
text = "下标"
page.canvas.drawstring(text, font, pdfbrushes.black, new pointf(x, y), format)
'保存文档
pdf.savetofile("subscript.pdf")