A footer is text or other content located at the bottom of a page, usually including page numbers, dates, authors, and other information. Footers contribute to the overall professional appearance of the document by maintaining a consistent layout across all pages. By incorporating footers, PDF documents become more professional, user-friendly, and compliant with legal requirements. This article demonstrates how to add a footer to an existing PDF document in Java using Spire.PDF for Java.

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

Background Knowledge

When using Spire.PDF for Java to process an existing PDF document, the origin of the coordinate system is located at the top left corner of the page, with the x-axis extending to the right and the y-axis extending downward. Adding a footer to a page means adding content, such as text, images, automatic fields and shapes, to a specified location in the bottom blank area of the page.

Java: Add a Footer to an Existing PDF Document

If the blank area is not large enough to accommodate the content you want to add, you can consider increasing the PDF page margins.

Add a Footer to an Existing PDF Document in Java

Spire.PDF for Java offers the PdfCanvas.drawString() method, PdfCanvas.drawImage() method, PdfCanvas.drawLine() method and its similar methods, allowing users to draw text, images and shapes on a PDF page at the specified location. To add dynamic data to the footer, such as page numbers, sections, dates, you need to use the automatic fields. Spire.PDF for Java provides the PdfPageNumberField class, PdfPageCountField calss, PdfSectionNumberField class etc. to achieve the addition of dynamic information.

The following are the steps to add a footer consisting of an image and page number to a PDF document using Spire.PDF for Java.

  • Create a PdfDocument object.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Load an image using PdfImage.fromFile() method.
  • Draw the image on the bottom blank area of a page using PdfPageBase.getCanvas().drawImage() method.
  • Create a PdfPageNumberField object, a PdfPageCountField object, and combine them in a PdfCompositefield object to return the string "Page X of Y".
  • Draw page number on the bottom blank area of a page using PdfCompositeField.draw() method.
  • Save the document to another PDF file using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.automaticfields.PdfCompositeField;
import com.spire.pdf.automaticfields.PdfPageCountField;
import com.spire.pdf.automaticfields.PdfPageNumberField;
import com.spire.pdf.graphics.PdfBrush;
import com.spire.pdf.graphics.PdfBrushes;
import com.spire.pdf.graphics.PdfImage;
import com.spire.pdf.graphics.PdfTrueTypeFont;

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

public class AddFooterToPdf {

    public static void main(String[] args) {

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

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

        //Load an image
        PdfImage footerImage = PdfImage.fromFile("C:\\Users\\Administrator\\Desktop\\bg.jpg");

        //Create a true type font
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Times New Roman", Font.BOLD, 12),true);

        //Create a brush
        PdfBrush brush = PdfBrushes.getWhite();

        //Create a page number field
        PdfPageNumberField pageNumberField = new PdfPageNumberField();

        //Create a page count field
        PdfPageCountField pageCountField = new PdfPageCountField();

        //Create a composite field to combine page count field and page number field in a single string
        PdfCompositeField compositeField = new PdfCompositeField(font, brush, "Page {0} of {1}", pageNumberField, pageCountField);

        //Get the text size
        Dimension2D fontSize = font.measureString(compositeField.getText());

        //Get the page size
        Dimension2D pageSize = doc.getPages().get(0).getSize();

        //Set the position of the composite field
        compositeField.setLocation(new Point2D.Double((pageSize.getWidth() - fontSize.getWidth())/2,  pageSize.getHeight() - 45));

        //Loop through the pages in the document
        for (int i = 0; i < doc.getPages().getCount(); i++)
        {
            //Get a specific page
            PdfPageBase page = doc.getPages().get(i);

            //Draw the image on the bottom blank area
            page.getCanvas().drawImage(footerImage, 55, pageSize.getHeight() - 65, pageSize.getWidth() - 110, 50);

            //Draw the composite field on the bottom blank area
            compositeField.draw(page.getCanvas());
        }

        //Save to file
        doc.saveToFile("output/AddFooter.pdf");
        doc.dispose();
    }
}

Java: Add a Footer to an Existing PDF 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.

Named ranges in Excel are valuable tools that empower you to assign meaningful names to specific cells or ranges within your spreadsheets. Instead of relying on traditional cell references like A1:B10, named ranges allow you to reference data by their logical names, making your formulas more intelligible and easier to understand and maintain. This article will demonstrate how to create, edit or delete named ranges in Excel in Java using Spire.XLS for Java.

Install Spire.XLS for Java

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

Create a Named Range in Excel in Java

