There is a library called itext7 that creates PDF, but there are few Japanese documents, so I will summarize it. I will do it with eclipse + gradle.
build.gradle
dependencies {
~ Abbreviation ~
//Added itext7
implementation 'com.github.itext.itext7:layout:7.1.4'
//Added Japanese font for itext7
implementation 'com.github.itext.itext7:font-asian:7.1.4'
//Added slf4j
implementation 'org.slf4j:slf4j-log4j12:1.7.21'
}
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
If you do not read "slf4j" as well, the following error will occur.
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.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();
}
}
Now, if you replace the "Hello World!" Part of the tutorial with Japanese ... that? Is it pure white? Next time starts from that area!
Recommended Posts