Hyperlinks in Word documents are usually displayed blue with an underline. Actually, if you don't like the default style of hyperlinks, you can change the color of hyperlinks or remove the underline to make your own hyperlink style while keeping the link. This article shows how to change the color or remove the underline of hyperlinks in Word by programming 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.4.0</version>
    </dependency>
</dependencies>

Change the Color of a Hyperlink or Remove the Underline of a Hyperlink in a Word Document

The detailed steps are as follows:

  • Create an object of Document class.
  • Add a section to the document using Document.addSection() method.
  • Add a paragraph to the section using Section.addParagraph() method.
  • Insert a normal hyperlink using Paragraph.appendHyperlink() method.
  • Add a paragraph and a hyperlink, and then change the color of the hyperlink text to red using TextRange.getCharacterFormat().setTextColor() method.
  • Add a paragraph and a hyperlink, and then remove the underline of the hyperlink using TextRange.getCharacterFormat().setUnderlineStyle() method.
  • Save the document using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class ChangeHyperlink {

    public static void main(String[] args) {

        //Create a Document object
        Document document = new Document();

        //Add a section
        Section section = document.addSection();

        //Add a paragraph
        Paragraph para= section.addParagraph();
        para.appendText("A regular hyperlink: ");

        //Insert a hyperlink
        TextRange textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link);
        textRange.getCharacterFormat().setFontName("Times New Roman");
        textRange.getCharacterFormat().setFontSize(12f);
        para.appendBreak(BreakType.Line_Break);


        //Add a paragraph
        para = section.addParagraph();
        para.appendText("Change the color of the hyperlink: ");

        //Insert a hyperlink
        textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link);
        textRange.getCharacterFormat().setFontName("Times New Roman");
        textRange.getCharacterFormat().setFontSize(12f);
        //Change the color of the hyperlink to red
        textRange.getCharacterFormat().setTextColor(Color.RED);
        para.appendBreak(BreakType.Line_Break);

        //Add a paragraph
        para = section.addParagraph();
        para.appendText("Remove the underline of the hyperlink: ");

        //Insert a hyperlink
        textRange = para.appendHyperlink("www.e-iceblue.com", "E-iceblue", HyperlinkType.Web_Link);
        textRange.getCharacterFormat().setFontName("Times New Roman");
        textRange.getCharacterFormat().setFontSize(12f);
        //Remove the underline of the hyperlink
        textRange.getCharacterFormat().setUnderlineStyle(UnderlineStyle.None);

        //Save the document
        document.saveToFile("ChangeHyperlink.docx", FileFormat.Docx_2013);
    }
}

Java: Change the Color or Remove the Underline of Hyperlinks 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.

There are two kinds of special indents styles for the paragraph on the presentation slides, first line and hanging. This article will demonstrate how to set the indents and spacing for the paragraph on presentation slide in Java applications.

import com.spire.presentation.*;

public class IndentStyle {
    public static void main(String[] args)  throws Exception{
        //Load the sample document
        Presentation presentation = new Presentation();
        presentation.loadFromFile("Indent.pptx");

        //get the shapes
        IAutoShape shape = (IAutoShape) presentation.getSlides().get(0).getShapes().get(0);

        //set the indent, margin and space for the first paragraph
        shape.getTextFrame().getParagraphs().get(0).setIndent(20);
        shape.getTextFrame().getParagraphs().get(0).setLeftMargin(10);
        shape.getTextFrame().getParagraphs().get(0).setSpaceAfter(10);

        //set the indent, margin and space for the third paragraph
        shape.getTextFrame().getParagraphs().get(2).setIndent(-100);
        shape.getTextFrame().getParagraphs().get(2).setLeftMargin(40);
        shape.getTextFrame().getParagraphs().get(2).setSpaceBefore(0);
        shape.getTextFrame().getParagraphs().get(2).setSpaceAfter(0);

        //save the document to file
        String output = "output/result.pptx";
        presentation.saveToFile(output, FileFormat.PPTX_2010);
    }
}

