Wednesday, 19 December 2018 09:01

Add Links to PDF in Java

This article demonstrates how to add links, including ordinary link, hypertext link, email link and document link, to a PDF document by using Spire.PDF for Java.

import com.spire.pdf.annotations.*;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;

public class AddLinksToPdf {

    public static void main(String[] args) throws Exception {

        //create a pdf document
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add();

        //initialize x, y coordinates
        float y = 30;
        float x = 0;

        //create a plain font
        PdfTrueTypeFont plainFont = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,13),true);

        //create a underlined font
        HashMap<TextAttribute, Object> hm = new HashMap<TextAttribute, Object>();
        hm.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
        hm.put(TextAttribute.SIZE, 13);
        hm.put(TextAttribute.FAMILY, "Arial");
        Font font = new Font(hm);
        PdfTrueTypeFont underlineFont = new PdfTrueTypeFont(font);

        //add a simply link to pdf
        String label = "Simply Link: ";
        PdfStringFormat format = new PdfStringFormat();
        format.setMeasureTrailingSpaces(true);
        page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y,format);
        x = (float)plainFont.measureString(label,format).getWidth();
        page.getCanvas().drawString("https://www.e-iceblue.com", underlineFont, PdfBrushes.getBlue(), x, y+2);
        y = y + 26;

        //add a hyper text link to pdf
        label= "Hypertext Link: ";
        page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);
        x = (float)plainFont.measureString(label,format).getWidth();
        PdfTextWebLink webLink = new PdfTextWebLink();
        webLink.setText("Home Page");
        webLink.setUrl("https://www.e-iceblue.com");
        webLink.setFont(plainFont);
        webLink.setBrush(PdfBrushes.getBlue());
        webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));
        y= y + 26;

        //add an email link to pdf
        label = "Email Link:  ";
        page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);
        x = (float)plainFont.measureString(label, format).getWidth();
        webLink = new PdfTextWebLink();
        webLink.setText("Contact Us");
        webLink.setUrl("mailto:support@e-iceblue.com");
        webLink.setFont(plainFont);
        webLink.setBrush(PdfBrushes.getBlue());
        webLink.drawTextWebLink(page.getCanvas(), new Point2D.Float(x, y));
        y = y + 26;

        //add a document link to pdf
        label = "Document Link: ";
        page.getCanvas().drawString(label, plainFont, PdfBrushes.getOrange(), 0, y, format);
        x = (float)plainFont.measureString(label, format).getWidth();
        page.getCanvas().drawString("Open File", plainFont, PdfBrushes.getBlue(), x, y, format);
        Rectangle2D rect = new Rectangle2D.Float(x,y+2,60,15);
        PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(rect,"C:\\Users\\Administrator\\Desktop\\Image.png");
        fileLinkAnnotation.setBorder(new PdfAnnotationBorder(0f));
        ((PdfNewPage) ((page instanceof PdfNewPage) ? page : null)).getAnnotations().add(fileLinkAnnotation);

        //save to file
        doc.saveToFile("output/AddLinks.pdf");
        doc.close();
    }
}

Output:

Add Links to PDF in Java

This article will demonstrate how to use Spire.Presentaion for Java to operate the presentation slides. Such as adding new slide in PowerPoint document, removing an existing slide, hiding slide from a PowerPoint document and changing the Slide Order within a Presentation in Java applications.

Add new slides to an existing PowerPoint document.

import com.spire.presentation.*;

public class Slides {
    public static void main(String[] args) throws Exception {

        //Create a PPT document and load file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pptx");
        
        //add new slide at the end of the document
        presentation.getSlides().append();

        //insert a blank slide before the second slide
        presentation.getSlides().insert(1);

       //Save the document
        presentation.saveToFile("output/AddSlide.pptx", FileFormat.PPTX_2010);
    }

}

Operate the presentation slide on Java applications

Hide a slide that we don't want to present to audiences without deleting it.

import com.spire.presentation.*;
public class Slides {
    public static void main(String[] args) throws Exception {
        //Create a PPT document and load file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pptx");
        
        //Hide the second slide
        presentation.getSlides().get(1).setHidden(true);

        //Save the document
        presentation.saveToFile("output/HideSlide.pptx", FileFormat.PPTX_2010);
    }
}

Operate the presentation slide on Java applications

