【问题标题】:Special alignment using itext使用 itext 进行特殊对齐
【发布时间】:2013-08-29 22:37:35
【问题描述】:

我怎样才能在 itext 上获得这种对齐方式:

从这行:

itext
I use java, itext to write pdf docs
java

我可以得到这个吗:

                        itext
                        I use java, itext to write pdf docs
                        java

第二行居中。

【问题讨论】:

  • “第二行居中”是什么意思?您想将线条居中对齐吗?
  • 我想将最长的线居中,并将其余的线对齐。
  • 我对 iText 不太熟悉,但您可以将文本放在单元格中。然后将单元格居中对齐并将单元格中的文本左对齐。
  • @araknoid 这绝对是要走的路
  • @AlexisPigeon 你有我可以使用的代码示例吗?谢谢。

标签: java itext alignment


【解决方案1】:

使用 iText 获得您想要的东西并不容易,但这可能是一种可行的方法。 您可以创建一个 1 行 3 列的表格,并将您的文本放在带有 ALIGN_LEFT 的中间列中。此解决方案的唯一问题是 iText 中的列宽必须手动设置,因此您必须在运行时计算中间单元格所需的宽度才能获得所需的输出。

这里是一个例子:

String[] rows = {"line 1", "line 222222222222222222", "line 3", "line 4 QWERTOPASDFVBNM"};

Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("C:/SimplePDF.pdf"));
document.open();

document.newPage();

// Creating PdfPTable with 3 cell [Empty][Your Test][Empty]
PdfPTable table = new PdfPTable(3);
PdfPCell fake = new PdfPCell();

fake.setBorder(Rectangle.NO_BORDER); // Hiding Border
table.addCell(fake);

// Creating middle cell
PdfPCell c = new PdfPCell();
c.setHorizontalAlignment(Element.ALIGN_LEFT);
c.setBorder(Rectangle.NO_BORDER);

// Adding strings to the middle cell
for (String string : rows) {
    c.addElement(new Paragraph(string));
}

table.addCell(c);

table.addCell(fake);

// Setting manually column widths
// Depending on String length added before, you should get the
// max length string and compute the width for the middle cell, 
// then the others 2 are just (100% - middle_cell_width)/2
float[] columnWidths = {30f, 40f, 30f};
table.setWidths(columnWidths);

document.add(table);
document.close();

显示方式如下:

【讨论】:

  • 第二列的大小是动态的,我不能选择固定的“40f”。
  • @Monssef 由于您在 iText 中不能有动态框/单元格,因此您必须根据必须放入的最长字符串来计算它...
【解决方案2】:

您可以在itext中设置标签设置,请参考此链接itext-tab

此代码将起作用,您可以自定义标签宽度

public class TabSpacing {

    public static final String DEST = "/home/sunil/Desktop/tab.pdf";

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

    public void createPdf(String dest) throws FileNotFoundException, DocumentException {
        Document document = new Document();

        PdfWriter.getInstance(document, new FileOutputStream(dest));

        document.open();

        Paragraph p = new Paragraph();
        p.setTabSettings(new TabSettings(200f));
        p.add(Chunk.TABBING);
        p.add(new Chunk("itext"));
        document.add(p);

        p = new Paragraph();
        p.setTabSettings(new TabSettings(200f));
        p.add(Chunk.TABBING);
        p.add(new Chunk("I use java, itext to write pdf docs"));

        document.add(p);

        p = new Paragraph();
        p.setTabSettings(new TabSettings(200f));
        p.add(Chunk.TABBING);
        p.add(new Chunk("java"));
        document.add(p);

        document.close();
    }

} 

【讨论】:

  • 问题是关于居中文本。我认为,使用标签不是一个好的解决方案。
猜你喜欢
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-28
  • 2016-09-13
  • 1970-01-01
  • 2019-04-28
相关资源
最近更新 更多