Custom PDF Size

import com.ironsoftware.ironpdf.*;  
import com.ironsoftware.ironpdf.render.*;  
import java.io.IOException;  
import java.nio.file.Paths;

String html = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>";  
  
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();  
renderOptions.setPaperSize(PaperSize.Custom);  
  
/*  
 * Setting page sizes using different measuring units: * 1. setCustomPaperWidth( width ) / setCustomPaperHeight( height ): for inches * 2. setCustomPaperSizeInCentimeters( width, height ): for centimeters. * 3. setCustomPaperSizeInMillimeters( width, height ): for millimeters * 4. setCustomPaperSizeInPixelsOrPoints( width, height ): for pixels/points */renderOptions.setCustomPaperSizeInCentimeters(13.97, 13.97);  
  
PdfDocument.renderHtmlAsPdf(html, renderOptions).saveAs(Paths.get("assets/CustomPaperSize.pdf"));
mvn install
Custom PDF Size

Custom PDF Size

使用IronPDF,開發者可以生成尺寸不同於標準A4(8½ x 11英寸或21.59 x 27.94厘米)的PDF文件。

通過在setPaperSize方法,指定所需的尺寸來渲染不同紙張大小的PDF。 在方法參數中使用預設的PaperSize值設置紙張大小。 之後,從來源內容(HTML字串、URL、HTML檔案等)照常渲染PDF文件。

PaperSize列舉型別支持超過100種行業認可的印刷尺寸。 這涵蓋了大多數商業案例中使用的尺寸。 若要使用PaperSize.Custom並手動設置所需的頁寬和頁高:

如上所示,setCustomPaperHeight接受表示所需尺寸的英寸數值。 若要使用厘米設置尺寸,請使用setCustomPaperSizeInCentimeters。 若要使用毫米設置尺寸,請使用setCustomPaperSizeInMillimeters。 若要使用像素/點設置尺寸,請使用setCustomPaperSizeInPixelsOrPoints。

了解更多有關使用IronPDF設置自定義PDF尺寸的資訊。
在 GitHub 查看

準備好開始了嗎?

版本:2026.10剛剛發布