Delete a slide from the PowerPoint document

import com.spire.presentation.*;
public class Slides {
    public static void main(String[] args) throws Exception {
        
        //Create a PPT document and load file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pptx");
        
        //Remove the third slide
        presentation.getSlides().removeAt(2);
      
        //Save the document
        presentation.saveToFile("output/Removeslide.pptx", FileFormat.PPTX_2010);
    }
}

Operate the presentation slide on Java applications

Change the Slide Order within a PowerPoint document

import com.spire.presentation.*;

public class Slides {
    public static void main(String[] args) throws Exception {

        //Create a PPT document and load file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pptx");

        //Move the first slide to the second slide position
        ISlide slide = presentation.getSlides().get(0);
        slide.setSlideNumber(2);

       //Save the document
        presentation.saveToFile("output/Reorderslide.pptx", FileFormat.PPTX_2010);
    }

}

Operate the presentation slide on Java applications

In this article, we're going to demonstrate how to insert and extract images from a PowerPoint document using Spire.Presentation for Java.

Insert images into a PowerPoint document

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.geom.Rectangle2D;

public class InsertImages {
    public static void main(String[] args) throws Exception {
            //Create a Presentation instance
            Presentation ppt = new Presentation();

            Rectangle2D rect = new Rectangle2D.Double(ppt.getSlideSize().getSize().getWidth() / 2 - 280, 140, 120, 120);

            //Get the first slide
            ISlide slide = ppt.getSlides().get(0);
            //Insert an image into the slide
            IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, "a.jpg", rect);
            image.getLine().setFillType(FillFormatType.NONE);

            //Append a new slide
            slide = ppt.getSlides().append();
            //Insert an image into the slide
            image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, "b.jpg", rect);
            image.getLine().setFillType(FillFormatType.NONE);

            //Save the document
            ppt.saveToFile("InsertImages.pptx", FileFormat.PPTX_2013);
    }
}

Insert and Extract Images from a PowerPoint Document in Java

Extract images from a PowerPoint document

Spire.Presentation for Java supports extracting all of the images in a PowerPoint document along with extracting images from a specific slide in a PowerPoint document.

The following code example shows how to extract all of the images in a PowerPoint document:

import com.spire.presentation.Presentation;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ExtractImages {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load the PowerPoint document
        ppt.loadFromFile("InsertImages.pptx");

        for (int i = 0; i < ppt.getImages().getCount(); i++) {
            //Extract images from the PowerPoint document
            BufferedImage image = ppt.getImages().get(i).getImage();
            ImageIO.write(image, "PNG",  new File(String.format("presentation/" + "extractImage-%1$s.png", i)));
        }
    }
}

Insert and Extract Images from a PowerPoint Document in Java

The following code example shows how to extract images from a specific slide in a PowerPoint document:

import com.spire.presentation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;

public class ExtractImages {
    public static void main(String[] args) throws Exception {
        //Create a Presentation instance
        Presentation ppt = new Presentation();
        //Load the PowerPoint document
        ppt.loadFromFile("InsertImages.pptx");

        //Get the first slide
        ISlide slide = ppt.getSlides().get(0);

        for(int i = 0; i< slide.getShapes().getCount(); i++)
        {
            IShape shape = slide.getShapes().get(i);
            //Extract images from the slide
            if(shape instanceof SlidePicture)
            {
                SlidePicture pic = (SlidePicture) shape;
                BufferedImage image = pic.getPictureFill().getPicture().getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(String.format("slide/" + "extractImage-%1$s.png", i)));
            }
            if(shape instanceof PictureShape)
            {
                PictureShape ps = (PictureShape) shape;
                BufferedImage image = ps.getEmbedImage().getImage();
                ImageIO.write(image, "PNG",  new File(String.format("slide/" + "extractImage-%1$s.png", i)));
            }
        }
    }
}

Insert and Extract Images from a PowerPoint Document in Java

Spire.Presentation for Java is a professional PowerPoint API that enables developers to create, read, write, convert and save PowerPoint documents in Java Applications. As an independent Java library, Spire.Presentation doesn't need Microsoft PowerPoint to be installed on system.

A rich set of features can be supported by Spire.Presentation for Java, such as add/edit/remove slide, create chart, create table, add bullets, encrypt and decrypt PPT, add watermark, add hyperlink, insert audio and video, paragraph settings, document properties settings, insert/extract image, extract text, set animation, add header and footer, add/delete comment, add note, create SmartArt.

