Sunday, 27 February 2022

Convert PDF to TIFF in Java

A TIFF, which stands for Tag Image File Format, is a computer file used to store raster graphics and image information. It is widely used due to its flexibility, inclusiveness, and independence. This article will introduce how to convert PDF to TIFF using Java codes from the two aspects below.

l  Convert all pages of a PDF file to TIFF

l  Convert some specified pages of a PDF file to TIFF

DEPENDENCY

First of all, you’re required to get the package of a free third-party library called Free Spire.PDF for Java from this link, and then find Spire.Pdf.jar in the “lib” folder, finally add it to your Java program. Or if you use Maven, just import the following codes in the pom.xml file to easily add the JAR 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.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

USING THE CODE

Convert All Pages of a PDF File to TIFF

The following steps show how to convert all pages of a PDF file to TIFF.

l  Initialize a PdfDocument object.

l  Load a PDF sample document using PdfDocument.loadFromFile() method.

l  Save all pages of the document to a TIFF file using PdfDocument.saveToTiff(String tiffFilename) method.

import com.spire.compression.TiffCompressionTypes;
import com.spire.pdf.PdfDocument;

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

//Initialize a PdfDocument object
PdfDocument pdf = new PdfDocument();

//Load a PDF sample document
pdf.loadFromFile("sample.pdf");

//Save all pages of the document to Tiff
pdf.saveToTiff("output/PDFtoTiff.tiff");
}
}

Convert Some Specified Pages of a PDF File to TIFF

 The following are steps to convert specified pages of a PDF file to TIFF.

l  Initialize a PdfDocument object.

l  Load a PDF sample document using PdfDocument.loadFromFile() method.

l  Save specified pages of the document to a TIFF file using PdfDocument.saveToTiff(String tiffFilename, int startPage, int endPage, int dpix, int dpiy) method.

import com.spire.pdf.PdfDocument;

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

//Save specified pages of the document to TIFF and set horizontal and vertical resolution
pdf.saveToTiff("output/ToTiff2.tiff",0,1,400,600);
}
}



Tuesday, 22 February 2022

Find and Highlight Text in Specific Paragraphs of Word documents using Java

In the previous article, I showed you how to find all matched text in a Word document and then highlight it with color. This tutorial will demonstrate how to find all matched text in a specific paragraph of a Word document and highlight it.

DEPENDENCY

First of all, you need to get the latest version of Spire.Doc for Java from this link, and then manually add Spire.Doc.jar to your Java program. Or if you use Maven, you can easily import the JAR file by using the following codes in the 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>5.2.3</version>
</dependency>
</dependencies>

USING THE CODE

The following are detailed steps to find and highlight text in a specific paragraph of a Word document.

l  Initialize a Document object and load a Word sample document.

l  Get a specific section of the document using Document.getSections().get() method.

l  Get a specific paragraph of the section using Section.getParagraphs().get() method.

l  Find matched text in this paragraph using Document.findAllString() method.

l  Get the character format of these text using TextSelection.getAsOneRange().getCharacterFormat() method, and then set the highlight color using CharacterFormat.setHighlightColor() method.

l  Save to another file using Document.saveToFile() method.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;

import java.awt.*;

