We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Invalid pdf format:pdf head signature is not found! - ImportAnnotations

Hi,

I am using PDF.Kit to consolidate comments from different pdfs, I get the invalid pdf format: pdf head signature is not found when i use the filestream in ImportAnnotations method. For the same pdfs instead of using filestream if i pass the pdfs itself it works fine. Attached are the files i am using. Please let me know if i missed something. I am using the latest version 3.3.0.0 Aspose.Pdf.kit

//Code for using filestream:

string pdfsFolder = "C:\\AsposePDFs";
int NumofPDFs = 0;

if( Directory.Exists( pdfsFolder ) )
{
string[] files = Directory.GetFiles( pdfsFolder );
NumofPDFs = files.Length;
List pdfList = new List<FileStrea ( NumofPDFs );
string[] pdfCommentFiles = new string[ NumofPDFs ];
for( int i = 0; i <= NumofPDFs - 1; i++ )
{
FileStream inStream = new FileStream( files[ i ].ToString(), FileMode.Open );
pdfList.Add( inStream ) ;
}

byte[] consolidateCommentsPdf = ConsolidateComments( pdfList );
string fileName = "C:\\AsposePDFs\\ConsolidatedCommentPdf";

FileStream file = new FileStream(fileName, FileMode.Create);
file.Write( consolidateCommentsPdf, 0, consolidateCommentsPdf.Length );
file.Flush();
file.Close();

//Method

private byte[] ConsolidateComments( List pdfStream )
{
FileStream inputPDFStream = pdfStream[ 0 ];
FileStream outputPDFStream = pdfStream[ 0 ];
FileStream[] inStreams = new FileStream [ pdfStream.Count ];
int i = 0;
foreach( FileStream stream in pdfStream )
{
inStreams[i] = stream;
i++;
}
PdfContentEditor pdfContenteditor = new PdfContentEditor();
pdfContenteditor.BindPdf( inputPDFStream );
pdfContenteditor.DeleteAnnotations();
pdfContenteditor.Save( inputPDFStream );
pdfContenteditor.Save( outputPDFStream );

PdfAnnotationEditor pAnnEditor = new PdfAnnotationEditor();
Enum[] annotType = { AnnotationType.HightLight, AnnotationType.Stamp };
pAnnEditor.BindPdf( inputPDFStream );
pAnnEditor.ImportAnnotations( inStreams, annotType );
pAnnEditor.Save( outputPDFStream );

byte[] consolidatePDFbyteArray = new byte[ outputPDFStream.Length ];
outputPDFStream.Read( consolidatePDFbyteArray, 0, ( int )outputPDFStream.Length );
// Now we can close the stream
outputPDFStream.Close();

return consolidatePDFbyteArray;
}
}

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I have tested the scenario using the code snippet and have noticed that the issue is occurring because you are using the same FileStream object to read and write the contents. Please refer to the section where you are deleting the annotations. I am not sure why you are deleting the annotations. If we comment out that piece of code, invalid pdf format does not appear. In my test I am unable to consolidate the annotations. We are working over this issue and will provide you the code that can resolve your problem. We apologize for your inconvenience.

Hi,

Please try following code:

private void ConsolidateComments( List<FileStream> pdfStream,string out_fileName)
...{

FileStream inputPDFStream
= pdfStream[ 0 ];
MemoryStream tempStream
= new MemoryStream();
MemoryStream outputPDFStream
= new
MemoryStream();
FileStream[] inStreams
= new FileStream [ pdfStream.Count ];
FileStream out_fileStream
= new FileStream(out_fileName, FileMode.Create);
int i = 0;
foreach( FileStream stream in pdfStream )
...{
inStreams[i]
= stream;
i
++;
}

PdfContentEditor pdfContenteditor
= new PdfContentEditor();
pdfContenteditor.BindPdf( inputPDFStream );
pdfContenteditor.DeleteAnnotations();
pdfContenteditor.Save(tempStream);
inputPDFStream.Seek(
0, SeekOrigin.Begin);
PdfAnnotationEditor pAnnEditor
= new PdfAnnotationEditor();
Enum[] annotType
= ...{ AnnotationType.Text,AnnotationType.Caret};
pAnnEditor.BindPdf(tempStream);
pAnnEditor.ImportAnnotations( inStreams, annotType );
pAnnEditor.Save(out_fileStream);
out_fileStream.Close();
}

Thanks,