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

PDF For Java(全能解決方案)

市場上有多個PDF Java程式庫可用,如iText Library和Apache PDFBox,但IronPDF是功能強大的Java程式庫之一,使您可以執行各種類型的PDF操作,包括數位簽名、從表單提取文本、插入文本等。 本文將指導您如何使用IronPDF for Java創建PDF文件,使用高效和易於使用的API。

IronPDF For Java - PDF程式庫

透過IronPDF Java程式庫概述,開發者可以創建PDF、編輯新文件、從PDF提取內容,並輕鬆地在其Java應用程式中使用API來更改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

以下是從上述源代碼生成的範本文件。

PDF For Java (All-in-One Solution), 圖1:輸出 輸出

HTML頁眉和頁腳

使用IronPDF,為您的文件添加HTML頁眉和頁腳變得簡單。 在許多PDF文件中,頁眉和頁腳是重要部分。 使用IronPDF,開發者可以自定義其PDF文件的頁眉和頁腳,包括文本、PNG圖像和頁碼。 需要在其出版物中添加商標或版權信息的企業,將發現此功能非常有益。

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程式庫的信息,請參考HTML轉PDF教程

結論

本文涵蓋的功能包括為PDF文檔添加註釋、書籤、HTML內容、背景色和前景色、頁眉和頁腳的能力。 開發者可以藉由文章中的逐步說明輕鬆地生產符合其個別需求的專業PDF文檔,這些說明展示了如何使用IronPDF整合這些功能。

授權價格為$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 文件上,並將半透明文字或圖像作為浮水印套用到 PDF 文件上。

IronPDF是否支援PDF文件的密碼保護?

是的,您可以使用IronPDF產生受密碼保護的 PDF 文件,確保您的文件安全可靠,只有授權使用者才能存取。

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

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 來說,工作令人滿意因為它被重視且有實際影響。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me