読者です 読者をやめる 読者になる 読者になる

PDFを結合する、javaプログラムで。

iText で実行するPDFを結合するクラスを書いてみた。

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfReader;

/**
 * PDF結合.
 */

public class PDFcat {
   private OutputStream out;
   /** コンストラクタ. 出力ストリームは結合実行でCLOSEする */
   public PDFcat(OutputStream out){
      this.out = out;
   }

   public void join(String...targets){
      File files = new File[targets.length];
      for(int i=0;i < files.length;i++) files[i] = new File(targets[i]);
      join(files);
   }

   public void join(Collection<File> files){
      join(files.toArray(new File
{}));
   }

   public void join(File...targets){
      if (targets.length < 1) return;
      try{
         FileInputStream input = new FileInputStream(targets[0]);
         PdfReader pdfreader = new PdfReader(input);
         pdfreader.consolidateNamedDestinations();
         Document document = new Document(pdfreader.getPageSizeWithRotation(1));
         PdfCopy writer = new PdfCopy(document, out);
         document.open();
         int readMax = pdfreader.getNumberOfPages();
         for(int i=1;i <= readMax;i++){
            writer.addPage(writer.getImportedPage(pdfreader, i));
         }
         writer.freeReader(pdfreader);
         input.close();

         for(int i=1;i < targets.length;i++){
            FileInputStream in = new FileInputStream(targets[i]);
            PdfReader reader = new PdfReader(in);
            reader.consolidateNamedDestinations();
            int pageCount = reader.getNumberOfPages();
            for(int p=1;p <= pageCount;p++){
               writer.addPage(writer.getImportedPage(reader, p));
            }
            writer.freeReader(reader);
            in.close();

         }
         document.close();
      }catch(IOException | DocumentException e){
         // TODO
         e.printStackTrace();
      }finally{
         try{ out.close(); }catch(IOException e){}
      }
   }
}

====================
itext-2.1.7.jar が必要で、Maven で取得するのが楽であろう。

dependencyManagement で取ってこなくてはならない。


<dependencyManagement>

   <dependencies>
      <dependency>
         <groupId>com.lowagie</groupId>
         <artifactId>itext</artifactId>
         <version>2.1.7</version>
      </dependency>
   </dependencies>
</dependencyManagement>


これだけの操作だけでなくPDFをいろいろと弄繰り回すなら、以下もpom,xml に書いて取得します。

<dependency>
   <groupId>com.lowagie</groupId>
   <artifactId>itext</artifactId>
   <exclusions>
      <exclusion>
         <artifactId>bcmail-jdk14</artifactId>
         <groupId>bouncycastle</groupId>
      </exclusion>
      <exclusion>
         <artifactId>bcprov-jdk14</artifactId>
         <groupId>bouncycastle</groupId>
      </exclusion>
   </exclusions>
</dependency>
<dependency>
   <groupId>org.bouncycastle</groupId>
   <artifactId>bcmail-jdk14</artifactId>
   <version>1.46</version>
</dependency>
<dependency>
   <groupId>org.bouncycastle</groupId>
   <artifactId>bcprov-jdk14</artifactId>
   <version>1.46</version>
</dependency>
<dependency>
   <groupId>org.bouncycastle</groupId>
   <artifactId>bctsp-jdk14</artifactId>
   <version>1.46</version>
</dependency>
<dependency>
   <groupId>com.lowagie</groupId>
   <artifactId>iTextAsian</artifactId>
   <version>1.0</version>
</dependency>
<dependency>
   <groupId>net.sf.jasperreports</groupId>
   <artifactId>jasperreports</artifactId>
   <version>5.6.0</version>
</dependency>