Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve pdf header signature not found error?

Tags:

java

pdf

itext

I'm using iText in my java program for editing an existing pdf.The generated pdf could not open and it shows pdf header signature not found error.I'm using both my input and output file in a same name.

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);
                }
like image 784
BobDroid Avatar asked Dec 28 '11 11:12

BobDroid


6 Answers

Then try at first renaming the input file to .bak, and reading the .bak, and writing the .pdf. That could give a clue whether the error is with reading or writing.

Itext is not a single API, but several ones, mixed together. Quite hard sometimes. I did:

Close both the PdfReader and FileInputStream.

Close both Document and PdfWriter.

like image 181
Joop Eggen Avatar answered Oct 03 '22 04:10

Joop Eggen


You may be importing from an empty source, or a invalid pdf file, in my case pdfCopy dont work, so here is the code I used.

Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, OutputStream );
PdfReader reader = new PdfReader(dato.getBinaryStream());

PdfImportedPage page1 = writer.getImportedPage(reader, 1);
PdfContentByte cb = writer.getDirectContent();
cb.addTemplate(page1, 1, 0, 0, 1, 0, 0);

 document.setPageSize(new Rectangle(page1.getWidth(),page1.getHeight()) );

...

This should work.

like image 39
Steven Lizarazo Avatar answered Oct 03 '22 05:10

Steven Lizarazo


You should use PdfCopy instead of PdfWriter.getInstance, since it fails to update the PDF object references otherwise.

Furthermore instead of adding an Image to the document, you can utilize the PdfCopy.addPage method, which accepts a PdfImportedPage as parameter.

Document doc = new Document();
PdfCopy writer = new PdfCopy(doc,new FileOutputStream(OUTPUTFILE));
doc.open();
PdfReader reader = new PdfReader(INPUTFILE);
int n = reader.getNumberOfPages();
System.out.println("No. of Pages :" +n);
for (int i = 1; i <= n; i++) {
        if (i == 1) {            
               // removed code for clarity
               PdfImportedPage page = writer.getImportedPage(reader, i);

               writer.addPage(page);
            }
}
like image 31
Jes Avatar answered Oct 03 '22 04:10

Jes


In My case PDF sample file was corrupted. upload new file it will works.

like image 27
tika Avatar answered Oct 03 '22 05:10

tika


I had the same error and I just changed my PdfReader from reading InputStreams to read Strings. So, it works perfectly with:

public static void doMerge(List<String> list, OutputStream outputStream)
    throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();

for (String in : list) {
    PdfReader reader = new PdfReader(in);
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
        document.newPage();
        // import the page from source pdf
        PdfImportedPage page = writer.getImportedPage(reader, i);
        // add the page to the destination pdf
        cb.addTemplate(page, 0, 0);
    }
}

outputStream.flush();
document.close();
outputStream.close();

}

*Originally I took this code from http://www.mindfiresolutions.com/Java-Merging-multiple-PDFs-into-a-single-PDF-using-iText-671.php

like image 35
imTachu Avatar answered Oct 03 '22 04:10

imTachu


Your pdf should start with %PDF.You can check it.My file was corrupted.

like image 21
Melahat Mindivanli Avatar answered Oct 03 '22 05:10

Melahat Mindivanli