Wednesday, 27 October 2021

Create and Extract Tables in PDF Documents using Java

Within this article, we'll demonstrate how to create a PDF document including a table and extract its data using Java code. It's important to note that I used a third-party library called Spire.PDF forJava to complete the operations above.

DEPENDENCY

Before coding, we need to add the Spire.PDF.jar file as a dependency in Java program. The JAR file can be downloaded from this link. Or we can easily import the JAR file by adding the following code to the pom.xml file in Java project.

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

Create a PDF Document and add a Table to it

Spire.PDF for Java supports creating a blank PDF document using PdfDocument class and add a table to a specified

page of PDF using PdfTable.draw(PdfPageBase.new Point2D.Float(x,y)) method. In the process of adding a table,

we can set its title, cell padding, and so on. Full code samples can be seen as below.

import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import com.spire.pdf.tables.*;
import java.awt.*;
import java.awt.geom.Point2D;

public class CreateTable {
public static void main(String[] args) {
//Create a PdfDocument instance
PdfDocument doc = new PdfDocument();

//Set margins
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.setTop(unitCvtr.convertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));
margin.setBottom(margin.getTop());
margin.setLeft(unitCvtr.convertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));
margin.setRight(margin.getLeft());

//Add one page
PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);

float y = 10;

//Draw title
PdfBrush brush1 = PdfBrushes.getBlack();
PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial",Font.BOLD ,16));
PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center);
page.getCanvas().drawString("Country List", font1, brush1, page.getCanvas().getClientSize().getWidth() / 2, y, format1);
y = y + (float) font1.measureString("Country List", format1).getHeight();
y = y + 5;

//Data source to create table
String[] data = {"Name;Capital;Continent;Area;Population", "Argentina;Buenos Aires;South America;2777815;32300003", "Bolivia;La Paz;South America;1098575;7300000", "Brazil;Brasilia;South America;8511196;150400000", "Canada;Ottawa;North America;9976147;26500000", "Chile;Santiago;South America;756943;13200000", "Colombia;Bagota;South America;1138907;33000000", "Cuba;Havana;North America;114524;10600000", "Ecuador;Quito;South America;455502;10600000", "El Salvador;San Salvador;North America;20865;5300000", "Guyana;Georgetown;South America;214969;800000", "Jamaica;Kingston;North America;11424;2500000", "Mexico;Mexico City;North America;1967180;88600000", "Nicaragua;Managua;North America;139000;3900000", "Paraguay;Asuncion;South America;406576;4660000", "Peru;Lima;South America;1285215;21600000", "United States of America;Washington;North America;9363130;249200000", "Uruguay;Montevideo;South America;176140;3002000", "Venezuela;Caracas;South America;912047;19700000"};

String[][] dataSource = new String[data.length][];
for (int i = 0; i < data.length; i++) {
dataSource[i] = data[i].split("[;]", -1);
}

//Create a PdfTable instance and set data source
PdfTable table = new PdfTable();
table.getStyle().setCellPadding(2);
table.getStyle().setHeaderSource(PdfHeaderSource.Rows);
table.getStyle().setHeaderRowCount(1);
table.getStyle().setShowHeader(true);
table.setDataSource(dataSource);

//Draw table to the page
PdfLayoutResult result = table.draw(page, new Point2D.Float(0, y));

y = y + (float) result.getBounds().getHeight() + 5;

//Draw string below table
PdfBrush brush2 = PdfBrushes.getGray();
PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 0,9));
page.getCanvas().drawString(String.format("* %1$s countries in the list.", data.length - 1), font2, brush2, 5, y);

//Save the file
doc.saveToFile("output/CreateTable.pdf");
}
}

The following screenshot is to show the result after adding a table to PDF


Extract Table Data from PDF

Spire.PDF for Java provides PdfTableExtractor.extractTable(int pageIndex) method to detect and extract tables

from PDF. The following are the steps to extract table data from PDF:

l  Load a PDF sample document using PdfDocument class.

l  Create a StringBuilder instance and a PdfTableExtractor instance.

