Summary of multi document conversion methods for Aspose components in Java

  • 2021-06-29 11:05:59
  • OfStack

What is Aspose?

Aspose.Total is the most complete office document management program owned by Aspose. It mainly provides control packages in the two development languages of.net and java. Through it, we can systematically manipulate some of the most popular file formats in the business: Word, Excel, PowerPoint, Project, and so on.In addition to the powerful file manipulation components, Aspose.Total provides components for mapping, writing e-mail, spell checking, creating barcodes, generating ad hoc queries, reproducing formats, and workflows, with which we can organize a complete document management scheme.

Functions included:

Aspose.Word:

Aspose.Words is an advanced class library that allows you to perform various document processing tasks directly in individual applications.Aspose.Words supports DOC, OOXML, RTF, HTML, OpenDocument, PDF, XPS, EPUB and other formats.With Aspose.Words, you can generate, change, convert, render, and print documents without using Microsoft Word.

Aspose.Cell:

Aspose.Cells is a widely recognized spreadsheet component that supports all Excel format types of operations. Users do not need to rely on Microsoft Excel to embed read, write and process Excel data tables into their applications.Aspose.Cells can import and export every specific data, table and format, import images at all levels, apply complex calculation formulas, save Excel's data in various formats, etc. - Microsoft Excel and Microsoft Office Automation are not required to complete all of these functions.

Aspose.PDF:

Aspose.Pdf is an PDF document creation component that helps users read, write and manipulate PDF files without using Adobe Acrobat.Aspose.Pdf Rich Features: PDF Document Compression Options, Table Creation and Operation, Chart Support, Image Functions, Rich Hyperlink Functions, Extended Security Components, and Custom Font Processing.

Aspose.BarCode:

Aspose.BarCode is a powerful and robust barcode generation and identification component.

Aspose.Slide:

Aspose.Slides is a unique control for PowerPoint management. Users can read, write and manipulate Microsoft PowerPoint files in applications without using Microsoft PowerPoint.Aspose.Slides is the first component that can manage PowerPoint documents in a user's application.

Aspose.Task:

Aspose.Tasks is a non-graphic.NET project management component that allows the.NET application to read, write, and manage project documents without using Microsoft Project.With Aspose.Tasks you can read and change tasks, reproduce tasks, resources, resource allocation, relationships, and calendars.Aspose.Tasks is a very mature product that provides stability and flexibility.

Today we will focus on the first three, Aspose.Words, Aspose.Cells, Aspose.Pdf. We use these three components to achieve the functions of Word to Pdf, Excel to Pdf, and several Pdf to merge into one Pdf. Here we are going to dry.

Jar package and how to obtain License:

Link: https://pan.baidu.com/s/1PwtvfPGLu1MIpMLNHKHTmg

Extraction code: 5la1


import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA
 * Created By  Gu Dongcheng 
 * Date: 2019/2/21
 * Time: 14:01
 * Desc:  Use Aspose.Words take word File conversion to pdf file 
 */
public class AsposeWordTest {

 /**
 *  Obtain license
 *
 * @return
 */
 private static boolean getLicense() {
 boolean result = false;
 try {
  //  voucher 
  String license =
   "<License>\n" +
    " <Data>\n" +
    " <Products>\n" +
    " <Product>Aspose.Total for Java</Product>\n" +
    " <Product>Aspose.Words for Java</Product>\n" +
    " </Products>\n" +
    " <EditionType>Enterprise</EditionType>\n" +
    " <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
    " <LicenseExpiry>20991231</LicenseExpiry>\n" +
    " <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
    " </Data>\n" +
    " <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
    "</License>";
  InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
  License asposeLic = new License();
  asposeLic.setLicense(is);
  result = true;
 } catch (Exception e) {
  e.printStackTrace();
 }
 return result;
 }

 /**
 * word  turn  pdf
 * @param docPath  To convert word File Path 
 * @param pdfPath  Output after conversion pdf File Path 
 */
 public static void doc2pdf(String docPath,String pdfPath) {
 if (!getLicense()) {
  return;
 }

 try {
  Document convertDoc = new Document(new FileInputStream(docPath));
  convertDoc.save(pdfPath, SaveFormat.PDF);
 } catch (Exception e) {
  e.printStackTrace();
 }
 }

 public static void main(String[] args) {
 // Create directly on the hard disk 1 Blanks pdf that will do 
 AsposeWordTest.doc2pdf("H:/test.docx","H:/testDoc.pdf");
 }

}

