Adding and removing watermarks in PDF documents play a crucial role in document management, copyright protection, and information security. A watermark can serve as a visual marker, such as a company logo, copyright notice, or the word "Confidential", indicating the source, status, or ownership of the document.

Spire.PDF provides a method for adding watermarks by embedding watermark annotations within the PDF document. This approach affords the flexibility to remove the watermark post-insertion, offering users greater control and options. This article will introduce how to add or remove watermark annotations in PDF documents using Spire.PDF for Java in Java projects.

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.12.16</version>
    </dependency>
</dependencies>

Add Watermark Annotations to PDF in Java

Spire.PDF provides a method to add watermark annotations to PDF pages using the PdfWatermarkAnnotation object. Notably, watermarks added by this method can be easily removed later. Here are the key steps involved:

  • Initialize an instance of the PdfDocument class.
  • Load a PDF document using the PdfDocument.loadFromFile() method.
  • Create a PdfTrueTypeFont font object to draw the watermark text.
  • Create a Rectangle2D type object to define the boundary of the page.
  • Use a for loop to iterate over all PdfPageBase objects in the PDF document.
  • Create a PdfTemplate object for drawing the watermark, setting its size to match the current page.
  • Call a custom insertWatermark() method to draw the watermark content onto the PdfTemplate object.
  • Create a PdfWatermarkAnnotation object and define the position where the watermark should be placed.
  • Create a PdfAppearance object to configure the visual effects of the watermark.
  • Use the PdfAppearance.setNormal(PdfTemplate) method to associate the PdfTemplate object with the PdfAppearance object.
  • Use the PdfWatermarkAnnotation.setAppearance(PdfAppearance) method to associate the PdfAppearance object with the PdfWatermarkAnnotation object.
  • Add the watermark annotation to the page by calling PdfPageBase.getAnnotationsWidget().add(PdfWatermarkAnnotation).
  • Save the changes to a file using the PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.annotations.appearance.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;

public class AddWatermarkInPDF {
    public static void main(String[] args) {
        // Create a PdfDocument object
        PdfDocument pdfDocument = new PdfDocument();

        // Load the PDF document from a file
        pdfDocument.loadFromFile("Sample1.pdf");

        // Set the font style
        Font font = new Font("Arial", Font.PLAIN, 22);

        // Create a PdfTrueTypeFont object for subsequent text rendering
        PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);

        // Get the page object
        PdfPageBase page;

        // Define the watermark text
        String watermarkAnnotationText = "ID_0";

        // Create a size object
        Dimension2D dimension2D = new Dimension();

        // Create a rectangle object
        Rectangle2D loRect = new Rectangle2D.Float();

        // Iterate through each page in the PDF
        for (int i = 0; i < pdfDocument.getPages().getCount(); i++) {
            // Get the current page
            page = pdfDocument.getPages().get(i);

            // Set the size object to the size of the current page
            dimension2D.setSize(page.getClientSize().getWidth(), page.getClientSize().getHeight());

            // Set the rectangle object frame, which is the entire page range
            loRect.setFrame(new Point2D.Float(0, 0), dimension2D);

            // Create a PdfTemplate object to draw the watermark
            PdfTemplate template = new PdfTemplate(page.getClientSize().getWidth(), page.getClientSize().getHeight());

            // Insert the watermark
            insertWatermark(template, trueTypeFont, "Non Editable");

            // Create a PdfWatermarkAnnotation object to define the watermark position
            PdfWatermarkAnnotation watermarkAnnotation = new PdfWatermarkAnnotation(loRect);

            // Create a PdfAppearance object to set the watermark appearance
            PdfAppearance appearance = new PdfAppearance(watermarkAnnotation);

            // Set the normal state template of the watermark
            appearance.setNormal(template);

            // Set the appearance to the watermark object
            watermarkAnnotation.setAppearance(appearance);

            // Set the watermark text
            watermarkAnnotation.setText(watermarkAnnotationText);

            // Set the watermark print matrix to control the watermark's position and size
            watermarkAnnotation.getFixedPrint().setMatrix(new float[]{1, 0, 0, 1, 0, 0});

            // Set the horizontal offset
            watermarkAnnotation.getFixedPrint().setHorizontalTranslation(0.5f);

            // Set the vertical offset
            watermarkAnnotation.getFixedPrint().setVerticalTranslation(0.5f);

            // Add the watermark to the page's annotation widget
            page.getAnnotationsWidget().add(watermarkAnnotation);
        }

