Embedding and applying private fonts in Word or PDF documents can make your documents unique. This article demonstrates how to embed fonts in a Word document as well as a converted PDF document, by using Spire.Doc for Java.

Embed Font in Word Document

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

public class EmbedPrivateFontInWord {

    public static void main(String[] args) {

        //create a Word document
        Document document = new Document();
        
        //add a paragraph
        Paragraph paragraph = document.addSection().addParagraph();
        
        //embed a private font in Word document
        document.setEmbedFontsInFile(true);
        PrivateFontPath fontPath = new PrivateFontPath();
        fontPath.setFontName("Otto");
        fontPath.setFontPath("C:\\Users\\Administrator\\Desktop\\Otto.ttf");
        document.getPrivateFontList().add(fontPath);

        //add text to the paragraph and apply the embedded font 
        TextRange tr = paragraph.appendText("Spire.Doc for Java is a professional Java Word API that enables "+
                        "Java applications to create, convert, manipulate and print Word documents without " +
                        "using Microsoft Office.");
        tr.getCharacterFormat().setFontName("Otto");
        tr.getCharacterFormat().setFontSize(26f);

        //save to file
        document.saveToFile("output/EmbedFont.docx", FileFormat.Docx);
    }
}

Embed Private Fonts When Saving Word to DOCX and PDF in Java

Embed Font in Converted PDF Document

import com.spire.doc.Document;
import com.spire.doc.PrivateFontPath;
import com.spire.doc.ToPdfParameterList;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

import java.util.*;

public class EmbedPrivateFontInConvertedPDF {

    public static void main(String[] args) {

        //create a Word document
        Document document = new Document();

        //add a paragraph
        Paragraph paragraph = document.addSection().addParagraph();

        //initialize PrivateFontPath object
        PrivateFontPath fontPath = new PrivateFontPath("Otto","C:\\Users\\Administrator\\Desktop\\Otto.ttf");

        //add text to paragraph and apply the private font
        TextRange tr = paragraph.appendText("Spire.Doc for Java is a professional Java Word API that enables "+
                "Java applications to create, convert, manipulate and print Word documents without " +
                "using Microsoft Office.");
        tr.getCharacterFormat().setFontName("Otto");
        tr.getCharacterFormat().setFontSize(26f);

        //create a ToPdfParameterList object
        ToPdfParameterList toPdfParameterList = new ToPdfParameterList();

        //set private fonts as a parameter for converting Word to PDF
        List pathList = new LinkedList<>();
        pathList.add(fontPath);
        toPdfParameterList.setPrivateFontPaths(pathList);

        //save to PDF file
        document.saveToFile("output/EmbedFont.pdf",toPdfParameterList);
    }
}

Embed Private Fonts When Saving Word to DOCX and PDF in Java

Wednesday, 03 July 2019 09:23

Compress PDF document in Java

Written by support iceblue

This article will demonstrate how to use Spire.PDF for Java to compress PDF document by compressing the PDF contents and compressing the images in the PDF document.

Compressing content

import com.spire.pdf.*;

public class CompressPDF {

    public static void main(String[] args) {

        String inputFile = "Sample.pdf";
        String outputFile = "output/CompressPDFcontent.pdf";

        PdfDocument document = new PdfDocument();
        document.loadFromFile(inputFile);

        document.getFileInfo().setIncrementalUpdate(false);

        document.setCompressionLevel(PdfCompressionLevel.Best);

        document.saveToFile(outputFile, FileFormat.PDF);
        document.close();
        }
    }

Effective screenshot after compressing the PDF content:

Compress PDF document in Java

Compressing image

import com.spire.pdf.*;
import com.spire.pdf.exporting.PdfImageInfo;
import com.spire.pdf.graphics.PdfBitmap;

public class CompressPDF {

