Knowledgebase (2345)
Children categories
The java.awt.print package provides classes and interfaces for a general printing API. It has the ability to specify the document types and manage the print options, such as specifying printer name, setting print range, printing in duplex, and printing in custom paper sizes. In this article, you will learn how to print PDF documents in Java using this package and Spire.PDF for Java library.
- Print PDF with the Default Printer Without a Print Dialog
- Print Page Ranges with a Specified Printer
- Print PDF with a Print Dialog
- Print PDF in Duplex Mode
- Print PDF in a Custom Paper Size
Install Spire.PDF for Java
First of all, you're required 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 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.pdf</artifactId>
<version>12.6.4</version>
</dependency>
</dependencies>
Print PDF with the Default Printer Without a Print Dialog
The following are the steps to print PDF documents with the default printer using java.awt.print and Spire.PDF for Java.
- Create an instance of PrinterJob class, and calls methods in this class to set up a job.
- Create a PdfDocument object, and load a PDF document using PdfDocument.LoadFromFile() method.
- Render each page of the document in the specified format using PrinterJob.setPrintable() method.
- Call PrinterJob.print() method to print the PDF pages.
- Java
import com.spire.pdf.PdfDocument;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class PrintWithDefaultPrinter {
public static void main(String[] args) {
//Create a PrinterJob object which is initially associated with the default printer
PrinterJob printerJob = PrinterJob.getPrinterJob();
// Create a PageFormat object and set it to a default size and orientation
PageFormat pageFormat = printerJob.defaultPage();
//Return a copy of the Paper object associated with this PageFormat
Paper paper = pageFormat.getPaper();
//Set the imageable area of this Paper
paper.setImageableArea(0, 0, pageFormat.getWidth(), pageFormat.getHeight());
//Set the Paper object for this PageFormat
pageFormat.setPaper(paper);
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Call painter to render the pages in the specified format
printerJob.setPrintable(pdf, pageFormat);
//Execute printing
try {
printerJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
Print Page Ranges with a Specified Printer
The following are the steps to print a page range with a specified printer using java.awt.print and Spire.PDF for Java.
- Create an instance of PrinterJob class and calls methods in this class to set up a job.
- Find the available print service using the custom method findPrintService(), and specify the printer name using PrinterJob.setPrintService() method.
- Create a PdfDocument object, and load a PDF document using PdfDocument.LoadFromFile() method.
- Render each page of the document in the specified format using PrinterJob.setPrintable() method.
- Create a PrintRequestAttributeSet object, and add the print range to the attribute set.
- Call PrinterJob.print() method to print the selected pages.
- Java
import com.spire.pdf.PdfDocument;
import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.PageRanges;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class PrintWithSpecifiedPrinter {
public static void main(String[] args) throws PrinterException {
//Create a PrinterJob object which is initially associated with the default printer
PrinterJob printerJob = PrinterJob.getPrinterJob();
//Specify printer name
PrintService myPrintService = findPrintService("\\\\192.168.1.104\\HP LaserJet P1007");
printerJob.setPrintService(myPrintService);
//Create a PageFormat instance and set it to a default size and orientation
PageFormat pageFormat = printerJob.defaultPage();
//Return a copy of the Paper object associated with this PageFormat.
Paper paper = pageFormat.getPaper();
//Set the imageable area of this Paper.
paper.setImageableArea(0, 0, pageFormat.getWidth(), pageFormat.getHeight());
//Set the Paper object for this PageFormat.
pageFormat.setPaper(paper);
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Call painter to render the pages in the specified format
printerJob.setPrintable(pdf, pageFormat);
//Create a PrintRequestAttributeSet object
PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
//Set print range
attributeSet.add(new PageRanges(1,7));
//Execute printing
try {
printerJob.print(attributeSet);
} catch (PrinterException e) {
e.printStackTrace();
}
}
//Find print service
private static PrintService findPrintService(String printerName) {
PrintService[] printServices = PrinterJob.lookupPrintServices();
for (PrintService printService : printServices) {
if (printService.getName().equals(printerName)) {
System.out.print(printService.getName());
return printService;
}
}
return null;
}
}
Print PDF with a Print Dialog
The following are the steps to print PDF documents with print dialog using java.awt.print and Spire.PDF for Java.
- Create an instance of PrinterJob class, and calls methods in this class to set up a job.
- Create a PdfDocument object, and load a PDF document using PdfDocument.LoadFromFile() method.
- Render each page of the document in the specified format using PrinterJob.setPrintable() method.
- Call PrinterJob.printDialog() method to display print dialog.
- Call PrinterJob.print() method to print the PDF pages.
- Java
import com.spire.pdf.PdfDocument;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class PrintWithPrintDialog {
public static void main(String[] args) {
//Create a PrinterJob object which is initially associated with the default printer
PrinterJob printerJob = PrinterJob.getPrinterJob();
//Create a PageFormat object and set it to a default size and orientation
PageFormat pageFormat = printerJob.defaultPage();
//Return a copy of the Paper object associated with this PageFormat
Paper paper = pageFormat.getPaper();
//Set the imageable area of this Paper
paper.setImageableArea(0, 0, pageFormat.getWidth(), pageFormat.getHeight());
//Set the Paper object for this PageFormat
pageFormat.setPaper(paper);
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Call painter to render the pages in the specified format
printerJob.setPrintable(pdf, pageFormat);
//Display the print dialog
if (printerJob.printDialog()) {
try {
printerJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
}
Print PDF in Duplex Mode
The following are the steps to print PDF documents in duplex mode using java.awt.print and Spire.PDF for Java.
- Create an instance of PrinterJob class and calls methods in this class to set up a job.
- Create a PdfDocument object, and load a PDF document using PdfDocument.LoadFromFile() method.
- Render each page of the document in the specified format using PrinterJob.setPrintable() method.
- Create a PrintRequestAttributeSet object, and add the two-sided printing mode to the attribute set.
- Call PrinterJob.print() method to print the PDF pages.
- Java
import com.spire.pdf.PdfDocument;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Sides;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class PrintInDuplexMode {
public static void main(String[] args) {
//Create a PrinterJob object which is initially associated with the default printer
PrinterJob printerJob = PrinterJob.getPrinterJob();
//Create a PageFormat object and set it to a default size and orientation
PageFormat pageFormat = printerJob.defaultPage();
//Return a copy of the Paper object associated with this PageFormat
Paper paper = pageFormat.getPaper();
//Set the imageable area of this Paper
paper.setImageableArea(0, 0, pageFormat.getWidth(), pageFormat.getHeight());
//Set the Paper object for this PageFormat
pageFormat.setPaper(paper);
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Call painter to render the pages in the specified format
printerJob.setPrintable(pdf, pageFormat);
//Create a PrintRequestAttributed object
PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
//Set to duplex printing mode
attributeSet.add(Sides.TWO_SIDED_SHORT_EDGE);
//Execute printing
try {
printerJob.print(attributeSet);
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
Print PDF in a Custom Paper Size
The following are the steps to print PDF documents in a custom paper size using java.awt.print and Spire.PDF for Java.
- Create an instance of PrinterJob class and calls methods in this class to set up a job.
- Set the with and height of the Paper object using Paper.setSize() method.
- Create a PdfDocument object, and load a PDF document using PdfDocument.LoadFromFile() method.
- Render each page of the document in the specified format using PrinterJob.setPrintable() method.
- Call PrinterJob.print() method to print the PDF pages.
- Java
import com.spire.pdf.PdfDocument;
import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
public class PrintInCustomPaperSize {
public static void main(String[] args) {
//Create a PrinterJob object which is initially associated with the default printer
PrinterJob printerJob = PrinterJob.getPrinterJob();
//Create a PageFormat object and set it to a default size and orientation
PageFormat pageFormat = printerJob.defaultPage();
//Return a copy of the Paper object associated with this PageFormat
Paper paper = pageFormat.getPaper();
//Set the width and height of this Paper object
paper.setSize(500,600);
//Set the Paper object for this PageFormat
pageFormat.setPaper(paper);
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a PDF file
pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//Call painter to render the pages in the specified format
printerJob.setPrintable(pdf, pageFormat);
//Execute printing
try {
printerJob.print();
} catch (PrinterException e) {
e.printStackTrace();
}
}
}
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.
Horizontally and Vertically Split a PDF Page into multiple Pages in C#
2019-01-23 06:36:03 Written by KoohjiSpire.PDF supports to horizontally and vertically split a PDF page into two or more pages. This article will show you how to use Spire.PDF to accomplish this function.
The sample PDF file:

Detail steps:
Step 1: Load the sample PDF file and get the first page.
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("New Zealand.pdf");
PdfPageBase page = pdf.Pages[0];
Step 2: Create a new PDF file and remove page margins.
PdfDocument newPdf = new PdfDocument(); newPdf.PageSettings.Margins.All = 0;
Step 3: Set page width and height in order to horizontally or vertically split the first page into 2 pages.
//Horizontally Split newPdf.PageSettings.Width = page.Size.Width; newPdf.PageSettings.Height = page.Size.Height / 2; //Vertically split //newPdf.PageSettings.Width = page.Size.Width / 2; //newPdf.PageSettings.Height = page.Size.Height;
Step 5: Add a new page to the new PDF file.
PdfPageBase newPage = newPdf.Pages.Add();
Step 6: Create layout format.
PdfTextLayout format = new PdfTextLayout(); format.Break = PdfLayoutBreakType.FitPage; format.Layout = PdfLayoutType.Paginate;
Step 7: Create template from the first Page of the sample PDF, and draw the template to the new added page with the layout format.
page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);
Step 8: Save and close.
newPdf.SaveToFile("SplitPage.pdf");
newPdf.Close();
pdf.Close();
Horizontally split:

Vertically split:

Full code:
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace SplitPDFPage
{
class Program
{
static void Main(string[] args)
{
//Load the sample PDF
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("New Zealand.pdf");
//Get the first page
PdfPageBase page = pdf.Pages[0];
//Create a new PDF
PdfDocument newPdf = new PdfDocument();
//Remove page margins
newPdf.PageSettings.Margins.All = 0;
//Set page width and height in order to horizontally split the first page into 2 pages
newPdf.PageSettings.Width = page.Size.Width;
newPdf.PageSettings.Height = page.Size.Height / 2;
//Set page width and height in order to vertically split the first page into 2 pages
//newPdf.PageSettings.Width = page.Size.Width / 2;
//newPdf.PageSettings.Height = page.Size.Height;
//Add a new page to the new PDF
PdfPageBase newPage = newPdf.Pages.Add();
//Create layout format
PdfTextLayout format = new PdfTextLayout();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
//Create template from the first Page of the sample PDF, and draw the template to the new added page with the layout format
page.CreateTemplate().Draw(newPage, new PointF(0, 0), format);
//Save and close
newPdf.SaveToFile("SplitPage.pdf");
newPdf.Close();
pdf.Close();
}
}
}
Merging cells involves combining adjacent cells into a larger one. This allows users to create the title cells that span multiple columns or rows, providing greater design flexibility. On the other hand, splitting cells means dividing a cell into several smaller ones, which is useful for creating detailed layouts or accommodating diverse content. In PowerPoint, users can merge and split table cells to adjust the structure and layout of tables. In this article, we will show you how to merge and split table cells in PowerPoint programmatically by using Spire.Presentation for Java.
Install Spire.Presentation for Java
First of all, you're required to add the Spire.Presentation.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.presentation</artifactId>
<version>11.5.1</version>
</dependency>
</dependencies>
Merge Table Cells in PowerPoint
Spire.Presentation for Java provides users with ITable.get(int columnIndex, int rowIndex) and ITable.mergeCells(Cell startCell, Cell endCell, boolean allowSplitting) methods to get and merge the specific cells. The detailed steps are as follows.
- Create an object of Presentation class.
- Load a sample file using Presentation.loadFromFile() method.
- Declare ITable variable.
- Get the table from the first slide by looping through all shapes.
- Get the specific cells by using ITable.get(int columnIndex, int rowIndex) method and merge them by using ITable.mergeCells(Cell startCell, Cell endCell, boolean allowSplitting) method.
- Save the result file using Presentation.saveToFile() method.
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
public class MergeCells {
public static void main(String[] args) throws Exception {
//Create an object of Presentation class
Presentation presentation = new Presentation();
//Load a sample PowerPoint file
presentation.loadFromFile("sample.pptx");
//Declare ITable variable
ITable table = null;
//Get the table from the first slide by looping through all shapes
for (Object shape : presentation.getSlides().get(0).getShapes()) {
if (shape instanceof ITable) {
table = (ITable) shape;
//Merge the cells from [0,0] to [3,0]
table.mergeCells(table.get(0, 0), table.get(3, 0), false);
//Merge the cells from [0,1] to [0,5]
table.mergeCells(table.get(0, 1), table.get(0, 5), false);
}
}
//Save the result file
presentation.saveToFile("MergeCells.pptx", FileFormat.PPTX_2010);
presentation.dispose();
}
}

Split Table Cells in PowerPoint
Spire.Presentation for Java also supports users to get the specific cell and split it into smaller ones by calling ITable.get(int columnIndex, int rowIndex) and Cell.split(int RowCount, int ColunmCount) methods. The detailed steps are as follows.
- Create an object of Presentation class.
- Load a sample file using Presentation.loadFromFile() method.
- Declare ITable variable.
- Get the table from the first slide by looping through all shapes.
- Get the specific cell by using ITable.get(int columnIndex, int rowIndex) method and split it into 2 rows and 2 columns by using Cell.split(int RowCount, int ColumnCount) method.
- Save the result file using Presentation.saveToFile() method.
- Java
import com.spire.presentation.FileFormat;
import com.spire.presentation.ITable;
import com.spire.presentation.Presentation;
public class SplitCells {
public static void main(String[] args) throws Exception {
//Create an object of Presentation class
Presentation presentation = new Presentation();
//Load a sample PowerPoint file
presentation.loadFromFile("sample.pptx");
//Declare ITable variable
ITable table = null;
//Get the table from the first slide by looping through all shapes
for (Object shape : presentation.getSlides().get(0).getShapes()) {
if (shape instanceof ITable) {
table = (ITable) shape;
//Split the cell[2,2] to 2 rows and 2 columns
table.get(2,2).split(2,2);
}
}
//Save the result file
presentation.saveToFile("SplitCells.pptx", FileFormat.PPTX_2010);
presentation.dispose();
}
}

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.