You can use the Workbook.getNameRanges().add(String name) method provided by Spire.XLS for Java to add a named range to an Excel workbook. Once the named range is added, you can define the cell or range of cells it refers to using the INamedRange.setRefersToRange(IXLSRange range) method. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel workbook using the Workbook.loadFromFile() method.
  • Add a named range to the workbook using the Workbook.getNameRanges().add(String name) method.
  • Get a specific worksheet in the workbook using the Workbook.getWorksheets().get(int index) method.
  • Set the cell range that the named range refers to using the INamedRange.setRefersToRange(IXLSRange range) method.
  • Save the result file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.spire.xls.core.INamedRange;

public class CreateNamedRange {
    public static void main(String[] args) {
        //Initialize an instance of the Workbook class
        Workbook workbook = new Workbook();
        //Load an Excel workbook
        workbook.loadFromFile("Sample.xlsx");

        //Add a named range to the workbook
        INamedRange namedRange = workbook.getNameRanges().add("Amount");

        //Get a specific worksheet in the workbook
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Set the cell range that the named range references
        namedRange.setRefersToRange(sheet.getCellRange("D2:D5"));

        //Save the result file to a specific location
        String result = "CreateNamedRange.xlsx";
        workbook.saveToFile(result, ExcelVersion.Version2013);
        workbook.dispose();
    }
}

Java: Create, Edit or Delete Named Ranges in Excel

Edit an Existing Named Range in Excel in Java

After you've created a named range, you may want to modify its name or adjust the cells it refers to. The following are the detailed steps:

  • Initialize an instance of the Workbook class.
  • Load an Excel workbook using the Workbook.loadFromFile() method.
  • Get a specific named range in the workbook using the Workbook.getNameRanges().get(int index) method.
  • Modify the name of the named range using the INamedRange.setName(String name) method.
  • Modify the cells that the named range refers to using the INamedRange.setRefersToRange(IXLSRange range) method.
  • Save the result file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.core.INamedRange;

public class ModifyNamedRange {
    public static void main(String[] args) {
        //Initialize an instance of the Workbook class
        Workbook workbook = new Workbook();
        //Load an Excel workbook
        workbook.loadFromFile("CreateNamedRange.xlsx");

        //Get a specific named range in the workbook
        INamedRange namedRange = workbook.getNameRanges().get(0);

        //Change the name of the named range
        namedRange.setName("MonitorAmount");

        //Set the cell range that the named range references
        namedRange.setRefersToRange(workbook.getWorksheets().get(0).getCellRange("D2"));

        //Save the result file to a specific location
        String result = "ModifyNamedRange.xlsx";
        workbook.saveToFile(result, ExcelVersion.Version2013);
        workbook.dispose();
    }
}

Java: Create, Edit or Delete Named Ranges in Excel

Delete a Named Range from Excel in Java

If you have made significant changes to the structure or layout of your spreadsheet, it might be necessary to delete a named range that is no longer relevant or accurate. The detailed steps are as follows:

  • Initialize an instance of the Workbook class.
  • Load an Excel workbook using the Workbook.loadFromFile() method.
  • Remove a specific named range by its index or name using the Workbook.getNameRanges().removeAt(int index) or Workbook.getNameRanges().remove(string name) method.
  • Save the result file using the Workbook.saveToFile() method.
  • Java
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;

public class DeleteNamedRange {
    public static void main(String[] args) {
        //Initialize an instance of the Workbook class
        Workbook workbook = new Workbook();
        //Load an Excel workbook
        workbook.loadFromFile("CreateNamedRange.xlsx");

        //Remove a specific named range by its index
        workbook.getNameRanges().removeAt(0);

        //Remove a specific named range by its name
        //workbook.getNameRanges().remove("Amount");

        //Save the result file to a specific location
        String result = "RemoveNamedRange.xlsx";
        workbook.saveToFile(result, ExcelVersion.Version2013);
        workbook.dispose();
    }
}

Java: Create, Edit or Delete Named Ranges in Excel

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.

Repeating watermarks, also called multi-line watermarks, are a type of watermark that appears multiple times on a page of a Word document at regular intervals. Compared with single watermarks, repeating watermarks are more difficult to remove or obscure, thus offering a better deterrent to unauthorized copying and distribution. This article is going to show how to insert repeating text and image watermarks into Word documents programmatically 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>14.4.0</version>
    </dependency>
</dependencies>

Add Repeating Text Watermarks to Word Documents in Java

