This question already has an answer here:
-
itext Split PDF Vertically
1 answer
I would like to take a single-page PDF, and than split it in two parts (cutting that page in the middle), without considering the text on that page. I'm using iText, but I don't find any examples on how to do this.
You cannot really split a page, it would be a quite difficult task, what you can do is to clone content of a page inside a new one with half its original size, and repeat for the second page applying a translation to the content.
I show an example with PDFBox , I'm using it lately and I had a sandbox project ready to do the test, surely you can do the same with iText.
package printit;
import java.io.File;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
public class CutIt {
public static void main(String[] args) throws IOException {
PDDocument outdoc = new PDDocument();
PDDocument doc = PDDocument.load(new File("sample_1.pdf"));
PDPage page = (PDPage) doc.getDocumentCatalog().getPages().get(0);
PDRectangle cropBox = page.getCropBox();
float upperRightY = cropBox.getUpperRightY();
float lowerLeftY = cropBox.getLowerLeftY();
cropBox.setLowerLeftY(upperRightY/2);
page.setCropBox(cropBox);
outdoc.importPage(page);
cropBox = page.getCropBox();
cropBox.setUpperRightY(upperRightY/2);
cropBox.setLowerLeftY(lowerLeftY);
page.setCropBox(cropBox);
outdoc.importPage(page);
outdoc.save("cut.pdf");
outdoc.close();
doc.close();
}
}