看來,你正在期待平鋪 PDF。您不會告訴我們您計劃使用哪種版本的iText。由於您似乎對此很陌生,因此我假設您使用的是iText 7.在這種情況下,請查閱iText 7 Jump-Start tutorial的Chapter 6,我們可以將帶有單頁的PDF格式化爲包含四頁的PDF格式。
原文PDF:
瓷磚PDF:
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfDocument sourcePdf = new PdfDocument(new PdfReader(src));
PdfPage origPage = sourcePdf.getPage(1);
PdfFormXObject pageCopy = origPage.copyAsFormXObject(pdf);
Rectangle orig = origPage.getPageSize();
//Tile size
Rectangle tileSize = PageSize.A4.rotate();
AffineTransform transformationMatrix = AffineTransform.getScaleInstance(
tileSize.getWidth()/orig.getWidth() * 2f,
tileSize.getHeight()/orig.getHeight() * 2f);
//The first tile
PdfPage page = pdf.addNewPage(PageSize.A4.rotate());
PdfCanvas canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, 0, -orig.getHeight()/2f);
//The second tile
page = pdf.addNewPage(PageSize.A4.rotate());
canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, -orig.getWidth()/2f, -orig.getHeight()/2f);
//The third tile
page = pdf.addNewPage(PageSize.A4.rotate());
canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, 0, 0);
//The fourth tile
page = pdf.addNewPage(PageSize.A4.rotate());
canvas = new PdfCanvas(page);
canvas.concatMatrix(transformationMatrix);
canvas.addXObject(pageCopy, -orig.getWidth()/2f, 0);
// closing the documents
pdf.close();
sourcePdf.close();
改變這個例子中爲代碼THA:
的代碼見TheGoldenGateBridge_Tiles t將帶有1頁的PDF分成2頁的PDF,這是使用一些簡單的數學的問題。
如果您使用的是iText 5,那麼以前的問題已經回答了很多次。例如見: