如何对齐块的左侧和右侧 [英] How to align a Chunk Left and Right Side
问题描述
我想将Chunk
的左侧和右侧对齐.另外,每个Chunk
的格式都不同,您可以在图像中看到它们.
i want to align the Chunk
Left and Right side. Also the each Chunk
have different format you can see in image.
我尝试以下代码:
Chunk invoiceid = new Chunk("Invoice ID: ", font9BoldDARK_GRAY);
Chunk invoiceidvalue = new Chunk("value from database", font10NoramlBlack);
Chunk inoicdate = new Chunk("Invoice Date: ", font9BoldDARK_GRAY);
Chunk inoicedatevalue = new Chunk(new SimpleDateFormat("MMMM dd yyyy").format(new Date()), font10NoramlBlack);// get From database
Paragraph invoiceParagraph = new Paragraph();
invoiceParagraph.setTabSettings(new TabSettings(325f));
invoiceParagraph.add(invoiceid);
invoiceParagraph.add(invoiceidvalue);
invoiceParagraph1.add(Chunk.TABBING);
invoiceParagraph1.add(inoicdate1);
invoiceParagraph1.add(inoicedatevalue1);
invoiceParagraph1.setAlignment(Element.ALIGN_JUSTIFIED);
pdfdocument.add(invoiceParagraph1);
这给了我结果右侧Chunk
格式不正确.
which gives me result the Right side Chunk
not in well format.
但是我想对齐 你能帮我么.
but i want align the like can you please help me.
推荐答案
The correct way to do this, is to use iText 7. Read chapter 3 of the building blocks tutorial to discover how to create a PDF that looks like this:
实现这一目标的代码非常简单:
The code to achieve this, is amazingly simple:
public void createPdf(String dest) throws IOException {
// create a low-level document
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
// create a high-level document
Document document = new Document(pdf);
// create a TabStop array
List<TabStop> tabstops = new ArrayList<TabStop>();
// don't forget to make a tab stop that aligns to the right
tabstops.add(new TabStop(325, TabAlignment.RIGHT));
// add paragraphs with Tab objects to the high-level document
document.add(new Paragraph().addTabStops(tabstops)
.add("Text to the left").add(new Tab()).add("Text to the right"));
document.add(new Paragraph().addTabStops(tabstops)
.add("ABCD").add(new Tab()).add("EFGH"));
document.add(new Paragraph().addTabStops(tabstops)
.add("01234").add(new Tab()).add("56789"));
document.add(new Paragraph().addTabStops(tabstops)
.add("iText 5 is old").add(new Tab()).add("iText 7 is new"));
// close the document
document.close();
}
在您说我不认识该代码之前,让我告诉您您是正确的:iText已完全重写.该代码现在可以更直观地创建,并且更易于阅读.
Before you say I don't recognize that code, let me tell you that you are right: iText was completely rewritten. The code is now more intuitive to create, and easier to read.
更新:您正在使用旧的iText 5版本,该版本不再受免费用户(仅针对付费客户)的支持.如2009年的书籍《 iText in Action-Second Edition》中所述,您这样创建了glue
:
Update: you are using the old iText 5 version that is no longer supported for free users (only for paying customers). As explained in the 2009 book "iText in Action - Second Edition", you created glue
like this:
Chunk glue = new Chunk(new VerticalPositionMark())
然后您在Paragraph
中使用这种胶水,如下所示:
You then used this glue in a Paragraph
like this:
Paragraph p = new Paragraph();
p.add("Text to the left");
p.add(glue);
p.add("Text to the right");
这样,您确实可以避免在iText 5中使用选项卡.
This way, you can indeed avoid having to use tabs in iText 5.
这篇关于如何对齐块的左侧和右侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!