l  Loop through the pages in the PDF, extract tables from each page into a PdfTable array using PdfTableExtractor.extractTable(int

pageIndex) method.

l  Loop through the rows and columns in each table, and then extract data from each table cell using PdfTable.getText(int

rowIndex, int columnIndex) method, finally append the data to the StringBuilder instance using StringBuilder.append() method.

l  Write the data to a .txt document using Writer.write() method.


import com.spire.pdf.PdfDocument;
import com.spire.pdf.utilities.PdfTable;
import com.spire.pdf.utilities.PdfTableExtractor;
import java.io.FileWriter;
import java.io.IOException;

public class ExtractTable {
public static void main(String[] args) throws IOException {
//Load a sample PDF document
PdfDocument pdf = new PdfDocument("C:\\Users\\Test1\\Desktop\\CreateTable.pdf");

//Create a StringBuilder instance
StringBuilder builder = new StringBuilder();
//Create a PdfTableExtractor instance
PdfTableExtractor extractor = new PdfTableExtractor(pdf);

//Loop through the pages in the PDF
for (int pageIndex = 0; pageIndex < pdf.getPages().getCount(); pageIndex++) {
//Extract tables from the current page into a PdfTable array
PdfTable[] tableLists = extractor.extractTable(pageIndex);

//If any tables are found
if (tableLists != null && tableLists.length > 0) {
//Loop through the tables in the array
for (PdfTable table : tableLists) {
//Loop through the rows in the current table
for (int i = 0; i < table.getRowCount(); i++) {
//Loop through the columns in the current table
for (int j = 0; j < table.getColumnCount(); j++) {
//Extract data from the current table cell and append to the StringBuilder
String text = table.getText(i, j);
builder.append(text + " | ");
}
builder.append("\r\n");
}
}
}
}

//Write data into a .txt document
FileWriter fw = new FileWriter("output/ExtractTable.txt");
fw.write(builder.toString());
fw.flush();
fw.close();
}
}

Below is a screenshot of the extracted table data



Monday, 25 October 2021

Add Footers to Presentation Documents in Java

Headers and footers in PowerPoint are ideal for displaying descriptive content, such as slide numbers, author information, the date and more. This article will introduce how to add footers to a specified slide in PowerPoint using Java codes.

TOOL: Free Spire.Presentation for Java (free version)

ADD JAR FILE: We need to get the package of the tool from the link, and then add Spire.Presentation.jar to IDEA. Or directly import the following Maven configurations.

<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.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

USING THE CODE

Free Spire.Presentation for Java provides Presentation.setFooterText() method to add a footer and set

its content for a specified slide. The following are some steps to add a footer.

l  Create a Presentation instance.

l  Load a PowerPoint sample document using Presentation.loadFromFile() method.

l  Add a footer and set its content using Presentation.setFooterText() method.

l  Set footer, slide number and date visible

l  Save the document using Presentation.saveToFile() method.

import com.spire.presentation.FileFormat;
import com.spire.presentation.Presentation;

public class Footer {
public static void main(String[] args) throws Exception {
//Load a PPT document
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.pptx");

//Add footer
presentation.setFooterText("An English Poem");
//Set the footer visible
presentation.setFooterVisible(true);
//Set the page number visible
presentation.setSlideNumberVisible(true);
//Set the date visible
presentation.setDateTimeVisible(true);

//Save the document
presentation.saveToFile("output/AddFooter.pptx", FileFormat.PPTX_2010);
}
}

The screenshot below shows relevant information after adding a footer



 

Friday, 22 October 2021

Convert Word to HTML/SVG/RTF/XPS/XML/TXT in Java

When we manipulate Word documents in daily life, we may need to convert Word to other document formats for some purposes. This tutorial will demonstrate how to realize the function of converting Word to HTML/SVG/RTF/XPS/XML/TXT in Java using Free Spire.Doc for Java.

About Free Spire.Doc for Java

Free Spire.Doc for Java is a free and professional Java Word API that enables Java applications to create, manipulate, convert and print Word documents without installing Microsoft Office.

Install Free Spire.Doc for Java

First of all, we need 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.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Convert Word to HTML/SVG/RTF/XPS/XML/TXT