public class FindAllStringsInAParagraph {
public static void main(String[] args) {
//Load a Word sample document
Document doc = new Document("sample.docx");

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

//Get second paragraph in this section
Paragraph para = section.getParagraphs().get(1);

//Find all "Word" strings in this paragraph
TextSelection[] textSelections = para.findAllString("Word", false, true);

//Sets highlight color of all text
for (TextSelection selection : textSelections)
{
selection.getAsOneRange().getCharacterFormat().setHighlightColor(new Color(255, 15, 19));
}

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





 

Tuesday, 15 February 2022

Extract Word Paragraphs That Use Specific Styles

In the process of manipulating Word documents, we often need to set specific styles for certain paragraphs in Word. For example, we can set “Heading 1” for the title of the document. That helps us extract the text of a paragraph by its specific style if it’s necessary. This article will show you how to do it using Java codes.

DEPENDENCY

First of all, you’re required to get the package of Free Spire.Doc forJava from this link, and then manually add Spire.Doc.jar to your Java program. Or if you use Maven, you can easily import the JAR file by using the following codes in the 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.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

USING THE CODE

Free Spire.Doc for Java offers the Paragraph.getStyleName() method to get the style name of a certain paragraph and determine if it is a specific style. If yes, then you can extract the Paragraph text using Paragraph.getText() method. The following are detailed steps.

l  Initialize a Document object and load a sample Word document.

l  Loop through all sections of the document.

l  Get a specific paragraph of a certain section using Section.getParagraphs().get() method.

l  Get the paragraph's style name using Paragraph.getStyleName() method and determine if the style is "Heading 1".

l  If yes, extract the text of the paragraph using Paragraph.getText() method.

import com.spire.doc.Document;
import com.spire.doc.documents.Paragraph;

public class GetParagraphWithStyle {
public static void main(String[] args) {
//Load a sample Word document while initializing the Document object
Document doc = new Document("C:\\Users\\Test1\\Desktop\\test.docx");

//Declare a variable
Paragraph paragraph;

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

//Loop through the paragraphs of a specific section
for (int j = 0; j < doc.getSections().get(i).getParagraphs().getCount(); j++) {

//Get a specific paragraph
paragraph = doc.getSections().get(i).getParagraphs().get(j);

//Determine if the paragraph style is "Heading 1"
if (paragraph.getStyleName().equals("Heading1")) {

//Get the text of the paragraph in "Heading 1"
System.out.println("Heading 1: " + paragraph.getText() + "\n");
}
}
}
}
}




 


Sunday, 13 February 2022

Set Gutter Margins for Word Documents in Java

A gutter margin is used to add extra space to the side, top margin, or inside margins of a document you plan to bind. That helps ensure that text cannot be obscured while binding. This article will show you how to set gutter margins for a Word document using Java codes.

DEPENDENCY

First of all, you’re required to get the package of Free Spire.Doc for Java from this link, and then manually add Spire.Doc.jar to your Java program. Or if you use Maven, just type the following codes in the pom.xml file to import the JAR 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.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

USING THE CODE

Free Spire.Doc for Java offers the Section.getPageSetup().setGutter() method to set gutter margins for a Word document. The detailed steps are listed below.

l  Create a Document instance.

l  Load a Word document using Document.loadFromFile() method.

l  Get a specific section using Document.getSections().get() method.

l  Set gutter margins for the specified section using Section.getPageSetup().setGutter() method.

l  Save the document to another file using Document.saveToFile() method.

import com.spire.doc.*;

public class AddGutterMargin {
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 the first section
Section section = document.getSections().get(0);

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

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




Monday, 7 February 2022

Increase or Decrease Margins of PDF Documents in Java

In the process of manipulating PDF documents, you may not want to directly place annotations or stamps over the text of the document, but the side, top or bottom margins are too narrow. In this case, adding an extra margin will give you more room. This article will demonstrate how to increase or decrease margins of a PDF document using Java codes.

DEPENDENCY

First of all, you’re required to download the package of Free Spire.PDF for Java from this link, and then manually add the Spire.Pdf.jar as a dependency in your Java program. Or if you use Maven, you can add the following code in the pom.xml file to easily import the JAR 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.free</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>

Increase Margins of a PDF Document

In order to increase the margins of a PDF document, Free Spire.PDF for Java provides relavant methods for creating a new PDF with a larger page size, and then placing the original pages at the proper position on the new page. The following are detailed steps to finish the operation listed above.

l  Load the original PDF document while initialing the PdfDocument object.

l  Create a new PDF document with a larger page size while creating another PdfDocument instance.

l  Set the increasing values of the margins.

l  Set the page size of the new PDF document.

l  Loop through the pages of the original document, and create a template based on a certain page using PdfPageBase.createTemplate() method.

l  Add a page to the new PDF document using PdfDocument.getPages().add() method.

l  Draw the template on the page from (0, 0) using PdfTemplate.draw() method.

l  Save the new PDF document to another file using PdfDocument.saveToFile() method.

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.graphics.PdfTemplate;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;

public class IncreaseMargin {
public static void main(String[] args) {
//Load the original PDF document
PdfDocument originalPdf = new PdfDocument("C:\\Users\\Test1\\Desktop\\Test.pdf");

//Get the first page
PdfPageBase firstPage = originalPdf.getPages().get(0);

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

//Set increasing value of the margins
PdfMargins margins = newPdf.getPageSettings().getMargins();
margins.setTop(70);
margins.setBottom(70);
margins.setLeft(70);
margins.setRight(70);

//Set the size for the new PDF document
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(firstPage.getSize().getWidth() + margins.getLeft() + margins.getRight(), firstPage.getSize().getHeight() + margins.getTop() + margins.getBottom());

//Loop through the pages in the original document
for (int i = 0; i < originalPdf.getPages().getCount(); i++) {

//Create a template based on the source page
PdfTemplate template = originalPdf.getPages().get(i).createTemplate();

//Add a page to the new PDF
PdfPageBase page = newPdf.getPages().add(dimension2D);

//Draw template on the page
template.draw(page.getCanvas(), new Point2D.Float(0, 0));
}

//Save the new document to file
newPdf.saveToFile("output/IncreaseMargins.pdf", FileFormat.PDF);
}
}

Decrease Margins of a PDF Document

Likewise, the way to decrease the margins of a PDF document is to create a new PDF with a smaller page size and then draw the original pages on the smaller pages at a specified coordinate. You can follow the detailed steps below.

l  Load the original PDF document while initialing the PdfDocument object.

l  Create another PdfDocument instance, which is used to create a new PDF document with a smaller page size.

l  Set the decreasing values of the margins.

l  Set the page size of the new PDF document.

l  Loop through the pages in the original document, and create a template based on a certain page using PdfPageBase.createTemplate() method.

l  Add a page to the new PDF document using PdfDocument.getPages().add() method.

l  Draw the template on the page at the specified position using PdfTemplate.draw() method.

l  Save the new PDF document to another file using PdfDocument.saveToFile() method.


import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.graphics.PdfTemplate;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;

public class DecreaseMargin {
public static void main(String[] args) {
//Load the original PDF document
PdfDocument originalPdf = new PdfDocument("C:\\Users\\Test1\\Desktop\\Test.pdf");

//Get the first page
PdfPageBase firstPage = originalPdf.getPages().get(0);

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

//Set decreasing value
double left = -20;
double right = -20;
double top = -20;
double bottom = -20;

//Set the page size of the new PDF document
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(originalPdf.getPages().get(0).getSize().getWidth() + left + right, originalPdf.getPages().get(0).getSize().getHeight() + top + bottom);

//Loop through the pages in the original document
for (int i = 0; i < originalPdf.getPages().getCount(); i++) {

//Create template based on the source page
PdfTemplate template = originalPdf.getPages().get(i).createTemplate();

//Add a page to the new PDF
PdfPageBase page = newPdf.getPages().add(dimension2D, new PdfMargins(0));

//Draw template on the page
template.draw(page.getCanvas(), new Point2D.Float((float) left, (float) top));
}

//Save the new document to file
newPdf.saveToFile("output/DecreaseMargins.pdf", FileFormat.PDF);
}
}



Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...