Get Bookmark Text in Java

2019-08-08 08:48:58 Written by Koohji

This article demonstrates how to get the text inside a bookmark in a Word document using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.documents.BookmarksNavigator;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextBodyPart;
import com.spire.doc.fields.TextRange;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class GetBookmarkText {

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

        //create a Document object
        Document doc = new Document();

        //load a sample Word file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

        //get the specific bookmark
        BookmarksNavigator navigator = new BookmarksNavigator(doc);
        navigator.moveToBookmark("MyBookmark");

        //get the bookmark content
        TextBodyPart textBodyPart = navigator.getBookmarkContent();

        //declare a String variable
        String text = "";

        //loop through body items
        for (Object item : textBodyPart.getBodyItems()) {

            //determine if an item is a paragraph
            if (item instanceof Paragraph) {
                Paragraph paragraph = (Paragraph) item;

                //loop through the child objects of the paragraph
                for (Object childObj : paragraph.getChildObjects()) {

                    //determine if a child object is a text range
                    if (childObj instanceof TextRange) {

                        //get text from the text range
                        TextRange textRange = (TextRange) childObj;
                        text = text + textRange.getText();
                    }
                }
            }
        }

        //write the bookmark text to a .txt file
        PrintWriter printWriter = new PrintWriter("output/BookmarkText.txt");
        printWriter.println(text);
        printWriter.close();
    }
}

Get Bookmark Text in Java

We can format Word document in a multi-column newsletter layout by adding columns. This article demonstrates how to add multiple columns to a Word document and specify the column width and the spacing between columns using Spire.Doc for Java.

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

public class CreateMutiColumnWordDocument {
    public static void main(String[] args){
        //create a Document object
        Document document = new Document();
        //add a section 
        Section section = document.addSection();

        //add 3 columns to the section
        section.addColumn(100, 20);
        section.addColumn(100, 20);
        section.addColumn(100, 20);

        //add a paragraph to the section
        Paragraph paragraph = section.addParagraph();
        //add a paragraph to the section
        paragraph = section.addParagraph();
        String text = "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.";
        //add text to the paragraph
        paragraph.appendText(text);
        //add column break to the paragraph
        paragraph.appendBreak(BreakType.Column_Break);

        //add a paragraph to the section
        paragraph = section.addParagraph();
        //add text to the paragraph
        paragraph.appendText(text);
        //add column break to the paragraph
        paragraph.appendBreak(BreakType.Column_Break);

        //add a paragraph to the section
        paragraph = section.addParagraph();
        //add text to the paragraph
        paragraph.appendText(text);

        //add line between columns
        section.getPageSetup().setColumnsLineBetween(true);

        //save the resultant document
        document.saveToFile("Muti-Column Document.docx", FileFormat.Docx_2013);

    }
}

Output:

Create a Multi-Column Word Document in Java

Java make a booklet from a PDF document

2019-08-07 07:28:56 Written by Koohji

When we print a huge PDF document, print as a booklet is a great way to save the paper and make the pages tidy. This article we will introduce how to create a booklet from a PDF document in Java applications.

import com.spire.pdf.*;


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

        String inputPath = "Sample.pdf";

        PdfDocument doc = new PdfDocument();
        doc.loadFromFile(inputPath);
        PdfPageBase page = doc.getPages().get(0);

        float width = (float) page.getSize().getWidth()*2;
        float height = (float) page.getSize().getHeight();

        doc.createBooklet(inputPath, width, height,true);

        doc.saveToFile("Output/Booklet.pdf");

    }
}

Effective screenshot after creating PDF booklet:

Java make a booklet from a PDF document

page 163