Free Spire.Doc for Java provides Document.saveToFile() method to convert Word to other document formats. Some steps to do it are listed below.

l  Create a Document instance.

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

l  Save the document as HTML/SVG/RTF/XPS/XML/TXT using Document.saveToFile() method.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;

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

//Load the Word document.
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.docx");

//Save Word as HTML
doc.saveToFile("output/toHtml.html", FileFormat.Html);

//Save Word as SVG.
doc.saveToFile("output/ToSVG.svg",FileFormat.SVG);

//Save Word as RTF.
doc.saveToFile("output/ToRTF.rtf",FileFormat.Rtf);

//Save Word as XPS.
doc.saveToFile("output/ToXPS.xps",FileFormat.XPS);

//Save Word as XML.
doc.saveToFile("output/ToXML.xml",FileFormat.Xml);

//Save Word as TXT.
doc.saveToFile("output/ToTXT.txt",FileFormat.Txt);
}
}

The following is a screenshot of conversion results.





Tuesday, 19 October 2021

Set Zoom Factor and Viewer Preference for PDF Documents in Java

Zoom Factor lets you zoom the display by a scale factor, and the Viewer Preference allows you to control the way the document shall be presented. This article will introduce how to set Zoom Factor and Viewer Preference for PDF documents using Java codes.

TOOL: Free Spire.PDF for Java (you can get it from the link, or directly add it to your project by using the following Maven configurations)

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

USING THE CODE

Set Zoom Factor

You can find the following steps to set zoom factor for a PDF document using Java codes.

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

l  Get the specified page of PDF using PdfDocument.getPages().get() method

l  Set PDF destination mode and action

l  Set zoom factor using PdfDocument.setZoom() method

l  Save the resulting document to a specified path using PdfDocument.saveToFile() method

Full code sample are shown as below.

import com.spire.pdf.*;
import com.spire.pdf.actions.*;
import com.spire.pdf.general.*;
import java.awt.geom.*;

public class ZoomFactor {
public static void main(String[] args) {
//Load the sample document
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.pdf");

//Get the first page of PDF
PdfPageBase page = doc.getPages().get(0);

//Set pdf destination
PdfDestination dest = new PdfDestination(page);
dest.setMode(PdfDestinationMode.Location);
dest.setLocation(new Point2D.Float(-40f, -40f));

//Set zoom factor
dest.setZoom(0.8f);

//Set action
PdfGoToAction gotoAction = new PdfGoToAction(dest);
doc.setAfterOpenAction(gotoAction);

//Save pdf document
String output = "output/setZoomFactor.pdf";
doc.saveToFile(output);
}
}

Output


Set Viewer Preference

Free Spire.PDF for Java supports setting viewer preference for a PDF document, for example, you can

specify whether to position the document’s window in the center of the screen using PdfDocument

.getViewerPreferences().setCenterWindow() method and whether the title of the document should be

displayed in the window’s title bar using PdfDocument.getViewerPreferences().setDisplayTitle()

method. The code snippets below will give you detailed information.

import com.spire.pdf.*;

public class ViewerPreference {
public static void main(String[] args) {
//Load the sample document
PdfDocument doc = new PdfDocument();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.pdf");

//Set viewer reference
doc.getViewerPreferences().setCenterWindow(true);
doc.getViewerPreferences().setDisplayTitle(false);
doc.getViewerPreferences().setFitWindow(false);
doc.getViewerPreferences().setHideMenubar(true);
doc.getViewerPreferences().setHideToolbar(true);
doc.getViewerPreferences().setPageLayout(PdfPageLayout.Two_Column_Left);

//Save pdf document
String output = "output/viewerPreference.pdf";
doc.saveToFile(output);
}
}

Output



Monday, 18 October 2021

Accept or Reject Tracked Changes in Excel using Java

In the process of operating Excel documents, we can share them with others for collaborative modification, and use Tracked Changes to track, maintain these changes. If you have the full authority over the document, you can accept or reject each change. This tutorial will show you how to accept or reject all tracked changes using Java codes with the help of Spire.XLS for Java.

About Spire.XLS for Java

