附加封面页

import com.ironsoftware.ironpdf.PdfDocument;  
import com.ironsoftware.ironpdf.headerfooter.HeaderFooterOptions;
import java.io.IOException;  
import java.nio.file.Paths;


// Create a Sample Cover Page using RenderHtmlAsPdf
PdfDocument coverPage = PdfDocument.renderHtmlAsPdf("<h1>This is a Cover Page</h1>");
PdfDocument webpage = PdfDocument.renderUrlAsPdf("https://www.nuget.org/packages/IronPdf/");

// Set the page number of the PDF document to be created to 2.
HeaderFooterOptions headerFooterOptions = new HeaderFooterOptions();
headerFooterOptions.setFirstPageNumber(1);
TextHeaderFooter footer = new TextHeaderFooter();
footer.setLeftText("");
footer.setCenterText("Page {page}");
footer.setRightText("");
webpage.addTextFooter(footer, headerFooterOptions);

// Convert a web page's content to a PDF document.
// Merge the cover page with the web page and save the new PDF to the filesystem.
try {
    PdfDocument.merge(coverPage, webpage).saveAs(Paths.get("assets/cover_page_pdf.pdf"));
} catch (IOException e) {
    throw new RuntimeException(e);
}
mvn install
附加封面页

附加封面页

IronPDF的PdfDocument.merge方法将一个PDF文档的内容附加到另一个PDF文档。

当调用时,a的页面之后。

此代码示例展示了开发人员如何使用此方法将封面附加到现有的 PDF 文档。 使用PdfDocument.fromFile加载已经创建的封面)。 然后,创建(或加载)需要封面的PDF文档,并调用PdfDocument.merge将它们组合在一起。 指定封面为merge方法的第一个参数,然后是其他PDF文档。

在代码示例中,观察对setFirstPageNumber的使用。 这使我们能够在合并之前调整 PDF 文档的页码,以反映封面的添加。 请注意,此功能仅适用于将动态生成的 PDF 文档。 对于现有文档,需要在合并前通过其他方式进行页码更改。

开发人员还可以使用PdfDocument.prependPdf方法将封面附加到PDF中。

Learn more about PdfDocument.merge method
在 GitHub 上查看

准备开始了吗?

版本:2026.10刚刚发布