Adding a cover page to an existing PDF
These examples were written in answer to the question Click How to add a cover page to an existing PDF document?
addcover1
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.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.utils.PdfMerger;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class AddCover1 {
public static final String DEST = "./target/sandbox/merge/add_cover.pdf";
public static final String COVER = "./src/main/resources/pdfs/hero.pdf";
public static final String RESOURCE = "./src/main/resources/pdfs/pages.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new AddCover1().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PdfDocument cover = new PdfDocument(new PdfReader(COVER));
PdfDocument resource = new PdfDocument(new PdfReader(RESOURCE));
PdfMerger merger = new PdfMerger(pdfDoc);
merger.merge(cover, 1, 1);
merger.merge(resource, 1, resource.getNumberOfPages());
// Source documents can be closed implicitly after merging,
// by passing true to the setCloseSourceDocuments(boolean) method
cover.close();
resource.close();
// The resultant pdf doc will be closed implicitly.
merger.close();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Utils;
namespace iText.Samples.Sandbox.Merge
{
public class AddCover1
{
public static readonly String DEST = "results/sandbox/merge/add_cover.pdf";
public static readonly String COVER = "../../../resources/pdfs/hero.pdf";
public static readonly String RESOURCE = "../../../resources/pdfs/pages.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new AddCover1().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
PdfDocument cover = new PdfDocument(new PdfReader(COVER));
PdfDocument resource = new PdfDocument(new PdfReader(RESOURCE));
PdfMerger merger = new PdfMerger(pdfDoc);
merger.Merge(cover, 1, 1);
merger.Merge(resource, 1, resource.GetNumberOfPages());
// Source documents can be closed implicitly after merging,
// by passing true to the setCloseSourceDocuments(boolean) method
cover.Close();
resource.Close();
// The resultant pdf doc will be closed implicitly.
merger.Close();
}
}
}
addcover2
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.forms.PdfPageFormCopier;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class AddCover2 {
public static final String DEST = "./target/sandbox/merge/add_cover2.pdf";
public static final String COVER = "./src/main/resources/pdfs/hero.pdf";
public static final String RESOURCE = "./src/main/resources/pdfs/pages.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new AddCover2().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(RESOURCE), new PdfWriter(dest));
PdfDocument cover = new PdfDocument(new PdfReader(COVER));
// Copier contains the additional logic to copy acroform fields to a new page.
// PdfPageFormCopier uses some caching logic which can potentially improve performance
// in case of the reusing of the same instance.
PdfPageFormCopier formCopier = new PdfPageFormCopier();
cover.copyPagesTo(1, 1, pdfDoc, 1, formCopier);
cover.close();
pdfDoc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.Forms;
using iText.Kernel.Pdf;
namespace iText.Samples.Sandbox.Merge
{
public class AddCover2
{
public static readonly String DEST = "results/sandbox/merge/add_cover2.pdf";
public static readonly String COVER = "../../../resources/pdfs/hero.pdf";
public static readonly String RESOURCE = "../../../resources/pdfs/pages.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new AddCover2().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(RESOURCE), new PdfWriter(dest));
PdfDocument cover = new PdfDocument(new PdfReader(COVER));
// Copier contains the additional logic to copy acroform fields to a new page.
// PdfPageFormCopier uses some caching logic which can potentially improve performance
// in case of the reusing of the same instance.
PdfPageFormCopier formCopier = new PdfPageFormCopier();
cover.CopyPagesTo(1, 1, pdfDoc, 1, formCopier);
cover.Close();
pdfDoc.Close();
}
}
}