2014-09-11 100 views
2

嗨我正在嘗試創建發票,其中公司徽標和地址彼此相鄰顯示。公司徽標顯示在左側,文本位於其下方。我試圖在FAR RIGHT旁邊顯示徽標旁邊的文字,但沒有出現。請幫助。如何在彼此旁邊顯示圖像和文字Itext

public class Test { 
    /** Path to the resulting PDF */ 
    public static final String RESULT = "C:/ex/test.pdf"; 

    /** 
    * Main method. 
    * @param args no arguments needed 
    * @throws DocumentException 
    * @throws IOException 
    */ 
    public static void main(String[] args) throws IOException, DocumentException { 
     new ComCrunchifyTutorials().createPdf(RESULT); 
    } 

    /** 
    * Creates a PDF with information about the movies 
    * @param filename the name of the PDF file that will be created. 
    * @throws DocumentException 
    * @throws IOException 
    */ 
    public void createPdf(String filename) 
     throws IOException, DocumentException { 

     Document document = new Document(); 

     PdfWriter.getInstance(document, new FileOutputStream(filename)); 

     document.open(); 

     Image image = Image.getInstance("C:/Users/user/Desktop/New folder (3)/shoes/shoes/web/images/abc.jpg"); 
                 image.scaleAbsolute(150f, 50f);//image width,height 


Paragraph p = new Paragraph(); 
Phrase pp = new Phrase(200); 
p.add(new Chunk(image, 0, 0)); 
pp.add(" a text after the image."); 

p.add(pp); 
document.add(p); 

     document.close(); 
    } 

} 
+0

你試過把html轉換成pdf嗎?只是使您的圖像和發票的HTML頁面,然後嘗試將HTML轉換爲PDF,這是行之有效的 – 2014-09-11 10:11:41

回答

7

人們通常通過使用PdfPTable來實現此目的。例如,參見ImageNextToText例如:

enter image description here

這是一個有兩列創建PDF文件和表的代碼:

public void createPdf(String dest) throws IOException, DocumentException { 
    Document document = new Document(); 
    PdfWriter.getInstance(document, new FileOutputStream(dest)); 
    document.open(); 
    PdfPTable table = new PdfPTable(2); 
    table.setWidthPercentage(100); 
    table.setWidths(new int[]{1, 2}); 
    table.addCell(createImageCell(IMG1)); 
    table.addCell(createTextCell("This picture was taken at Java One.\nIt shows the iText crew at Java One in 2013.")); 
    document.add(table); 
    document.close(); 
} 

這是創建與圖像單元格中的代碼。請注意,true參數將導致圖像縮放。由於我們已經定義了第一列佔第二列寬度的一半,因此圖像的寬度將佔整個表格寬度的三分之一。

public static PdfPCell createImageCell(String path) throws DocumentException, IOException { 
    Image img = Image.getInstance(path); 
    PdfPCell cell = new PdfPCell(img, true); 
    return cell; 
} 

這是用文本創建單元格的一種方法。有許多不同的方法來達到相同的結果。只是試驗文本模式複合模式

public static PdfPCell createTextCell(String text) throws DocumentException, IOException { 
    PdfPCell cell = new PdfPCell(); 
    Paragraph p = new Paragraph(text); 
    p.setAlignment(Element.ALIGN_RIGHT); 
    cell.addElement(p); 
    cell.setVerticalAlignment(Element.ALIGN_BOTTOM); 
    cell.setBorder(Rectangle.NO_BORDER); 
    return cell; 
} 
+0

你好布魯諾Lowagie非常感謝你的上述post.It現在終於工作。 – 2014-09-12 15:28:07

+0

現在你所要做的就是接受答案;-) – 2014-09-12 15:36:55

0

在我的情況下,我在使用現有的功能,並不得不在一個單元格中這樣做。所以這裏是我所做的

public PdfPCell buildDesiredCell() throws Exception { 

PdfPCell cell = new PdfPCell(); 

PdfPTable headerTable = new PdfPTable(2); 
headerTable.setWidthPercentage(100f); 

PdfPCell leftCell = new PdfPCell(); 
String imagePath; 
imagePath = D:\....;//specify any path 

Image img = Image.getInstance(imagePath); 

img.scaleAbsoluteHeight(120f); 
img.scaleAbsoluteWidth(120f); 
img.setAlignment(Element.ALIGN_LEFT); 
leftCell.addElement(img); 
leftCell.setBorder(Rectangle.NO_BORDER); 

headerTable.addCell(leftCell); 

PdfPCell rightCell = new PdfPCell(); 
Paragraph addText= new Paragraph(" This is required text", smallStandardFont); 
rightCell .addElement(addText); 

rightCell .setBorder(Rectangle.NO_BORDER); 
rightCell .setPaddingLeft(5); 
headerTable.addCell(rightCell); 

cell.setBorder(Rectangle.NO_BORDER); 
cell.addElement(headerTable); 

return cell; 


} 
相關問題