We can insert repeating text watermarks to Word documents by adding repeating WordArt to the headers of a document at specified intervals. The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Create an object of ShapeObject class and set the WordArt text using ShapeObject.getWordArt().setText() method.
  • Specify the rotation angle and the number of vertical repetitions and horizontal repetitions.
  • Set the format of the shape using methods under ShapeObject class.
  • Loop through the sections in the document to insert repeating watermarks to each section by adding the WordArt shape to the header of each section multiple times at specified intervals using Paragraph.getChildObjects().add(ShapeObject) method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;

import java.awt.*;

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

        //Create an object of Document class
        Document doc = new Document();

        //Load a Word document
        doc.loadFromFile("Sample.docx");

        //Create an object of ShapeObject class and set the WordArt text
        ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
        shape.getWordArt().setText("DRAFT");

        //Specify the watermark rotating angle and the number of vertical repetitions and horizontal repetitions
        double rotation = 315;
        int ver = 5;
        int hor = 3;

        //Set the format of the WordArt shape
        shape.setWidth(60);
        shape.setHeight(20);
        shape.setVerticalPosition(30);
        shape.setHorizontalPosition(20);
        shape.setRotation(rotation);
        shape.setFillColor(Color.BLUE);
        shape.setLineStyle(ShapeLineStyle.Single);
        shape.setStrokeColor(Color.CYAN);
        shape.setStrokeWeight(1);

        //Loop through the sections in the document
        for (Section section : (Iterable<Section>) doc.getSections()) {
            //Get the header of a section
            HeaderFooter header = section.getHeadersFooters().getHeader();
            //Add paragraphs to the header
            Paragraph paragraph = header.addParagraph();
            for (int i = 0; i < ver; i++) {
                for (int j = 0; j < hor; j++) {
                    //Add the WordArt shape to the header
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition((float) (section.getPageSetup().getPageSize().getHeight()/ver * i + Math.sin(rotation) * shape.getWidth()/2));
                    shape.setHorizontalPosition((float) ((section.getPageSetup().getPageSize().getWidth()/hor - shape.getWidth()/2) * j));
                    paragraph.getChildObjects().add(shape);
                }
            }
        }

        //Save the document
        doc.saveToFile("RepeatingTextWatermark.docx");
        doc.dispose();
    }
}

Java: Insert Repeating Watermarks into Word Documents

Add Repeating Picture Watermarks to Word Documents in Java

Similarly, we can insert repeating image watermarks into Word documents by adding repeating pictures to headers at regular intervals. The detailed steps are as follows:

  • Create an object of Document class.
  • Load a Word document using Document.loadFromFile() method.
  • Load a picture using DocPicture.loadImage() method.
  • Set the text wrapping style of the picture as Behind using DocPicture.setTextWrappingStyle(TextWrappingStyle.Behind) method.
  • Specify the number of vertical repetitions and horizontal repetitions.
  • Loop through the sections in the document to insert repeating picture watermarks to the document by adding a picture to the header of each section at specified intervals using Paragraph.getChildObjects().add(DocPicture) method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;

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

        //Create an object of Document class
        Document doc = new Document();

        //Load a Word document
        doc.loadFromFile("Sample.docx");

        //Load a picture
        DocPicture pic = new DocPicture(doc);
        pic.loadImage("watermark.png");

        //Set the text wrapping style of the picture as Behind
        pic.setTextWrappingStyle(TextWrappingStyle.Behind);

        //Specify the number of vertical repetitions and horizontal repetitions
        int ver = 4;
        int hor = 3;

        //Loop through the sections in the document
        for (Section section : (Iterable<Section>) doc.getSections()) {
            //Get the header of a section
            HeaderFooter header = section.getHeadersFooters().getHeader();
            //Add a paragraph to the section
            Paragraph paragraph = header.addParagraph();
            for (int i = 0; i < ver; i++) {
                for (int j = 0; j < hor; j++) {
                    //Add the picture to the header
                    pic = (DocPicture) pic.deepClone();
                    pic.setVerticalPosition((float) ((section.getPageSetup().getPageSize().getHeight()/ver) * i));
                    pic.setHorizontalPosition((float) (section.getPageSetup().getPageSize().getWidth()/hor - pic.getWidth()/2) * j);
                    paragraph.getChildObjects().add(pic);
                }
            }
        }

        //Save the document
        doc.saveToFile("RepeatingPictureWatermark.docx", FileFormat.Auto);
        doc.dispose();
    }
}

Java: Insert Repeating Watermarks into Word 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.

Page 10 of 81
page 10