跳過到頁腳內容
使用 IRONPDF FOR JAVA

PDF For Java(全能解決方案)

市面上有許多 PDF Java 庫,例如 iText Library 和 Apache PDFBox,但 IronPDF 是一個功能強大的 Java 庫,它允許您執行各種類型的 PDF 操作,包括數位簽章、從表單中提取文字、插入文字等等。 本文將指導您如何使用 IronPDF for Java 透過高效易用的 API 建立 PDF 文件。

IronPDF for Java - PDF 函式庫

借助IronPDF Java 庫概述,開發人員可以使用 API 輕鬆地在 Java 應用程式中建立 PDF、編輯新文件、從 PDF 提取內容以及修改 PDF 文件。 對於需要從應用程式資料建立 PDF 檔案的 Java 開發人員來說,該程式庫是一個絕佳的選擇,因為它提供了許多功能,例如支援 CJK 字體。 IronPDF for Java 也提供了將多個 PDF 檔案無縫合併成一個 PDF 檔案的功能。

IronPDF 支援從範本建立 PDF 、新增新的 HTML 內容、自訂頁首和頁尾產生受密碼保護的 PDF對 PDF 檔案進行數位簽章、新增背景和前景、建立大綱和書籤、從 XML 文件形成完整的 PDF 檔案以及新增和編輯註解。

使用 HTML 建立 PDF 文檔

IronPDF 讓開發人員能夠輕鬆地將新的 HTML 資訊整合到整個 PDF 文件中。 對於希望動態建立包含豐富 HTML 資訊的 PDF 表單文件的開發人員來說,這是一個非常有用的工具,而且易於整合。 該庫支援多種 HTML 元件,例如表格、連結和圖像。 使用 CSS 為 HTML 文字資料或圖像設定樣式,可以輕鬆建立外觀專業的 PDF 檔案。

import com.ironsoftware.ironpdf.*;

import java.io.IOException;
import java.nio.file.Paths;

public class GeneratePdf {
    public static void main(String[] args) throws IOException {
        // Apply your commercial license key
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set a log file path
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Render the HTML as a PDF. Store in myPdf as type PdfDocument;
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Hello World</h1>");

        // Save the PdfDocument to a file
        myPdf.saveAs(Paths.get("Demo.pdf"));
    }
}
import com.ironsoftware.ironpdf.*;

import java.io.IOException;
import java.nio.file.Paths;

public class GeneratePdf {
    public static void main(String[] args) throws IOException {
        // Apply your commercial license key
        License.setLicenseKey("YOUR-LICENSE-KEY");

        // Set a log file path
        Settings.setLogPath(Paths.get("C:/tmp/IronPdfEngine.log"));

        // Render the HTML as a PDF. Store in myPdf as type PdfDocument;
        PdfDocument myPdf = PdfDocument.renderHtmlAsPdf("<h1>Hello World</h1>");

        // Save the PdfDocument to a file
        myPdf.saveAs(Paths.get("Demo.pdf"));
    }
}
JAVA

以下是根據上述原始程式碼產生的範例文件。

! Java PDF(一體化解決方案),圖 1:輸出

HTML頁首和頁尾

使用 IronPDF,您可以輕鬆地為文件添加 HTML 頁首和頁尾。 在許多 PDF 文件中,頁首和頁尾是必不可少的部分。 透過 IronPDF,開發人員可以使用文字、PNG 圖像和頁碼自訂 PDF 文件的頁首和頁尾。 需要在出版物中添加商標或版權資訊的企業會發現此功能非常有用。

