2014-09-11 2 views
2

안녕하세요, 회사 로고와 주소가 나란히 표시되는 송장을 만들려고합니다. 회사 로고가 왼쪽과 텍스트 아래에 표시됩니다. 나는 로고 옆에있는 텍스트를 아주 오른쪽으로 보려고했으나 오지 않았다. 도와주세요.이미지와 텍스트를 나란히 표시하는 방법 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 페이지를 만들고 그 후에 pdf에 html를 변환하는 것을 시도하십시오, 그것의 일 –

답변

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 매개 변수는 이미지의 크기를 조정합니다. 첫 번째 열이 두 번째 열의 너비의 절반을 차지하도록 정의 했으므로 이미지의 너비는 전체 표의 너비의 1/3을 차지합니다.

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

안녕 Bruno Lowagie 위 post.It를 위해 순전히 고맙다 당신은 지금 일하고있다. –

+0

이제 답을 수락하면됩니다. –

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; 


} 
관련 문제