publicclassPdfDemo {
// 生产的pdf文件路径publicstaticfinalStringDEST="D:\\demo.pdf";
// 字体publicstaticfinalStringFONT="ttf/方正小篆体.ttf";
public Document getDocument(){
PdfDocument pdfDocument;
Document document;
PdfFont font ;
try{
pdfDocument = newPdfDocument(newPdfWriter(DEST));
// 项目指定字体, 通过类加载项目字体资源
font = font = PdfFontFactory.createFont(
IOUtils.toByteArray(PdfDemo.class.getClassLoader().getResourceAsStream(FONT)),
PdfEncodings.IDENTITY_H, false);;
document = newDocument(pdfDocument)
document.setFont(font);
return document;
}catch(Exception e){
e.printStackTrace();
从案例可以, 生成Document对象的步骤.接下来,一步一步看Document对象创建的过程
com.itextpdf.layout.Document类部分源码:
* Document is the default root element when creating a self-sufficient PDF. It
* mainly operates high-level operations e.g. setting page size and rotation,
* adding elements, and writing text at specific coordinates. It has no
* knowledge of the actual PDF concepts and syntax.
* A {@link Document}'s rendering behavior can be modified by extending
* {@link DocumentRenderer} and setting an instance of this newly created with
* {@link #setRenderer(com.itextpdf.layout.renderer.DocumentRenderer) }.
// Document文档是创建自定义PDF时的默认根元素// 它主要操作高级操作,例如设置页面大小和旋转,添加元素,并在特定坐标处写入文本publicclassDocumentextendsRootElement<Document> {
* Creates a document from a {@link PdfDocument}. Initializes the first page
* with the {@link PdfDocument}'s current default {@link PageSize}.
* @param pdfDoc the in-memory representation of the PDF document
// 常用构造方法 使用PdfDocument对象publicDocument(PdfDocument pdfDoc) {
this(pdfDoc, pdfDoc.getDefaultPageSize());
@Overridepublic Document add(IBlockElement element) {
checkClosingStatus();
super.add(element);
if (element instanceof ILargeElement) {
((ILargeElement) element).setDocument(this);
((ILargeElement) element).flushContent();
returnthis;
* Sets the font of this Element.
* This property overrides the value set by {@link #setFontFamily}. Font is set either via exact {@link PdfFont}
* instance or via font-family name that should correspond to the font in {@link FontProvider}, but not both.
* @param font a {@link PdfFont font}
* @return this Element.
// 设置元素的字体public T setFont(PdfFont font) {
setProperty(Property.FONT, font);
return (T) (Object) this;
com.itextpdf.kernel.pdf.PdfDocument类部分源码:
* Main enter point to work with PDF document.
// 使用PDF文档的主输入点publicclassPdfDocumentimplementsIEventDispatcher, Closeable, Serializable {
* Open PDF document in writing mode.
* Document has no pages when initialized.
* @param writer PDF writer
// 以编写模式打开PDF文档 ,文档初始化时没有页面 publicPdfDocument(PdfWriter writer) {
this(writer, newDocumentProperties());
com.itextpdf.kernel.pdf.PdfWriter类部分源码:
* Create a PdfWriter writing to the passed filename and with default writer properties.
* @param filename filename of the resulting pdf.
* @throws FileNotFoundException
// 创建一个PdfWriter,将其写入传递的文件名并具有默认的writer属性publicPdfWriter(String filename)throws FileNotFoundException {
this(filename, newWriterProperties());
* Create an ImageData instance representing the image from the file located at the specified url.
* @param url location of the image
* @return The created ImageData object.
publicstatic ImageData create(URL url) {
return create(url, false);
* Create an ImageData instance representing the image from the specified file.
* @param filename filename of the file containing the image
* @param recoverImage whether to recover from a image error (for TIFF-images)
* @return The created ImageData object.
* @throws MalformedURLException
publicstatic ImageData create(String filename, boolean recoverImage)throws MalformedURLException {
return create(UrlUtil.toURL(filename), recoverImage);
* Create an ImageData instance representing the image from the specified file.
* @param filename filename of the file containing the image
* @return The created ImageData object.
* @throws MalformedURLException
publicstatic ImageData create(String filename)throws MalformedURLException {
return create(filename, false);
Image类部分源码:
* Creates an {@link Image} from an image resource, read in from a file
* with the iText I/O module.
* @param img an internal representation of the {@link com.itextpdf.io.image.ImageData image resource}
publicImage(ImageData img) {
this(newPdfImageXObject(checkImageType(img)));
setProperty(Property.FLUSH_ON_DRAW, true);
* Scale the image to an absolute size. This method will <em>not</em>
* preserve the width-height ratio of the image.
* @param fitWidth the new absolute width of the image
* @param fitHeight the new absolute height of the image
* @return this element
public Image scaleAbsolute(float fitWidth, float fitHeight) {
floathorizontalScaling= fitWidth / xObject.getWidth();
floatverticalScaling= fitHeight / xObject.getHeight();
return scale(horizontalScaling, verticalScaling);
* Sets the margins around the image to a series of new widths.
* @param marginTop the new margin top width
* @param marginRight the new margin right width
* @param marginBottom the new margin bottom width
* @param marginLeft the new margin left width
* @return this image
public Image setMargins(float marginTop, float marginRight, float marginBottom, float marginLeft) {
return setMarginTop(marginTop).setMarginRight(marginRight).setMarginBottom(marginBottom).setMarginLeft(marginLeft);
* Scale the image relative to its default size.
* @param horizontalScaling the horizontal scaling coefficient. default value 1 = 100%
* @param verticalScaling the vertical scaling coefficient. default value 1 = 100%
* @return this element
public Image scale(float horizontalScaling, float verticalScaling) {
setProperty(Property.HORIZONTAL_SCALING, horizontalScaling);
setProperty(Property.VERTICAL_SCALING, verticalScaling);
returnthis;