如何排列在同一行两段左,右? [英] How to align two paragraphs to the left and right on the same line?

查看:188
本文介绍了如何排列在同一行两段左,右?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示的内容两片(它可以段落或文本)上一行的左侧和右侧。我的输出应该像

I want to display two pieces of content (it may a paragraph or text) to the left and right side on a single line. My output should be like

 Name:ABC                                                               date:2015-03-02

我怎样才能做到这一点?

How can I do this?

推荐答案

请看看在 LeftRight 的例子。它提供了两种不同的解决方案,为您的问题:

Please take a look at the LeftRight example. It offers two different solutions for your problem:

解决方案1:使用胶水

通过胶水,我的意思是一个特殊的,其作用类似于分开分隔两个(或更多)等对象:

By glue, I mean a special Chunk that acts like a separator that separates two (or more) other Chunk objects:

Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph p = new Paragraph("Text to the left");
p.add(new Chunk(glue));
p.add("Text to the right");
document.add(p);

这样的话,你将有左侧文本到左边的文本右侧的右侧

解决方案2:使用 PdfPTable

假设有一天,有人问你把东西在中间过,然后用 PdfPTable 是最面向未来的解决方案:

Suppose that some day, somebody asks you to put something in the middle too, then using PdfPTable is the most future-proof solution:

PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100);
table.addCell(getCell("Text to the left", PdfPCell.ALIGN_LEFT));
table.addCell(getCell("Text in the middle", PdfPCell.ALIGN_CENTER));
table.addCell(getCell("Text to the right", PdfPCell.ALIGN_RIGHT));
document.add(table);

在你的情况,你只需要的东西的左侧和东西的权利,所以你需要创建一个只有两列的表:表=新PdfPTable(2)

In your case, you only need something to the left and something to the right, so you need to create a table with only two columns: table = new PdfPTable(2).

在情况下,你漫步关于 getCell()的方法,这是什么样子:

In case you wander about the getCell() method, this is what it looks like:

public PdfPCell getCell(String text, int alignment) {
    PdfPCell cell = new PdfPCell(new Phrase(text));
    cell.setPadding(0);
    cell.setHorizontalAlignment(alignment);
    cell.setBorder(PdfPCell.NO_BORDER);
    return cell;
}

解决方案3:对齐文本

这是在回答解释这个问题:如何使用iTextSharp的

This is explained in the answer to this question: How justify text using iTextSharp?

不过,这会尽快出现在你的字符串空间导致奇怪的结果。例如::ABC名如果你有它会工作。 布鲁诺布鲁诺Lowagie名将无法正常工作>Lowagie如果你证明行将向中间移动。

However, this will lead to strange results as soon as there are spaces in your strings. For instance: it will work if you have "Name:ABC". It won't work if you have "Name: Bruno Lowagie" as "Bruno" and "Lowagie" will move towards the middle if you justify the line.

这篇关于如何排列在同一行两段左,右?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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