Add, Lock and Remove Watermarks in PowerPoint in Java

You can add watermarks to a PowerPoint document to prevent it from illegal usage, or to specify if the document is a “draft” version. Here you will see how to work with watermarks, especially add, lock and remove watermarks in Java using Free Spire.Presentation for Java library.

Before running the following code example, you need to add dependencies for including Free Spire.Presentation for Java library in your Java project.

Add and Lock Watermarks

In general, there are two types of watermarks: text watermark and image watermark. Free Spire.Presentation library supports adding text and image watermarks to a PowerPoint document through the following steps:

  • Load the document.
  • Iterate through the slides in the document.
  • Add a shape (ex. rectangle) to each slide and then insert text/image to the shape.
  • Lock the shape to prevent them from selecting and editing   
  • Save the result document.

Add Text Watermark

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

import java.awt.*;
import java.awt.geom.Rectangle2D;

public class TextWatermark {
    public static void main(String []args) throws Exception {
        //Load a sample document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Input.pptx");

        //Iterate through the slides in the document
        for(int i = 0; i < ppt.getSlides().getCount(); i++)
        {
            ISlide slide = ppt.getSlides().get(i);
            //Add shape to each slide
            IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, new Rectangle2D.Double(340, 150, 300, 200));
            //Set shape name
            shape.setName("textWatermark");
            //Set shape fill type as no fill
            shape.getFill().setFillType(FillFormatType.NONE);
            //Set shape rotation
            shape.setRotation(-45);
            //Lock the shape to protect it from selecting and editing
            shape.getLocking().setSelectionProtection(true);
            //Set shape border type as no border
            shape.getLine().setFillType(FillFormatType.NONE);

            //Add text to shape and set font of the text
            shape.getTextFrame().setText("Confidential");
            PortionEx textRange = shape.getTextFrame().getTextRange();
            textRange.getFill().setFillType(FillFormatType.SOLID);
            textRange.getFill().getSolidColor().setColor(Color.GRAY);
            textRange.setFontHeight(40);
            textRange.setLatinFont(new TextFont("Arial"));
        }

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

Output:

Add Image Watermark

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

import java.awt.geom.Rectangle2D;

public class ImageWatermark {
    public static void main(String []args) throws Exception {
        //Load a document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("Input.pptx");

        //Iterate through the slides in the document
        for(int i = 0; i< ppt.getSlides().getCount(); i++)
        {
            ISlide slide = ppt.getSlides().get(i);
            //Add image to the slide
            IEmbedImage image = slide.getShapes().appendEmbedImage(ShapeType.RECTANGLE, "img.jpg", new Rectangle2D.Double(340, 150, 200, 200));
            //Set image name
            image.setName("imageWatermark");
            //Set image transparency
            image.getPictureFill().getPicture().setTransparency(70);
            //Lock image
            image.getShapeLocking().setSelectionProtection(true);
            image.getLine().setFillType(FillFormatType.NONE);
        }
        
        //Save the result document
        ppt.saveToFile("ImageWatermark.pptx", FileFormat.PPTX_2013);
    }
}

Output:

Remove Watermarks

Use the following code, you’re able to remove either the text watermark shape or the image watermark shape by detecting shape name.

import com.spire.presentation.*;

public class RemoveWatermark {
    public static void main(String []args) throws Exception {
        //Load a document
        Presentation ppt = new Presentation();
        ppt.loadFromFile("imageWatermark.pptx");

        //Iterate through the slides in the document
        for(int i = 0; i < ppt.getSlides().getCount(); i++)
        {
            ISlide slide = ppt.getSlides().get(i);
            //Iterate through the shapes on each slide
            for (int j = slide.getShapes().getCount()-1; j >= 0; j--)
            {
                IShape shape = slide.getShapes().get(j);
                //Remove the shape named "imageWatermark"
                if (shape.getName().equals("imageWatermark"))
                {
                    slide.getShapes().removeAt(j);
                }
            }
        }

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

Output:

Change/Reset PDF Page Size to Standard or Custom Paper Size in Java

Here I am going to introduce how to change/reset PDF page size programmatically in Java using Free Spire.PDF for Java library.

Free Spire.PDF for Java supports to resize pages in a PDF document by creating a new document with new pages of a desired paper size, then creating templates from the original PDF document and later drawing the templates to the new pages of the new document. This process will preserve text, images and all other elements present in the original PDF.

Contents Summary

  • Change PDF Page Size to a Standard Paper Size
  • Change PDF Page Size to a Custom Paper Size
  • Change PDF Page Size by Scaling Down Original Page Size

Before running the following examples, you need to add dependencies for including Free Spire.PDF for Java library in your Java project.

Change PDF Page Size to a Standard Paper Size

Free Spire.PDF for Java supports various standard paper sizes, such as A0, A1, A2, A3, A4…, B0, B1, B2, B3, B4… and many more. The complete types of supported paper sizes can be found in the PdfPageSize Enum.

In the following example, you will see how to change the page size of a PDF document to A1.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageSize;
import com.spire.pdf.graphics.*;

import java.awt.geom.Point2D;

public class ResetToStandardPageSize {
    public static void main(String []args){

        //Create a new PDF document
        PdfDocument newPdf = new PdfDocument();
        //Set page size as A1
        newPdf.getPageSettings().setSize(PdfPageSize.A1);
        //Remove page margins
        newPdf.getPageSettings().setMargins(new PdfMargins(0));

        //Load the sample PDF document
        PdfDocument pdf = new PdfDocument("Sample.pdf");

        //Iterate through the pages in the sample PDF
        for(int i = 0; i< pdf.getPages().getCount(); i++)
        {
            //Add pages to the new PDF
            PdfPageBase newPage = newPdf.getPages().add();
            //Set text layout as 1 page, if not set the content will not scale to fit page size
            PdfTextLayout layout = new PdfTextLayout();
            layout.setLayout(PdfLayoutType.One_Page);
            //Create templates for the pages in sample PDF and draw templates to the pages of the new PDF
            pdf.getPages().get(i).createTemplate().draw(newPage, new Point2D.Float(0,0), layout);
        }

        //Save the result document
        newPdf.saveToFile("ChangePageSizeToA1.pdf");
    }
}

Output PDF:

Change PDF Page Size to a Custom Paper Size

The unit used in Free Spire.PDF library is pt (point). In order to reset PDF page size to a custom paper size, you need to convert the unit from inch, millimeter/centimeter to point.

The following example shows how to change the page size of a PDF document to a custom paper size of 6.5*8.5 inches.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;

public class ResetToCustomPageSize {
    public static void main(String []args){

        //Create a new PDF
        PdfDocument newPdf = new PdfDocument();
        //Convert inches to points 
        PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
        float width = unitCvtr.convertUnits(6.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
        float height = unitCvtr.convertUnits(8.5f, PdfGraphicsUnit.Inch, PdfGraphicsUnit.Point);
        //Specify custom size
        Dimension2D customSize = new Dimension((int)width, (int)height);
        //Set page size as the custom size
        newPdf.getPageSettings().setSize(customSize);
        //Remove page margins
        newPdf.getPageSettings().setMargins(new PdfMargins(0));

        //Load the sample PDF
        PdfDocument pdf = new PdfDocument("Sample.pdf");

        //Iterate through the pages in the sampe PDF
        for(int i = 0; i< pdf.getPages().getCount(); i++)
        {
            //Add pages to the new PDF
            PdfPageBase newPage = newPdf.getPages().add();
            //Set text layout as 1 page, if not set the content will not scale to fit page size
            PdfTextLayout layout = new PdfTextLayout();
            layout.setLayout(PdfLayoutType.One_Page);
            //Create templates for the pages in sample PDF and draw templates to the pages of the new PDF
            pdf.getPages().get(i).createTemplate().draw(newPage, new Point2D.Float(0,0), layout);
        }

        //Save the result document
        newPdf.saveToFile("ChangePageSizetoCustomPaperSize.pdf");
    }
}

Output PDF:

Change PDF Page Size by Scaling Down Original Page Size

The following example shows how to change PDF page size by scaling down the page size of the original PDF document.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;

public class ResetPageSize {
    public static void main(String []args){

        //Load the sample PDF
        PdfDocument pdf = new PdfDocument("Sample.pdf");

        //Sepecify new size by scaling down the orginal page size
        float width = pdf.getPageSettings().getWidth() * 0.8f;
        float height = pdf.getPageSettings().getHeight() * 0.8f;
        Dimension2D newSize = new Dimension((int)width, (int)height);

        //Create a new PDF
        PdfDocument newPdf = new PdfDocument();
        //Set page size as the new size
        newPdf.getPageSettings().setSize(newSize);
        //Remove page margins
        newPdf.getPageSettings().setMargins(new PdfMargins(0));

        //Iterate through the pages in the sampe PDF
        for(int i = 0; i< pdf.getPages().getCount(); i++)
        {
            //Add pages to the new PDF
            PdfPageBase newPage = newPdf.getPages().add();
            //Set text layout as 1 page, if not set the content will not scale to fit page size
            PdfTextLayout layout = new PdfTextLayout();
            layout.setLayout(PdfLayoutType.One_Page);
            //Create templates for the pages in sample PDF and draw templates to the pages of the new PDF
            pdf.getPages().get(i).createTemplate().draw(newPage, new Point2D.Float(0,0), layout);
        }

        //Save the result document
        newPdf.saveToFile("ScaleDownPageSize.pdf");
    }
}

Output PDF:

Add Page Numbers to Existing PDF in Java

Page numbers can help readers to quickly locate a specific page of a document. Every document should have page numbers. This article is aimed to show you how to add page numbers to an existing PDF document programmatically in Java.

Contents Summary

  • Add Dependencies
  • Add Page Numbers to PDF
  • Start Page Number from a Specific Number

Before running the following examples, you need to add dependencies for including Free Spire.PDF for Java library in your Java project.

Add Page Numbers to PDF

import com.spire.pdf.*;
import com.spire.pdf.automaticfields.PdfCompositeField;
import com.spire.pdf.automaticfields.PdfPageCountField;
import com.spire.pdf.automaticfields.PdfPageNumberField;
import com.spire.pdf.graphics.*;

public class AddPageNumbers {
    public static void main(String []args){

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load the PDF document
        pdf.loadFromFile("Sample.pdf");

        //Create a font
        PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11f, PdfFontStyle.Bold);

        //Create a page number field
        PdfPageNumberField pageNumberField = new PdfPageNumberField(font, PdfBrushes.getBlack());

        //Create a page count field
        PdfPageCountField pageCountField = new PdfPageCountField(font, PdfBrushes.getBlack());

        //Add the page number filed and the page count field to a composite filed
        PdfCompositeField compositeField = new PdfCompositeField(font, PdfBrushes.getBlack(), "Page {0} of {1}", pageNumberField, pageCountField);

        //Iterate PDF pages
        for(int i =0; i< pdf.getPages().getCount();i++)
        {
            PdfPageBase page = pdf.getPages().get(i);
            float x = (float) page.getSize().getWidth()/2 - 15;
            float y = (float)page.getSize().getHeight() - font.getHeight() - 20;
            //Draw the composite field to specific location of every page
            compositeField.draw(page.getCanvas(), x, y);
        }

        //Save the result file
        pdf.saveToFile("AddPageNumbers.pdf");
    }
}

Output PDF:

Start Page Number from a Specific Number

In the above example, the page number starts from 1. In the following example, you will see how to start page number from a specific number 10.

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

public class StartFromSpecificNumber {
    public static void main(String []args){

        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load a PDF document
        pdf.loadFromFile("Sample.pdf");

        //Create a font
        PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11f, PdfFontStyle.Bold);

        //Specify the start number
        int startNumber = 10;
        //Get count of pages
        int pageCount = pdf.getPages().getCount();
        //Iterate PDF pages
        for(int i = 0; i< pageCount;i++)
        {
            PdfPageBase page = pdf.getPages().get(i);
            float x = (float) page.getSize().getWidth()/2 - 15;
            float y = (float)page.getSize().getHeight() - font.getHeight() - 20;
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Center);
            String numberLabel = String.format("Page %1$s", startNumber++);
            //Add page numbers to pages
            page.getCanvas().drawString(numberLabel, font, PdfBrushes.getBlack(), x, y, format);
        }

        //Save the result file
        pdf.saveToFile("StartPageNumberFromTen.pdf");
    }
}

Shrink Text to Fit in a Cell in Excel using Java

When inputting a long text in a cell, Excel provides two ways to enable you to fit the text within one cell, they are: wrap text and shrink to fit. In the previous article, I already introduced how to wrap text in Java, in this article, I am going to demonstrate how to shrink text to fit the text in a cell in Java.

The library I used

Free Spire.XLS for Java

Before using the following code, you need to add dependencies for including Free Spire.XLS for Java library in your Java project. For maven project, add the following code to your project’s pom.xml file.

<repositories>    
    <repository>    
        <id>com.e-iceblue</id>    
        <name>e-iceblue</name>    
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>    
    </repository>    
</repositories>    
<dependencies>    
    <dependency>    
        <groupId> e-iceblue </groupId>    
        <artifactId>spire.xls.free</artifactId>    
        <version>3.9.1</version>    
    </dependency>    
</dependencies>

For non-maven project, download Free Spire.XLS for Java pack from this website and add Spire.Xls.jar in the lib folder into your project as a dependency.

Shrink Text to Fit in a Cell

The main steps to shrink text to fit in a cell are:

