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

Free Support Forum - aspose.com

Aspose.Pdf.Kit.PdfKitInvalidFormatException: Invalid pdf format:pdf head signature is not found!

We are running Aspose for .Net (APDNPDSE) and have begun receiving the following error. Aspose.Pdf.Kit.PdfKitInvalidFormatException: Invalid pdf format:pdf head signature is not found! at xeb116a323308e2f7.xa780c1549f927915.checkPdfHeader() at xeb116a323308e2f7.x7759a935a2782a02.readPdf() at Aspose.Pdf.Kit.PdfFileEditor.xb863edc37fb3922f(Stream[] xe82587bb558dba47) at Aspose.Pdf.Kit.PdfFileEditor.x16518bba0f7b77c9(Stream[] xe82587bb558dba47, Int32 x9542370f777d8414, Int32 x7ad47f71c8fff7a8, Boolean xe0d7f403537e028e, Boolean x417e7bb221928b31) at Aspose.Pdf.Kit.PdfFileEditor.xe2908e3f82c98080(Stream[] xbdeb1e89c00b5b84, Int32 x9542370f777d8414, Int32 x7ad47f71c8fff7a8, Stream xf823f0edaa261f3b, Boolean x417e7bb221928b31) at Aspose.Pdf.Kit.PdfFileEditor.Concatenate(Stream firstInputStream, Stream secInputStream, Stream outputStream) Has this type of error been reported and if so what is the resolution?

Thank you.


Hi,


Thanks for contacting support.

Can you please share the code snippet and resource PDF files which are generating this problem so that we can test the scenario in our environment. We are sorry for this inconvenience.

Enclosed below are the two methods involved. We are only seeing this issue occasionally and not for all instances where we create a PDF and merge the PDF with a cover page. One thing I have noticed which explains the error message which is “Aspose.Pdf.Kit.PdfKitInvalidFormatException: Invalid pdf format:pdf head signature is not found!” is that the “merged” PDF has 0 bytes which should not be the case. The same is true of the unmerged document as well. It appears that something may be corrupting the process or the files that are being merged.

FIRST PROCESS

'''

''' It will merger the original file with cover page file and return the merge file.

'''

'''

'''

'''

