【发布时间】:2012-01-29 02:44:06
【问题描述】:
我在我的 java 程序中使用 iText 来编辑现有的 pdf。生成的 pdf 无法打开,它显示 pdf header signature not found 错误。我正在使用同名的输入和输出文件。
private static String INPUTFILE = "/sample.pdf";
private static String OUTPUTFILE = "/sample.pdf";
public static void main(String[] args)
throws DocumentException,
IOException
{
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance(doc,new FileOutputStream(OUTPUTFILE));
doc.open();
PdfReader reader = new PdfReader(INPUTFILE);
int n;
n = reader.getNumberOfPages();
System.out.println("No. of Pages :" +n);
for (int i = 1; i <= n; i++)
{
if (i == 1)
{
Rectangle rect = new Rectangle(85,650,800,833);
PdfFormField pushbutton = PdfFormField.createPushButton(writer);
pushbutton.setWidget(rect, PdfAnnotation.HIGHLIGHT_PUSH);
PdfContentByte cb = writer.getDirectContent();
PdfAppearance app = cb.createAppearance(380,201);
app.rectangle(62,100,50,-1);
app.fill();
pushbutton.setAppearance(PdfAnnotation.APPEARANCE_NORMAL,app);
writer.addAnnotation(pushbutton);
PdfImportedPage page = writer.getImportedPage(reader, i);
Image instance = Image.getInstance(page);
doc.add(instance);
}
【问题讨论】:
-
如果您向我们展示您的代码会更好
-
@Francisco Puga....以上是我的代码
-
我认为您应该使用此答案中给出的建议更新您的代码stackoverflow.com/a/8655157/930271