  • Create a Workbook object and load an Excel file
  • Get the worksheet and cell range to shrink text
  • Call setShrinkToFit method to enable shrinkToFit.
  • Save the result file.

Here is the full code for your reference.

import com.spire.xls.*;

public class ShrinkTextToFitInACell {
    public static void main(String []args) throws Exception {
        //Create a workbook instance
        Workbook workbook = new Workbook();

        //Load the Excel file
        workbook.loadFromFile("Sample.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Get the cell range to shrink text
        CellRange cell = sheet.getRange().get("B2:B3");

        //Enable ShrinkToFit
        cell.getCellStyle().setShrinkToFit(true);

        //Save the file
        workbook.saveToFile("ShrinkTextToFitInACell.xlsx", ExcelVersion.Version2013);
    }
}

The input Excel:

Input Excel before Shrinking Text

The output Excel:

Output Excel after Shrinking Text

Wrap or Unwrap Text in Excel in Java

When a text input in an Excel cell is too long, it overflows to other cells. In Microsoft Excel, you can use the wrap text feature to display the text in multiple lines within one cell, you can also display the text back in one line by unwrapping it. In this article, I am going to demonstrate how to wrap and unwrap text in an Excel file programmatically in Java using Free Spire.XLS for Java library.

Add Dependencies

If you use maven, specify the following dependencies for Free Spire.XLS for Java library in your project’s pom.xml file.

<repositories>    
    <repository>    
        <id>com.e-iceblue</id>    
        <name>e-iceblue</name>    
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>    
    </repository>    
</repositories>    
<dependencies>    
    <dependency>    
        <groupId> e-iceblue </groupId>    
        <artifactId>spire.xls.free</artifactId>    
        <version>3.9.1</version>    
    </dependency>    
</dependencies>

For non-maven projects, download Free Spire.XLS for Java pack from this website and add Spire.Xls.jar in the lib folder into your project as a dependency.

The input Excel:                                                                    

The output Excel:

Wrap Text and Unwrap Text

import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;

public class WrapOrUnwrapText {
    public static void main(String []args) throws Exception {
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load the Excel file
        workbook.loadFromFile("Input.xlsx");

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Wrap text in cell "A1"
        sheet.getRange().get("A1").getStyle().setWrapText(true);

        //Unwrap text in cell "A6"
        sheet.getRange().get("A6").getStyle().setWrapText(false);

        //Save the file
        workbook.saveToFile("WrapOrUnwrapText.xlsx", ExcelVersion.Version2013);
    }
}

Troubleshooting: Wrap Text Didn’t Work?

Sometimes, you may find the wrap text didn’t work. Check if you have set the height of the row as a fixed value. In Microsoft Excel, if you manually modify the height of a row and then format a cell in that row to wrap text, Excel does not change the height of the row to fit all the text in the cell.

The workaround is to change the row height as autofit first and then wrap text, as shown in the following code.

sheet.getRange().get("A1").autoFitRows();
sheet.getRange().get("A1").getStyle().setWrapText(true);

Thanks for taking time to read this article, I hope it’s helpful for you.

Replace Text and Image in PowerPoint in Java

Replacing text or image one-by-one manually is time consuming and inefficient, especially when dealing with a large document with a huge amount of data. In this article, I will demonstrate how to replace text and image in PowerPoint programmatically in Java using Free Spire.Presentation for Java library.

Contents