'''

Public Function mergeFiles(ByVal CoverPageFile As IContent, ByVal OriginalFile As IContent, ByVal filetitle As String, is_restricted As Boolean) As IContent Implements IPdfConverterMediator.mergeFiles

'Coverpage file

Dim CoverFile As TFile = DirectCast(CoverPageFile, TFile)

Dim pdf1 As New FileStream(CoverFile.getPhysicalPath, FileMode.Open)

'Original File

Dim Original As TFile = DirectCast(OriginalFile, TFile)

Dim pdf2 As New FileStream(Original.getPhysicalPath, FileMode.Open)

Dim output_filename As String = Path.GetFileNameWithoutExtension(filetitle) & ".pdf"

'Create output file stream

Dim outputPDFPath As String = cnf(Of String)(ECoreConfiguration.UploadDirectoryTmpFiles) & Path.GetFileNameWithoutExtension(Original.filename) & "_Merged.pdf"

Dim ofs As New FileStream(outputPDFPath, FileMode.Create)

Dim pdfEditor As New PdfFileEditor()

'Merge the two Document

Dim ret As Boolean = pdfEditor.Concatenate(pdf1, pdf2, ofs)

'Dispose the objects.

ofs.Close()

pdf1.Close()

pdf2.Close()

Dim FileInfo As New IO.FileInfo(outputPDFPath)

Dim fo As Aspose.Pdf.Kit.PdfFileInfo = New Aspose.Pdf.Kit.PdfFileInfo(outputPDFPath)

Dim totalPages As Integer = fo.NumberofPages

Dim outPutPath As String = GenerateTempFilePathInWordDir(FileInfo.Name)

'' We stamp image stating that document is IO RESTRICTED on coverpage & at everypage

If is_restricted Then

Dim conf As IContent = RetrieveMediator(Of IConfigurationContentFinder).GetConfigurationObject(EMetaTypes.IDMConfiguration)

Dim DoStamping As Boolean = conf.getValue(Of Boolean)(IDMConfiguration.stamp_io_restricted_image)

If DoStamping Then

Dim first_page_image As TFile = conf.getValue(Of TFile)(IDMConfiguration.first_page_image)

Dim other_page_image As TFile = conf.getValue(Of TFile)(IDMConfiguration.other_page_image)

Dim stamper As PdfFileStamp = New PdfFileStamp(outputPDFPath, outPutPath)

'Coverpage image insertion

Dim width As Single = fo.GetPageWidth(1)

Dim height As Single = fo.GetPageHeight(1)

Dim Stamp As Aspose.Pdf.Kit.Stamp = New Aspose.Pdf.Kit.Stamp()

Stamp.SetOrigin((width / 2) - 150, height - 73)

Stamp.SetImageSize(120, 30)

Stamp.IsBackground = True

Stamp.Pages = New Integer() {1}

Dim first_page_image_stream As New FileStream(first_page_image.getPhysicalPath, FileMode.Open, FileAccess.Read, FileShare.Read)

Stamp.BindImage(first_page_image_stream)

stamper.AddStamp(Stamp)

'Other page image insertion

Dim other_page_image_stream As New FileStream(other_page_image.getPhysicalPath, FileMode.Open, FileAccess.Read, FileShare.Read)

For i As Integer = 1 To totalPages - 1

Dim page_no As Integer = i + 1

width = fo.GetPageWidth(page_no)

height = fo.GetPageHeight(page_no)

Stamp = New Aspose.Pdf.Kit.Stamp()

Stamp.SetOrigin((width / 2) - 60, height - 30)

'Stamp.SetOrigin(width - 150, height - 40)

Stamp.SetImageSize(120, 15)

Stamp.IsBackground = True

Stamp.Pages = New Integer() {page_no}

Stamp.BindImage(other_page_image_stream)

stamper.AddStamp(Stamp)

' page bottom

Stamp = New Aspose.Pdf.Kit.Stamp()

Stamp.SetOrigin((width / 2) - 60, 50)

Stamp.SetImageSize(120, 15)

Stamp.IsBackground = True

Stamp.Pages = New Integer() {page_no}

Stamp.BindImage(other_page_image_stream)

stamper.AddStamp(Stamp)

Next

stamper.Close()

first_page_image_stream.Close()

other_page_image_stream.Close()

FileInfo = New IO.FileInfo(outPutPath)

End If

End If

Dim global_file_folder As IContent = icp.ContentDispatcher.GetContentsByMetaType(ECoreMetaTypes.GlobalFileFolder)(0)

Dim upload_file As IContent = CType(global_file_folder.CreateSubContent(ECoreMetaTypes.File), TFile)

Dim filemediator As IFileContentTypeMediator = upload_file.getMyMediator(Of IFileContentTypeMediator)()

filemediator.UploadFile(FileInfo)

upload_file.title = output_filename

upload_file.save()

If ret Then Return upload_file

Return Nothing

End Function

SECOND PROCESS

Public Sub insertCoverPage() Implements IIDMPdfConverter.insertCoverPage

Dim cover_page_path As String = Me.GetCoverPageTempalatePath(doc_version)

'If Cover page Template Parameter is missing then return nothing.

If String.IsNullOrEmpty(cover_page_path) Then Exit Sub

'Check for the Template file exists or not.

If Not File.Exists(cover_page_path) Then Exit Sub

Dim version As IDocumentVersion = doc_version

'Consider PDF file as Original file if one exists

Dim original_file As IContent = version.PDFFile

If original_file Is Nothing Then original_file = version.OriginalFile

Dim my_doc As IDocument = DirectCast(version.parent_content, IDocument)

Dim strFileTitle As String = original_file.title

Dim filecontent As IContent = Nothing

Dim docPrefix As String = cnf(Of String)(ECoreConfiguration.doc_prefix)

Dim version_number As String = version.getValue(Of String)(EDocumentProperties.version_number)

Dim version_watermark As String = String.Format("{0}{1} {2}", docPrefix, my_doc.uid, version_number)

Dim objConvertDoc As New PDFConverter

'' Stamp version UID in top center of page

filecontent = objConvertDoc.convertDocument(original_file, version_watermark)

Dim isSecureIDM As Boolean = False

If RetrieveMediator(Of IConfigurationContentFinder).GetConfigurationObject(EMetaTypes.IDMConfiguration).getValue(Of Boolean)(IDMConfiguration.secureIDM) Then

isSecureIDM = RetrieveMediator(Of IConfigurationContentFinder).GetConfigurationObject(EMetaTypes.IDMConfiguration).getValue(Of Boolean)(IDMConfiguration.secureIDM)

End If

'Add the watermark Symbol for the document.

filecontent = AddWaterMark(filecontent, isSecureIDM, version.version_status)

'when the cover page option Autogenerate cover page is checked the process,

'will go in to the if condition to create a coverpage and if obsolete it will watermark with coverpage,

'else it will go to the else condition and if find obsolete then it will watermark the document

'without coverpage

Dim merged_file As TFile

Dim strCoverpage As String = CreateCoverPageTemplate(cover_page_path, version)

Dim FileInfo As New IO.FileInfo(strCoverpage)

Dim global_file_folder As IContent = icp.ContentDispatcher.GetContentsByMetaType(ECoreMetaTypes.GlobalFileFolder)(0)

Dim upload_file As IContent = CType(global_file_folder.CreateSubContent(ECoreMetaTypes.File), TFile)

Dim filemediatorC As IFileContentTypeMediator = upload_file.getMyMediator(Of IFileContentTypeMediator)()

filemediatorC.UploadFile(FileInfo)

upload_file.save()

'Add the watermark Symbol to the cover page template.(First Page of the Coverpage)

upload_file = AddWaterMark(upload_file, isSecureIDM, version.version_status)

merged_file = CType(objConvertDoc.mergeFiles(upload_file, filecontent, strFileTitle, version.is_document_restricted), TFile)

If merged_file Is Nothing Then Exit Sub

'' store merged pdf into covered_page_file

' We forcably set the id on the merged file BEFORE we push it in so that the history does not turn up

' [I am nervous about making the id copy first generally as it could have other consequences]

merged_file.id = EDocumentVersionProperties.cover_page_file

merged_file.save()

version(EDocumentVersionProperties.cover_page_file) = merged_file

'Legcy(Support)

' if we don't have originaly_binary then copyo primary binary into it

Dim ob As IContent = version.getValue(Of IContent)(EDocumentProperties.original_binary)

If ob IsNot Nothing AndAlso original_file Is Nothing Then version(EDocumentVersionProperties.original_file) = ob

If ob Is Nothing AndAlso original_file Is Nothing AndAlso

version(EDocumentProperties.primary_binary) IsNot Nothing Then _

version(EDocumentVersionProperties.original_file) = version(EDocumentProperties.primary_binary)

version(EDocumentProperties.cover_page_date) = Now

version.save()

End Sub

Thank you for any assistance you may provide.

JakeORNL:

Enclosed below are the two methods involved. We are only
seeing this issue occasionally and not for all instances where we create a PDF
and merge the PDF with a cover page. One thing I have noticed which explains
the error message which is “Aspose.Pdf.Kit.PdfKitInvalidFormatException:
Invalid pdf format:pdf head signature is not found!” is that the “merged” PDF
has 0 bytes which should not be the case. The same is true of the unmerged
document as well. It appears that something may be corrupting the process or
the files that are being merged.<o:p></o:p>

FIRST PROCESS

'''

