page 103

Java: Hide a Specific Paragraph in Word

2021-11-18 02:19:47 Written by Koohji

In the process of manipulating Word documents, it is sometimes necessary to keep some important information from others. For this reason, we can hide them to ensure confidentiality. This article shows how to hide a specific paragraph in a Word document using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

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

Hide a Specific Paragraph in Word

Spire.Doc for Java supports hiding a specific paragraph in Word by using TextRange.getCharacterFormat().setHidden(boolean value) method. Here are detailed steps to follow.

  • Create a Document instance.
  • Load a sample Word document using Document.loadFromFile() method.
  • Get a specific section of the Word document using Document.getSections().get() method.
  • Get a specific paragraph of the section using Section.getParagraphs().get() method.
  • Loop through the child objects of the paragraph, and convert each child object as a text range if it is plain text. Then hide the text range using TextRange.getCharacterFormat().setHidden(boolean value) method.
  • Save the document to another file using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.*;

public class HideParagraph {
    public static void main(String[] args) {
        //Create a Document instance
        Document document = new Document();

        //Load a sample Word document
        document.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.docx");

        //Get a specific section of Word
        Section sec = document.getSections().get(0);

        //Get a specific paragraph of the section
        Paragraph para = sec.getParagraphs().get(1);

        //Loop through the child objects
        for (Object docObj : para.getChildObjects()) {
            DocumentObject obj = (DocumentObject)docObj;

            //Determine if a child object is an instance of TextRange
            if ((obj instanceof TextRange)) {
                TextRange range = ((TextRange)(obj));

                //Hide the text range
                range.getCharacterFormat().setHidden(true);
            }
        }

        //Save the document to another file
        document.saveToFile("output/hideParagraph.docx", FileFormat.Docx_2013);
    }
}

Java: Hide a Specific Paragraph in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Java: Set Gutter Margins in Word

2021-11-17 06:41:18 Written by Koohji

Gutter margins are designed to add extra space to the existing margins of a document, which ensures that the text of the document will not be obscured when binding. This is very useful when you need to bind some important official documents, books, or examination papers. This article will introduce how to set gutter margins on the left edges of the pages in a Word document using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

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

Set Gutter Margins in Word

Although you can adjust the normal margins to add more space for document binding, setting gutter margins is a more efficient way to achieve the same function. The following are the steps to set gutter margins in a Word document:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Get a specific section using Document.getSections().get() method.
  • Set gutter margin for that specified section using Section.getPageSetup().setGutter() method.
  • Save the document to file using Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import java.io.IOException;

public class addGutter {
    public static void main(String[] args) throws IOException {
        //Create a Document instance
        Document document = new Document();

        //Load a sample Word document
        document.loadFromFile("input.docx");

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

        //Set gutter margin
        section.getPageSetup().setGutter(100f);

        //Save the file
        document.saveToFile("addGutter_output.docx", FileFormat.Docx);
    }
}

Java: Set Gutter Margins in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Java: Find and Replace Text in PDF

2024-03-22 02:57:00 Written by Koohji

Finding and replacing text in PDF documents is essential for updating reports, legal contracts, or any other type of document where accurate and consistent information is crucial. The process involves identifying specific pieces of text and replacing them with new content, allowing users to update placeholder text, correct mistakes, customize document details, or undertake other modifications involving the written word.

This article introduces how to find and replace text in a PDF document in Java by using the Spire.PDF for Java library.

Install Spire.PDF for Java

First of all, you're required to add the Spire.Pdf.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>11.11.11</version>
    </dependency>
</dependencies>

Replace Text in a Specific PDF Page in Java

In Spire.PDF for Java, the PdfTextReplacer class is designed to facilitate text replacement within PDF documents. One of its primary methods, replaceAllText(), enables developers to replace all instances of a specified text on a page with new text.

To replace text in a specific page in Java, follow these steps:

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Get a specific page from the document.
  • Create a PdfTextReplaceOptions object, and specify the replace options using setReplaceType() method of the object.
  • Create a PdfTextReplacer object, and apply the replace options using setOptions() method of it.
  • Replace all instances of the target text in the page with new text using PdfTextReplacer.replaceAllText() method.
  • Save the document to a different PDF file.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.texts.PdfTextReplaceOptions;
import com.spire.pdf.texts.PdfTextReplacer;
import com.spire.pdf.texts.ReplaceActionType;

import java.util.EnumSet;

public class ReplaceTextInPage {

    public static void main(String[] args) {

        // Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        // Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

        // Create a PdfTextReplaceOptions object
        PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

        // Specify the options for text replacement
        textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.IgnoreCase));
        textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.WholeWord));

        // Get a specific page
        PdfPageBase page = doc.getPages().get(0);

        // Create a PdfTextReplacer object based on the page
        PdfTextReplacer textReplacer = new PdfTextReplacer(page);

        // Set the replace options
        textReplacer.setOptions(textReplaceOptions);

        // Replace all instances of target text with new text
        textReplacer.replaceAllText("MySQL", "mysql");

        // Save the document to a different PDF file
        doc.saveToFile("output/ReplaceTextInPage.pdf");

        // Dispose resources
        doc.dispose();
    }
}

Java: Find and Replace Text in PDF

Replace Text in an Entire PDF Document in Java