Effective screenshot after setting the indent style of paragraphs on presentation slide:

Java set the intents and spacing for paragraph on the presentation slide

A large number of users today preserve different files within PDF documents as attachments. These attachments can be extracted and used for other purposes when necessary. Basically, there are two types of attachments in PDF: document level attachment and annotation attachment. Below are the differences between them.

  • Document Level Attachment (represented by PdfAttachment class): A file attached to a PDF at the document level won't appear on a page, but can only be viewed in the "Attachments" panel of a PDF reader.
  • Annotation Attachment (represented by PdfAttachmentAnnotation class): A file will be added to a specific position of a page. Annotation attachments are shown as a paper clip icon on the page; reviewers can double-click the icon to open the file.

In this article, you will learn how to extract these two kinds of attachments from a PDF document in Java using Spire.PDF for Java.

Install Spire.PDF for Java

First, you need 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 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>

Extract Attachments from PDF in Java

The document level attachments of a PDF document can be obtained using PdfDocument.getAttachments() method. The following steps show you how to extract attachments and save them to a local folder.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Get the attachment collection from the document using PdfDocument.getAttachments() method.
  • Get a specific attachment using PdfAttachmentCollection.get() method and get its data using PdfAttachment.getData() method. Write the data to a file and save to a specified folder.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.annotations.*;
import com.spire.pdf.attachments.PdfAttachmentCollection;

import java.io.*;

public class ExtractAttachments {

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

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

        //Load a PDF file that contains attachments
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Attachments.pdf");

        //Get the attachment collection of the PDF document
        PdfAttachmentCollection attachments = doc.getAttachments();

        //Loop through the collection
        for (int i = 0; i < attachments.getCount(); i++) {

        //Specify the output file path and name
        File file = new File("C:\\Users\\Administrator\\Desktop\\output\\" + attachments.get(i).getFileName());
        OutputStream output = new FileOutputStream(file);
        BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);

        //Get a specific attachment and write to file
        bufferedOutput.write(attachments.get(i).getData());
        bufferedOutput.close();
        }
    }
}

Java: Extract Attachments from PDF Documents

Extract Annotation Attachments from PDF in Java

Annotation attachment is a page-based element. To get annotations from a specific page, use PdfPageBase.getAnnotationsWidget() method. After that, you'll need to determine if a specific annotation is an annotation attachment. The follows are the steps to extract annotation attachments from a whole document and save them to a local folder.

  • Create a PdfDocument object.
  • Load a PDF file using PdfDocument.loadFromFile() method.
  • Get a specific page from the document using PdfDocument.getPages().get() method.
  • Get the annotation collection from the page using PdfPageBase.getAnnotationsWidget() method.
  • Determine if a specific annotation is an instance of PdfAttachmentAnnotationWidget. If yes, write the annotation attachment to a file and save it to a specified folder.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.annotations.PdfAnnotationCollection;
import com.spire.pdf.annotations.PdfAttachmentAnnotationWidget;

import java.io.*;

public class ExtractAnnotationAttachments {

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

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

        //Load a PDF file that contains attachments
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\AnnotationAttachments.pdf");

        //Loop through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

        //Get the annotation collection
        PdfAnnotationCollection collection = doc.getPages().get(i).getAnnotationsWidget();

        //Loop through the annotations
        for (Object annotation : collection) {

        //Determine if an annotation is an instance of PdfAttachmentAnnotationWidget
        if (annotation instanceof PdfAttachmentAnnotationWidget) {

        //Save the annotation attachment out of the document
        String fullPath = ((PdfAttachmentAnnotationWidget) annotation).getFileName();
        String fileName = fullPath.substring(fullPath.lastIndexOf("\\") + 1);
        File file = new File("C:\\Users\\Administrator\\Desktop\\output\\" + fileName);
        OutputStream output = new FileOutputStream(file);
        BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
        bufferedOutput.write(((PdfAttachmentAnnotationWidget) annotation).getData());
        bufferedOutput.close();
                }
            }
        }
    }
}

Java: Extract Attachments from 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.

page 41