In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces Java how to merge, split PDF documents, the article is very detailed, has a certain reference value, interested friends must read!
Tools: Free Spire.PDF for Java (Free Edition)
Jar file acquisition and import:
Method 1: Download the package through the official website. After downloading, extract the file and import the Spire.Pdf.jar file under lib folder into java program.
Method 2: Import to maven project via maven repository installation.
Java code examples
I. Merger
1. Merge PDF documents
import com.spire.pdf.*; import java.io.*; public class Merge2 { public static void main(String[] args) throws Exception { String outputFile = "output/mergeFilesByStream.pdf"; FileInputStream stream1 = new FileInputStream(new File("sample1.pdf")); FileInputStream stream2 = new FileInputStream(new File("sample2.pdf")); FileInputStream stream3 = new FileInputStream(new File("sample3.pdf")); //Load PDF sample documents InputStream[] streams = new FileInputStream[]{stream1, stream2, stream3}; //Merge PDF documents PdfDocumentBase doc = PdfDocument.mergeFiles(streams); //Save document doc.save(outputFile); doc.close(); }}
Before consolidation:
After consolidation:
2. Merge PDF pages
import com.spire.pdf.*; import com.spire.pdf.graphics.PdfBlendMode; import com.spire.pdf.graphics.PdfTemplate; import java.awt.geom.Point2D; public class MergePages { public static void main(String[] args) { //Load two PDF documents PdfDocument pdf1 = new PdfDocument(); pdf1.loadFromFile("test1.pdf"); PdfDocument pdf2 = new PdfDocument(); pdf2.loadFromFile("test2.pdf"); //Draw template according to the first page of pdf1 PdfTemplate template = pdf1.getPages().get(0).createTemplate(); //Get the second page of pdf2 PdfPageBase page = pdf2.getPages().get(1); //Set transparency and overlap styles page.getCanvas().setTransparency(1f,1f, PdfBlendMode.Normal); //Write the contents of the first page of pdf1 to the specified position on the second page of pdf2 page.getCanvas().drawTemplate(template,new Point2D.Float(0,350)); //Save pdf2 pdf2.saveToFile("MergePages.pdf",FileFormat.PDF); pdf2.dispose(); } }
Before consolidation:
After consolidation:
II. Split
The test documentation is as follows:
1. Split by page
import com.spire.pdf.*; public class SplitPDF1 { public static void main(String[] args) { //Load PDF document to be split PdfDocument doc = new PdfDocument(); doc.loadFromFile("test.pdf"); //Call split() to split PDF documents into separate documents per page doc.split("output/splitDocument-{0}.pdf", 0); doc.close(); }}
Split result:
2. Split by specified page range
import com.spire.pdf.*; import com.spire.pdf.graphics.PdfMargins; import java.awt.geom.Point2D; public class SplitPDF2 { public static void main(String[] args) { //Load PDF document to be split PdfDocument doc = new PdfDocument(); doc.loadFromFile("test.pdf"); //Create the first PDF document 1 PdfDocument newpdf1 = new PdfDocument(); PdfPageBase page; //Split pages 1 and 2 of the original PDF document and save it to newpdf1 for(int i = 0;i
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.