iText字体无法正确识别

时间:2015-11-28 10:35:25

标签: java itext font-size

我正在尝试使用iText库创建PDF文件。但是当我将add函数直接用于document时,font功能无法识别出属性。

实际上字体大小已识别,并且&在太多之前,我认为String本身仍然是默认的。

奇怪的是,当我使用add cell功能正确识别所有font功能时。

以下是输出pdf的自描述代码和屏幕截图;

package samplepdfcreator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import static com.itextpdf.text.Element.ALIGN_CENTER;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;


public class NewClass {

    public static final String RESULT = "C:\\Users\\World_Home\\Desktop\\hello.pdf";

    public static void main(String[] args) throws DocumentException, IOException {
        new NewClass().createPdf(RESULT);
    }

    public void createPdf(String filename)throws DocumentException, IOException {

        Document document = new Document();

        //Create PDF at specified filepath above.
        PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.setPageSize(PageSize.A4);
        document.setMargins(36, 36, 50, 50);
        document.setMarginMirroring(true);

        //Open the created PDF and begin editing.
        document.open();

        //First Table Adding
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell;
        Font font;
        Paragraph paragraph;

        cell = new PdfPCell();
        cell.setColspan(1);
        font = new Font();
        font.setSize(12);
        font.setStyle("bold");
        paragraph = new Paragraph("Header Inside table1");
        paragraph.setFont(font);
        paragraph.setAlignment(ALIGN_CENTER);
        cell.addElement(paragraph);


        paragraph = new Paragraph("Info in table1");
        paragraph.setAlignment(ALIGN_CENTER);
        cell.addElement(paragraph);

        paragraph = new Paragraph(" ");
        cell.addElement(paragraph);

        table.addCell(cell);
        table.setWidthPercentage(100);
        table.setSpacingAfter((float) 0);
        table.setSpacingBefore((float) 0);

        document.add(table);

        //Adding one blank line
        font = new Font();
        font.setSize(12);
        paragraph = new Paragraph(" ");
        document.add(paragraph);

        //Adding Some String to Document directly 2times **Problem is Here**
        font = new Font();
        font.setStyle("bold");
        font.setSize(50);
        paragraph = new Paragraph("String In Document");
        paragraph.setFont(font);
        paragraph.setAlignment(ALIGN_CENTER);

        document.add(paragraph);
        document.add(paragraph);

        //Adding one blank line
        font = new Font();
        font.setSize(12);
        paragraph = new Paragraph(" ");
        document.add(paragraph);


        //Second Table Adding
        PdfPTable table2 = new PdfPTable(2);

        cell = new PdfPCell();
        cell.setColspan(2);
        font.setSize(12);
        font.setStyle("bold");
        paragraph = new Paragraph("Header Inside table2");
        paragraph.setFont(font);
        paragraph.setAlignment(ALIGN_CENTER);
        cell.addElement(paragraph);

        paragraph = new Paragraph("Info in table2");
        paragraph.setAlignment(ALIGN_CENTER);
        cell.addElement(paragraph);

        paragraph = new Paragraph(" ");
        cell.addElement(paragraph);

        table2.addCell(cell);
        table2.setWidthPercentage(100);
        table2.setSpacingAfter((float) 0);
        table2.setSpacingBefore((float) 0);

        document.add(table2);

        //Close the PDF
        document.close();


    }

}

enter image description here

0 个答案:

没有答案