        // Save the PDF document to a file
        pdfDocument.saveToFile("AddWatermark.pdf");

        // Close and release the PDF document resources
        pdfDocument.dispose();
    }

    static void insertWatermark(PdfTemplate template, PdfTrueTypeFont font, String watermark) {
        // Create a Dimension2D object to set the size of the watermark
        Dimension2D dimension2D = new Dimension();

        // Set the size of the watermark to half the width and one third the height of the template
        dimension2D.setSize(template.getWidth() / 2, template.getHeight() / 3);

        // Create a PdfTilingBrush object for repeating pattern fill of the watermark
        PdfTilingBrush brush = new PdfTilingBrush(dimension2D);

        // Set the transparency of the watermark to 0.3
        brush.getGraphics().setTransparency(0.3F);

        // Start a group of graphic state saves
        brush.getGraphics().save();

        // Translate the graphics context so its center aligns with the center of the watermark tile
        brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 2, (float) brush.getSize().getHeight() / 2);

        // Rotate the graphics context to tilt the watermark at 45 degrees
        brush.getGraphics().rotateTransform(-45);

        // Draw the watermark text in the graphics context using the specified font, color, and centered alignment
        brush.getGraphics().drawString(watermark, font, PdfBrushes.getGray(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));

        // End the group of graphic state saves and restore
        brush.getGraphics().restore();

        // Reset the watermark transparency to 1, i.e., completely opaque
        brush.getGraphics().setTransparency(1);

        // Create a Rectangle2D object to define the area for filling the watermark
        Rectangle2D loRect = new Rectangle2D.Float();

        // Set the fill area for the watermark to cover the entire size of the template
        loRect.setFrame(new Point2D.Float(0, 0), template.getSize());

        // Draw the watermark on the template using the watermark tile
        template.getGraphics().drawRectangle(brush, loRect);
    }
}

Java: Add or Remove Watermark Annotations in PDF Documents

Remove Watermark Annotations from PDF in Java

Spire.PDF can remove watermark annotations added to PDF pages via the PdfWatermarkAnnotation object. Here are the detailed steps:

  • Initialize an instance of the PdfDocument class.
  • Load a PDF document using the PdfDocument.loadFromFile() method.
  • Iterate over every PdfPageBase object in the PDF document using a for loop.
  • Retrieve all annotations on the current page using the PdfPageBase.getAnnotationsWidget() method.
  • Again, use a for loop to iterate over every annotation object on the current page, filtering out annotations of type PdfWatermarkAnnotationWidget.
  • Determine the target watermark annotation by invoking the PdfWatermarkAnnotationWidget.getText() method and perform the deletion operation.
  • Save the changes to a file using the PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.*;
import com.spire.pdf.annotations.*;

public class RemoveWatermarkFromPDF {
    public static void main(String[] args) {
        // Create a PdfDocument object
        PdfDocument pdfDocument = new PdfDocument();

        // Load the PDF document from a file
        pdfDocument.loadFromFile("Sample2.pdf");

        // Define a string ID to match and remove a specific watermark
        String id = "ID_0";

        // Iterate through every page in the PDF document
        for (int i = 0; i < pdfDocument.getPages().getCount(); i++) {
            // Get all annotations on the current page
            PdfAnnotationCollection annotationWidget = pdfDocument.getPages().get(i).getAnnotationsWidget();

            // Iterate through all annotations on the current page
            for (int j = 0; j < annotationWidget.getCount(); j++) {
                // Check if the current annotation is a watermark annotation
                if (annotationWidget.get(j) instanceof PdfWatermarkAnnotationWidget) {
                    // If the watermark text equals the ID, remove the watermark
                    if (annotationWidget.get(j).getText().equals(id)) {
                        annotationWidget.remove(annotationWidget.get(j));
                    }
                }
            }
        }

        // Save the modified PDF document to a new file
        pdfDocument.saveToFile("RemoveWatermark.pdf");

        // Dispose of the PdfDocument object resources
        pdfDocument.dispose();
    }
}

Java: Add or Remove Watermark Annotations in PDF Documents

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.

In Word documents, the ability to add, modify, and remove table borders flexibly can significantly enhance readability and professionalism. Firstly, customizing border styles highlights important information, helping readers quickly locate key data or paragraphs and enhancing visual impact. Secondly, by adjusting the thickness, color, and style of border lines, finer design control can be achieved, ensuring a uniform and aesthetically pleasing document style. Lastly, removing unnecessary borders helps reduce visual clutter, making page layouts cleaner and more comprehensible, improving the reading experience. This article will introduce how to add, modify, or remove Word table borders in Java projects using Spire.Doc for Java.

