Merge pdf,

Source: Internet
Author: User

Merge pdf,

Merge pdf
Add Jar dependencies to pom. xml
<! -- Pdf Processing jar dependency start -->
<Dependency>
<GroupId> com. itextpdf </groupId>
<ArtifactId> itextpdf </artifactId>
<Version> 5.5.12 </version>
</Dependency>
<! -- Pdf Processing jar dependency end -->

<! -- Pdf Processing jar package dependency start -->
<Dependency>
<GroupId> com. itextpdf </groupId>
<ArtifactId> itext-asian </artifactId>
<Version> 5.2.0 </version>
</Dependency>
<Dependency>
<GroupId> com. lowagie </groupId>
<ArtifactId> itext </artifactId>
<Version> 4.2.1 </version>
</Dependency>
<! -- Pdf Processing jar package dependency end -->


Package com.bjpowernode.p2p.admin.pdf;

Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. util. ArrayList;
Import java. util. Iterator;
Import java. util. List;

Import org. apache. logging. log4j. LogManager;
Import org. apache. logging. log4j. Logger;

Import com. itextpdf. text. Document;
Import com.itext;.text=. BaseFont;
Import com.itext;.text=. inclucontentbyte;
Import com.itext;.text}. PdfImportedPage;
Import com.itext;.text=. PdfReader;
Import com.itext;.text=. compile writer;

/**
* PDF file Merging
*
* @ Author ldd
*
*/
Public class extends mergeutil {

/** Log4j2 logger */
Private static final Logger logger = LogManager. getLogger (mongomergeutil. class );

/**
* PDF file Merging
*
* @ Param streamOfPDFFiles
* @ Param outputStream
* @ Param paginate
*/
Public static void mergePDFs (List <String> inputFileList, String outputFile, boolean paginate ){

Logger.info ("pdf file merging ......");
// Create a new pdf document
Document document = new Document ();

Specified writer = null;
FileInputStream FCM = null;
FileOutputStream fos = null;

Try {
// Create an object for reading Pdf files
List <PdfReader> readers = new ArrayList <PdfReader> ();

For (String filePath: inputFileList ){
FCM = new FileInputStream (filePath );

PdfReader pdfReader = new PdfReader (FCM );
Readers. add (pdfReader );
}
// Read the PDF file
Fos = new FileOutputStream (outputFile );
// Generate a new document
Writer = writable writer. getInstance (document, fos );
// Open the document
Document. open ();
// Set the font
BaseFont bf = BaseFont. createFont (BaseFont. HELVETICA, BaseFont. CP1252, BaseFont. NOT_EMBEDDED );
Required contentbyte cb = writer. getDirectContent ();

PdfImportedPage = null;
Int currentPageNumber = 0;
Int pageOfCurrentReaderPDF = 0;

Iterator <PdfReader> iteratorPDFReader = readers. iterator ();
// Read pdf in sequence
While (iteratorPDFReader. hasNext ()){

PdfReader pdfReader = iteratorPDFReader. next ();

// Create a new page in the target for each source page.
While (pageOfCurrentReaderPDF <pdfReader. getNumberOfPages ()){
Document. newPage ();
PageOfCurrentReaderPDF ++;
CurrentPageNumber ++; // create the current page number
Page = writer. getImportedPage (pdfReader, pageOfCurrentReaderPDF );
Cb. addTemplate (page, 0, 0 );

// Code for pagination.
If (paginate ){
Cb. beginText ();
Cb. setFontAndSize (bf, 9); // font
Cb. showTextAligned (contentbyte. ALIGN_CENTER, String. valueOf (currentPageNumber), 290, 50, 0 );
Cb. endText ();
}
}
PageOfCurrentReaderPDF = 0;
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
If (document. isOpen ()){
Document. close ();
}
If (null! = Fiis ){
FCM. close ();
}
If (null! = Fos ){
Fos. close ();
}
If (null! = Writer ){
Writer. close ();
}
} Catch (IOException e ){
E. printStackTrace ();
}
}
}

/**
* File merging Test
*
* @ Param args
* @ Throws Throwable
*/
Public static void main (String [] args) throws Throwable {
List <String> inputFileList = new ArrayList <String> ();
InputFileList. add ("C: \ dev \ eclipse \ workspace \ p2p-admin \ src \ main \ resources \ sb_loan_contract_borrow.pdf ");
InputFileList. add ("C: \ dev \ eclipse \ workspace \ p2p-admin \ src \ main \ resources \ sb_loan_contract_borrow.pdf ");
String outputFile = "C: \ dev \ eclipse \ workspace \ p2p-admin \ src \ main \ resources \ sb_loan_contract_borrow_merge.pdf ";
// Merge PDF files
PDFMergeUtil. mergePDFs (inputFileList, outputFile, true );
}
}





------------------------------------------- Gorgeous split line ----------------------------------------------------------
List <String> pdfList = new ArrayList <String> ();
PdfList. add (outPutFileName); // company seal pdf
PdfList. add (outFile_table); // table pdf

// PDF file Merging
PDFMergeUtil. mergePDFs (pdfList, outFile_final, true );








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.