  • Add dependencies
  • Replace text with new text
  • Replace image with new image

Add Dependencies

There are two ways to include Free Spire.Presentation for Java in your Java project.

For maven projects, add the following dependencies to your project’s pom.xml file:

<repositories>    
    <repository>    
        <id>com.e-iceblue</id>    
        <name>e-iceblue</name>    
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>    
    </repository>    
</repositories>    
<dependencies>    
    <dependency>    
        <groupId> e-iceblue </groupId>    
        <artifactId>spire.presentation.free</artifactId>    
        <version>3.9.0</version>    
    </dependency>    
</dependencies>

For non-maven projects, download Free Spire.Presentation for Java pack, extract the zip file, then add Free Spire.Presentation.jar in the lib folder into your project as a dependency.

Replace text with new text

You can use the replaceFirstText(String matchedString, String newValue, boolean caseSensitive) or  the replaceAllText(String matchedString, String newValue, boolean caseSensitive) methods provided by ISlide class to replace the first occurrence of a text or all the occurrences of a text in a PowerPoint slide.

The input PowerPoint document:

import com.spire.presentation.FileFormat;
import com.spire.presentation.ISlide;
import com.spire.presentation.Presentation;

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

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

        //Replace the first occurrence of a text
        //slide.replaceFirstText("Old String", "New String", false);
        //Replace all the occurrences of a text
        slide.replaceAllText("Old String", "New String", false);

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

Output:

Replace image with new image

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

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

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

