2016-08-10 15 views
0

私はitextを使って固定幅のpdfを作成しようとしています。 pdfの幅は85.0fに固定されています。pdfには 'border'という幅の境界があります これに対して、幅85.0f-(border * 2)の表を作成し、データを追加します。 (border * 2) しかし、私が得るpdfは、境界とテーブルの間に細い間隔を持っています。 そのスペースを削除したいと思います。 私は既にゼロなどにパディングを設定しようとしました。しかし、その薄い間隔がまだあります。 私のコードは次のとおりです。固定幅の後にitextテーブルが正確にフィットしない

public class Template2100_2 { 

    public static final String DEST = "D:\\uploads\\abc.pdf"; 
    public static void main(String[] args) throws DocumentException, IOException { 
     Template2100_2 t=new Template2100_2(); 
     Font font = new Font(Font.FontFamily.TIMES_ROMAN, 7f, Font.NORMAL); 

     t.createPdfOrdinary("The Commerce Ministry has received several references from various stakeholders seeking clarification", "E-mail ID\n" 
       + " 1854213265\n" 
       + " Ph: 12547869",1,font); 
    } 
    public void createPdfOrdinary(String chunk1Text, String chunk2Text,Integer border,Font font) throws DocumentException, IOException 
    { 


     Document document = new Document(PageSize.A4, 0, 0, 0, 0); 
     PdfPTable table = new PdfPTable(1); 
     table.setTotalWidth(85f-(border*2)); 
     table.setLockedWidth(true); 
     //table.setWidthPercentage(100); 

     table.setSpacingBefore(0f); 
     table.setSpacingAfter(0f); 


     PdfPCell cell; 
     cell = new PdfPCell(new Phrase(chunk1Text)); 
     cell.setBorder(0); 
     cell.setPadding(0); 
     cell.setColspan(1); 
     cell.isUseBorderPadding(); 
     cell.setPaddingBottom(1f); 
     cell.setPaddingRight(-1f); 
     cell.setBackgroundColor(BaseColor.PINK); 
     cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
     table.addCell(cell); 

     cell = new PdfPCell(new Phrase(chunk2Text)); 
     cell.setBorder(0); 
     cell.setPadding(0); 
     cell.setPaddingBottom(1f); 
     cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
     cell.setRowspan(1); 
     cell.setBackgroundColor(BaseColor.PINK); 
     cell.isUseBorderPadding(); 



     table.addCell(cell); 
     FileOutputStream fos=new FileOutputStream(DEST); 
     PdfWriter writer = PdfWriter.getInstance(document, fos); 
     document.open(); 
     document.add(table); 
     Float height=table.getTotalHeight(); 
     System.out.println(table.getTotalHeight()+"::"+table.getTotalWidth()); 
     document.close(); 
     fos.close(); 
     writer.close(); 
     Rectangle pagesize = new Rectangle(85f, height+(border*2)); 
     pagesize.setBorder(Rectangle.BOX); 
     pagesize.setBorderWidth(border); 
     document = new Document(pagesize, border, border,border, border); 
     fos=new FileOutputStream(DEST); 
     writer = PdfWriter.getInstance(document, fos); 
     document.open(); 
     document.add(table); 

     document.close(); 
     fos.close(); 
     writer.close(); 
    } 
} 

は、誰もが国境とテーブルの間に薄い間隔がどこから来ているお勧めできますし、どのように私はit.Thanksを削除することができます。

+0

サンプルにコードが多すぎます。あなたの質問に関係のない部分を削除してください。あなたの質問に答えたい人は、関係のないものから関連性のあるものを解き放つために、あまりにも多くの時間を尋ねていると決めるかもしれません。 –

答えて

-1

今、私が使用してその間隔を削除することができています:

pagesize.setUseVariableBorders(true); 
+2

これは答えではありませんか?あなたの元の質問に対する追加のメモです。そのようなメモをコメントとして追加するか、質問を更新する必要があります。 –

1

あなたのコードは複雑すぎます。あなたがそれを書いた後にコードを維持しなければならない人々は、それを好まないでしょう。

これはあなたが望む結果である場合:

enter image description here

次にこれはあなたが必要とするコードです:

public void createPdf(String dest) throws IOException, DocumentException { 
    float width = 85; 
    float border = 1; 
    PdfPTable table = new PdfPTable(1); 
    table.setTotalWidth(width - 2 * border); 
    table.setLockedWidth(true); 
    table.addCell("Some text in some cell."); 
    table.addCell("In some very narrow table"); 
    Rectangle rect = new Rectangle(width, table.getTotalHeight() + 2 * border); 
    Document document = new Document(rect, border, border, border, border); 
    PdfWriter.getInstance(document, new FileOutputStream(dest)); 
    document.open(); 
    document.add(table); 
    document.close(); 
} 

二度同じ文書を作成する必要はありません。最初から正しいディメンションを使用してドキュメントを作成するだけです。

関連する問題