问题
i want to align the Chunk
Left and Right side. Also the each Chunk
have different format you can see in image.
i try the below code:
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);
which gives me result the Right side Chunk
not in well format.
but i want align the like can you please help me.
回答1:
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();
}
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.
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())
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");
This way, you can indeed avoid having to use tabs in iText 5.
回答2:
this work for me:
Paragraph p = new Paragraph("some Text");
p.setAlignment(Element.ALIGN_RIGHT);
来源:https://stackoverflow.com/questions/47635334/how-to-align-a-chunk-left-and-right-side