Mohsin Naeem
Mohsin Naeem

Reputation: 12642

Generate a new PDF using a PDF file result in white places

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);

Upvotes: 1

Views: 878

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

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.

Upvotes: 2

Related Questions