Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to provide alignment to chunk or phrases in itext?

I have two chunks, added in phrases and then in paragraph.

Chunk reportTitle= new Chunk("Candidate Login Report ",catFont);
Chunk divisiontitle = new Chunk("Division : \t\t"+divisionName);

Phrase phrase = new Phrase();
phrase.add(reportTitle);
phrase.add(divisiontitle);

Paragraph para = new Paragraph();
para.add(phrase);

I have to set chunk divisiontitle to right aligned. Is there any provision to do this in iIext?

like image 415
Amit Singh Avatar asked Aug 28 '13 11:08

Amit Singh


4 Answers

via https://stackoverflow.com/a/47644620:

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

tested as working under iTextSharp.LGPLv2.Core 1.4.5 (iText 4)

like image 160
user326608 Avatar answered Nov 15 '22 14:11

user326608


You really answered yourself in your comment.

Chunk reportTitle= new Chunk("Candidate Login Report ",catFont);
Chunk divisiontitle = new Chunk("Division : \t\t"+divisionName);

Phrase phrase = new Phrase();
phrase.add(reportTitle);
phrase.add(divisiontitle);

Paragraph para = new Paragraph();
para.add(phrase);
para.setAlignment(Element.ALIGN_RIGHT);

Then you can add it to any cell and it will be correctly aligned.

like image 39
Rodrigo Godoy Avatar answered Nov 15 '22 14:11

Rodrigo Godoy


