Knowledgebase (2311)
Children categories
This article is going to demonstrate how to split a specific table cell in PowerPoint using Spire.Presentation.
The original table:

Detail steps:
Step 1: Instantiate a Presentation object and load the PowerPoint file.
Presentation ppt = new Presentation();
ppt.LoadFromFile("Input.pptx");
Step 2: Get the first slide.
ISlide slide = ppt.Slides[0];
Step 3: Get the table on the slide.
ITable table = slide.Shapes[0] as ITable;
Step 4: Split the cell [1, 2] into 3 rows and 2 columns.
table[1, 2].Split(3, 2);
Step 5: Save the file.
ppt.SaveToFile("Split.pptx", FileFormat.Pptx2013);
Screenshot after splitting:

Full code:
using Spire.Presentation;
namespace Split_Table_Cells_in_PPT
{
class Program
{
static void Main(string[] args)
{
//Instantiate a Presentation object
Presentation ppt = new Presentation();
//Load the PowerPoint file
ppt.LoadFromFile("Input.pptx");
//Get the first slide
ISlide slide = ppt.Slides[0];
//Get the table
ITable table = slide.Shapes[0] as ITable;
//Split cell [1, 2] into 3 rows and 2 columns
table[1, 2].Split(3, 2);
//Save the file
ppt.SaveToFile("Split.pptx", FileFormat.Pptx2013);
}
}
}

PDF is a cornerstone for document sharing across diverse platforms. In Java development, the ability to generate PDF in Java efficiently is a common requirement for applications ranging from invoicing systems to report generators. Among the myriad libraries available, Spire.PDF for Java stands out as a robust solution. This comprehensive guide explores how to use this powerful Java library to create PDF files from scratch, from templates or from HTML.
- Getting Started with Spire.PDF for Java
- Background: The Coordinate System
- Generate a Basic PDF in Java
- Create PDF from Template in Java
- Bonus: Generate PDF from HTML in Java
- Conclusion
- Frequently Asked Questions (FAQs)
Getting Started with Spire.PDF for Java
Spire.PDF for Java is a robust library simplifies PDF generation in Java without Adobe dependencies. Key features:
- Cross-Platform Support: Runs seamlessly on Windows, Linux, and macOS.
- Rich Content Creation: Add text, images, tables, lists, and barcodes.
- Advanced Security: Apply passwords, digital signatures, and permission controls.
- Easy Integration: Works seamlessly with Java SE and EE environments.
Setup & Installation
To start creating PDF in Java, you first need to add Spire.PDF for Java to your project. You can download the JAR files from the E-iceblue website or add it as a Maven dependency:
<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>
Background: The Coordinate System
In Spire.PDF for Java, the coordinate system defines the positioning of elements (text, images, shapes) on a PDF page. Here’s the key concepts:
- Origin Point (0,0): The origin of the coordinate system is located at the top-left corner of the content area.
- X-axis: Extends horizontally from the left to the right.
- Y-axis: Extends vertically from the top downward.

Generate a Basic PDF in Java
Let’s start with a simple example of creating a PDF document with text. Spire.PDF for Java provides two methods to draw text on a PDF page:
- PdfCanvas.drawString(): Draws single-line text at exact coordinates. Best for headings, labels, or short text snippets.
- PdfTextWidget.draw(): Manages multi-line text with automatic word wrapping and line breaks. Best for paragraphs, long content, paginated text.
Here's the Jave code to create PDF:
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
public class CreatePdfDocument {
public static void main(String[] args) {
// Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
// Add a page with specified size and margin
PdfPageBase page = pdf.getPages().add(PdfPageSize.A4, new PdfMargins(35f));
// Specify page content
String titleText = "Spire.PDF for Java";
String paraText = "Spire.PDF for Java is a PDF API that enables Java applications to read, write and save PDF documents. " +
"Using this Java PDF component, developers and programmers can implement rich capabilities to" +
"create PDF files from scratch or process existing PDF documents entirely on Java applications (J2SE and J2EE). " +
"Spire.PDF for Java is a totally independent Java PDF library. " +
"It does not require Adobe Acrobat or any other 3rd party software/library installed on system.";
// Create solid brushes
PdfSolidBrush titleBrush = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
PdfSolidBrush paraBrush = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));
// Create true type fonts
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("Times New Roman",Font.BOLD,18));
PdfTrueTypeFont paraFont = new PdfTrueTypeFont(new Font("Times New Roman",Font.PLAIN,12));
// Set the text alignment via PdfStringFormat class
PdfStringFormat format = new PdfStringFormat();
format.setAlignment(PdfTextAlignment.Center);
// Draw title on the page
page.getCanvas().drawString(titleText, titleFont, titleBrush, new Point2D.Float((float)page.getClientSize().getWidth()/2, 40),format);
// Create a PdfTextWidget object to hold the paragraph content
PdfTextWidget widget = new PdfTextWidget(paraText, paraFont, paraBrush);
// Create a rectangle where the paragraph content will be placed
Rectangle2D.Float rect = new Rectangle2D.Float(0, 70, (float)page.getClientSize().getWidth(),(float)page.getClientSize().getHeight());
// Set the PdfLayoutType to Paginate to make the content paginated automatically
PdfTextLayout layout = new PdfTextLayout();
layout.setLayout(PdfLayoutType.Paginate);
// Draw paragraph text on the page
widget.draw(page, rect, layout);
// Save the PDF file
pdf.saveToFile("CreatePdfDocument.pdf");
pdf.dispose();
}
}
The code creates a PDF with a centered title and a paragraph that automatically paginates if it exceeds the page height.
The generated PDF file:

