Thank you.
Hi,
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