2016-08-29 127 views
0

放置在小型頁面文字和圖像,我需要做一個PDF頁面看起來是這樣的:如何利用iText

Desired output

我有問題,做出適合在兩列小頁面。

這是我的代碼:爲您解決問題,是創建與AcroForm領域模板PDF

public void createSizedPdf(String dest) throws IOException, DocumentException { 

     Document document = new Document(); 
     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));   
     document.setMargins(5,5,5,5); 
     Rectangle one = new Rectangle(290,100); 
     one.setBackgroundColor(Color.YELLOW);   
     document.setPageSize(one); 
     document.open(); 

     Paragraph consigneeName = new Paragraph("Ahmed"); 
     Paragraph address = new Paragraph("Casa ST 121"); 
     String codeBL = "14785236987541"; 

     PdfContentByte cb = writer.getDirectContent(); 

     Barcode128 code128 = new Barcode128(); 
     code128.setBaseline(9); 
     code128.setSize(9); 
     code128.setCode(codeBL); 
     code128.setCodeType(Barcode128.CODE128); 
     Image code128Image = code128.createImageWithBarcode(cb, null, null); 
     Paragraph right = new Paragraph(); 
     right.add(consigneeName); 
     right.add(address); 
     right.add(code128Image); 

     Chunk glue = new Chunk(new VerticalPositionMark()); 
     Paragraph p = new Paragraph(); 
     p.add(right); 
     p.add(new Chunk(glue)); 
     p.add(code128Image); 

     document.add(p); 
     document.close(); 
    } 
+1

您使用的是iText 5還是iText 7?你有什麼嘗試?你問的東西*非常微不足道,所以我們很難理解你的問題:你無法縮放圖像嗎?你無法定義更小的字體大小嗎?你無法創建具有特定維度的表格嗎?你說*我有問題需要製作適合小尺寸頁面的兩列*但你忘了描述問題。請改善你的問題。 –

+0

我剛剛編輯了一些代碼片段的問題,因爲我使用iText5的iText版本,再加上我想要做的事情的鏈接 – ahmed

+0

是否有一個特定的原因,您爲什麼不想使用AcroForm模板做這種工作?這不會更優雅和靈活嗎? –

回答

0

的一種方式。您可以手動創建一個漂亮的設計,然後通過將數據(文本,條形碼)放置在由充當佔位符的字段定義的適當位置,以編程方式填寫表單。

另一種方法是從頭創建PDF,這是您似乎採取的方法,查看您的代碼。

從分享代碼的意義上說,你的問題並不完全清楚,但是你沒有解釋你遇到的問題。正如我已經評論:

你無法縮放圖像?你無法定義更小的 字體大小嗎?你無法創建具有特定維度的表格嗎? 你說我有問題需要製作兩列,可以放在一個小尺寸的頁面上,但是你忘了描述問題。

你沒有給出這些問題的答案,這使得很難有人回答你的問題。 Stack Overflow讀者可以做的唯一事情就是在你的位置完成你的工作。這不是Stack Overflow的原因。

此外,您的問題的答案是如此的微不足道,以至於Stack Overflow讀者很難理解您發佈問題的原因。

你說你需要在兩列中添加數據(文本和條形碼),你實際上是在說你想創建一個表格。這是這樣一個表的示例:

enter image description here

如果你看SmallTable例如,你可以看到它是如何構建的。

您需要一個以100個用戶單位衡量290的PDF,每邊有5個用戶單位的邊距。這意味着您可以使用280個乘90個用戶單位的表格。看看你的屏幕截圖,我想說,你有一列160個用戶單元和一列120個用戶單元。我還會說,你有三行,每行30個用戶單位。

好的,那你爲什麼不根據這些尺寸創建表格?

public void createPdf(String dest) throws IOException, DocumentException { 
    Rectangle small = new Rectangle(290,100); 
    Font smallfont = new Font(FontFamily.HELVETICA, 10); 
    Document document = new Document(small, 5, 5, 5, 5); 
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); 
    document.open(); 
    PdfPTable table = new PdfPTable(2); 
    table.setTotalWidth(new float[]{ 160, 120 }); 
    table.setLockedWidth(true); 
    PdfContentByte cb = writer.getDirectContent(); 
    // first row 
    PdfPCell cell = new PdfPCell(new Phrase("Some text here")); 
    cell.setFixedHeight(30); 
    cell.setBorder(Rectangle.NO_BORDER); 
    cell.setColspan(2); 
    table.addCell(cell); 
    // second row 
    cell = new PdfPCell(new Phrase("Some more text", smallfont)); 
    cell.setFixedHeight(30); 
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE); 
    cell.setBorder(Rectangle.NO_BORDER); 
    table.addCell(cell); 
    Barcode128 code128 = new Barcode128(); 
    code128.setCode("14785236987541"); 
    code128.setCodeType(Barcode128.CODE128); 
    Image code128Image = code128.createImageWithBarcode(cb, null, null); 
    cell = new PdfPCell(code128Image, true); 
    cell.setBorder(Rectangle.NO_BORDER); 
    cell.setFixedHeight(30); 
    table.addCell(cell); 
    // third row 
    table.addCell(cell); 
    cell = new PdfPCell(new Phrase("and something else here", smallfont)); 
    cell.setBorder(Rectangle.NO_BORDER); 
    cell.setHorizontalAlignment(Element.ALIGN_RIGHT); 
    table.addCell(cell); 
    document.add(table); 
    document.close(); 
} 

在這個例子中,

  • 您將學習如何更改單元格內容的字體,
  • 你學習如何改變水平和垂直對齊,
  • 您將學習如何按比例縮放條形碼使其適合單元格,
  • ...

所有這些功能都在官方文檔中解釋。正如我之前所說的:你沒有解釋你的問題的性質。什麼不清楚在你的文件?你的問題是什麼?

+0

感謝您的支持和詳細的答案,我很抱歉我的問題含糊不清,我想要做的是找出如何將段落,圖片條形碼等元素放置在PDF頁面中的絕對位置,當你設置一個JPanel圖層爲null並通過設置邊界來放置按鈕和標籤時​​,就像在java swing中一樣,你的回答很有幫助,再次感謝。 – ahmed