Beyond simple text, you can also add other elements to PDF, such as:
Add Images to PDF
Add images (JPG, PNG, etc.) at specified locations on a PDF page:
//Load an image
PdfImage image = PdfImage.fromFile("image.jpg");
//Specify the width and height of the image area on the page
float width = image.getWidth() * 0.50f;
float height = image.getHeight() * 0.50f;
//Draw the image at a specified location on the page
page.getCanvas().drawImage(image, 100f, 60f, width, height);
Add Tables to PDF
Organize data with tables, a useful feature when you generate PDF reports:
//Create a PdfTable object
PdfTable table = new PdfTable();
//Define data
String[] data = {"ID;Name;Department;Position",
"1; David; IT; Manager",
"3; Julia; HR; Manager",
"4; Sophie; Marketing; Manager",
"7; Wickey; Marketing; Sales Rep",
"9; Wayne; HR; HR Supervisor",
"11; Mia; Dev; Developer"};
String[][] dataSource = new String[data.length][];
for (int i = 0; i < data.length; i++) {
dataSource[i] = data[i].split("[;]", -1);
}
//Set data as the table data
table.setDataSource(dataSource);
//Set the first row as header row
table.getStyle().setHeaderSource(PdfHeaderSource.Rows);
table.getStyle().setHeaderRowCount(1);
//Show header(the header is hidden by default)
table.getStyle().setShowHeader(true);
//Draw table on the page
table.draw(page, new Point2D.Float(0, 30));
Refer to: Create Tables in PDF in Java
Create PDF from Template in Java
This Java code demonstrates how to generate a PDF by dynamically replacing placeholders in a pre-designed PDF template. Common use cases include generating emails, reports, invoice or contracts.
import com.spire.pdf.*;
import com.spire.pdf.texts.PdfTextReplaceOptions;
import com.spire.pdf.texts.PdfTextReplacer;
import com.spire.pdf.texts.ReplaceActionType;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
public class GeneratePdfFromTemplate
{
public static void main(String[] args)
{
// Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
// Load a template
pdf.loadFromFile("PdfTemplate.pdf");
// Create a PdfTextReplaceOptions object
PdfTextReplaceOptions textReplaceOptions = new PdfTextReplaceOptions();
// Specify the replace options
textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.IgnoreCase));
textReplaceOptions.setReplaceType(EnumSet.of(ReplaceActionType.WholeWord));
// Get the first page
PdfPageBase page = pdf.getPages().get(0);
// Create a PdfTextReplacer object based on the page
PdfTextReplacer textReplacer = new PdfTextReplacer(page);
// Set replace options
textReplacer.setOptions(textReplaceOptions);
// Specify the placeholder-value pairs in a map
Map<String, String> replacements = new HashMap<>();
replacements.put("{name}", "John Smith");
replacements.put("{date}", "2023-10-05");
replacements.put("{number}", "ID0001265");
replacements.put("{address}", "123 Northwest Freeway, Houston, Texas USA 77040");
// Iterate over the map to replace each placeholder
for (Map.Entry<String, String> entry : replacements.entrySet())
{
textReplacer.replaceAllText(entry.getKey(), entry.getValue());
}
// Save the result PDF
pdf.saveToFile("GeneratePDFromTemplate.pdf");
pdf.dispose();
}
}
Explanation:
Here are some core components for the template-based PDF generation:
- PdfTextReplaceOptions: Defines how replacements are performed (case-insensitive, whole words).
- PdfTextReplacer: Represents the text replacement in a PDF page.
- replaceAllText(): Replaces all occurrences of old text (a placeholder like "{name}") with new text (e.g., "John Smith").
Output:

