使用 iText 平铺并添加边距
Tiling with iText, and adding margins
我正在按照 Tiling Hero 示例 (http://itextpdf.com/examples/iia.php?id=116) 进行操作,但我希望能够为每个页面添加页边距。
请查看 TileClipped example. It is based on the TilingHero 示例,但它有一个转折点:
public void manipulatePdf(String src, String dest)
throws IOException, DocumentException {
float margin = 30;
// Creating a reader
PdfReader reader = new PdfReader(src);
Rectangle rect = reader.getPageSizeWithRotation(1);
Rectangle pagesize = new Rectangle(rect.getWidth() + margin * 2, rect.getHeight() + margin * 2);
// step 1
Document document = new Document(pagesize);
// step 2
PdfWriter writer
= PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte content = writer.getDirectContent();
PdfImportedPage page = writer.getImportedPage(reader, 1);
// adding the same page 16 times with a different offset
float x, y;
for (int i = 0; i < 16; i++) {
x = -rect.getWidth() * (i % 4) + margin;
y = rect.getHeight() * (i / 4 - 3) + margin;
content.rectangle(margin, margin, rect.getWidth(), rect.getHeight());
content.clip();
content.newPath();
content.addTemplate(page, 4, 0, 0, 4, x, y);
document.newPage();
}
// step 4
document.close();
reader.close();
}
你看到我们如何区分 rect
和 pagesize
了吗?我们将 rect
定义为原始页面的大小,并将 pagesize
定义为稍大的大小(取决于 margin
的值)。
我们在定义偏移量 x
和 y
时使用 rect,但我们添加 margin
以稍微更改该偏移量。我们更改了偏移量,因为我们剪辑了 pagesize
。裁剪是通过定义一个裁剪路径:
来完成的
content.rectangle(margin, margin, rect.getWidth(), rect.getHeight());
content.clip();
content.newPath();
这三行之后添加的所有内容都将被我们在rectangle()
方法中定义的矩形裁剪。如果您还想添加其他内容,您可能需要添加额外的 saveState()
/restoreState()
方法,尤其是当该内容需要添加到剪切路径之外时。
我正在按照 Tiling Hero 示例 (http://itextpdf.com/examples/iia.php?id=116) 进行操作,但我希望能够为每个页面添加页边距。
请查看 TileClipped example. It is based on the TilingHero 示例,但它有一个转折点:
public void manipulatePdf(String src, String dest)
throws IOException, DocumentException {
float margin = 30;
// Creating a reader
PdfReader reader = new PdfReader(src);
Rectangle rect = reader.getPageSizeWithRotation(1);
Rectangle pagesize = new Rectangle(rect.getWidth() + margin * 2, rect.getHeight() + margin * 2);
// step 1
Document document = new Document(pagesize);
// step 2
PdfWriter writer
= PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte content = writer.getDirectContent();
PdfImportedPage page = writer.getImportedPage(reader, 1);
// adding the same page 16 times with a different offset
float x, y;
for (int i = 0; i < 16; i++) {
x = -rect.getWidth() * (i % 4) + margin;
y = rect.getHeight() * (i / 4 - 3) + margin;
content.rectangle(margin, margin, rect.getWidth(), rect.getHeight());
content.clip();
content.newPath();
content.addTemplate(page, 4, 0, 0, 4, x, y);
document.newPage();
}
// step 4
document.close();
reader.close();
}
你看到我们如何区分 rect
和 pagesize
了吗?我们将 rect
定义为原始页面的大小,并将 pagesize
定义为稍大的大小(取决于 margin
的值)。
我们在定义偏移量 x
和 y
时使用 rect,但我们添加 margin
以稍微更改该偏移量。我们更改了偏移量,因为我们剪辑了 pagesize
。裁剪是通过定义一个裁剪路径:
content.rectangle(margin, margin, rect.getWidth(), rect.getHeight());
content.clip();
content.newPath();
这三行之后添加的所有内容都将被我们在rectangle()
方法中定义的矩形裁剪。如果您还想添加其他内容,您可能需要添加额外的 saveState()
/restoreState()
方法,尤其是当该内容需要添加到剪切路径之外时。