任何人都知道为什么我的标题或边距不会在页面上生成/生成?它只生成带有“hello neck”
段落的pdfimport java.awt.Desktop;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.*;
public class Report {
public static void main(String arg[])throws Exception
{
try{
File temp = File.createTempFile("tempfile", ".pdf");
OutputStream file = new FileOutputStream(temp);
Document document = new Document();
PdfWriter.getInstance(document, file);
document.open();
document.addHeader("header1", "this is my header file");
document.setMargins(50, 50, 100, 100);
document.add(new Paragraph("hello neck"));
document.close();
file.close();
if (Desktop.isDesktopSupported()) {
Desktop dtop = Desktop.getDesktop();
if (dtop.isSupported(Desktop.Action.OPEN)) {
String temp2 = temp.getPath();
dtop.open(new File(temp2));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
错误的标题。那是元信息而不是页面标题&页脚。
想想“内容类型”而不是“y页面x”。
//these two lines of code are identical
document.addHeader("a", "b");
document.add(new Header("a", "b"));
Header
继承自Meta
,处理作者/ title / etc / etc.标题用于不属于标准值之一的任意字符串。
此外,您只能在调用document.open()之前更改元数据。之后,任何变化都会被忽略(或者他们会抛出......我不记得了)
但是你需要页眉和页脚。传统的处理方式是通过PdfPageEvent
的OnEndPage函数。如果你继承自PdfPageEventHelper
,它已经删除了PdfPageEvent
接口中的所有函数,所以你只需要覆盖你想要的那个。方便。
在OnEndPage中,您需要使用ColumnText
对象将文本写入提供的PdfContentByte。