Bonus: Generate PDF from HTML in Java
Spire.PDF for Java also provides intuitive APIs to convert web URLs, local HTML files, or raw HTML strings to PDF files. For a comprehensive implementation guide, refer to:
Convert HTML to PDF in Java – URLs and HTML Strings/ Files
By mastering the HTML to PDF conversion, Java developers can automate invoice/report generation from web templates, or archive web pages as searchable PDFs.
Conclusion
Spire.PDF for Java provides an efficient way to generate PDF in Java—whether creating basic documents, generating PDFs from HTML or templates. By following the examples in this article, you can quickly integrate professional PDF creation in your Java projects.
Explore Full Features: Spire.PDF for Java Online Documentation
Frequently Asked Questions (FAQs)
Q1: Is Spire.PDF for Java free?
A: Spire.PDF for Java offers both commercial and free versions (with limitations). You can request a trial license to test the commercial version without any restrictions.
Q2: Does it support non-English languages (e.g., Chinese, Japanese)?
A: Yes, use the font that supports the target language. For example:
// To display Chinese text, use a font like "SimSun" or "Microsoft YaHei"
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("SimSun", Font.PLAIN, 12));
// To display Japanese text, use a font like "MS Gothic" or "Yu Gothic"
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("MS Gothic", Font.PLAIN, 12));
Q3: How to secure PDFs with passwords?
A: Set open/permission passwords:
// Create a password-based security policy with open and permission passwords
PdfSecurityPolicy securityPolicy = new PdfPasswordSecurityPolicy("openPwd", "permissionPwd");
// Set the encryption algorithm to AES 256-bit
securityPolicy.setEncryptionAlgorithm(PdfEncryptionAlgorithm.AES_256);
// Encrypt the PDF file
pdf.encrypt(securityPolicy);
Charts are commonly used in Microsoft Excel files to visualize numeric data. In some cases, you may need to save the charts in an Excel file as images in order to use them in other programs or other files such as PDFs and PowerPoint presentations. In this article, we will demonstrate how to convert charts in Excel to images in C# and VB.NET using Spire.XLS for .NET.
- Convert a Specific Chart in an Excel Worksheet to Image
- Convert All Charts in an Excel Worksheet to Images
- Convert a Chart Sheet to Image in Excel
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert a Specific Chart in an Excel Worksheet to Image in C# and VB.NET
Spire.XLS provides the Workbook.SaveChartAsImage(Worksheet worksheet, int chartIndex) method which enables you to convert a specific chart in a worksheet as image. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet by its index through Workbook.Worksheets[int worksheetIndex] property.
- Save a specific chart in the worksheet as image using Workbook.SaveChartAsImage(Worksheet worksheet, int chartIndex) method.
- Save the image to a PNG file.
- C#
- VB.NET
using Spire.Xls;
using System.Drawing;
using System.Drawing.Imaging;
namespace ConvertAExcelChartToImage
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Workbook class
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.LoadFromFile("Charts.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Save the first chart in the first worksheet as image
Image image = workbook.SaveChartAsImage(sheet, 0);
//Save the image to .png file
image.Save(@"output\chart.png", ImageFormat.Png);
}
}
}

Convert All Charts in an Excel Worksheet to Images in C# and VB.NET
To convert all charts in an Excel worksheet to images, you can use the Workbook.SaveChartAsImage(Worksheet worksheet) method. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet by its index through Workbook.Worksheets[int worksheetIndex] property.
- Save all charts in the worksheet as images using Workbook.SaveChartAsImage(Worksheet worksheet) method.
- Save the images to PNG files.
- C#
- VB.NET
using Spire.Xls;
using System.Drawing;
using System.Drawing.Imaging;
namespace ConvertAllExcelChartsToImages
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Workbook class
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.LoadFromFile("Charts.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Save charts in the first worksheet as images
Image[] imgs = workbook.SaveChartAsImage(sheet);
//Save the images to png files
for (int i = 0; i < imgs.Length; i++)
{
imgs[i].Save(string.Format(@"output\chart-{0}.png", i), ImageFormat.Png);
}
}
}
}

Convert a Chart Sheet to Image in Excel in C# and VB.NET
You can use the Workbook.SaveChartAsImage(ChartSheet chartSheet) method to convert a chart sheet in Excel to image. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specific chart sheet by its index through Workbook.Chartsheets[int chartSheetIndex] property.
- Save the chart sheet as image using Workbook.SaveChartAsImage(ChartSheet chartSheet) method.
- Save the image to .png file.
- C#
- VB.NET
using Spire.Xls;
using System.Drawing;
using System.Drawing.Imaging;
namespace ConvertExcelChartSheetToImage
{
internal class Program
{
static void Main(string[] args)
{
//Initialize an instance of the Workbook class
Workbook workbook = new Workbook();
//Load a sample Excel file
workbook.LoadFromFile("ChartSheet.xlsx");
//Get the first chart sheet
ChartSheet chartSheet = workbook.Chartsheets[0];
//Save the first chart sheet as image
Image image = workbook.SaveChartAsImage(chartSheet);
//Save the image to .png file
image.Save(@"output\chartSheet.png", ImageFormat.Png);
}
}
}

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.