Install Spire.Doc for Java

First, 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>14.1.3</version>
    </dependency>
</dependencies>

Java Add Word Table Borders

To uniformly add borders to all cells in a table, you need to visit each cell individually and visually set its border properties. Here are the detailed steps:

  • Create a Document object.
  • Load a document using the Document.loadFromFile() method.
  • Retrieve the first section of the document using Document.getSections().get(0).
  • Get the first table within the section using Section.getTables().get(0).
  • Use a for loop to iterate through all cells in the table.
  • Set the cell border to a single line style by using TableCell.getCellFormat().getBorders().setBorderType(BorderStyle.Single).
  • Define the border width to 1 point by using TableCell.getCellFormat().getBorders().setLineWidth(1f).
  • Set the border color to black by using TableCell.getCellFormat().getBorders().setColor(Color.black).
  • Save the changes to the document using the Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;

public class AddBorder {
    public static void main(String[] args) throws Exception{
        // Create a new Document object
        Document doc = new Document();

        // Load the document from file
        doc.loadFromFile("TableExample1.docx");

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

        // Get the first table in the section
        Table table = section.getTables().get(0);

        // Declare TableRow and TableCell variables for use in the loop
        TableRow tableRow;
        TableCell tableCell;

        // Iterate through all rows of the table
        for (int i = 0; i < table.getRows().getCount(); i++) {
            // Get the current row
            tableRow = table.getRows().get(i);

            // Iterate through all cells in the current row
            for (int j = 0; j < tableRow.getCells().getCount(); j++) {
                // Get the current cell
                tableCell = tableRow.getCells().get(j);

                // Set the border style of the current cell to single line
                tableCell.getCellFormat().getBorders().setBorderType(BorderStyle.Single);

                // Set the width of the border
                tableCell.getCellFormat().getBorders().setLineWidth(1f);

                // Set the color of the border
                tableCell.getCellFormat().getBorders().setColor(Color.black);
            }
        }

        // Save the modified document as a new file
        doc.saveToFile("AddBorders.docx", FileFormat.Docx);

        // Close the document to release resources
        doc.close();
    }
}

Java: Add, Modify, or Remove Word Table Borders

Java Modify Word Table Borders

Spire.Doc empowers users with extensive customization options for borders, allowing adjustments such as selecting border styles through TableCell.getCellFormat().getBorders().getBottom().setBorderType(), setting border thickness via TableCell.getCellFormat().getBorders().getBottom().setLineWidth(), and specifying border colors with TableCell.getCellFormat().getBorders().getBottom().setColor(). This enables fine-tuned design of table borders within documents according to personal or project needs. Below are the detailed steps:

  • Instantiate a Document object.
  • Load a document using the Document.loadFromFile() method.
  • Retrieve the first section of the document by calling Document.getSections().get(0).
  • Get the first table within the section using Section.getTables().get(0).
  • Iterate over the cells in the table that require border style changes using a for loop.
  • Change the color of the bottom border to orange by invoking TableCell.getCellFormat().getBorders().getBottom ().setColor(Color.ORANGE).
  • Alter the style of the bottom border to a dashed line by calling TableCell.getCellFormat().getBorders().getBottom ().setBorderType(BorderStyle.Dot_Dash).
  • Modify the width of the bottom border to 2 points by executing TableCell.getCellFormat().getBorders().getBottom ().setLineWidth(2).
  • Save the document using the Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;

public class ModifyBorder {
    public static void main(String[] args) {
        // Create a new Document object
        Document doc = new Document();

        // Load the document from a file
        doc.loadFromFile("TableExample2.docx");

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

        // Get the first table in that section
        Table table = section.getTables().get(0);

        // Declare a TableRow to use within the loop
        TableRow tableRow;

        // Iterate through all rows of the table
        for (int i = 1; i < table.getRows().getCount() - 1; i++) {
            tableRow = table.getRows().get(i);

            // Set the border color of the current cell     tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setColor(Color.ORANGE);
            // Set the border style of the current cell to dotted line
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Dot_Dash);

            // Set the width of the border
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setLineWidth(2);
        }

        // Save the modified document as a new file
        doc.saveToFile("ModifyBorder.docx", FileFormat.Docx);

        // Close the document to release resources
        doc.close();
    }
}