import com.aspose.cells.License;
import com.aspose.cells.SaveFormat;
import com.aspose.cells.Workbook;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * Created with IntelliJ IDEA
 * Created By  Gu Dongcheng 
 * Date: 2019/2/22
 * Time: 9:01
 * Desc:  Use Aspose.Cells take excel File conversion to pdf file 
 */
public class AsposeCellTest {
 /**
 *  Obtain license
 *
 * @return
 */
 private static boolean getLicense() {
 boolean result = false;
 try {
  //  voucher 
  String license =
   "<License>\n" +
    " <Data>\n" +
    " <Products>\n" +
    " <Product>Aspose.Total for Java</Product>\n" +
    " <Product>Aspose.Words for Java</Product>\n" +
    " </Products>\n" +
    " <EditionType>Enterprise</EditionType>\n" +
    " <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
    " <LicenseExpiry>20991231</LicenseExpiry>\n" +
    " <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
    " </Data>\n" +
    " <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
    "</License>";
  InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
  License asposeLic = new License();
  asposeLic.setLicense(is);
  result = true;
 } catch (Exception e) {
  e.printStackTrace();
 }
 return result;
 }

 /**
 * excel  turn  pdf
 * @param excelPath  To convert excel File Path 
 * @param pdfPath  Output after conversion pdf File Path 
 */
 public static void excel2pdf(String excelPath,String pdfPath) {
 if (!getLicense()) {
  return;
 }
 try {
  Workbook convertExcel = new Workbook(new FileInputStream(excelPath));
  convertExcel.save(pdfPath, SaveFormat.PDF);
 }catch (Exception e) {
  e.printStackTrace();
 }
 }

 public static void main(String[] args) {
 // Create directly on the hard disk 1 Blanks pdf that will do 
 AsposeCellTest.excel2pdf("H:/test.xlsx","H:/testExcel.pdf");
 }
}

import com.aspose.pdf.Document;
import com.aspose.pdf.License;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Created By  Gu Dongcheng 
 * Date: 2019/2/22
 * Time: 9:24
 * Desc:  Use Aspose.Pdf Will be multiple pdf Merge into 1 individual pdf file 
 */
public class AsposePdfTest {
 /**
 *  Obtain license
 *
 * @return
 */
 private static boolean getLicense() {
 boolean result = false;
 try {
  //  voucher 
  String license =
   "<License>\n" +
    " <Data>\n" +
    " <Products>\n" +
    " <Product>Aspose.Total for Java</Product>\n" +
    " <Product>Aspose.Words for Java</Product>\n" +
    " </Products>\n" +
    " <EditionType>Enterprise</EditionType>\n" +
    " <SubscriptionExpiry>20991231</SubscriptionExpiry>\n" +
    " <LicenseExpiry>20991231</LicenseExpiry>\n" +
    " <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" +
    " </Data>\n" +
    " <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n" +
    "</License>";
  InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
  License asposeLic = new License();
  asposeLic.setLicense(is);
  result = true;
 } catch (Exception e) {
  e.printStackTrace();
 }
 return result;
 }

 /**
 *
 * @param pdfPaths  To merge pdf File Path 
 * @param mergeSavePath  Final merge completes output pdf File Path 
 */
 public static void pdfMergePdf(List<String> pdfPaths,String mergeSavePath){
 try {
  Document mergeDocument = new Document();
  for (String pdfPath : pdfPaths) {
  Document document = new Document(new FileInputStream(pdfPath));
  mergeDocument.getPages().add(document.getPages());
  }
  mergeDocument.save(mergeSavePath);
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }
 }

 public static void main(String[] args) {
 // Need to be merged pdf File Placement list
 List<String> pdfPaths = Arrays.asList("H:/testDoc.pdf","H:/testExcel.pdf");
 // Create directly on the hard disk 1 Blanks pdf that will do 
 AsposePdfTest.pdfMergePdf(pdfPaths,"H:/merge.pdf");
 }
}

Matters needing attention:

Aspose.Words, Aspose.Cells, Aspose.Pdf all have Document, License, SaveFormat classes in the three Jar packages. If there is no error in the Idea or Eclipse console, but opening PDF prompts PDF that the format is incorrect or the file is corrupted, check that one report package is correct. There is also a way to get license.xml via class loader on the License network, but I try to get license by null pointer exception many times, so I use string splicing directly, if you have a better way to leave a message directly. The blog serves only as a learning record for the blogger and is used for communication. If there are any inaccuracies, leave a message in the comments area to correct them.

Related articles: