Reputation: 12642
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.
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
Reputation: 77528
I voted the question down because of two reasons:
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