将页面分为两部分,以便我们可以用不同的来源填充每一部分 [英] Divide page in 2 parts so we can fill each with different source

查看:150
本文介绍了将页面分为两部分,以便我们可以用不同的来源填充每一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个用户指南,在其中必须将内容以2种不同的语言放置在同一页面上.因此页面的前半部分将使用英语,而下半部分将使用法语. (将来,他们可能还会要求使用第三种语言,但最多只能使用3种语言).因此每个页面将有2个块.如何在Java中使用iTextPDF实现此目的?

I need to create an User guide, where I've to put the content in 2 different language but on the same page. so the first half of the page would be in English while the second part would be in French. (In future they might ask for 3rd language also, but maximum 3). So each page would have 2 blocks. How can I achieve this using iTextPDF in java ?

更新

以下是对问题进行更深入了解的结构.

Following is the structure for more insight of the question.

推荐答案

如果我正确理解了您的问题,则需要创建如下内容:

If I understand your question correctly, you need to create something like this:

在此屏幕快照中,您会看到《凯撒关于高卢战争的评论》第一本书的第一部分. Gallia omnia est divisa in partes tres ,本文档中的每一页也是如此:上半部分显示的是拉丁文,中间部分显示的是英文文本,下半部分显示为法文.如果您阅读本文,您会发现像我这样的比利时人被认为是所有人中最勇敢的(尽管我们的文明程度不如人们希望的那样).如果要查看PDF,请参见 three_parts.pdf .

In this screen shot, you see the first part of the first book of Caesar's Commentaries on the Gallic War. Gallia omnia est divisa in partes tres, and so is each page in this document: the upper part shows the text in Latin, the middle part shows the text in English, the lower part shows the text in French. If you read the text, you'll discover that Belgians like me are considered being the bravest of all (although we aren't as civilized as one would wish). See three_parts.pdf if you want to take a look at the PDF.

此PDF是使用 ThreeParts 示例创建的.在此示例中,我有9个文本文件:

This PDF was created with the ThreeParts example. In this example, I have 9 text files:

  • http://itextpdf.com/sites/default/files/liber1_1_la.txt
  • http://itextpdf.com/sites/default/files/liber1_1_en.txt
  • http://itextpdf.com/sites/default/files/liber1_1_fr.txt
  • http://itextpdf.com/sites/default/files/liber1_2_la.txt
  • http://itextpdf.com/sites/default/files/liber1_2_en.txt
  • http://itextpdf.com/sites/default/files/liber1_2_fr.txt
  • http://itextpdf.com/sites/default/files/liber1_3_la.txt
  • http://itextpdf.com/sites/default/files/liber1_3_en.txt
  • http://itextpdf.com/sites/default/files/liber1_3_fr.txt

Liber是书中的拉丁语单词,因此所有文件都是第一本书的摘录,更具体地说是拉丁文,英文和法文的第1、2和3节.

Liber is the latin word for book, so all files are snippets from the first book, more specifically sections 1, 2, and 3, in Latin, English and French.

这是我定义语言以及每种语言的矩形的方法:

This is how I defined the languages and he rectangles for each language:

public static final String[] LANGUAGES = { "la", "en", "fr" };
public static final Rectangle[] RECTANGLES = {
    new Rectangle(36, 581, 559, 806),
    new Rectangle(36, 308.5f, 559, 533.5f),
    new Rectangle(36, 36, 559, 261) };

在我的代码中,遍历不同的部分,并为每种语言创建一个ColumnText对象:

In my code, I loop over the different sections, and I create a ColumnText object for each language:

PdfContentByte cb = writer.getDirectContent();
ColumnText[] columns = new ColumnText[3];
for (int section = 1; section <= 3; section++) {
    for (int la = 0; la < 3; la++) {
        columns[la] = createColumn(cb, section, LANGUAGES[la], RECTANGLES[la]);
    }
    while (addColumns(columns)) {
        document.newPage();
        for (int la = 0; la < 3; la++) {
            columns[la].setSimpleColumn(RECTANGLES[la]);
        }
    }
    document.newPage();
}

如果检查内部循环的主体,您会发现我首先定义了三个ColumnText对象,每种语言都定义了一个对象:

If you examine the body of the inner loop, you see that I first define three ColumnText objects, one for each language:

public ColumnText createColumn(PdfContentByte cb, int i, String la, Rectangle rect)
    throws IOException {
    ColumnText ct = new ColumnText(cb);
    ct.setSimpleColumn(rect);
    Phrase p = createPhrase(String.format("resources/text/liber1_%s_%s.txt", i, la));
    ct.addText(p);
    return ct;
}

在这种情况下,我在文本模式下使用ColumnText ,然后将不同文件中的文本读入Phrase中,如下所示:

In this case, I'm using ColumnText in text mode, and I read the text from the different files into a Phrase like this:

public Phrase createPhrase(String path) throws IOException {
    Phrase p = new Phrase();
    BufferedReader in = new BufferedReader(
        new InputStreamReader(new FileInputStream(path), "UTF8"));
    String str;
    while ((str = in.readLine()) != null) {
        p.add(str);
    }
    in.close();
    return p;
}

一旦定义了ColumnText对象并添加了它们的内容,就需要将内容呈现到更多页面之一,直到从所有列中呈现所有文本为止.为此,我们使用以下方法:

Once I have defined the ColumnText objects and added their content, I need to render the content to one of more pages until all the text is rendered from all columns. To achieve this, we use this method:

public boolean addColumns(ColumnText[] columns) throws DocumentException {
    int status = ColumnText.NO_MORE_TEXT;
    for (ColumnText column : columns) {
        if (ColumnText.hasMoreText(column.go()))
            status = ColumnText.NO_MORE_COLUMN;
    }
    return ColumnText.hasMoreText(status);
}

如您所见,我还将为我开始的每个新部分创建一个新页面.这并不是真的必要:我可以将所有部分添加到单个ColumnText中,但是根据拉丁文本翻译成英文和法文的方式,最终可能会出现较大的差异,其中拉丁文本的X部分以一个开头页和英文或法文的同一部分从另一页开始.因此,我选择开始一个新页面,尽管在这个小的概念证明中并没有必要.

As you can see, I also create a new page for every new section I start. This isn't really necessary: I could add all the section to a single ColumnText, but depending on how the Latin text translated into English and French, you could end up with large discrepancies where section X of the Latin text starts on one page and the same section in English or French starts on another page. Hence my choice to start a new page, although it's not really necessary in this small proof of concept.

这篇关于将页面分为两部分,以便我们可以用不同的来源填充每一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