10

我有两个块,在短语中添加,然后在段落中。

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);

我必须将块设置divisiontitle为右对齐。在 iIext 中是否有任何规定可以做到这一点?

4

5 回答 5

8

您在评论中确实回答了自己。

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);

然后你可以将它添加到任何单元格中,它将正确对齐。

于 2014-12-12T13:25:11.893 回答
4

巴让吉,

您可能想要使用 ColumnText.showTextAligned() 来对齐您的文本。尝试以下操作:

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);

干杯!

-埃里克

于 2014-03-08T02:40:35.943 回答
3
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 = "test@test.com";
        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........");
    }
}
于 2013-09-02T06:44:50.780 回答
1

通过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");

测试为在 iTextSharp.LGPLv2.Core 1.4.5 (iText 4) 下工作

于 2020-09-29T00:02:41.637 回答
0
@WebServlet(name = "Servlet")

public class PDFServlet extends HttpServlet {

    private String RESOURCE = "images/logo-new.png";


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            response.setContentType("application/pdf");
            Document document = new Document(PageSize.LETTER);
            PdfWriter writer = null;

            try {
                writer = PdfWriter.getInstance(document, response.getOutputStream());
                document.open();

                document.add(createHeaderTable());
                document.add(comapnyNameTable());
                document.add(createAddressTable());
                document.add(getTable());
                document.add(createFooterTable());

                PdfContentByte canvas = writer.getDirectContent();
                canvas.setLineWidth((float) 10 / 10);
                canvas.moveTo(40,  765 - (5 * 10));
                canvas.lineTo(570, 770 - (5 * 10));
                canvas.stroke();

                document.close();
            } catch (DocumentException e) {
                e.printStackTrace();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private PdfPTable createHeaderTable(){
        Font bold = new Font(Font.FontFamily.HELVETICA, 8f, Font.BOLD);
        Font normal = new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL);
        PdfPTable table = new PdfPTable(2);

        try{
           table.setWidthPercentage(100f);
           table.setHorizontalAlignment(0);;
           table.setSpacingBefore(10);

           table.setWidths(new int[]{2, 6});
           Image img = Image.getInstance(RESOURCE);
           img.scalePercent(28f);

           PdfPCell pdfPCell = new PdfPCell(img,true);
           //pdfPCell.addElement(img);
           pdfPCell.setBorder(Rectangle.NO_BORDER);
           pdfPCell.setFixedHeight(50);
           table.addCell(pdfPCell);

            Paragraph paragraph = new Paragraph(10);
            paragraph.add(new Chunk("Date:",bold));
            paragraph.add(new Chunk("11-09-2015",normal));
            paragraph.add(new Chunk("\nOrder Id:",bold));
            paragraph.add(new Chunk("OrderId#12345",normal));
            paragraph.setIndentationLeft(260);
            table.addCell(newPdfPcel(paragraph,50));
        }
        catch (DocumentException e){
           e.printStackTrace();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return table;
    }

    private PdfPTable createAddressTable(){
        PdfPTable table = new PdfPTable(2);
        Font bold = new Font(Font.FontFamily.HELVETICA, 8f, Font.BOLD);
        Font normal = new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL);

        try{
            table.setWidthPercentage(100f);
            table.setHorizontalAlignment(0);;
            table.setSpacingBefore(10);
            table.setSpacingAfter(10);

            table.setWidths(new int[]{2, 6});

            Paragraph paragraph = new Paragraph(10);
            paragraph.add(new Chunk("Billed To",bold));
            paragraph.add(new Chunk("\nOrdered By tdhdghfjghjg fvgfdghgfhg gfhggjdjh ghgfhgjhgjhg gfhghjghj dghgfh ghggfhgfh ghgffgh ghjggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg", normal));
            paragraph.setIndentationRight(40);
            table.addCell(newPdfPcel(paragraph,60));

            Paragraph paragraph2 = new Paragraph(10);
            paragraph2.add(new Chunk("Service By ",bold));
            paragraph2.add(new Chunk("\nService B gfdsgfdhghhhhy  tdhdghfjghjg fvgfdghgfhg gfhggjdjh ghgfhgjhgjhg gfhghjghj dghgfhh ghfddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd",normal));
            paragraph2.setIndentationLeft(260);
            table.addCell(newPdfPcel(paragraph2,60));

            Paragraph paragraph3 = new Paragraph(10);
            paragraph3.add(new Chunk("Payment Method:", bold));
            paragraph3.add(new Chunk("\nVisa ending with *888888888888888888888888888888888888888888888**********123,\nseeenu@gmail.com",normal));
            table.addCell(newPdfPcel(paragraph3,60));

            Paragraph paragraph4 = new Paragraph(10);
            paragraph4.add(new Chunk("Order Date:",bold));
            paragraph4.add(new Chunk("\nMarch 7,2014",normal));
            paragraph4.setIndentationLeft(260);
            table.addCell(newPdfPcel(paragraph4,60));
        }
        catch (DocumentException e){
            e.printStackTrace();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return table;
    }

    private PdfPTable comapnyNameTable(){
        PdfPTable table = new PdfPTable(1);
        Font bold = new Font(Font.FontFamily.HELVETICA, 8f, Font.BOLD);
        Font normal = new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL);

        try{
            table.setWidthPercentage(100f);

            Paragraph paragraph = new Paragraph(10);
            paragraph.add(new Chunk("Travels Company",bold));
            paragraph.add(new Chunk("\nOrdered By tdhdghfjghjg fvgfdghgfhg gfhggjdjh ghgfhgjhgjhg gfhghjghj dghgfh ghggfhgfh ghgffgh ghjggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg",normal));
            paragraph.setIndentationLeft(200f);
            paragraph.setIndentationRight(200f);
            table.addCell(newPdfPcel(paragraph,60));
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return table;
    }

    private PdfPTable createFooterTable(){
        PdfPTable table = new PdfPTable(2);
        Font bold = new Font(Font.FontFamily.HELVETICA, 8f, Font.BOLD);
        Font normal = new Font(Font.FontFamily.HELVETICA, 8f, Font.NORMAL);

        try{
            table.setWidthPercentage(100f);
            table.setHorizontalAlignment(0);;
            table.setSpacingBefore(10);
            table.setSpacingAfter(10);

            table.setWidths(new int[]{2, 6});
            Paragraph paragraph = new Paragraph(10);
            paragraph.add(new Chunk("Tearms & Conditions",bold));
            paragraph.add(new Chunk("\ngfdhgfhgfh",normal));

            Paragraph paragraph2 = new Paragraph(10);
            paragraph2.add(new Chunk("Aggrigate Service By",bold));
            paragraph2.add(new Chunk("\nTruckWay",normal));
            paragraph2.setIndentationLeft(260);
            table.addCell(newPdfPcel(paragraph2,0));
        }
        catch (DocumentException e){
            e.printStackTrace();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return table;
    }

    private Paragraph createParagraph(String text,Font font,float indentationLeft,float indentationRight){
        Paragraph paragraph = new Paragraph(10);
        paragraph.add(new Chunk(text,font));
        paragraph.setIndentationLeft(indentationLeft);
        paragraph.setIndentationRight(indentationRight);
        return paragraph;
    }

    private PdfPCell newPdfPcel(Paragraph paragraph,float fixedHight){
        PdfPCell pdfPCell = new PdfPCell();
        pdfPCell.addElement(paragraph);
        pdfPCell.setBorder(Rectangle.NO_BORDER);
        pdfPCell.setFixedHeight(fixedHight);
        return pdfPCell;
    }

    public PdfPTable getTable()throws DocumentException, IOException {
        // Create a table with 7 columns
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(100f);

        // Add the first header row
        PdfPCell cell = new PdfPCell(new Phrase("Order Details", FontFactory.getFont("Arial",10,Font.BOLD,BaseColor.BLACK)));
        cell.setBackgroundColor(BaseColor.CYAN);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setColspan(7);
        table.addCell(cell);
        Font font = new Font();
        font.setSize(10);
        font.setFamily("Arial");

        table.addCell(createCel("From Place",font,Element.ALIGN_LEFT));
        table.addCell(createCel("Hyderabad",font,Element.ALIGN_LEFT));
        table.addCell(createCel("To Place",font,Element.ALIGN_LEFT));
        table.addCell(createCel("Bangolore",font,Element.ALIGN_LEFT));
        table.addCell(createCel("Truck RequestDate",font,Element.ALIGN_LEFT));
        table.addCell(createCel("09/9/15",font,Element.ALIGN_LEFT));
        table.addCell(createCel("Truck Type",font,Element.ALIGN_LEFT));
        table.addCell(createCel("6 wheeler",font,Element.ALIGN_LEFT));
        table.addCell(createCel("No OfTrucks",font,Element.ALIGN_LEFT));
        table.addCell(createCel("1",font,Element.ALIGN_LEFT));
        table.addCell(createCel("Total Capacity",font,Element.ALIGN_LEFT));
        table.addCell(createCel("8",font,Element.ALIGN_LEFT));
        table.addCell(createCel("Total Amount",font,Element.ALIGN_LEFT));
        table.addCell(createCel("1000.00",font,Element.ALIGN_LEFT));
        table.addCell(createCel("From Addressy",font,Element.ALIGN_LEFT));
        table.addCell(createCel("hyd",font,Element.ALIGN_LEFT));
        table.addCell(createCel("To Address",font,Element.ALIGN_LEFT));
        table.addCell(createCel("Bang",font,Element.ALIGN_LEFT));
        table.addCell(createCel("Advance",font,Element.ALIGN_RIGHT));
        table.addCell(createCel("1000.00",font,Element.ALIGN_RIGHT));
        table.addCell(createCel("Service Charges",font,Element.ALIGN_RIGHT));
        table.addCell(createCel("25.00",font,Element.ALIGN_RIGHT));
        table.addCell(createCel("Total",font,Element.ALIGN_RIGHT));
        table.addCell(createCel("1000.25",font,Element.ALIGN_RIGHT));


        return table;
    }

    private PdfPCell createCel(String text,Font font,int alignment){
        PdfPCell pdfPCel = new PdfPCell(new Phrase(text,font));
        pdfPCel.setHorizontalAlignment(alignment);
        return pdfPCel;
    }
}
于 2015-09-11T11:02:54.800 回答