这篇文章将介绍如何使用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("宋体", 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("宋体", 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("宋体", 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("宋体", 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")