Add Borders to Some Text in Word in Java

2020-07-16 07:13:58 Written by Koohji

This article demonstrates how to apply a border around a set of charaters and how to apply a border around a whole paragraph, by using Spire.Doc for Java.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.BorderStyle;
import com.spire.doc.documents.BreakType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;

import java.awt.*;

public class AddBorders {

    public static void main(String[] args) {

        //Create a Document instance
        Document doc = new Document();

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

        //Add a border to a set of characters
        Paragraph para = section.addParagraph();
        TextRange tr = para.appendText("Spire.Doc for Java");
        tr.getCharacterFormat().getBorder().setBorderType(BorderStyle.Single);
        tr.getCharacterFormat().getBorder().setColor(Color.BLACK);
        String text = " is a professional Java library specifically designed for developers to create, read, " +
                "write, convert and print Word document files on Java platform.";
        para.appendText(text);
        para.appendBreak(BreakType.Line_Break);

        //Add a border to a paragraph
        para = section.addParagraph();
        String text2 = "A plenty of Word document processing tasks can be performed by Spire.Doc for Java, such as " +
                "creating, reading, editing, converting and printing Word documents." ;
        para.appendText(text2);
        para.getFormat().getBorders().setBorderType(BorderStyle.Single);
        para.getFormat().getBorders().setColor(Color.BLACK);

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

Add Borders to Some Text in Word in Java

Java: Flatten Form Fields in PDF

2022-08-16 08:35:00 Written by Koohji

Form fields are often used in PDF documents to collect information from users. In some cases, you may need to flatten form fields in a PDF. For instance, when you want to prevent other viewers from editing the information you entered in the form fields of a PDF questionnaire. This article will introduce how to flatten form fields in PDF in Java using Spire.PDF for Java.

Install Spire.PDF for Java

First of all, 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>11.12.16</version>
    </dependency>
</dependencies>

Flatten a Specific Form Field in PDF in Java

The following are the steps to flatten a specific form field in a PDF document using Spire.PDF for Java:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Get the form widget collection from the document.
  • Get a specific form field from the widget collection by its name or index through PdfFormWidget.getFieldsWidget().get() method.
  • Flatten the form field through PdfField.setFlatten() method.
  • Save the result document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.fields.PdfField;
import com.spire.pdf.widget.PdfFormWidget;

public class FlattenSpecificFormField {
    public static void main(String[] args){
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load a PDF document
        pdf.loadFromFile("Form.pdf");

        //Get the form widget collection
        PdfFormWidget formWidget = (PdfFormWidget)pdf.getForm();
        //Get a specific form field by its name
        PdfField form = formWidget.getFieldsWidget().get("Address");
        //Get a specific form field by its index
        //PdfField form = formWidget.getFieldsWidget().get(2);
        //Flatten the form
        form.setFlatten(true);

        //Save the result document
        pdf.saveToFile("FlattenSpecific.pdf");
    }
}

Java:  Flatten Form Fields in PDF

Flatten All Form Fields in PDF in Java

The following are the steps to flatten all the form fields in a PDF document using Spire.PDF for Java:

  • Initialize an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.loadFromFile() method.
  • Flatten all the form fields in the document through PdfDocument.getForm().isFlatten() method.
  • Save the result document using PdfDocument.saveToFile() method.
  • Java
import com.spire.pdf.PdfDocument;

public class FlattenAllFormFields {
    public static void main(String[] args){        
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();
        //Load a PDF document
        pdf.loadFromFile("Form.pdf");

        //Flatten all the forms in the document
        pdf.getForm().isFlatten(true);

        //Save the result document
        pdf.saveToFile("FlattenAll.pdf");
    }
}

Java:  Flatten Form Fields 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.

Insert Images to a Table in Word in Java

2020-07-10 07:21:49 Written by Koohji

This article demonstrates how to insert images to table cells in a Word document using Spire.Doc for Java.

import com.spire.doc.AutoFitBehaviorType;
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.Table;
import com.spire.doc.fields.DocPicture;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class InsertImageToTableCell {

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

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

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

        //Add a table
        Table table = section.addTable(true);
        table.resetCells(2,2);
        table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);

        //Load an image to InputStream
        InputStream inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\company-logo.png");
        
        //Insert the image to the cell(0,0)
        DocPicture picture = table.get(0,0).addParagraph().appendPicture(inputStream);
        
        //Set the width and height of the image
        picture.setWidth(100);
        picture.setHeight(100);

        //Insert another image to the cell(1,1)
        inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\intro.png");
        picture = table.get(1,1).addParagraph().appendPicture(inputStream);
        picture.setWidth(100);
        picture.setHeight(100);

        //Save the document
        document.saveToFile("InsertImgToCell.docx");
    }
}

Insert Images to a Table in Word in Java

page 43