2015-09-04 28 views
3

我生成pdf利用iText动态内容生成pdftable在图像显示 enter image description here 方法我使用产生这种格式是使用Pdfptable在机器人使用<code>itext</code> library.Format PDF的在机器人

描述标签是动态的,意味着它可以是多行。当描述标签为4-5行时,每件事物都是完美的,但如果它是半页的,则由于所有不必要的空白空间,PDF看起来不太好;见sample.pdf

// TODO Auto-generated method stub 

      Document document = new Document(PageSize.A4); 
      File f = null; 
      try { 

       f = createFile("sample.pdf"); 

       FileOutputStream ficheroPdf = new FileOutputStream(
         f.getAbsolutePath()); 

       PdfWriter writer = PdfWriter.getInstance(document, ficheroPdf); 


       document.open(); 



       /*LineSeparator line = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2); 
       paragraph.add(line);*/ 


       ArrayList<SampleModel> movies = new ArrayList<SampleModel>(15); 
       for (int i = 0; i < 13; i++) { 
        movies.add(new SampleModel()); 
       } 
       for (int j = 0; j < movies.size(); j++) { 

        Bitmap bitmap = BitmapFactory.decodeResource(mActivity.getResources(), 
          movies.get(j).getDrawableId()); 

        ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
        Image img = Image.getInstance(stream.toByteArray()); 
        img.setAlignment(Image.LEFT | Image.TEXTWRAP); 
        img.setBorder(Image.BOX); 
        img.setBorderWidth(20); 
        img.setBorderColor(Color.WHITE); 
        img.scaleAbsolute(200f, 200f); 

        //table with 2 columns 
        PdfPTable table = new PdfPTable(2); 

        table.setHorizontalAlignment(Element.ALIGN_LEFT); 
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER); 


        PdfPCell cell = new PdfPCell(img); 
        cell.setVerticalAlignment(Element.ALIGN_TOP); 
        cell.setBorderColor(new Color(16777215)); 
        cell.setRowspan(5); 
        table.addCell(cell); 

        table.addCell(new Paragraph(" Snag Name")); 
        table.addCell(new Paragraph(" Date   : 25 Aug 2015")); 
        table.addCell(new Paragraph(" Sub Contractor : Test company 2")); 
        table.addCell(new Paragraph(" Status   : Completed")); 
        table.addCell(new Paragraph(" Description : Description Description Description Description Description Description Description Description Description Description Description Description " 
          + "Description" 
          + "Description" 
          + "Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description Description")); 

        document.add(table); 


        // add line seperator 
        document.add(Chunk.NEWLINE); 
        LineSeparator line = new LineSeparator(1, 100, null, Element.ALIGN_CENTER, -2); 
        document.add(line); 
        document.add(Chunk.NEWLINE); 

       } 


       Toast.makeText(mActivity, "Pdf generated", Toast.LENGTH_SHORT).show(); 

      } catch (DocumentException e) { 

       Log.e("pdf error", e.getMessage()); 

      } catch (IOException e) { 

       Log.e("pdf error", e.getMessage()); 

      } finally { 


       document.close(); 
       try { 
        File file = f; 
        Intent intent = new Intent(Intent.ACTION_VIEW); 
        intent.setDataAndType(Uri.fromFile(file), "application/pdf"); 
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
        startActivity(intent); 

       } 
       catch (ActivityNotFoundException e){ 
        Toast.makeText(mActivity, "No app found to open pdf", Toast.LENGTH_SHORT).show(); 
       } 
       catch (Exception e) { 
        // TODO: handle exception 

        Toast.makeText(mActivity, "Exception", Toast.LENGTH_SHORT).show(); 
       } 

      } 
     ` 

引入setSplitLate()后,我的PDF看起来像这样sample_new.pdf和,要么是不能令人满意的。

+0

定义失真。你有这种扭曲图像的屏幕截图吗?在你的问题中目前可见的屏幕截图对我来说看起来很好(除非我忽略了某些内容)。 –

+0

是从这里下载pdf https://www.dropbox.com/s/sctfw97uyqpe2eg/sample.pdf?dl=0 –

+0

谢谢,现在我可以回答这个问题。只需一分钟... –

回答

2

默认情况下,表格行不会被分割。 iText会尝试添加一个完整的行到当前页面,如果该行不合适,它会在下一页再次尝试。只有当它不适合下一页时,它才会拆分该行。这是默认行为,所以您不应该对您在应用程序中看到的内容感到惊讶。

您可以更改此默认行为。有一种方法可以让你删除不匹配的内容(这是你想要的而不是),并且有一种方法可以让你在不符合当前页面时拆分行(这就是你想)。

你需要的方法是setSplitLate()

PdfPTable table = new PdfPTable(2); 
table.setSplitLate(false); 

缺省情况下,setSplitLate()true:iText的会尽可能晚地拆分行,这会导致所有您在文档中看到白色的空间。通过将此默认值更改为false,iText将立即拆分行。

+0

您正在使用iText 2.1.7。正如多次解释的那样,iText的版本不应该因*技术*和法律问题而被使用。您遇到了2009年7月发布的版本中的技术问题之一。请升级到更新的版本。 –

0

当文档中的页面发生变化时,表格内容会失真。 为了避免这种情况,您需要为页面设置页眉和页脚,并跳过内容并将其设置为新页面(如果页面分为两页)。 下面是一个例子here

要设置页眉和页脚here