You already know how to replace text in one page. To replace all instances of a specific text within a PDF document with new text, you just need to iterate through each page of the document and use the PdfTextReplacer.replaceAllText() method to update the text on every page.

The following are the steps to replace text in an entire PDF document using Java.

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Create a PdfTextReplaceOptions object, and specify the replace options using setReplaceType() method of the object.
  • Iterate through the pages in the document.
    • Create a PdfTextReplacer object based on a specified page, and apply the replace options using setOptions() method.
    • Replace all instances of the target text in the page with new text using PdfTextReplacer.replaceAllText() method.
  • Save the document to a different PDF file.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.texts.PdfTextReplaceOptions;
import com.spire.pdf.texts.PdfTextReplacer;
import com.spire.pdf.texts.ReplaceActionType;

import java.util.EnumSet;

public class ReplaceTextInDocument {

    public static void main(String[] args) {

        // Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        // Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

        // Create a PdfTextReplaceOptions object
        PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

        // Specify the options for text replacement
        textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.IgnoreCase));
        textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.WholeWord));

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

            // Get a specific page
            PdfPageBase page = doc.getPages().get(i);

            // Create a PdfTextReplacer object based on the page
            PdfTextReplacer textReplacer = new PdfTextReplacer(page);

            // Set the replace options
            textReplacer.setOptions(textReplaceOptions);

            // Replace all instances of target text with new text
            textReplacer.replaceAllText("MySQL", "mysql");
        }

        // Save the document to a different PDF file
        doc.saveToFile("output/ReplaceTextInDocument.pdf");

        // Dispose resources
        doc.dispose();
    }
}

Replace the First Instance of the Target Text in Java

To replace the first instance of the target text in a page, you can make use of the replaceText() method from the PdfTextReplacer class. Here are the steps to accomplish this task in Java.

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Get a specific page from the document.
  • Create a PdfTextReplaceOptions object, and specify the replace options using replaceType method of the object.
  • Create a PdfTextReplacer object, and apply the replace options using setOptions() method.
  • Replace the first occurrence of the target text in the page with new text using PdfTextReplacer.replaceText() method.
  • Save the document to a different PDF file.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.texts.PdfTextReplaceOptions;
import com.spire.pdf.texts.PdfTextReplacer;
import com.spire.pdf.texts.ReplaceActionType;

import java.util.EnumSet;

public class ReplaceFirstInstance {

    public static void main(String[] args) {

        // Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        // Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

        // Create a PdfTextReplaceOptions object
        PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

        // Specify the options for text replacement
        textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.IgnoreCase));
        textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.WholeWord));

        // Get a specific page
        PdfPageBase page = doc.getPages().get(0);

        // Create a PdfTextReplacer object based on the page
        PdfTextReplacer textReplacer = new PdfTextReplacer(page);

        // Set the replace options
        textReplacer.setOptions(textReplaceOptions);

        // Replace the first instance of target text with new text
        textReplacer.replaceText("MySQL", "mysql");

        // Save the document to a different PDF file
        doc.saveToFile("output/ReplaceFirstInstance.pdf");

        // Dispose resources
        doc.dispose();
    }
}

Java: Find and Replace Text in PDF

Replace Text Based on a Regular Expression in Java

Regular expressions are incredibly powerful and flexible patterns that are commonly used for matching text. When working with Spire.PDF for Java, you can harness the capabilities of regular expressions to search for specific text within a PDF document and replace it or them with new text.

To replace text in a PDF based on a regular expression, you can follow these steps:

  • Create a PdfDocument object.
  • Load a PDF file for a specified path.
  • Get a specific page from the document.
  • Create a PdfTextReplaceOptions object.
  • Specify the replace type as Regex using PdfTextReplaceOptions.setReplaceType() method.
  • Create a PdfTextReplacer object, and apply the replace options using setOptions() method.
  • Find and replace the text that matches a specified regular expression using PdfTextReplacer.replaceAllText() method.
  • Save the document to a different PDF file.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.texts.PdfTextReplaceOptions;
import com.spire.pdf.texts.PdfTextReplacer;
import com.spire.pdf.texts.ReplaceActionType;

import java.util.EnumSet;

public class ReplaceBasedOnRegularExpression {

    public static void main(String[] args) {

        // Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        // Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf");

        // Create a PdfTextReplaceOptions object
        PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();

        // Set the replace type as Regex
        textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.Regex));

        // Get a specific page
        PdfPageBase page = doc.getPages().get(0);

        // Create a PdfTextReplacer object based on the page
        PdfTextReplacer textReplacer = new PdfTextReplacer(page);

        // Set the replace options
        textReplacer.setOptions(textReplaceOptions);

        // Specify the regular expression
        String regularExpression = "\\bS\\w*L\\b";

        // Replace all instances that match the regular expression with new text
        textReplacer.replaceAllText(regularExpression, "NEW");

        // Save the document to a different PDF file
        doc.saveToFile("output/ReplaceWithRegularExpression.pdf");

        // Dispose resources
        doc.dispose();
    }
}

Java: Find and Replace Text in PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

page 103

Coupon Code Copied!

Christmas Sale

Celebrate the season with exclusive savings

Save 10% Sitewide

Use Code:

View Campaign Details