使用itext7在PDF中使用三列布局

时间:2016-12-28 03:34:01

标签: java itext

我是itext7的新手 我看到了一个关于columndocumentrenderer的例子。代码如下。

package com.itextpdf.highlevel.chapter02;

import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.ColumnDocumentRenderer;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.hyphenation.HyphenationConfig;
import com.itextpdf.layout.property.AreaBreakType;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.test.annotations.WrapToTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author RNDPC-03
 */@WrapToTest
 public class C02E08_JekyllHydeV4 {


     public static final String SRC = "src/main/resources/txt/jekyll_hyde.txt";
     public static final String DEST = "results/chapter02/jekyll_hyde_v4.pdf";

     public static void main(String args[]) throws IOException {
         File file = new File(DEST);
         file.getParentFile().mkdirs();
         new C02E08_JekyllHydeV4().createPdf(DEST);
     }

     public void createPdf(String dest) throws IOException {
         //Initialize PDF document
         PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

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

         //Set column parameters
         float offSet = 36;
         float gutter = 23;
         float columnWidth = (PageSize.A4.getWidth() - offSet * 2) / 2 - gutter;
         float columnHeight = PageSize.A4.getHeight() - offSet * 2;

         //Define column areas
         Rectangle[] columns = {
             new Rectangle(offSet, offSet, columnWidth, columnHeight),
             new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight)};
         document.setRenderer(new ColumnDocumentRenderer(document, columns));    

         PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
         PdfFont bold = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
         document.setTextAlignment(TextAlignment.JUSTIFIED)
             .setFont(font)
             .setHyphenation(new HyphenationConfig("en", "uk", 3, 3));

         BufferedReader br = new BufferedReader(new FileReader(SRC));
         String line;
         Paragraph p;
         boolean title = true;
         AreaBreak nextArea = new AreaBreak(AreaBreakType.NEXT_AREA);
         while ((line = br.readLine()) != null) {
             p = new Paragraph(line);
             if (title) {
                 p.setFont(bold).setFontSize(12);
                 title = false;
             }
             else {
                 p.setFirstLineIndent(36);
             }
             if (line.isEmpty()) {
                 document.add(nextArea);
                 title = true;
             }
             document.add(p);
         }

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

现在这将有结果。下图

enter image description here

现在我想把它变成三(3)列。这该怎么做?请有人吗?或者只是引导我谢谢!

1 个答案:

答案 0 :(得分:2)

您可以在此处定义两列:

float offSet = 36;
float gutter = 23;
float columnWidth = (PageSize.A4.getWidth() - offSet * 2) / 2 - gutter;
float columnHeight = PageSize.A4.getHeight() - offSet * 2;

//Define column areas
Rectangle[] columns = {
    new Rectangle(offSet, offSet, columnWidth, columnHeight),
    new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight)};

将此更改为三列是明智之举。

只需用以下内容替换上述所有内容:

Rectangle[] columns = {
    new Rectangle(20, 20, 180, 802),
    new Rectangle(207, 15, 180, 802),
    new Rectangle(394, 15, 180, 802) };

现在columns有三个Rectangle元素,将绘制三列。如果三列没有所需的大小,请随意在x构造函数中调整ywidthheightRectangle的值。