''' It will merger the original file with cover page file and return the merge file.

'''

'''

'''

'''

'''

Public Function mergeFiles(ByVal CoverPageFile As IContent, ByVal OriginalFile As IContent, ByVal filetitle As String, is_restricted As Boolean) As IContent Implements IPdfConverterMediator.mergeFiles

'Coverpage file

Dim CoverFile As TFile = DirectCast(CoverPageFile, TFile)

Dim pdf1 As New FileStream(CoverFile.getPhysicalPath, FileMode.Open)

'Original File

Dim Original As TFile = DirectCast(OriginalFile, TFile)

Dim pdf2 As New FileStream(Original.getPhysicalPath, FileMode.Open)

Dim output_filename As String = Path.GetFileNameWithoutExtension(filetitle) & ".pdf"

'Create output file stream

Dim outputPDFPath As String = cnf(Of String)(ECoreConfiguration.UploadDirectoryTmpFiles) & Path.GetFileNameWithoutExtension(Original.filename) & "_Merged.pdf"

Dim ofs As New FileStream(outputPDFPath, FileMode.Create)

Dim pdfEditor As New PdfFileEditor()

'Merge the two Document

Dim ret As Boolean = pdfEditor.Concatenate(pdf1, pdf2, ofs)

'Dispose the objects.

ofs.Close()

pdf1.Close()

pdf2.Close()

