4

JavaでPDFリーダーを作成しています。PDFファイルを読むために、iTextライブラリを使用しています。PDFファイルを読み取るサンプルコードがありますが、iText ImageオブジェクトをJFrameに表示する方法がわかりません.BelowはPDFファイルを読み取るコードです。

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class ReadAndUsePdf {
private static String INPUTFILE = "c:/temp/FirstPdf.pdf";
private static String OUTPUTFILE = "c:/temp/ReadPdf.pdf";

public static void main(String[] args) throws DocumentException,
        IOException {
    Document document = new Document();

    PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(OUTPUTFILE));
    document.open();
    PdfReader reader = new PdfReader(INPUTFILE);
    int n = reader.getNumberOfPages();
    PdfImportedPage page;
    // Go through all pages
    for (int i = 1; i <= n; i++) {
        // Only page number 2 will be included
        if (i == 2) {
            page = writer.getImportedPage(reader, i);
            Image instance = Image.getInstance(page); //how to show this object in a JFrame
            document.add(instance);
        }
    }
    document.close();

}

}
4

1 に答える 1

1

私はまったく同じ問題を抱えており、ICEPdfを使用してSwingでPDFをレンダリングし、iTextを使用してそれを操作することで解決しました。

重複の可能性: Swing アプリケーションで iText を使用して PDF をレンダリングできますか?

于 2012-08-27T19:03:47.023 に答える