In this article, we're going to demonstrate how to draw superscript and subscript text in PDF using Spire.PDF.
Draw Superscript Text
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace Superscript { class Program { static void Main(string[] args) { //Instantiate a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Add a page PdfPageBase page = pdf.Pages.Add(); //Set initial (x, y) coordinate float x = 0; float y = 50; //Set font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true); //Draw string string text = "Sample Text"; page.Canvas.DrawString(text, font, PdfBrushes.Black, new PointF(x, y)); //Measure the string SizeF size = font.MeasureString(text); //Set the x coordinate of the superscript text x += size.Width; //Instantiate a PdfStringFormat instance PdfStringFormat format = new PdfStringFormat(); //Set format as superscript format.SubSuperScript = PdfSubSuperScript.SuperScript; //Draw superscript text with format text = "Superscript"; page.Canvas.DrawString(text, font, PdfBrushes.Black, new PointF(x, y), format); //Save the document pdf.SaveToFile("SuperScript.pdf"); } } }
Draw Superscript Text
using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace Subscript { class Program { static void Main(string[] args) { //Instantiate a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Add a page PdfPageBase page = pdf.Pages.Add(); //Set initial (x, y) coordinate float x = 0; float y = 50; //Set font PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS", 11f), true); //Draw string string text = "Sample Text"; page.Canvas.DrawString(text, font, PdfBrushes.Black, new PointF(x, y)); //Measure the string SizeF size = font.MeasureString(text); //Set the x coordinate of the subscript text x += size.Width; //Instantiate a PdfStringFormat instance PdfStringFormat format = new PdfStringFormat(); //Set format as subscript format.SubSuperScript = PdfSubSuperScript.SubScript; //Draw subscript text = "Subscript"; page.Canvas.DrawString(text, font, PdfBrushes.Black, new PointF(x, y), format); //Save the document pdf.SaveToFile("SubScript.pdf"); } } }