package com.gb.samples;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
public class Sample {
public static void main(String[] args) {
try {
String inFile = "C:/Users/vgrandhi/Desktop/sam/bill.pdf";
System.out.println ("Reading " + inFile);
PdfReader reader = new PdfReader(inFile);
Rectangle cropBox = reader.getCropBox(1);
Rectangle psize = reader.getPageSize(1);
cropBox.setRight(psize.getWidth()/2);
System.out.println(psize.getWidth());
System.out.println(psize.getHeight());
int n = reader.getNumberOfPages();
System.out.println ("Number of pages : " + n);
int i = 0;
while ( i < n ) {
String outFile = inFile.substring(0, inFile.indexOf(".pdf"))
+ "-" + String.format("%03d", i + 1) + ".pdf";
System.out.println ("Writing " + outFile);
Document document = new Document(cropBox);
PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
writer.setCropBoxSize(cropBox);
document.open();
PdfImportedPage page = writer.getImportedPage(reader, ++i);
writer.addPage(page);
document.close();
writer.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
public class Sample {
public static void main(String[] args) {
try {
String inFile = "C:/Users/vgrandhi/Desktop/sam/bill.pdf";
System.out.println ("Reading " + inFile);
PdfReader reader = new PdfReader(inFile);
Rectangle cropBox = reader.getCropBox(1);
Rectangle psize = reader.getPageSize(1);
cropBox.setRight(psize.getWidth()/2);
System.out.println(psize.getWidth());
System.out.println(psize.getHeight());
int n = reader.getNumberOfPages();
System.out.println ("Number of pages : " + n);
int i = 0;
while ( i < n ) {
String outFile = inFile.substring(0, inFile.indexOf(".pdf"))
+ "-" + String.format("%03d", i + 1) + ".pdf";
System.out.println ("Writing " + outFile);
Document document = new Document(cropBox);
PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
writer.setCropBoxSize(cropBox);
document.open();
PdfImportedPage page = writer.getImportedPage(reader, ++i);
writer.addPage(page);
document.close();
writer.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
No comments:
Post a Comment