itext
列:iText 5とiText 7
サーチ…
備考
iText 5では、コンテンツを列に整理する場合は、 add()
メソッドを使用してDocument
Paragraph
を追加することはできません。 Text2Pdf.java(iText 5)のコードのコードを再利用することはできません。
代わりにColumnText
オブジェクトを作成し、すべてのParagraph
オブジェクトをこのオブジェクトに追加する必要があります。すべてのコンテンツの追加が完了したら、 go()
メソッドを使用してそのコンテンツのレンダリングを開始できます。そうしている間、私たちは列を追跡し、必要に応じて新しいページを作成する必要があります。
私たちがiText 7で修正したもの:
iText 7では、 Text2Pdf.java(iText 7)のコードをコピー&ペーストすることができます。以前と同じようにadd()
メソッドを引き続き使用できます。コンテンツを1つではなく2つの列でレンダリングしたい場合は、単純にドキュメントレンダラーを変更する必要があります。
Rectangle[] columns = {
new Rectangle(36, 36, 254, 770),
new Rectangle(305, 36, 254, 770)};
document.setRenderer(new ColumnDocumentRenderer(document, columns));
もっと知りたい?
iText 7:ビルディングブロックチュートリアルの第5章であるRootElementの使用を読んでください。 無料の電子ブックを手に入れよう!
Text2PdfColumns.java(iText 5)
次のテキストファイルがあるとします。jekyll_hyde.txt
どうすればこのようなPDFに変換できますか?
iText 5を使用する場合は、次のようなコードが必要です。
public void createPdf(String dest)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
ColumnText ct = new ColumnText(writer.getDirectContent());
BufferedReader br = new BufferedReader(new FileReader(TEXT));
String line;
Paragraph p;
Font normal = new Font(FontFamily.TIMES_ROMAN, 12);
Font bold = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD);
boolean title = true;
while ((line = br.readLine()) != null) {
p = new Paragraph(line, title ? bold : normal);
p.setAlignment(Element.ALIGN_JUSTIFIED);
title = line.isEmpty();
ct.addElement(p);
}
Rectangle[] columns = {
new Rectangle(36, 36, 290, 806), new Rectangle(305, 36, 559, 806)
};
int c = 0;
int status = ColumnText.START_COLUMN;
while (ColumnText.hasMoreText(status)) {
ct.setSimpleColumn(columns[c]);
status = ct.go();
if (++c == 2) {
document.newPage();
c = 0;
}
}
document.close();
}
Text2PdfColumns.java(iText 7)
次のテキストファイルがあるとします。jekyll_hyde.txt
どうすればこのようなPDFに変換できますか?
iText 7を使用する場合は、次のようなコードが必要です。
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf)
.setTextAlignment(TextAlignment.JUSTIFIED);
Rectangle[] columns = {
new Rectangle(36, 36, 254, 770),
new Rectangle(305, 36, 254, 770)};
document.setRenderer(new ColumnDocumentRenderer(document, columns));
BufferedReader br = new BufferedReader(new FileReader(TEXT));
String line;
PdfFont normal = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
boolean title = true;
while ((line = br.readLine()) != null) {
document.add(new Paragraph(line).setFont(title ? bold : normal));
title = line.isEmpty();
}
document.close();
}
出典: developers.itextpdf.comおよびiText 7:Building Blocksチュートリアル