Tiling and N-upping
Examples written in answer to questions such as:
tileintwo
TileInTwo JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.merge;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class TileInTwo {
public static final String DEST = "./target/sandbox/merge/tile_in_two.pdf";
public static final String SRC = "./src/main/resources/pdfs/united_states.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new TileInTwo().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws IOException {
PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
int numberOfPages = srcDoc.getNumberOfPages();
for (int i = 1; i <= numberOfPages; i++) {
PageSize mediaBox = getHalfHeightPageSize(srcDoc.getPage(i).getPageSizeWithRotation());
resultDoc.setDefaultPageSize(mediaBox);
PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(resultDoc);
PdfCanvas canvas = new PdfCanvas(resultDoc.addNewPage());
canvas.addXObjectAt(page, 0, -mediaBox.getHeight());
canvas = new PdfCanvas(resultDoc.addNewPage());
canvas.addXObjectAt(page, 0, 0);
}
resultDoc.close();
srcDoc.close();
}
private static PageSize getHalfHeightPageSize(Rectangle pageSize) {
float width = pageSize.getWidth();
float height = pageSize.getHeight();
return new PageSize(width, height / 2);
}
}
TileInTwo C#
C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
namespace iText.Samples.Sandbox.Merge
{
public class TileInTwo
{
public static readonly String DEST = "results/sandbox/merge/tile_in_two.pdf";
public static readonly String SRC = "../../../resources/pdfs/united_states.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new TileInTwo().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
int numberOfPages = srcDoc.GetNumberOfPages();
for (int i = 1; i <= numberOfPages; i++)
{
PageSize mediaBox = GetHalfHeightPageSize(srcDoc.GetPage(i).GetPageSizeWithRotation());
resultDoc.SetDefaultPageSize(mediaBox);
PdfFormXObject page = srcDoc.GetPage(i).CopyAsFormXObject(resultDoc);
PdfCanvas canvas = new PdfCanvas(resultDoc.AddNewPage());
canvas.AddXObjectAt(page, 0, -mediaBox.GetHeight());
canvas = new PdfCanvas(resultDoc.AddNewPage());
canvas.AddXObjectAt(page, 0, 0);
}
resultDoc.Close();
srcDoc.Close();
}
private static PageSize GetHalfHeightPageSize(Rectangle pageSize)
{
float width = pageSize.GetWidth();
float height = pageSize.GetHeight();
return new PageSize(width, height / 2);
}
}
}
tileintwo2
TileInTwo Java
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.merge;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class TileInTwo2 {
public static final String DEST = "./target/sandbox/merge/tile_in_two2.pdf";
public static final String SRC = "./src/main/resources/pdfs/united_states.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new TileInTwo2().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws IOException {
PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
int numberOfPages = srcDoc.getNumberOfPages();
for (int i = 1; i <= numberOfPages; i++) {
PageSize mediaBox = getHalfWidthPageSize(srcDoc.getPage(i).getPageSizeWithRotation());
pdfDoc.setDefaultPageSize(mediaBox);
PdfFormXObject page = srcDoc.getPage(i).copyAsFormXObject(pdfDoc);
PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());
canvas.addXObjectAt(page, 0, 0);
canvas = new PdfCanvas(pdfDoc.addNewPage());
canvas.addXObjectAt(page, -mediaBox.getWidth(), 0);
}
pdfDoc.close();
srcDoc.close();
}
private static PageSize getHalfWidthPageSize(Rectangle pageSize) {
float width = pageSize.getWidth();
float height = pageSize.getHeight();
return new PageSize(width / 2, height);
}
}
TileInTwo2 C#
C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
namespace iText.Samples.Sandbox.Merge
{
public class TileInTwo2
{
public static readonly String DEST = "results/sandbox/merge/tile_in_two2.pdf";
public static readonly String SRC = "../../../resources/pdfs/united_states.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new TileInTwo2().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
int numberOfPages = srcDoc.GetNumberOfPages();
for (int i = 1; i <= numberOfPages; i++)
{
PageSize mediaBox = GetHalfWidthPageSize(srcDoc.GetPage(i).GetPageSizeWithRotation());
pdfDoc.SetDefaultPageSize(mediaBox);
PdfFormXObject page = srcDoc.GetPage(i).CopyAsFormXObject(pdfDoc);
PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());
canvas.AddXObjectAt(page, 0, 0);
canvas = new PdfCanvas(pdfDoc.AddNewPage());
canvas.AddXObjectAt(page, -mediaBox.GetWidth(), 0);
}
pdfDoc.Close();
srcDoc.Close();
}
private static PageSize GetHalfWidthPageSize(Rectangle pageSize)
{
float width = pageSize.GetWidth();
float height = pageSize.GetHeight();
return new PageSize(width / 2, height);
}
}
}
tilinghero
TilingHero Java
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.merge;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class TilingHero {
public static final String DEST = "./target/sandbox/merge/tiling_hero.pdf";
public static final String RESOURCE = "./src/main/resources/pdfs/hero.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new TilingHero().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws IOException {
PdfDocument srcDoc = new PdfDocument(new PdfReader(RESOURCE));
PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
PdfPage srcFirstPage = srcDoc.getFirstPage();
Rectangle pageSize = srcFirstPage.getPageSizeWithRotation();
float width = pageSize.getWidth();
float height = pageSize.getHeight();
// The top left rectangle of the tiled pdf picture
Rectangle mediaBox = new Rectangle(0, 3 * height, width, height);
resultDoc.setDefaultPageSize(new PageSize(mediaBox));
PdfFormXObject page = srcFirstPage.copyAsFormXObject(resultDoc);
for (int i = 1; i <= 16; i++) {
PdfCanvas canvas = new PdfCanvas(resultDoc.addNewPage());
canvas.addXObjectWithTransformationMatrix(page, 4, 0, 0, 4, 0, 0);
float xCoordinate = (i % 4) * width;
float yCoordinate = (4 - (i / 4)) * height;
mediaBox = new Rectangle(xCoordinate, yCoordinate, width, -height);
resultDoc.setDefaultPageSize(new PageSize(mediaBox));
}
srcDoc.close();
resultDoc.close();
}
}
TilingHero
C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
namespace iText.Samples.Sandbox.Merge
{
public class TilingHero
{
public static readonly String DEST = "results/sandbox/merge/tiling_hero.pdf";
public static readonly String RESOURCE = "../../../resources/pdfs/hero.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new TilingHero().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument srcDoc = new PdfDocument(new PdfReader(RESOURCE));
PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
PdfPage srcFirstPage = srcDoc.GetFirstPage();
Rectangle pageSize = srcFirstPage.GetPageSizeWithRotation();
float width = pageSize.GetWidth();
float height = pageSize.GetHeight();
// The top left rectangle of the tiled pdf picture
Rectangle mediaBox = new Rectangle(0, 3 * height, width, height);
resultDoc.SetDefaultPageSize(new PageSize(mediaBox));
PdfFormXObject page = srcFirstPage.CopyAsFormXObject(resultDoc);
for (int i = 1; i <= 16; i++)
{
PdfCanvas canvas = new PdfCanvas(resultDoc.AddNewPage());
canvas.AddXObjectWithTransformationMatrix(page, 4, 0, 0, 4, 0, 0);
float xCoordinate = (i % 4) * width;
float yCoordinate = (4 - (i / 4)) * height;
mediaBox = new Rectangle(xCoordinate, yCoordinate, width, -height);
resultDoc.SetDefaultPageSize(new PageSize(mediaBox));
}
srcDoc.Close();
resultDoc.Close();
}
}
}
tileclipped
TileClipped Java
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.stamper;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import java.io.File;
public class TileClipped {
public static final String DEST = "./target/sandbox/stamper/tile_clipped.pdf";
public static final String SRC = "./src/main/resources/pdfs/hero.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new TileClipped().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
float margin = 30;
PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
Rectangle rect = srcDoc.getFirstPage().getPageSizeWithRotation();
Rectangle pageSize = new Rectangle(rect.getWidth() + margin * 2, rect.getHeight() + margin * 2);
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
// The functionality below will work only for the pages, added after the method is called
pdfDoc.setDefaultPageSize(new PageSize(pageSize));
PdfCanvas content = new PdfCanvas(pdfDoc.addNewPage());
PdfFormXObject page = srcDoc.getFirstPage().copyAsFormXObject(pdfDoc);
// Adding the same page 16 times with a different offset
for (int i = 0; i < 16; i++) {
float x = -rect.getWidth() * (i % 4) + margin;
float y = rect.getHeight() * (i / 4 - 3) + margin;
content.rectangle(margin, margin, rect.getWidth(), rect.getHeight());
content.clip();
content.endPath();
content.addXObjectWithTransformationMatrix(page, 4, 0, 0, 4, x, y);
if (15 != i) {
content = new PdfCanvas(pdfDoc.addNewPage());
}
}
pdfDoc.close();
}
}
TileClipped C#
C#
using System;
using System.IO;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Kernel.Pdf.Xobject;
namespace iText.Samples.Sandbox.Stamper
{
public class TileClipped
{
public static readonly String DEST = "results/sandbox/stamper/tile_clipped.pdf";
public static readonly String SRC = "../../../resources/pdfs/hero.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new TileClipped().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
float margin = 30;
PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC));
Rectangle rect = srcDoc.GetFirstPage().GetPageSizeWithRotation();
Rectangle pageSize = new Rectangle(rect.GetWidth() + margin * 2, rect.GetHeight() + margin * 2);
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
// The functionality below will work only for the pages, added after the method is called
pdfDoc.SetDefaultPageSize(new PageSize(pageSize));
PdfCanvas content = new PdfCanvas(pdfDoc.AddNewPage());
PdfFormXObject page = srcDoc.GetFirstPage().CopyAsFormXObject(pdfDoc);
// Adding the same page 16 times with a different offset
for (int i = 0; i < 16; i++)
{
float x = -rect.GetWidth() * (i % 4) + margin;
float y = rect.GetHeight() * (i / 4 - 3) + margin;
content.Rectangle(margin, margin, rect.GetWidth(), rect.GetHeight());
content.Clip();
content.EndPath();
content.AddXObjectWithTransformationMatrix(page, 4, 0, 0, 4, x, y);
if (15 != i)
{
content = new PdfCanvas(pdfDoc.AddNewPage());
}
}
pdfDoc.Close();
}
}
}