Java: Add, Modify, or Remove Word Table Borders

Java Remove Word Table Borders

When editing Word documents, the flexibility of border design extends beyond the entire table level, allowing for meticulous personalized adjustments at the individual cell level. To comprehensively remove all traces of borders both inside and outside of tables, a phased approach is recommended: Firstly, address the macro-level by clearing the overall border style of the table; subsequently, enter the micro-adjustment phase where each cell within the table is iterated over to revoke its unique border settings. Below are the detailed steps:

  • Instantiate a Document object.
  • Load a document using the Document.loadFromFile() method.
  • Retrieve the first section of the document by calling Document.getSections().get(0).
  • Access the first table within the section using Section.getTables().get(0).
  • Iterate over all cells in the table using a for loop.
  • Remove the border of the table by invoking Table.getTableFormat().getBorders().setBorderType(BorderStyle.None).
  • Eliminate the borders of each cell individually by applying TableCell.getCellFormat().getBorders().setBorderType(BorderStyle.None).
  • Save the modified document using the Document.saveToFile() method.
  • Java
import com.spire.doc.*;
import com.spire.doc.documents.BorderStyle;

public class RemoveBorder {
    public static void main(String[] args) {
        // Create a new Document object
        Document doc = new Document();

        // Load the document from a file
        doc.loadFromFile("TableExample2.docx");

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

        // Get the first table in the section
        Table table = section.getTables().get(0);

        // Remove the borders set on the table
        table.getTableFormat().getBorders().setBorderType(BorderStyle.None);

        // Declare a TableRow to use in the loop
        TableRow tableRow;

        // Iterate through all rows of the table
        for (int i = 0; i < table.getRows().getCount(); i++) {
            tableRow = table.getRows().get(i);
            for (int j = 0; j < tableRow.getCells().getCount(); j++) {
                // Remove all borders set on the cell          tableRow.getCells().get(j).getCellFormat().getBorders().setBorderType(BorderStyle.None);
            }
        }

        // Save the modified document as a new file
        doc.saveToFile("RemoveBorder.docx", FileFormat.Docx);

        // Close the document and release resources
        doc.close();
    }
}

Java: Add, Modify, or Remove Word Table Borders

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.

Presenting information in a logical sequence is vital for an effective PowerPoint presentation. Reordering slides in a PowerPoint document gives you the flexibility to fine-tune your presentation and ensure that it delivers your message with maximum impact. By organizing your slides strategically, you can create a dynamic and engaging presentation experience.

In this article, you will learn how to reorder slides in a PowerPoint document in Java using the Spire.Presentation for Java library.

Install Spire.Presentation for Java

First of all, you're required to add the Spire.Presentation.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.presentation</artifactId>
        <version>11.1.1</version>
    </dependency>
</dependencies>

Reorder Slides in a PowerPoint Document in Java

To rearrange the slides in a PowerPoint presentation, you need to create two presentation objects. One object will be used to load the original document while the other will be used to create a new document. By copying the slides from the original document to the new document in the desired order, you can effectively rearrange the slides.

The following are the steps to rearrange slides in a PowerPoint document using Java.

  • Create a Presentationobject.
  • Load a PowerPoint document using Presentation.loadFromFile() method.
  • Specify the slide order within an array.
  • Create another Presentation object for creating a new presentation.
  • Add the slides from the original document to the new presentation in the specified order using Presentation.getSlides().append() method.
  • Save the new presentation to a PPTX file using Presentation.saveToFile() method.
  • Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class ReorderSlides {

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

        // Create a Presentation object
        Presentation presentation = new Presentation();

        // Load a PowerPoint file
        presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx");

        // Specify the new slide order within an array
        int[] newSlideOrder = new int[] { 3, 4, 1, 2 };

        // Create another Presentation object
        Presentation new_presentation = new Presentation();

        // Remove the default slide
        new_presentation.getSlides().removeAt(0);

        // Iterate through the array
        for (int i = 0; i < newSlideOrder.length; i++)
        {
            // Add the slides from the original PowerPoint file to the new PowerPoint document in the new order
            new_presentation.getSlides().append(presentation.getSlides().get(newSlideOrder[i] - 1));
        }

        // Save the new presentation to file
        new_presentation.saveToFile("output/NewOrder.pptx", FileFormat.PPTX_2019);

        // Dispose resources
        presentation.dispose();
        new_presentation.dispose();
    }
}

Java: Reorder Slides in a PowerPoint Document

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 7 of 81
page 7