当前位置: 首页 > java > 正文

使用itext工具,实现java分割pdf文件

itext果然是个好东西,虽然很是复杂,但是使用起来确是很好,很弓虽大!

《itext in action》写的很是详细,看了其中的一个分割pdf的类,修改了一下。

参数2个,tag file-you-want-split.pdf

/**
 * pdf split tools
 * @author 2hei.net
 */

package com.2hei.net.pdftools;

import java.io.*;
import java.util.List;
import java.util.Date;
import java.util.ArrayList;
import java.util.Iterator;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.SimpleBookmark;

public class Main extends java.lang.Object {
   
    public static void main(String[] args)
    {
        if (args.length < 2)
        {
            System.err.println(“参数 error “);
        }
        else
        {
            // split pdf
            if(args[0].equals(“1”))
            {  
                StringBuffer path = new StringBuffer();
                for(int i = 0 ;i< args[1].split(“\\.”)[0].split(“\\\\”).length-1;i++)
                {
                    path.append(args[1].split(“\\.”)[0].split(“\\\\”)[i]+”\\“);
                }              
               
                try {
                        int pagenumber = 0;
                        // we create a reader for a certain document
                        PdfReader reader = new PdfReader(args[1]);
                        // we retrieve the total number of pages
                        int n = reader.getNumberOfPages();
                        System.out.println(“”);
                        for(int i=1;i<=n;i++)
                        {
                            Document document = new Document(reader.getPageSizeWithRotation(i));
                            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path+”\\A”+i+”.pdf“));
                            document.open();
                            PdfContentByte cb = writer.getDirectContent();
                            PdfImportedPage page;
                            document.setPageSize(reader.getPageSizeWithRotation(i));
                            page = writer.getImportedPage(reader, i);                       
                            int rotation;
                            rotation = reader.getPageRotation(i);
                            if (rotation == 90 || rotation == 270) {
                                    cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).getHeight());
                            }
                            else {
                                    cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0);
                            }
                            document.close();                           
                        }  
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }

      }

}

本文固定链接: http://2hei.net/itext-java-split-pdf.html | 2hei's site

该日志由 2hei 于2008年03月01日发表在 java 分类下,
原创文章转载请注明: 使用itext工具,实现java分割pdf文件 | 2hei's site

报歉!评论已关闭.