This article will demonstrate how to use Spire.Presentaion for Java to add text watermark to PowerPoint document.

import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class watermark {
    public static void main(String[] args) throws Exception {

        //Create a PPT document and load the sample file
        Presentation presentation = new Presentation();
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pptx");

        //Set the width and height of watermark string
        int width= 400;
        int height= 300;
        //Define a rectangle range
        Rectangle2D.Double rect = new Rectangle2D.Double((presentation.getSlideSize().getSize().getWidth() - width) / 2,
                (presentation.getSlideSize().getSize().getHeight() - height) / 2, width, height);

        //Add a rectangle shape with a defined range
        IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);

        //Set the style of shape
        shape.getFill().setFillType(FillFormatType.NONE);
        shape.getShapeStyle().getLineColor().setColor(Color.white);
        shape.setRotation(-45);
        shape.getLocking().setSelectionProtection(true);
        shape.getLine().setFillType(FillFormatType.NONE);

        //Add text to shape
        shape.getTextFrame().setText("Confidential");
        PortionEx textRange = shape.getTextFrame().getTextRange();

        //Set the style of the text range
        textRange.getFill().setFillType(FillFormatType.SOLID);
        textRange.getFill().getSolidColor().setColor(Color.pink);
        textRange.setFontHeight(50);

        //Save the document
        presentation.saveToFile("output/result.pptx", FileFormat.PPTX_2010);
    }

}

Java insert text watermark to presentation slides

Wednesday, 12 December 2018 03:22

Create Grid in PDF in Java

In this article, you'll learn how to add a grid to PDF and how to format the grid as well, by using Spire.PDF with Java.

import com.spire.pdf.graphics.*;
import com.spire.pdf.grid.PdfGrid;

import java.awt.*;

public class CreateGrid {

    public static void main(String[] args) {

        //create a pdf document
        PdfDocument doc = new PdfDocument();
        PdfPageBase page = doc.getPages().add();

        //create a PdfGrid object
        PdfGrid grid = new PdfGrid();

        //set the cell padding, font, text brush, background brush of the grid
        grid.getStyle().setCellPadding(new PdfPaddings(3,3,3,3));
        grid.getStyle().setFont(new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.PLAIN,10), true));
        grid.getStyle().setTextBrush(PdfBrushes.getBlack());
        grid.getStyle().setBackgroundBrush(PdfBrushes.getLightGray());

        //create a PdfBorders object
        PdfBorders borders= new PdfBorders();
        borders.setAll(new PdfPen(PdfBrushes.getWhite(),1f));

        //define sample data
        String[] data = {"Continent;Country;Population;Ratio to World Pop;Flag",
                "Asia;China;1,391,190,000;18.2%; ",
                "Asia;Japan;126,490,000;1.66%; ",
                "Europe;United Kingdom;65,648,054;0.86%; ",
                "Europe;Germany;82,665,600;1.08%; ",
                "North America; Canada; 37,119,000; 0.49%; ",
                "North America; United States; 327,216,000; 4.29%; "
                };
        String[][] dataSource = new String[data.length][];
        for (int i = 0; i < data.length; i++) {
            dataSource[i] = data[i].split("[;]", -1);
        }

        //fill the grid with data
        grid.setDataSource(dataSource);

        //fill the cells with background images
        grid.getRows().get(1).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-China.png"));
        grid.getRows().get(2).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-Japan.png"));
        grid.getRows().get(3).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-United-Kingdom.png"));
        grid.getRows().get(4).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-Germany.png"));
        grid.getRows().get(5).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-Canada.png"));
        grid.getRows().get(6).getCells().get(4).getStyle().setBackgroundImage(PdfImage.fromFile("F:\\Documents\\flags\\flag-of-United-States-of-America.png"));

        //set the width of the last column
        grid.getColumns().get(grid.getColumns().getCount()-1).setWidth(60f);

        //vertically span cells
        grid.getRows().get(1).getCells().get(0).setRowSpan(2);
        grid.getRows().get(3).getCells().get(0).setRowSpan(2);
        grid.getRows().get(5).getCells().get(0).setRowSpan(2);

        for (int i = 0; i < data.length ; i++) {

            //set the height of each row
            grid.getRows().get(i).setHeight(30f);
            //set the background color of the first column
            grid.getRows().get(i).getCells().get(0).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());
            //set the font of the first column
            grid.getRows().get(i).getCells().get(0).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12),true));

            for (int j = 0; j < grid.getColumns().getCount(); j++) {

                //apply border style to all cells
                grid.getRows().get(i).getCells().get(j).getStyle().setBorders(borders);
                //apply text alignment to all cells
                grid.getRows().get(i).getCells().get(j).setStringFormat(new PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Middle));
                //set the font of the first row
                grid.getRows().get(0).getCells().get(j).getStyle().setFont(new PdfTrueTypeFont(new Font("Arial",Font.PLAIN,12),true));
                //set the background color of the first row
                grid.getRows().get(0).getCells().get(j).getStyle().setBackgroundBrush(PdfBrushes.getDarkGray());

            }
        }

        //draw grid on the pdf page
        grid.draw(page,0,30);

        //save to file
        doc.saveToFile("Grid.pdf");
        doc.close();
    }
}