Spire.XLS for Java is a professional Java Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets without using Microsoft Office or Microsoft Excel.

Install Spire.Xls.jar

First of all, you need to add 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 by adding the following code to your project's pom.xml file.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.xls</artifactId>
<version>4.9.0</version>
</dependency>
</dependencies>

Accept or Reject Tracked Changes in a Workbook

Spire.XLS for Java provides Workbook.hasTrackedChanegs() method to detect whether a workbook

has tracked changes. If yes, you can use Workbook.acceptAllTrackedchanges() method to accept all

changes. On the contrary, you can reject them using Workbook.rejectAllTrackedChanges() method.

The full code sample can be found as below.

import com.spire.xls.*;

public class TrackChanges {
public static void main(String[] args) {
//Create a Workbook object
Workbook wb = new Workbook();code

//Load the sample Excel file
wb.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.xlsx");

//Detect if the workbook has tracked changes
if (wb.hasTrackedChanges())
{
//Accept tracked changes in the workbook
wb.acceptAllTrackedChanges();
            //Reject tracked changes in the workbook
//wb.rejectAllTrackedChanges();
}

//Save to file
wb.saveToFile("output/AcceptTrackedChanges.xlsx", FileFormat.Version2013);
}
}



Monday, 11 October 2021

Add Multiple Text Watermarks to PowerPoint slides in Java

In previous article, I introduced how to add/remove image watermarks or text watermarks to PowerPoint slides using Java codes with the help of a free third-party library called Free Spire.Presentation for Java. Actually, the library also supports adding multiple text watermarks to PowerPoint slides. Today this tutorial will demonstrate how to do it.

DEPENDENCY

You can get the API’s JAR from the link or install it using the following Maven configurations.

<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.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Add Multiple Text Watermarks to PowerPoint Slides

You can add multiple text watermarks to the specified slide in a PowerPoint document by following the

steps below.

l  Load a PowerPoint document using Presentation class

l  Set the content of text watermark and get its size using PdfTrueTypeFont. measureString(watermarkText) method

l  Add a shape using presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE,Rectangle2D) method

l  Set line color, rotation and other styles of the shape

l  Add watermark text to the shape using shape.getTextFrame().getTextRange() method and set styles of the text range.

l  Save the resulting document to a specified path using presentation.saveToFile(string,FileFormat.PPTX_2013) method

The full sample code can be shown as below.

import com.spire.pdf.graphics.PdfTrueTypeFont;
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;

public class MultipleTextWatermarks {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();

//Load the sample PowerPoint file
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

//Specify watermark text
String watermarkText = "DRAFT";

//Get the size of the watermark text
Font font = new java.awt.Font("Arial", java.awt.Font.BOLD, 20);
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);
Dimension2D strSize = trueTypeFont.measureString(watermarkText);

//Initialize x and y coordinate
float x = 30;
float y = 80;

for (int rowNum = 0; rowNum < 4; rowNum++) {
for (int colNum = 0; colNum < 5; colNum++) {

//Add a rectangle shape
Rectangle2D rect = new Rectangle2D.Float(x, y, (float) strSize.getWidth() + 10, (float) strSize.getHeight());
IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);

//Set the style of the shape
shape.getFill().setFillType(FillFormatType.NONE);
shape.getShapeStyle().getLineColor().setColor(new Color(1, 1, 1, 0));
shape.setRotation(-45);
shape.getLocking().setSelectionProtection(true);
shape.getLine().setFillType(FillFormatType.NONE);

//Add watermark text to the shape
shape.getTextFrame().setText(watermarkText);
PortionEx textRange = shape.getTextFrame().getTextRange();

//Set the style of the text range
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(Color.pink);
textRange.setLatinFont(new TextFont(trueTypeFont.getName()));
textRange.setFontMinSize(trueTypeFont.getSize());

x += (100 + strSize.getWidth());

}
x = 30;
y += (100 + strSize.getHeight());
}

//Save the document
presentation.saveToFile("output/MultipleWatermark.pptx", FileFormat.PPTX_2013);
}
}

The screenshot below shows the output generated by the sample code



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...