Dim FileInfo As New IO.FileInfo(outputPDFPath)

Dim fo As Aspose.Pdf.Kit.PdfFileInfo = New Aspose.Pdf.Kit.PdfFileInfo(outputPDFPath)

Dim totalPages As Integer = fo.NumberofPages

Dim outPutPath As String = GenerateTempFilePathInWordDir(FileInfo.Name)

'' We stamp image stating that document is IO RESTRICTED on coverpage & at everypage

If is_restricted Then

Dim conf As IContent = RetrieveMediator(Of IConfigurationContentFinder).GetConfigurationObject(EMetaTypes.IDMConfiguration)

Dim DoStamping As Boolean = conf.getValue(Of Boolean)(IDMConfiguration.stamp_io_restricted_image)

If DoStamping Then

Dim first_page_image As TFile = conf.getValue(Of TFile)(IDMConfiguration.first_page_image)

Dim other_page_image As TFile = conf.getValue(Of TFile)(IDMConfiguration.other_page_image)

Dim stamper As PdfFileStamp = New PdfFileStamp(outputPDFPath, outPutPath)

'Coverpage image insertion

Dim width As Single = fo.GetPageWidth(1)

Dim height As Single = fo.GetPageHeight(1)

Dim Stamp As Aspose.Pdf.Kit.Stamp = New Aspose.Pdf.Kit.Stamp()

Stamp.SetOrigin((width / 2) - 150, height - 73)

Stamp.SetImageSize(120, 30)

Stamp.IsBackground = True

Stamp.Pages = New Integer() {1}

Dim first_page_image_stream As New FileStream(first_page_image.getPhysicalPath, FileMode.Open, FileAccess.Read, FileShare.Read)

Stamp.BindImage(first_page_image_stream)

stamper.AddStamp(Stamp)

'Other page image insertion

Dim other_page_image_stream As New FileStream(other_page_image.getPhysicalPath, FileMode.Open, FileAccess.Read, FileShare.Read)

For i As Integer = 1 To totalPages - 1

Dim page_no As Integer = i + 1

width = fo.GetPageWidth(page_no)

height = fo.GetPageHeight(page_no)

Stamp = New Aspose.Pdf.Kit.Stamp()

Stamp.SetOrigin((width / 2) - 60, height - 30)

'Stamp.SetOrigin(width - 150, height - 40)

Stamp.SetImageSize(120, 15)

Stamp.IsBackground = True

Stamp.Pages = New Integer() {page_no}

Stamp.BindImage(other_page_image_stream)

stamper.AddStamp(Stamp)

' page bottom

Stamp = New Aspose.Pdf.Kit.Stamp()

Stamp.SetOrigin((width / 2) - 60, 50)

Stamp.SetImageSize(120, 15)

Stamp.IsBackground = True

Stamp.Pages = New Integer() {page_no}

Stamp.BindImage(other_page_image_stream)

stamper.AddStamp(Stamp)

Next

stamper.Close()

first_page_image_stream.Close()

other_page_image_stream.Close()

FileInfo = New IO.FileInfo(outPutPath)

End If

End If

Dim global_file_folder As IContent = icp.ContentDispatcher.GetContentsByMetaType(ECoreMetaTypes.GlobalFileFolder)(0)

Dim upload_file As IContent = CType(global_file_folder.CreateSubContent(ECoreMetaTypes.File), TFile)

Dim filemediator As IFileContentTypeMediator = upload_file.getMyMediator(Of IFileContentTypeMediator)()

filemediator.UploadFile(FileInfo)

upload_file.title = output_filename

upload_file.save()

If ret Then Return upload_file

Return Nothing

End Function

Hi Jake,

Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for .NET 9.7.0 in Visual Studio 2010 application running over Windows 7 (x64) and I am unable to notice any issue. I have used the same code snippet which you have shared (except for changes in file paths, change of namespace from Aspose.Pdf.Kit to Aspose.Pdf.Facades and hard coded the strings). As per my observations, _Merged.pdf and resultant PDF with images stamped over first and second page are properly being generated.

Please note that Aspose.Pdf.Kit for .NET has been obsolete as separate product since July-2012. Therefore we recommend you to migrate your code to latest version of MergedAPI of Aspose.Pdf for .NET. In case you still face the same problem, please share some sample project so that we can again try replicating the problem in our environment.

You may consider visiting the following link for further information on Migration from Aspose.Pdf.Kit