        //Load the sample PowerPoint document
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pptx");

        //Add an image to the image collection
        String imagePath = "C:\\Users\\Administrator\\Desktop\\Rabbit.png";
        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(imagePath));
        IImageData image = presentation.getImages().append(bufferedImage);

        //Get the shape collection from the first slide
        ShapeCollection shapes = presentation.getSlides().get(0).getShapes();

        //Loop through the shape collection
        for (int i = 0; i < shapes.getCount(); i++) {

            //Determine if a shape is a picture
            if (shapes.get(i) instanceof SlidePicture) {

                //Fill the shape with a new image
                ((SlidePicture) shapes.get(i)).getPictureFill().getPicture().setEmbedImage(image);
            }
        }

        //Save the document
        presentation.saveToFile("ReplaceImage.pptx", FileFormat.PPTX_2013);
    }
}

Output:

Insert Symbols in Word in Java

You can easily insert symbols like currency (¥), check marks (✔) or copyright sign (©) in your Word document in Microsoft Word. In this article, I will show you how to insert symbols in a Word document programmatically in Java.

Add Dependencies

Free Spire.Doc for Java library is used here for inserting symbols. There are two ways to include Free Spire.Doc for Java in your Java project.

For maven projects, add the following dependencies to your project’s pom.xml file.

