0
0

More than 3 years have passed since last update.

Java PDF添付ファイルの追加

Posted at

PDF およびそれ以外のタイプのファイルを PDF に添付できます。PDF を別の場所に移動すると、添付ファイルも PDF 文書と一緒に移動します。今回は Spire.PDF for Java を使ってPDFに添付ファイルを追加する方法を紹介します。

下準備

1.E-iceblueの公式サイトからFree Spire.PDF for Java無料版をダウンロードしてください。

f:id:lendoris:20210331103946p:plain

2.IDEを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire.PDF.jarを参照に追加してください。

f:id:lendoris:20210331104004p:plain


import com.spire.pdf.annotations.*;
import com.spire.pdf.attachments.PdfAttachment;
import com.spire.pdf.graphics.*;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class AttachFiles {

    public static void main(String[] args) throws IOException {

        //PdfDocument objectを作成します。 
        PdfDocument doc = new PdfDocument();

        //PDFをロードします。
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pdf");

        // PDFに添付ファイルを追加します。
        PdfAttachment attachment = new PdfAttachment("添付ファイル.docx");
        doc.getAttachments().add(attachment);

        //ラベルを作成します。
        String label = "ラベル.xlsx";
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,12),true);
        double x = 35;
        double y = doc.getPages().get(0).getActualSize().getHeight() - 200;
        doc.getPages().get(0).getCanvas().drawString(label, font, PdfBrushes.getOrange(), x, y);

        //補注ファイルを追加します。
        String filePath = "補注ファイル.xlsx";
        byte[] data = toByteArray(filePath);
        Dimension2D size = font.measureString(label);
        Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 2), (float) y, 10, 15);
        PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data);
        annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)));
        annotation.setFlags(PdfAnnotationFlags.Default);
        annotation.setIcon(PdfAttachmentIcon.Graph);
        annotation.setText("クリックして.xlsx");
        doc.getPages().get(0).getAnnotationsWidget().add(annotation);

        //保存します。
        doc.saveToFile("Attachments.pdf");
    }

    // byte配列にデータを読み取ります。
    public static byte[] toByteArray(String filePath) throws IOException {

        File file = new File(filePath);
        long fileSize = file.length();
        if (fileSize > Integer.MAX_VALUE) {
            System.out.println("file too big...");
            return null;
        }
        FileInputStream fi = new FileInputStream(file);
        byte[] buffer = new byte[(int) fileSize];
        int offset = 0;
        int numRead = 0;
        while (offset < buffer.length
                && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
            offset += numRead;
        }

        if (offset != buffer.length) {
            throw new IOException("Could not completely read file "
                    + file.getName());
        }
        fi.close();
        return buffer;
    }
}

実行結果

f:id:lendoris:20210331104057p:plain

 

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0