3

I am generating pdf in android using itext library.Format of pdf is shown in image enter image description here Approach i am using to generate this format is using Pdfptable.

The description tag is dynamic means it can be of multiple lines. when description tag is of 4-5 line every thing is perfect but if it is of half page the PDF doesn't look nice because of all the unnecessary white space; see 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();
                }

            }
        `

After introducing setSplitLate(), my PDF looks like this sample_new.pdf and that's not satisfactory either.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Sourabh soni
  • 488
  • 7
  • 11
  • Define distorted. Do you have a screen shot of such a distorted image? The screen shot that is currently visible in your question looks fine to me (unless I'm overlooking something). – Bruno Lowagie Sep 04 '15 at 06:35
  • yes download pdf from here https://www.dropbox.com/s/sctfw97uyqpe2eg/sample.pdf?dl=0 – Sourabh soni Sep 04 '15 at 06:40
  • Thanks, now I can answer the question. Just a minute... – Bruno Lowagie Sep 04 '15 at 06:46
  • well sir i made pdf satisfactory but still at some place each list item is not formatted at some places .You can download new pdf i have generated https://www.dropbox.com/s/oi66h04airv7l4t/sample_new.pdf?dl=0. Please provide some hint. – Sourabh soni Sep 04 '15 at 06:51
  • I have looked at your PDF and I've discovered that you are using an iText version that dates from July 2009. Many iText versions have been released since that date. iText 2.1.7 has been declared dead and buried a long time ago. It is no longer supported. You should upgrade to a more recent version, and as you're working on Android, you should use the Android port of iText called [iTextG](http://itextpdf.com/product/itextg) – Bruno Lowagie Sep 04 '15 at 06:59
  • Sure ..thanks for the right information – Sourabh soni Sep 04 '15 at 07:25

2 Answers2

2

By default, table rows aren't split. iText will try to add a complete row to the current page, and if the row doesn't fit, it will try again on the next page. Only if it doesn't fit on the next page, it will split the row. This is the default behavior, so you shouldn't be surprised by what you see in your application.

You can change this default behavior. There's a method that will allow you to drop content that doesn't match (this is not what you want) and there's a method that will allow you to split rows when they don't fit the current page (this is what you want).

The method you need is setSplitLate():

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

By default, the value of setSplitLate() is true: iText will split rows as late as possible, which leads to all the white space you see in your document. By changing this default to false, iText will split rows immediately.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • You are using iText 2.1.7. As explained many times, that version of iText shouldn't be used because of *technical* and legal issues. You are experiencing one of the technical issues with the version that was released in July 2009. Please upgrade to a more recent version. – Bruno Lowagie Sep 04 '15 at 06:58
0

Your table content is distorted when the page in your document is changing. To avoid this you need to set header and footer to your pages and skip the content and set it to new page if its divided between two pages. Here is an example here

To set header and footer here

Community
  • 1
  • 1
Bhushan
  • 424
  • 5
  • 15