<repositories>    
    <repository>    
        <id>com.e-iceblue</id>    
        <name>e-iceblue</name>    
        <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>    
    </repository>    
</repositories>    
<dependencies>    
    <dependency>    
        <groupId> e-iceblue </groupId>    
        <artifactId>spire.doc.free</artifactId>    
        <version>3.9.0</version>    
    </dependency>    
</dependencies>

For non-maven projects, download Free Spire.Doc for Java pack from here: Download- Free Spire.Doc for Java, extract the zip file, then add Spire.Doc.jar in the lib folder into your project as a dependency.

Insert Symbols

Symbols or special characters are either inserted using ASCII or Unicode character codes. In Microsoft Word, you can find the ASCII or Unicode character code of a symbol through the following steps:

  1. Go to Insert >Symbol > More Symbols.
  2. Find the symbol you want.
  3. On the bottom right you’ll see Character code and from. The Character code is what you’ll enter to insert the symbol from the keyboard. The from field tells you if it’s a Unicode or an ASCII character.Symbol ASCII CodeSymbol Unicode code

In the following examples, you will see how to insert checked and unchecked checkbox symbols to a Word document using ASCII and Unicode character codes respectively.

Insert Symbols with ASCII Character Codes

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

public class InsertSymbolsWithASCII {
    public static void main(String []args) throws Exception {
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Get the first section
        Section section = document.getSections().get(0);

        //Add a paragraph to the section
        Paragraph paragraph = section.addParagraph();

        //Use ASCII character code "82" to create a checked checkbox symbol
        String chk_True = Character.toString(((char)82));
        //Append the symbol to the paragraph
        TextRange tr = paragraph.appendText(chk_True);
        //Specify the font name of the symbol as "Wingdings 2"
        tr.getCharacterFormat().setFontName("Wingdings 2");
        //Set font size
        tr.getCharacterFormat().setFontSize(18f);
        //Append some spaces after the created symbol
        paragraph.appendText("              ");

        //Use ASCII character code "163" to create an unchecked checkbox symbol
        String chk_False = Character.toString(((char)163));
        //Append the symbol to the paragraph
        tr = paragraph.appendText(chk_False);
        //Specify the font name of the symbol as "Wingdings 2"
        tr.getCharacterFormat().setFontName("Wingdings 2");
        //Set font size
        tr.getCharacterFormat().setFontSize(18f);

        //Save the document
        document.saveToFile("InsertSymbolsWithASCII.docx", FileFormat.Docx_2013);
    }
}

