Insert Audio in PowerPoint in Java
This article demonstrates how to insert an audio file (.wav) in a presentation slide by using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import java.awt.*; import java.awt.geom.Rectangle2D; public class InsertAudio { public static void main(String[] args) throws Exception { //create a Presentation object and load an example PowerPoint file Presentation presentation = new Presentation(); presentation.loadFromFile("C:/Users/Administrator/Desktop/example.pptx"); //add a shape to the first slide Rectangle2D.Double labelRect= new Rectangle2D.Double(50, 120, 100, 50); IAutoShape labelShape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, labelRect); labelShape.getLine().setFillType(FillFormatType.NONE); labelShape.getFill().setFillType(FillFormatType.NONE); labelShape.getTextFrame().setText("Audio:"); labelShape.getTextFrame().getTextRange().setFontHeight(28); labelShape.getTextFrame().getTextRange().setLatinFont(new TextFont("Myriad Pro Light")); labelShape.getTextFrame().getTextRange().getFill().setFillType(FillFormatType.SOLID); labelShape.getTextFrame().getTextRange().getFill().getSolidColor().setColor(Color.BLACK); //append an audio file to the slide Rectangle2D.Double audioRect = new Rectangle2D.Double(170, 120, 50, 50); presentation.getSlides().get(0).getShapes().appendAudioMedia((new java.io.File("C:/Users/Administrator/Desktop/Music.wav")).getAbsolutePath(), audioRect); //save to file presentation.saveToFile("output/InsertAudio.pptx", FileFormat.PPTX_2010); presentation.dispose(); } }
Java: Print PDF Documents
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>10.4.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#
Spire.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(); } } }
Java: Merge and Split Table Cells in PowerPoint
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>9.4.5</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.
How to install Spire Series Products for Java from Maven Repository
Developers can easily use Spire Series Products for Java directly in their Maven Projects with simple configurations. E-iceblue hosts all Java APIs on Maven repository. In this article, we’ll show you how to install Spire Series Products for Java from Maven.
Firstly, please specify e-iceblue Maven Repository configuration / location in your Maven pom.xml as below:
<repositories> <repository> <id>com.e-iceblue</id> <name>e-iceblue</name> <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url> </repository> </repositories>
Then, Specify Maven dependencies for Spire products in the POM.xml file. The following lists the main products configuration sample for your reference.
Please use Spire.Office for Java if you need to operate multiple file formats in the same program, such as Word, Excel, PDF, or other formats. Otherwise, using more than one Spire products in the same program will cause program conflicts and result in exceptions.
Spire.PDF for Java
<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>10.4.4</version> </dependency> </dependencies>
Spire.Doc for Java
<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>12.4.6</version> </dependency> </dependencies>
Spire.XLS for Java
<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.xls</artifactId> <version>14.4.1</version> </dependency> </dependencies>
Spire.Presentation for Java
<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>9.4.5</version> </dependency> </dependencies>
Spire.Office for Java
<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.office</artifactId> <version>9.3.1</version> </dependency> </dependencies>
Additionally, if you want to configure the FREE Spire products, just change the artifactId and version as follows:
Free Spire.PDF for Java
<dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.pdf.free</artifactId> <version>5.1.0</version> </dependency> </dependencies>
Free Spire.Doc for Java
<dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc.free</artifactId> <version>5.2.0</version> </dependency> </dependencies>
Free Spire.XLS for Java
<dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls.free</artifactId> <version>5.1.0</version> </dependency> </dependencies>
Free Spire.Presentation for Java
<dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.presentation.free</artifactId> <version>5.1.0</version> </dependency> </dependencies>
Free Spire.Office for Java
<dependencies> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.office.free</artifactId> <version>5.3.1</version> </dependency> </dependencies>
Kindly Reminder: You can query the historical versions of all Spire Java products in the Maven repository. Take Spire.Doc for Java as an example, all versions of the product are available in the e-IceBlue/spIre.doc/folder path:
Please use Spire.Office for Java or Free Spire.Office for Java if you need to operate multiple file formats in the same program, such as Word, Excel, PDF, or other formats. Otherwise, using more than one Spire products in the same program will cause program conflicts and result in exceptions.
For IDEA, you only need to click "Import Changes" to import the Spire jars.
For Eclipse, you only need to click the "Save" button, then Spire jars will be downloaded automatically.
Now you have successfully defined the Spire Series Product for Java dependency in your Maven project.
Step details for IDEA
Create a new Maven Project: File - New - Project
Set the GroupId for the project:
Update the porm.xml and then Import Changes:
Step details for Eclipse
Create a new Maven Project:
Define the location and set the Group ID:
Update the pom.xml and then click Save button:
Java: Convert PDF to XPS and XPS to PDF
PDF (Portable Document Format) and XPS (XML Paper Specification) are two commonly used document formats for sharing and printing documents. While PDF is widely known and supported, XPS is a Microsoft-developed format that has gained popularity due to its superior graphics rendering capabilities. In this article, we will demonstrate how to use Spire.PDF for Java to convert PDF to XPS and XPS to PDF in high quality.
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>10.4.4</version> </dependency> </dependencies>
Convert PDF to XPS in Java
Spire.PDF for Java has a powerful conversion feature, which can convert PDF to XPS in just three steps. The detailed steps are as follows:
- Create a PdfDocument instance.
- Load a PDF sample document using PdfDocument.loadFromFile() method.
- Save the document as XPS using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*; public class PDFtoXPS { public static void main(String[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load the PDF file pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\Input.pdf.pdf"); //Save to XPS pdf.saveToFile("ToXPS.xps", FileFormat.XPS); pdf.close(); } }
Convert XPS to PDF in Java
The PdfDocument.saveToFile() method provided by Spire.PDF for Java enables the conversion of a XPS file into a PDF document. The following are steps to convert XPS to PDF.
- Create a PdfDocument instance.
- Load a XPS file document using PdfDocument.loadFromFile() method.
- Save the document as PDF using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*; public class XPStoPDF { public static void main(String[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load a XPS file pdf.loadFromXPS("C:\\Users\\Administrator\\Desktop\\sample.xps"); //Save to PDF pdf.saveToFile("toPDF.pdf", FileFormat.PDF); pdf.close(); } }
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.
Java: Convert PDF to SVG
SVG, short for scalable vector graphics, is a vector image format based on XML for two-dimensional graphics. Vector image files, like SVG and PDF files, are very similar. They can display text, images, and other elements in the same appearance and keep the definition no matter how you zoom them. And because of their similarity, PDF files can be converted to SVG files almost losslessly. This article shows an easy method to convert PDF files to SVG files using Spire.PDF for Java.
- Convert Each Page of a PDF File to an SVG File
- Convert All the Pages of a PDF File to a Single SVG File
Install Spire.PDF for Java
First, 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>10.4.4</version> </dependency> </dependencies>
Convert Each Page of a PDF File to an SVG File
The detailed steps are as follows:
- Create an object of PdfDocument class.
- Load a PDF document from disk using PdfDocument.loadFromFile() method.
- Convert the document to SVG file and save it using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*; public class PDFToSVG { public static void main(String[] args) { //Create an object of Document class PdfDocument pdf = new PdfDocument(); //Load a PDF document from disk pdf.loadFromFile("D:/Samples/Sample.pdf"); //Convert the document to SVG and Save it pdf.saveToFile("D:/javaOutput/PDFToSVG.svg", FileFormat.SVG); } }
Convert All the Pages of a PDF File to a Single SVG File
The detailed steps are as follows:
- Create an object of PdfDocument class.
- Load a PDF document from disk using PdfDocument.loadFromFile() method.
- Change the conversion settings to convert the PDF file to a single SVG file using PdfDocument.getConvertOptions().setOutputToOneSvg() method.
- Convert the document to SVG file and save it using PdfDocument.saveToFile() method.
- Java
import com.spire.pdf.*; public class PDFToSVG { public static void main(String[] args) { //Create an object of Document class PdfDocument pdf = new PdfDocument(); //Load a PDF document from disk pdf.loadFromFile("D:/Samples/Sample.pdf"); //Change the conversion settings to convert the PDF file to a single SVG file pdf.getConvertOptions().setOutputToOneSvg(true); //Convert the document to SVG and Save it pdf.saveToFile("D:/javaOutput/PDFToSVG.svg", FileFormat.SVG); } }
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.
Java: Convert PDF to HTML
PDF file format makes the presentation of documents consistent across devices. However, when you need to put PDF documents on web pages, it's better to convert them to HTML files. In this way, all the content of your document can be displayed in the browser directly, with no need for downloading files. And the loading of large PDF documents takes a long time, while HTML files can be rendered in the browser very quickly. In addition, compared to PDF files, it is much easier for search engines to crawl HTML web pages to get information, which will give your website more exposure. This article will show how to convert PDF documents into HTML files in Java using Spire.PDF for Java.
- Convert a PDF document to an HTML file in Java
- Convert a PDF document to an HTML file with SVG Embedded
- Convert a PDF document to HTML Stream in Java
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>10.4.4</version> </dependency> </dependencies>
Convert a PDF document to an HTML file in Java
The conversion from a PDF document to an HTML file can be directly done by loading a PDF document and saving it as an HTML file using PdfDocument.saveToFile(String filename, FileFormat.HTML) method provided by Spire.PDF for Java. The detailed steps are as follows.
- Create an object of PdfDocument.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Save the PDF file as an HTML file using PdfDocument.saveToFle() method.
- Java
Java import com.spire.pdf.*; public class convertPDFToHTML { public static void main(String[] args) { //Create an object of PdfDocument PdfDocument pdf = new PdfDocument(); //Load a PDF file pdf.loadFromFile("C:/Guide to a Foreign Past.pdf"); //Save the PDF file as an HTML file pdf.saveToFile("PDFToHTML.html",FileFormat.HTML); pdf.close(); } }
Convert a PDF document to an HTML file with SVG Embedded
Spire.PDF for Java also provides the PdfDocument.getConvertOptions().setPdfToHtmlOptions(true) method to enable embedding SVG while converting. The detailed steps for converting a PDF file to an HTML file with SVG embedded are as follows.
- Create an object of PdfDocument.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Enable embedding SVG using PdfDocument.getConvertOptions().setPdfToHtmlOptions(true) method.
- Save the PDF file as an HTML file using PdfDocument.saveToFle() method.
- Java
import com.spire.pdf.*; public class convertPDFToHTMLEmbeddingSVG { public static void main(String[] args) { //Create an object of PdfDocument PdfDocument doc = new PdfDocument(); //Load a PDF file doc.loadFromFile("C:/Guide to a Foreign Past.pdf"); //Set embedding SVG doc.getConvertOptions().setPdfToHtmlOptions(true); //Save the PDF file as an HTML file doc.saveToFile("PDFToHTMLEmbeddingSVG.html", FileFormat.HTML); doc.close(); } }
Convert a PDF document to HTML Stream in Java
Spire.PDF for Java also supports converting PDF documents to HTML stream. The detailed steps are as follows.
- Create an object of PdfDocument.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Save the PDF file as HTML stream using PdfDocument.saveToStream() method.
- Java
import com.spire.pdf.*; import java.io.*; public class convertPDFToHTMLStream { public static void main(String[] args) throws FileNotFoundException { //Create an object of PdfDocument PdfDocument pdf = new PdfDocument(); //Load a PDF file pdf.loadFromFile("C:/Guide to a Foreign Past.pdf"); //Save the PDF file as HTML stream File outFile = new File("PDFToHTMLStream.html"); OutputStream outputStream = new FileOutputStream(outFile); pdf.saveToStream(outputStream, FileFormat.HTML); pdf.close(); } }
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.
Java: Convert PDF to Word
Nowadays, it is not difficult to convert PDF documents into Word files using a software. However, if you want to maintain the layout and even the font formatting while converting, it is not something that every software can accomplish. Spire.PDF for Java does it well and offers you the following two modes when converting PDF to Word in Java.
Fixed Layout mode has fast conversion speed and is conducive to maintaining the original appearance of PDF files to the greatest extent. However, the editability of the resulting document will be limited since each line of text in PDF will be presented in a separate frame in the generated Word document.
Flowable Structure is a full recognition mode. The converted content will not be presented in frames, and the structure of the resulting document is flowable. The generated Word document is easy to re-edit but may look different from the original PDF file.
Install Spire.PDF for Java
First, 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>10.4.4</version> </dependency> </dependencies>
Convert PDF to Doc/Docx with Fixed Layout
The following are the steps to convert PDF to Doc or Docx with fixed layout.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Convert the PDF document to a Doc or Docx format file using PdfDocument.saveToFile(String fileName, FileFormat fileFormat) method.
- Java
import com.spire.pdf.FileFormat; import com.spire.pdf.PdfDocument; public class ConvertPdfToWordWithFixedLayout { public static void main(String[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Load a sample PDF document doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf"); //Convert PDF to Doc and save it to a specified path doc.saveToFile("output/ToDoc.doc", FileFormat.DOC); //Convert PDF to Docx and save it to a specified path doc.saveToFile("output/ToDocx.docx", FileFormat.DOCX); doc.close(); } }
Convert PDF to Doc/Docx with Flowable Structure
The following are the steps to convert PDF to Doc or Docx with flowable structure.
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.loadFromFile() method.
- Set the conversion mode as flow using PdfDocument. getConvertOptions().setConvertToWordUsingFlow() method.
- Convert the PDF document to a Doc or Docx format file using PdfDocument.saveToFile(String fileName, FileFormat fileFormat) method.
- Java
import com.spire.pdf.FileFormat; import com.spire.pdf.PdfDocument; public class ConvertPdfToWordWithFlowableStructure { public static void main(String[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Load a sample PDF document doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf"); //Convert PDF to Word with flowable structure doc.getConvertOptions().setConvertToWordUsingFlow(true); //Convert PDF to Doc doc.saveToFile("output/ToDoc.doc", FileFormat.DOC); //Convert PDF to Docx doc.saveToFile("output/ToDocx.docx", FileFormat.DOCX); doc.close(); } }
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.
Add Hyperlinks to a PowerPoint slide in Java
This article demonstrate how to add hyperlinks to a PowerPoint slide using Spire.Presentation for Java.
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import java.awt.geom.Rectangle2D; public class AddHyperlink { public static void main(String[] args) throws Exception { //Create a Presentation instance Presentation presentation = new Presentation(); //Add a shape Rectangle2D.Double rec = new Rectangle2D.Double(presentation.getSlideSize().getSize().getWidth() / 2 - 255, 120, 300, 150); IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rec); shape.getFill().setFillType(FillFormatType.NONE); shape.getLine().setWidth(0); shape.getTextFrame().getParagraphs().clear(); //Add some paragraphs with hyperlinks to the shape ParagraphEx para1 = new ParagraphEx(); PortionEx tr1 = new PortionEx(); tr1.setText("Spire.Presentation for Java"); tr1.getClickAction().setAddress("https://www.e-iceblue.com/Introduce/presentation-for-java.html"); tr1.setLatinFont(new TextFont("Lucida Sans Unicode")); tr1.setFontHeight(20); para1.getTextRanges().append(tr1); shape.getTextFrame().getParagraphs().append(para1); shape.getTextFrame().getParagraphs().append(new ParagraphEx()); ParagraphEx para2 = new ParagraphEx(); PortionEx tr2 = new PortionEx(); tr2.setText("Spire.Presentation for .NET"); tr2.getClickAction().setAddress("https://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html"); tr2.setLatinFont(new TextFont("Lucida Sans Unicode")); tr2.setFontHeight(20); para2.getTextRanges().append(tr2); shape.getTextFrame().getParagraphs().append(para2); //Save the document presentation.saveToFile("AddHyperlink.pptx", FileFormat.PPTX_2010); } }
Output: