LoginSignup
3
3

More than 5 years have passed since last update.

itext7でPDFを作成する~HelloWorld~

Last updated at Posted at 2018-12-30

itext7でPDFを作成

itext7というPDFを作成するライブラリがあるのですが、日本語のドキュメントが少ないので、まとめていこうと思います。
eclipse+gradleでやっていこうと思います。

build.gradleの設定

build.gradle
dependencies {
~略~
    // itext7を追加
    implementation 'com.github.itext.itext7:layout:7.1.4'
    // itext7用日本語フォントを追加
    implementation 'com.github.itext.itext7:font-asian:7.1.4'
    // slf4jを追加
    implementation 'org.slf4j:slf4j-log4j12:1.7.21'
}
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

「slf4j」も読み込んでおかないと以下のようなエラーが出ます。

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

HelloWorldを実行

HelloWorld.java
import java.io.File;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

public class HelloWorld {
    public static void main(String[] args) throws Exception {
        //Initialize PDF writer
        PdfWriter writer = new PdfWriter(new File("HelloWorld.pdf"));


        //Initialize PDF document
        PdfDocument pdf = new PdfDocument(writer);

        // Initialize document
        Document document = new Document(pdf);

        //Add paragraph to the document
        document.add(new Paragraph("Hello World!"));

        //Close document
        document.close();
   }
}

次回:日本語を出そう

さて、チュートリアルの"Hello World!"部分を日本語に置き換えてみると・・・
あれ?真っ白け?
次回はその辺りから!

3
3
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
3
3