Create Grid in PDF in Java

Friday, 07 December 2018 07:37

Set PDF Document Properties in Java

In this article, we're going to demonstrate how to set PDF document properties, such as title, author, subject, key words, creation date, creator and producer, using Spire.PDF for Java.

import com.spire.pdf.PdfDocument;

import java.util.Date;

public class SetDocumentProperties {
    public static void main(String[] args)
    {
        //Instantiate a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load the PDF document
        pdf.loadFromFile("original.pdf");

        //Set title
        pdf.getDocumentInformation().setTitle("Spire.PDF for Java Demo");
        //Set author
        pdf.getDocumentInformation().setAuthor("Spire Team");
        //Set subject
        pdf.getDocumentInformation().setSubject("Document Properties Setting");
        //Set key words
        pdf.getDocumentInformation().setKeywords("Document Properties, Spire.PDF");
        //Set creation date
        pdf.getDocumentInformation().setCreationDate(new Date());
        //set creator
        pdf.getDocumentInformation().setCreator("Spire.PDF for Java");
        //set producer
        pdf.getDocumentInformation().setProducer("Spire.PDF for Java");

        //save the document
        pdf.saveToFile("SetProperties.pdf");
    }
}

Set PDF Document Properties in Java

Friday, 07 December 2018 06:16

Apply Different Fonts in PDF in Java

Spire.PDF supports Standard fonts, TrueType fonts and CJK fonts. This tutorial shows you how to apply these fonts when drawing text on a PDF page in Java.

import com.spire.pdf.graphics.*;

public class PdfFonts {

    public static void main(String[] args) {

        //create a pdf document
        PdfDocument doc = new PdfDocument();

        //add a page
        PdfPageBase page = doc.getPages().add();

        //create a brush
        PdfBrush brush = PdfBrushes.getBlack();

        //initialize y coordinate
        float y = 30;

        //draw text using standard fonts
        PdfFont standardFont = new PdfFont(PdfFontFamily.Helvetica, 14f);
        page.getCanvas().drawString("Standard Font - Helvetica", standardFont, brush, 0, y);
        standardFont = new PdfFont(PdfFontFamily.Times_Roman, 14f);
        page.getCanvas().drawString("Standard Font - Times_Roman", standardFont, brush, 0, (y = y + 16));
        standardFont = new PdfFont(PdfFontFamily.Courier, 14f);
        page.getCanvas().drawString("Standard Font - Courier", standardFont, brush, 0, (y = y + 16));

        // draw text using truetype font
        java.awt.Font font = new java.awt.Font("Arial", java.awt.Font.BOLD, 14);
        PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);
        page.getCanvas().drawString("TrueType Font - Arial", trueTypeFont, brush, 0, (y = y + 30f));

        // draw text using private font
        String fontFileName = "C:\\Users\\Administrator\\Desktop\\Khadija.ttf";
        trueTypeFont = new PdfTrueTypeFont(fontFileName, 14f);
        page.getCanvas().drawString("Private Font - Khadija", trueTypeFont, brush, 0, (y = y + 30f));

