Attach a Cover Page

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
Attach a Cover Page

Attach a Cover Page

IronPDF的PdfDocument.merge方法將一個PDF文件的內容附加到另一個PDF文件中。

當調用時,a的頁面後面。

此程式碼範例顯示如何讓開發者使用此方法將封面頁附加到現有的PDF文件。 使用PdfDocument.fromFile載入已建立的封面)。 然後,建立(或載入)需要封面的PDF文件並呼叫PdfDocument.merge將它們合併在一起。 將封面頁指定為merge方法的第一個參數,接著是其他PDF文件。

觀察在程式碼範例中ChromePdfRenderOptions物件上的使用,以達到頁面2。 這允許我們在合併前調整PDF文件的頁碼以反映封面頁的加入。 請注意,此功能僅適用於即時生成的PDF文件。 對於現有的文件,頁碼的更改需在合併前以其他方式完成。

開發者也可以使用PdfDocument.prependPdf方法將封面頁附加到PDF。

Learn more about PdfDocument.merge method
在 GitHub 查看

準備好開始了嗎?

版本:2026.10剛剛發布