    public static void main(String[] args) {

        String inputFile = "Sample.pdf";
        String outputFile = "output/CompressPDFImage.pdf";

        PdfDocument document = new PdfDocument();
        document.loadFromFile(inputFile);

        document.getFileInfo().setIncrementalUpdate(false);

        for (int i = 0; i < document.getPages().getCount(); i++) {

            PdfPageBase page = document.getPages().get(i);
            PdfImageInfo[] images = page.getImagesInfo();
            if (images != null && images.length > 0)
                for (int j = 0; j < images.length; j++) {
                    PdfImageInfo image = images[j];
                    PdfBitmap bp = new PdfBitmap(image.getImage());
                    bp.setQuality(20);
                    page.replaceImage(j, bp);

                }
        }
        document.saveToFile(outputFile, FileFormat.PDF);
        document.close();
    }
}

Effective screenshot after compressing the PDF image:

Compress PDF document in Java

Friday, 28 June 2019 08:34

Replace Bookmark Content in Word in Java

Written by support iceblue

This article will show you have to replace the content of a bookmark in Word using Spire.Doc for Java.

Replace bookmark content with text

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class ReplaceBookmark {
    public static void main(String[] args){
        //load the Word document
        Document doc = new Document("Bookmark.docx");

        //locate the bookmark."SimpleBookmark"
        BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
        bookmarkNavigator.moveToBookmark("SimpleBookmark");

        //replace the content of the bookmark with text
        bookmarkNavigator.replaceBookmarkContent("This is new content", false);

        //save the resultant document
        doc.saveToFile("ReplaceWithText.docx", FileFormat.Docx);
    }
}

Replace Bookmark Content in Word in Java

Replace bookmark content with HTML string

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.ParagraphBase;

public class ReplaceBookmark {
    public static void main(String[] args){
        //load the Word document
        Document doc = new Document("Bookmark.docx");

        //add a temp section
        Section tempSection = doc.addSection();
        //add a paragraph and append Html string
        String html = "

This Bookmark has been edited

"; tempSection.addParagraph().appendHTML(html); //Get the first item and the last item of the paragraph ParagraphBase firstItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getFirstItem(); ParagraphBase lastItem = (ParagraphBase)tempSection.getParagraphs().get(0).getItems().getLastItem(); //create a TextBodySelection object TextBodySelection selection = new TextBodySelection(firstItem, lastItem); //create a TextBodyPart object TextBodyPart bodyPart = new TextBodyPart(selection); //locate the bookmark "SimpleBookmark" BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc); bookmarkNavigator.moveToBookmark("SimpleBookmark"); //replace the content of the bookmark with Html string bookmarkNavigator.replaceBookmarkContent(bodyPart); //remove the temp section doc.getSections().remove(tempSection); //save the resultant document doc.saveToFile("ReplaceWithHTMLString.docx", FileFormat.Docx); } }

Replace Bookmark Content in Word in Java

Replace bookmark content with table

import com.spire.doc.*;
import com.spire.doc.documents.*;

public class ReplaceBookmark {
    public static void main(String[] args){
        //load the Word document
        Document doc = new Document("Bookmark.docx");

        String[][] data =
                {
                        new String[]{"Name", "Capital", "Continent", "Area"},
                        new String[]{"Bolivia", "La Paz", "South America", "1098575"},
                        new String[]{"Brazil", "Brasilia", "South America", "8511196"},
                        new String[]{"Canada", "Ottawa", "North America", "9976147"},
                        new String[]{"Chile", "Santiago", "South America", "756943"},
                };

        //create a table
        Table table = new Table(doc,true);
        table.resetCells(5, 4);
        for (int i = 0; i < data.length; i++) {
            TableRow dataRow = table.getRows().get(i);
            for (int j = 0; j < data[i].length; j++) {
                dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
            }
        }

        //create a TextBodyPart object
        TextBodyPart bodyPart= new TextBodyPart(doc);
        bodyPart.getBodyItems().add(table);

        //locate the bookmark "SimpleBookmark"
        BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
        bookmarkNavigator.moveToBookmark("SimpleBookmark");

        //replace the content of the bookmark with table
        bookmarkNavigator.replaceBookmarkContent(bodyPart);

        //save the resultant document
        doc.saveToFile("ReplaceWithTable.docx", FileFormat.Docx);
    }
}

Replace Bookmark Content in Word in Java

Page 2 of 195