        // draw text using cjk fonts
        PdfCjkStandardFont cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.Monotype_Hei_Medium, 14f);
        page.getCanvas().drawString("How to say 'Font' in Chinese? \u5B57\u4F53", cjkFont, brush, 0, (y = y + 30f));
        cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.Hanyang_Systems_Gothic_Medium, 14f);
        page.getCanvas().drawString("How to say 'Font' in Japanese? \u30D5\u30A9\u30F3\u30C8", cjkFont, brush, 0, (y = y + 16f));
        cjkFont = new PdfCjkStandardFont(PdfCjkFontFamily.Hanyang_Systems_Shin_Myeong_Jo_Medium, 14f);
        page.getCanvas().drawString("How to say 'Font' in Korean? \uAE00\uAF34", cjkFont, brush, 0, (y = y + 16f));

        //save to file.
        doc.saveToFile("Fonts.pdf");
        doc.close();
    }
}

Apply Different Fonts in PDF in Java

This article demonstrates how to use Spire. PDF for Java to create the bullet list and number list to PDF file.

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.lists.*;
import java.awt.*;
import java.awt.geom.*;

public class PDFlist {
    public static void main(String[] args) {
        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();
        //Set the margin
        PdfMargins margin = new PdfMargins(60,60,40,40);
        //Create one page
        PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);
        float y = 10;
        //Draw title
        PdfBrush brush1 = PdfBrushes.getBlack();
        PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 16), true);
        PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
        page.getCanvas().drawString("Categories List", font1, brush1, page.getCanvas().getClientSize().getWidth() / 2, y, format1);
        y = y + (float) font1.measureString("Categories List", format1).getHeight();
        y = y + 5;

        //Draw text string and set the font
        Rectangle2D rctg = new Rectangle2D.Float();
        rctg.setFrame(new Point(0, 0), page.getCanvas().getClientSize());
        PdfLinearGradientBrush brush = new PdfLinearGradientBrush(rctg, new PdfRGBColor(new PdfRGBColor(new Color(0,0,128))), new PdfRGBColor(new Color(255,69,0)), PdfLinearGradientMode.Vertical);
        PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
        String formatted = "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood";
      
        //Create a number list
        PdfSortedList list = new PdfSortedList(formatted);
        list.setFont(font);
        list.setIndent(8);
        list.setTextIndent(5);
        list.setBrush(brush);
        PdfLayoutResult result = list.draw(page, 0, y);

        doc.saveToFile("output/numberlist.pdf");
        doc.close();
    }
}

Create number list and bullet list on PDF in JAVA

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.lists.*;
import java.awt.*;
import java.awt.geom.*;

public class PDFlist {
    public static void main(String[] args) {
        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();
        //Set the margin
        PdfMargins margin = new PdfMargins(60,60,40,40);
        //Create one page
        PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);
        float y = 10;
        //Draw title
        PdfBrush brush1 = PdfBrushes.getBlack();
        PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 16), true);
        PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
        page.getCanvas().drawString("Categories List", font1, brush1, page.getCanvas().getClientSize().getWidth() / 2, y, format1);
        y = y + (float) font1.measureString("Categories List", format1).getHeight();
        y = y + 5;

        //Draw text string and set the font
        Rectangle2D rctg = new Rectangle2D.Float();
        rctg.setFrame(new Point(0, 0), page.getCanvas().getClientSize());
        PdfLinearGradientBrush brush = new PdfLinearGradientBrush(rctg, new PdfRGBColor(new PdfRGBColor(new Color(0,0,128))), new PdfRGBColor(new Color(255,69,0)), PdfLinearGradientMode.Vertical);
        PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 12f, PdfFontStyle.Bold);
        String formatted = "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood";
      
        //Create a bullet list
        PdfListBase list = new PdfUnorderedList(formatted);
        list.setFont(font);
        list.setIndent(8);
        list.setTextIndent(5);
        list.setBrush(brush);
        PdfLayoutResult result = list.draw(page, 0, y);

        doc.saveToFile("output/list.pdf");
        doc.close();
    }
}

Create number list and bullet list on PDF in JAVA

Wednesday, 05 December 2018 08:56

Draw Superscript and Subscript Text in PDF in C#

In this article, we're going to demonstrate how to draw superscript and subscript text in PDF using Spire.PDF.

Draw Superscript Text

//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 and Subscript Text in PDF in C#

Draw Superscript Text

//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");

Draw Superscript and Subscript Text in PDF in C#