The output document:

Insert Symbols with Unicode Character Codes

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

public class InsertSymbolsWithUnicode {
    public static void main(String []args) throws Exception {
        //Create a Document instance
        Document document = new Document();
        //Load a Word document
        document.loadFromFile("Sample.docx");

        //Get the first section
        Section section = document.getSections().get(0);

        //Add a paragraph to the section
        Paragraph paragraph = section.addParagraph();

        //Use Unicode character code "0052" to create a checked checkbox symbol
        String chk_True = "\u0052".toString();
        //Append the symbol to the paragraph
        TextRange tr = paragraph.appendText(chk_True);
        //Specify the font name of the symbol as "Wingdings 2"
        tr.getCharacterFormat().setFontName("Wingdings 2");
        //Set font size
        tr.getCharacterFormat().setFontSize(18f);
        //Append some spaces after the created symbol
        paragraph.appendText("              ");

        //Use Unicode character code "00A3" to create an unchecked checkbox symbol
        String chk_False = "\u00A3".toString();
        //Append the symbol to the paragraph
        tr = paragraph.appendText(chk_False);
        //Specify the font name of the symbol as "Wingdings 2"
        tr.getCharacterFormat().setFontName("Wingdings 2");
        //Set font size
        tr.getCharacterFormat().setFontSize(18f);

        //Save the document
        document.saveToFile("InsertSymbolsWithUnicode.docx", FileFormat.Docx_2013);
    }
}

The output document:

This image has an empty alt attribute; its file name is wordsymbolwithascii.png


That’s all. I hope it’s helpful for you. Thanks for reading and happy coding.

Design a site like this with WordPress.com
Get started