add multiple Chunk and Phrase in itextpdf
package com.pdf.hl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.html.WebColors;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class GraphicsStateOperators {

    public static final String RESULT = "d:/graphics_state.pdf";
    static Font.FontFamily ff = Font.FontFamily.HELVETICA;
    static float size = 10;
    static float size1 = 8;

    public void createPdf(String filename) throws Exception {
        Font bold = new Font(Font.FontFamily.HELVETICA, 8f, Font.BOLD);
        Font normal = new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL);
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream(filename));
        document.open();
        // /////////////////////////////////////////////
        PdfPTable tabletmp = new PdfPTable(1);
        tabletmp.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        tabletmp.setWidthPercentage(100);
        PdfPTable table = new PdfPTable(2);
        float[] colWidths = { 45, 55 };
        table.setWidths(colWidths);
        String imageUrl = "http://logo.com/content1/images/logo_heartSmart.jpg";
        Image image2 = Image.getInstance(new URL(imageUrl));
        image2.setWidthPercentage(60);
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
        table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP);
        PdfPCell cell = new PdfPCell();
        cell.setBorder(Rectangle.NO_BORDER);
        cell.addElement(image2);
        table.addCell(cell);
        String receiptNo = "123455555555555555";
        String collectionDate = "09/09/09";
        Chunk chunk1 = new Chunk("Date: ", normal);
        Phrase ph1 = new Phrase(chunk1);

        Chunk chunk2 = new Chunk(collectionDate, bold);
        Phrase ph2 = new Phrase(chunk2);

        Chunk chunk3 = new Chunk("\nReceipt No: ", normal);
        Phrase ph3 = new Phrase(chunk3);

        Chunk chunk4 = new Chunk(receiptNo, bold);
        Phrase ph4 = new Phrase(chunk4);

        Paragraph ph = new Paragraph();
        ph.add(ph1);
        ph.add(ph2);
        ph.add(ph3);
        ph.add(ph4);

        table.addCell(ph);
        tabletmp.addCell(table);
        PdfContentByte canvas = writer.getDirectContent();
        canvas.saveState();
        canvas.setLineWidth((float) 10 / 10);
        canvas.moveTo(40, 806 - (5 * 10));
        canvas.lineTo(555, 806 - (5 * 10));
        canvas.stroke();
        document.add(tabletmp);
        canvas.restoreState();
        PdfPTable tabletmp1 = new PdfPTable(1);
        tabletmp1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        tabletmp1.setWidthPercentage(100);
        PdfPTable table1 = new PdfPTable(2);
        table1.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        table1.getDefaultCell().setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
        table1.getDefaultCell().setVerticalAlignment(Element.ALIGN_JUSTIFIED);
        float[] colWidths1 = { 60, 40 };
        table1.setWidths(colWidths1);
        String Patient = "abcddd";
        String Email = "[email protected]";
        String Phone = "89890099890890";
        PdfPCell cell3 = new PdfPCell();
        cell3.setBorder(Rectangle.NO_BORDER);
        Chunk chunkPatientLabal = new Chunk("Patient: ", normal);
        Phrase phPatientLabal = new Phrase(chunkPatientLabal);
        Chunk chunkPatient = new Chunk(Patient, bold);
        Phrase phPatient = new Phrase(chunkPatient);
        Chunk chunkEmailLabal = new Chunk("\nEmail: ", normal);
        Phrase phEmailLabal = new Phrase(chunkEmailLabal);
        Chunk chunkEmail = new Chunk(Email, bold);
        Phrase phEmail = new Phrase(chunkEmail);
        Chunk chunkPhoneLabal = new Chunk("\nPhone: ", normal);
        Phrase phPhoneLabal = new Phrase(chunkPhoneLabal);
        Chunk chunkPhone = new Chunk(Phone, bold);
        Phrase phPhone = new Phrase(chunkPhone);
        Paragraph phN = new Paragraph();
        phN.add(phPatientLabal);
        phN.add(phPatient);
        phN.add(phEmailLabal);
        phN.add(phEmail);
        phN.add(phPhoneLabal);
        phN.add(phPhone);
        cell3.addElement(phN);
        table1.addCell(cell3);
        PdfPCell cell4 = new PdfPCell();
        cell4.getBorderWidthRight();
        cell4.setBorder(Rectangle.NO_BORDER);
        String ReferingPhysician = "phy Patient";
        Chunk chunkRefPhyLabal = new Chunk("Refering Physician: ", normal);
        Phrase phRefPhyLabal = new Phrase(chunkRefPhyLabal);
        Chunk chunkRefPhy = new Chunk(ReferingPhysician, bold);
        Phrase phRefPhy = new Phrase(chunkRefPhy);
        Paragraph phN1 = new Paragraph();
        phN1.add(phRefPhyLabal);
        phN1.setAlignment(com.itextpdf.text.Element.ALIGN_RIGHT);
        phN1.add(phRefPhy);
        cell4.addElement(phN1);
        table1.addCell(cell4);
        tabletmp1.addCell(table1);
        tabletmp1.setSpacingAfter(10);
        document.add(tabletmp1);
        PdfPTable table7 = new PdfPTable(1);
        table7.setWidthPercentage(100);
        PdfPCell c7 = new PdfPCell(new Phrase("Payment Summry", new Font(ff,
                size, Font.BOLD)));
        BaseColor headingColor7 = WebColors.getRGBColor("#989898");
        c7.setBackgroundColor(headingColor7);
        c7.setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_LEFT);
        table7.addCell(c7);
        table7.setSpacingAfter(2f);
        document.add(table7);
        // ////////////////////////////////////////////////////////
        PdfPTable tabletmp2 = new PdfPTable(1);
        BaseColor headingColor8 = WebColors.getRGBColor("#F0F0F0");
        tabletmp2.setWidthPercentage(100);
        PdfPTable table8 = new PdfPTable(2);
        table8.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        table8.getDefaultCell().setHorizontalAlignment(Element.ALIGN_JUSTIFIED);
        table8.getDefaultCell().setVerticalAlignment(Element.ALIGN_JUSTIFIED);
        float[] colWidths8 = { 50, 50 };
        table8.setWidths(colWidths8);
        PdfPCell cellPaymentSummry = new PdfPCell();
        cellPaymentSummry.setBackgroundColor(headingColor8);
        cellPaymentSummry.setBorder(Rectangle.NO_BORDER);

        Chunk chunkPaymentInvoiceLabal = new Chunk("Invoice Id", normal);
        Phrase phPaymentInvoiceLabal = new Phrase(chunkPaymentInvoiceLabal);

        Chunk chunkPaymentModeLabal = new Chunk("\nPayment Mode", normal);
        Phrase phPaymentModeLabal = new Phrase(chunkPaymentModeLabal);
        Chunk chunkAmountReceivedLabal = new Chunk("\nAmount Received", normal);
        Chunk chunkAmountDueLabal = new Chunk("\nAmount Due", normal);
        Phrase phAmountDueLabal = new Phrase(chunkAmountDueLabal);

        Phrase phAmountReceivedLabal = new Phrase(chunkAmountReceivedLabal);
        Paragraph phPaymentSummry = new Paragraph();
        phPaymentSummry.add(phPaymentInvoiceLabal);
        phPaymentSummry.add(phPaymentModeLabal);
        phPaymentSummry.add(phAmountReceivedLabal);
        phPaymentSummry.add(phAmountDueLabal);
        cellPaymentSummry.addElement(phPaymentSummry);
        table8.addCell(cellPaymentSummry);

        PdfPCell cellPaymentSummry1 = new PdfPCell();
        cellPaymentSummry1.setBackgroundColor(headingColor8);
        cellPaymentSummry1.getBorderWidthRight();
        cellPaymentSummry1.setBorder(Rectangle.NO_BORDER);
        String PaymentMode = "rannnnnnnnnnnnnnnnn";
        String amountReceived = "2.33";
        String amountDue = "28.00";
        String invoice = "123";

        Chunk chunkPaymentInvoicel = new Chunk(invoice, bold);
        Phrase phPaymentInvoice = new Phrase(chunkPaymentInvoicel);

        Chunk chunkPaymentMode = new Chunk("\n" + PaymentMode, bold);
        Phrase phPaymentMode = new Phrase(chunkPaymentMode);

        Chunk chunkAmountReceived = new Chunk("\n$" + amountReceived, bold);
        Phrase phAmountReceived = new Phrase(chunkAmountReceived);

        Chunk chunkAmountDue = new Chunk("\n$" + amountDue, bold);
        Phrase phAmountDue = new Phrase(chunkAmountDue);

        Paragraph phPaymentSummry1 = new Paragraph();
        phPaymentSummry1.add(phPaymentInvoice);
        phPaymentSummry1.add(phPaymentMode);
        phPaymentSummry1.setAlignment(com.itextpdf.text.Element.ALIGN_RIGHT);

        phPaymentSummry1.add(phAmountReceived);

        phPaymentSummry1.add(phAmountDue);
        cellPaymentSummry1.addElement(phPaymentSummry1);
        table8.addCell(cellPaymentSummry1);
        tabletmp2.addCell(table8);
        document.add(tabletmp2);
        document.close();

    }

    /**
     * Main method.
     * 
     * @param args
     *            no arguments needed
     * @throws DocumentException
     * @throws IOException
     */
    public static void main(String[] args) throws Exception {
        new GraphicsStateOperators().createPdf(RESULT);
        System.out.println("Done Please check........");
    }
}
like image 34
amit Avatar answered Nov 15 '22 14:11

amit


bajrangi,

You might want to use ColumnText.showTextAligned() to align your text. Try the following:

Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
PdfContentByte canvas = writer.getDirectContent();
ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, phrase, xPosition, yPosition, 0);

Cheers!

-Eric

like image 22
eric.soto Avatar answered Nov 15 '22 12:11

eric.soto