1

iText を使用して 4 つの PDF ファイルを作成する Java アプリを作成しています。画像を含む PDF を作成するものでは、.jar は 0 バイトのファイルを作成し、実行を続行しません。ただし、右クリック >> Run As >> Java Application を実行すると、問題なく動作します。jarを作成するには、次のことを行っています

  • ソースを右クリック
  • 書き出す
  • 実行可能な JAR ファイル
  • 必要なライブラリを生成された JAR に抽出します
  • 終了

そして、ファイル「penguin.jpg」は src ディレクトリの下にあります。

これが私のコードです

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;

public class ITextHelloWorld 
{
    public ITextHelloWorld() throws Exception
    {
        // Create the new document objects
        Document helloWorld = new Document();
        Document bigHello = new Document();
        Document linux = new Document();
        Document tables = new Document();


    /**********************************************************
                        start Big Hello.pdf
    This document is a huge document of text. Approximately 
    28 million characters, 24,391 pages, and 9.5 MB.
     **********************************************************/
    PdfWriter.getInstance(bigHello, new FileOutputStream("Big Hello.pdf"));
    bigHello.open();

    for (int i=0; i <1000000; i++)
    {
        bigHello.add(new Paragraph("Hello World. It's me again."));
    }

    bigHello.close();
    /**********************************************************
                        end Big Hello.pdf
     **********************************************************/

    /**********************************************************
                        start Tables.pdf
    This document will have tables in it
     **********************************************************/

    PdfWriter.getInstance(tables, new FileOutputStream("Tables.pdf"));
    tables.open();

    PdfPTable table=new PdfPTable(4);
    for (int i = 1; i<100; i++)
    {       
        table.addCell("This is cell #" + i + ".\n");
    }

    tables.add(table);
    tables.close();
    /**********************************************************
                        end Tables.pdf
     **********************************************************/

    /**********************************************************
                        start Linux.pdf
    This is a document that has text, graphics, and links.
     **********************************************************/
    PdfWriter.getInstance(linux, new FileOutputStream("Graphics and Text.pdf"));
    linux.open();       
    Image image = Image.getInstance("penguin.jpg");
    linux.add(image);

    linux.add(new Paragraph("Let's talk about Linux. \n\n" +
            "Linux (commonly pronounced /ˈlɪnəks/ LIN-əks in American English, also pronounced " +
            "/ˈlɪnʊks/ LIN-ooks in Europe and Canada) refers to the family of Unix-like computer " +
            "operating systems using the Linux kernel. Linux can be installed on a wide variety of " +
            "computer hardware, ranging from mobile phones, tablet computers and video game consoles, " +
            "to mainframes and supercomputers. Linux is predominantly known for its use " +
            "in servers; in 2009 it held a server market share ranging between 20–40%. Most desktop " +
            "computers run either Microsoft Windows or Mac OS X, with Linux having anywhere from a " +
            "low of an estimated 1–2% of the desktop market to a high of an estimated 4.8%. " +
            "However, desktop use of Linux has become increasingly popular in recent years, partly " +
            "owing to the popular Ubuntu, Fedora, Mint, and openSUSE distributions and the emergence" +
            " of netbooks and smartphones running an embedded Linux."));

    linux.close();
    /**********************************************************
                        end Linux.pdf
     **********************************************************/

    /**********************************************************
                        start Hello World.pdf
    This document is one line of text.
     **********************************************************/
    PdfWriter.getInstance(helloWorld, new FileOutputStream("Hello World.pdf"));
    helloWorld.open();
    helloWorld.add(new Paragraph("Hello World. It's me again."));
    helloWorld.close();
    /**********************************************************
                        end Hello World.pdf
     **********************************************************/

}

public static void main (String args[])
{
    try
    {
        new ITextHelloWorld();
    }

    catch (Exception e)
    {
        System.out.println(e);
    }
}

}

助けてくれてありがとう!
トーマス

4

3 に答える 3

4

トーマス、問題は、jar を作成するときに、ディレクトリ構造を「台無しにする」ことです。次の方法を使用して、jar からイメージを抽出する必要があります。

InputStream stream = this.getClass().getClassLoader()
                              .getResourceAsStream("/images/image.jpg");

必要に応じてイメージへのパスを編集する必要があります。

コードImageは次のようになります。

Image image = Image.getInstance(this.getClass().getResource("/penguin.jpg"));

関連する質問:

Jar内から画像を表示するJava Swing

于 2010-09-01T14:18:04.463 に答える
2

推測では、問題は次の行にあります。

Image image = Image.getInstance("penguin.jpg");

これは src ディレクトリにあるため、最終的に JAR ファイルになります。ただし、ファイル名だけで JAR ファイルからファイルを直接ロードすることはできません。

ただし、Image.getInstance受け取るオーバーロードがあるためURL、これはかなり簡単になります。

Image image = Image.getInstance(this.getClass().getResource("/penguin.jpg"));

/ はファイルシステムのルートではなく、src ディレクトリまたは jar ファイルのルートです。

于 2010-09-01T14:20:45.993 に答える
0

あなたが得ているエラーを知らなくても、それは CLASSPATH の問題だと思います。コマンド ラインから jar ファイルを実行する場合、依存する jar ファイルを指すクラスパスを渡すか、システム クラスパス (環境変数) がアプリケーションの実行に必要なすべての jar ファイルを指す必要があります。

于 2010-09-01T14:18:37.810 に答える