import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.headerfooter.HtmlHeaderFooter;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class HeaderFooterExample {
    public static void main(String[] args) throws IOException {
        PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");

        // Build a footer using HTML
        // Merge Fields are: {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
        HtmlHeaderFooter footer = new HtmlHeaderFooter();
        footer.setMaxHeight(15); // millimeters
        footer.setHtmlFragment("<center><i>{page} of {total-pages}</i></center>");
        footer.setDrawDividerLine(true);
        pdf.addHtmlFooter(footer);

        // Build a header using an image asset
        // Note the use of BaseUrl to set a relative path to the assets
        HtmlHeaderFooter header = new HtmlHeaderFooter();
        header.setMaxHeight(20); // millimeters
        header.setHtmlFragment("<img src=\"logo.png\" />");
        header.setBaseUrl("./assets/");
        pdf.addHtmlHeader(header);

        try {
            pdf.saveAs(Paths.get("assets/html_headers_footers.pdf"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.headerfooter.HtmlHeaderFooter;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

public class HeaderFooterExample {
    public static void main(String[] args) throws IOException {
        PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");

        // Build a footer using HTML
        // Merge Fields are: {page} {total-pages} {url} {date} {time} {html-title} & {pdf-title}
        HtmlHeaderFooter footer = new HtmlHeaderFooter();
        footer.setMaxHeight(15); // millimeters
        footer.setHtmlFragment("<center><i>{page} of {total-pages}</i></center>");
        footer.setDrawDividerLine(true);
        pdf.addHtmlFooter(footer);

        // Build a header using an image asset
        // Note the use of BaseUrl to set a relative path to the assets
        HtmlHeaderFooter header = new HtmlHeaderFooter();
        header.setMaxHeight(20); // millimeters
        header.setHtmlFragment("<img src=\"logo.png\" />");
        header.setBaseUrl("./assets/");
        pdf.addHtmlHeader(header);

        try {
            pdf.saveAs(Paths.get("assets/html_headers_footers.pdf"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
JAVA

印章和浮水印

開發者可以使用 IronPDF 為 PDF 文件添加浮水印和圖章。 使用圖章為新文件新增自訂訊息或影像; 水印是顯示在文件背景中的半透明圖像或文字。

對於需要添加個人化資訊或保護文件免遭未經授權使用的公司來說,這些選項非常棒。

import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;
import java.io.IOException;
import java.nio.file.Paths;

public class WatermarkExample {
    public static void main(String[] args) throws IOException {
        // Apply your commercial license key
        License.setLicenseKey("Your-License");

        // Create a new PDF or load an existing one from the filesystem
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("C:\\byteToPdf.pdf"));

        // Apply a text watermark to the PDF document
        pdf.applyWatermark("<h2 style='color:red'>SAMPLE</h2>", 
                           30, VerticalAlignment.TOP, HorizontalAlignment.CENTER);

        // Save the updated PDF document
        pdf.saveAs(Paths.get("assets/watermark.pdf"));
    }
}
import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;
import java.io.IOException;
import java.nio.file.Paths;

public class WatermarkExample {
    public static void main(String[] args) throws IOException {
        // Apply your commercial license key
        License.setLicenseKey("Your-License");

        // Create a new PDF or load an existing one from the filesystem
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("C:\\byteToPdf.pdf"));

        // Apply a text watermark to the PDF document
        pdf.applyWatermark("<h2 style='color:red'>SAMPLE</h2>", 
                           30, VerticalAlignment.TOP, HorizontalAlignment.CENTER);

        // Save the updated PDF document
        pdf.saveAs(Paths.get("assets/watermark.pdf"));
    }
}
JAVA

背景與前景

使用 IronPDF,開發人員還可以自訂 PDF 文件的前景色和背景。 可以將自訂文字或圖像新增至文件的前景色或背景色,也可以將自訂顏色或圖像新增至背景色。 如果企業主希望在文件或 PDF 表格中添加個人化品牌或圖形,他們會發現此選項特別有用。

import com.ironsoftware.ironpdf.*;

import java.io.IOException;
import java.nio.file.Paths;

public class BackgroundForegroundExample {
    public static void main(String[] args) throws IOException {
        // Load background and foreground PDFs from the filesystem (or create them programmatically)
        PdfDocument backgroundPdf = PdfDocument.fromFile(Paths.get("assets/MyBackground.pdf"));
        PdfDocument foregroundPdf = PdfDocument.fromFile(Paths.get("assets/MyForeground.pdf"));

        // Render content (HTML, URL, etc.) as a PDF Document
        PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.nuget.org/packages/IronPdf");

        // Add the background and foreground PDFs to the newly-rendered document
        pdf.addBackgroundPdf(backgroundPdf);
        pdf.addForegroundPdf(foregroundPdf);

        // Save the updated PDF document
        pdf.saveAs(Paths.get("assets/BackgroundForegroundPdf.pdf"));
    }
}
import com.ironsoftware.ironpdf.*;

import java.io.IOException;
import java.nio.file.Paths;

public class BackgroundForegroundExample {
    public static void main(String[] args) throws IOException {
        // Load background and foreground PDFs from the filesystem (or create them programmatically)
        PdfDocument backgroundPdf = PdfDocument.fromFile(Paths.get("assets/MyBackground.pdf"));
        PdfDocument foregroundPdf = PdfDocument.fromFile(Paths.get("assets/MyForeground.pdf"));

        // Render content (HTML, URL, etc.) as a PDF Document
        PdfDocument pdf = PdfDocument.renderUrlAsPdf("https://www.nuget.org/packages/IronPdf");

        // Add the background and foreground PDFs to the newly-rendered document
        pdf.addBackgroundPdf(backgroundPdf);
        pdf.addForegroundPdf(foregroundPdf);

        // Save the updated PDF document
        pdf.saveAs(Paths.get("assets/BackgroundForegroundPdf.pdf"));
    }
}
JAVA

要了解有關 IronPDF for Java PDF 庫的更多信息,請參閱Java 的 HTML 轉 PDF 教程

結論

本文將介紹 PDF 文件的眾多功能,例如新增註解、書籤、HTML 內容、背景色彩和前景色、頁首和頁尾等等。 開發人員可以按照本文中的逐步說明,使用 IronPDF 整合這些功能,輕鬆製作符合其個人化需求的專業外觀 PDF 文件。

許可證價格為$799 。為了幫助開發者在購買前評估該庫的功能,IronPDF 提供免費試用版。 試用期內,圖書館的所有功能,包括技術支援和升級服務,均可使用。 使用者可以選擇購買許可證,以便在試用期結束後繼續造訪該圖書館。

常見問題解答

開發人員如何在 Java 中使用 HTML 建立 PDF 文件?

您可以使用 IronPDF 的 API 將 HTML 內容轉換成 PDF 文件。這可將豐富的 HTML 內容,如表格、連結和圖片,以 CSS 設定風格,直接加入您的 PDF 檔案中。

IronPDF 提供哪些自訂 PDF 頁頭和頁尾的功能?

IronPDF 允許您使用文字、圖片和頁碼自訂頁首和頁尾。此功能對於加入個人化品牌或法律資訊(如商標和版權)非常有用。

我可以使用 IronPDF 將多個 PDF 文件合併為一個嗎?

是的,IronPDF 提供了透過其全面的 API 將多個 PDF 檔案無縫合併為單一文件的功能。

是否可以使用 IronPDF 為 PDF 新增數位簽章?

是的,IronPDF 支援在 PDF 文件中加入數位簽章,強化檔案的安全性與真實性。

IronPDF 如何處理 PDF 文件的水印添加?

IronPdf 可讓您在 PDF 文件上覆蓋自訂訊息或影像作為圖章,並套用半透明文字或影像作為水印。

IronPDF 是否支持 PDF 文档的密码保护?

是的,您可以使用 IronPDF 生成受密码保护的 PDF,确保您的文档安全且只有目标用户才能访问。

Java 開發人員使用 IronPDF 有哪些優勢?

IronPDF 提供直觀的 API 以進行 PDF 的無縫整合,支援廣泛的 PDF 作業,並提供廣泛的客製化選項,使其成為 Java 開發人員管理 PDF 檔案的重要工具。

IronPDF 是否有提供給開發人員的試用版?

是的,IronPDF 提供免費試用版,讓開發人員在購買授權之前探索所有功能並評估資料庫的能力。

開發人員可以使用 IronPDF 為 PDF 添加背景和前景嗎?

是的,IronPDF 可以添加自定義的背景和前景,允許在 PDF 文件中使用個性化的品牌或圖形增強。

IronPDF 為 PDF 文件管理提供哪些自訂選項?

IronPdf 提供一系列自訂選項,包括新增註解、書籤、大綱、頁首、頁尾、水印、背景和數位簽章。

Darrius Serrant
全棧軟件工程師 (WebOps)

Darrius Serrant 擁有邁阿密大學計算機科學學士學位,目前任職於 Iron Software 的全栈 WebOps 市場營銷工程師。從小就迷上編碼,他認為計算既神秘又可接近,是創意和解決問題的完美媒介。

在 Iron Software,Darrius 喜歡創造新事物,並簡化複雜概念以便於理解。作為我們的駐場開發者之一,他也自願教學生,分享他的專業知識給下一代。

對 Darrius 來說,工作令人滿意因為它被重視且有實際影響。