1

I fails to give an appropriate title to the questions. :)

Any how I need to split (get) a page from a existing PDF file. I am using droidtext for this.

My code is

  try {

        String path = Environment.getExternalStorageDirectory()
                + "/test123.pdf";
                    /*Read Existing PDF*/ 
        PdfReader reader = new PdfReader(path);

        Document doc = new Document();
        doc.open();

        File outfile = new File(Environment.getExternalStorageDirectory()
                + "/test_new.pdf");
        if (!outfile.exists())
            outfile.createNewFile();

        FileOutputStream decfos = new FileOutputStream(outfile);

        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, decfos);
        document.open();
        /*Getting First page*/  
        PdfImportedPage page = writer.getImportedPage(reader, 1);

        Image instance = Image.getInstance(page);

        document.add(instance);
        document.close();

    } catch (DocumentException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I want to create a one page pdf from "test123.pdf" file. It is creating new PDF.

But the problem is in new PDF file there are white borders. How can I remove these white spaces. In original PDF there is no such white borders.

enter image description here

EDIT I give another try with follwing code. But it gives null pointer exception at copy.addPage(page);

String path = Environment.getExternalStorageDirectory()
                + "/test123.pdf";
        PdfReader reader = new PdfReader(path);

        PdfImportedPage page;
        PdfSmartCopy.PageStamp stamp;
        File outfile = new File(Environment.getExternalStorageDirectory()
                + "/test_new.pdf");
        Document doc = new Document();
        if (!outfile.exists())
            outfile.createNewFile();

        FileOutputStream decfos = new FileOutputStream(outfile);
        PdfSmartCopy copy = new PdfSmartCopy(doc, decfos);
        page = copy.getImportedPage(reader, 5);

        stamp = copy.createPageStamp(page);

        stamp.alterContents();
        copy.addPage(page);
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53

1 Answers1

2

I voted the question down because of two reasons:

  1. You didn't read the official documentation. See Edit DirectContent of iTextSharp PdfSmartCopy class to find out why not reading the documentation is wrong.
  2. I'm the original developer of iText, and I DO NOT ENDORSE the use of DroidText. It's not an official iText release. I strongly discourage its use: http://lowagie.com/itext2 Note that I live in Belgium, and by Belgian law, I have the moral rights on all the copyright I produced. This includes iText 2.1.7.

As for your question: you're creating pages with format A4. To these pages you add imported pages with an unknown size. If those pages are of size A4 too, they will fit. If they have a different size, they won't. Either they'll be clipped, or they'll have unnecessary margins.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • about **1** `I am looking in droidtext code to find the answer. I will read the chapter, you mentioned and try the solution `. **2** `I will keep in mind this.` – Mohsin Naeem Oct 12 '12 at 04:20
  • 1
    You have two better options to split PDFs: using PdfCopy (in combination with PdfReader.selectPages() or selecting the pages yourself) and using PdfStamper and PdfReader.selectPages(). The former is better if you need different PDFs as a result of the split action; I prefer the latter if I need only one PDF: a selection of pages of the original document. – Bruno Lowagie Oct 12 '12 at 05:49
  • I really appreciate your help. I will defiantly move to `iText`. I told my client to buy the license. But for now I need to do with droidtext to show something. I really fell myself dumb at the moment. Can you please help me with some code? I try with other code but failed to do so. See my edits in question. – Mohsin Naeem Oct 12 '12 at 07:05
  • Why are you creating a PageStamp? You aren't doing anything with it. Maybe that's the problem. – Bruno Lowagie Oct 13 '12 at 07:21