System.IO.IOException: Invalid pdf format:pdf head signature is not found!

Hi Aspose,

I recieve an error 'System.IO.IOException: Invalid pdf format:pdf head signature is not found!'when I try to stamp different pages in a pdf.file .

In pageEditorKBO is a pdf file. I resize that and put it in a memorystream resizedKboStream.

Then I create a loop to walk through all the pages in resizedKboStream and stamp it page after page.

The error raises at stamper.Close();

What's wrong?

Code: (aantalPaginasKBO = number of pages)

MemoryStream resizedKboStream = new MemoryStream();
pageEditorKBO.Zoom = (float)0.88;
//Save de resized informatie in een stream
pageEditorKBO.Save(resizedKboStream);

//Stempel nu de resized PDF in de MasterTemplate
for (int i = 0; i < aantalPaginasKBO; i++)
{
//Zet de positie van de stream aan het begin
resizedKboStream.Position = 0;
Stamp stampKBO = new Stamp();
//Bind de PDF met de juiste pagina (i) aan de stempel
stampKBO.BindPdf(resizedKboStream, (i + 1));

//Stel de X,Y punten in, waar de linkerbovenhoek van de resized PDF moet komen
stampKBO.SetOrigin(20, 25);
//Geef de positie van de stempel aan. (De positie in de MasterTemplate.)
int[] pagesKBO = { (4 + i) };
stampKBO.Pages = pagesKBO;

//Stempel nu ook de 1e pagina van KBO
stamper.AddStamp(stampKBO);
}
stamper.Close();

Hi,

Thank you for considering Aspose.

Can you please provide your resource files and let us test it?

Hello Jaco,

I have tested the issue and I am able to reproduce the problem. The problem occurs when stampKBO.BindPdf(resizedKboStream, (i + 1)); is called. I have modified the code, please try it.

PdfPageEditor pageEditorKBO = new PdfPageEditor();
pageEditorKBO.BindPdf(@"C:\pdftest\wordfiles\07-0932_s.pdf");
MemoryStream resizedKboStream = new MemoryStream();
pageEditorKBO.Zoom = (float)0.88;
pageEditorKBO.Save(resizedKboStream);

PdfFileStamp stamper = new PdfFileStamp();
FileStream fs = new FileStream(@"C:\pdftest\wordfiles\07-0932_NewFile.pdf",FileMode.Create);
Stamp stampKBO = new Stamp();

stampKBO.BindLogo(new FormattedText("Hello Aspose.Pdf.Kit!"));
// array to hold the number of pages information, that need to be stamped
int[] pag = new int[100];

for (int i = 4; i < aantalPaginasKBO; i++)
pag[i] = i+1;

// specify the pages that need to be stamped
stampKBO.Pages = pag;
stampKBO.SetOrigin(20, 425);
stamper = new PdfFileStamp(resizedKboStream, fs);
stamper.AddStamp(stampKBO);
stamper.Close();
resizedKboStream.Close();
fs.Close();

If it does not satisfy your requirement, please feel free to contact.

Hi,

When I try your for-loop, it only stamps the last page in fs.

What I want is, to stamp different pages (who are in resizedKboStream) on different pages in the main Pdf.

Jaco Rebel

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

I am not sure what value you have assigned to the loop variable i. In my case I used

for (int i = 4; i < aantalPaginasKBO; i++)

where I started from page 4, and I had a pdf with 12 pages. I think you are using a Pdf with 5 pages or so. Please initialize the variable i value accordingly. In case the issue still persists, please share the Resource file, so that we can test the issue at our end.

Hi Nayyer,

Please, can you give me sample code how to stamp different pages in a pdf?

What I have:

A PDF file with empty pages.

If NumberOfEmptyPages = 5 Then I have to stamp 5 pages out of a resized pdf (in a stream) into the

empty pages. So in this case page 1 from the resized pdf (in a stream) must be stamped on the first empty page in the pdf file. Then page 2 from the resized pdf (in a stream) must be stamped on the second empty page in the pdf file and so on till all the 5 empty pages are filled with different stamps.

If NumberOfEmptyPages = 9 Then 9 empty pages must be stamped with 9 different pages from the resized pdf (in a stream) .

How can I do that?

Hello Jaco,

I am working over this issue, and will reply to you soon. We really appreciate your patience in this regard.

Hi,

The following code works:

PdfFileStamp stamper = new PdfFileStamp(@"C:\pdftest\wordfiles\Blank_8.pdf", @"C:\pdftest\wordfiles\8_Stamped.pdf");

for (int i = 1; i < 3; i++)
{

Stamp pdfFileStamp = new Stamp();
pdfFileStamp.BindPdf(@"C:\pdftest\wordfiles\07-0932_s.pdf", i); // Selected page from Pdf file, which will be added as stamp to input pdf
pdfFileStamp.Pages = new int[] { i }; // Page in Pdf file to which Stamp will be added
stamper.AddStamp(pdfFileStamp);
}
stamper.Close();

Hello Jaco,

Adding more to Tommy's code snippet, please use this code snippet, if which is same like the original code that you have shared, except, you have used memory stream and here I am using FileStream.

[C#]

PdfPageEditor pageEditorKBO = new PdfPageEditor();
// Bind the pdf to Pdf pageeditor class that need to be modifed
pageEditorKBO.BindPdf(@"C:\pdftest\wordfiles\07-0932_s.pdf");
// set the zoom factor to 0.88
pageEditorKBO.Zoom = (float)0.88;
//Save de resized informatie in een stream
pageEditorKBO.Save(@"C:\pdftest\wordfiles\07-0561_resized.pdf");

// instance of PdfFileStamp class, that will add the contents of Pdf file into input file and will generate the output file
PdfFileStamp stamper = new PdfFileStamp(@"C:\pdftest\wordfiles\Blank_8.pdf", @"C:\pdftest\wordfiles\8_Stamped.pdf");
// iterate through each page of Pdf file, whose pages need to be added as stamp.
for (int i = 1; i <= 5; i++) // it will iterate through 5 pages of Pdf file whose pages will be added as stamp
{
Stamp pdfFileStamp = new Stamp();
// Bind the pdf file whose zoom factor was set to .088, and specify the page number which will be added as stamp to input pdf.
pdfFileStamp.BindPdf(@"C:\pdftest\wordfiles\07-0561_resized.pdf", i);
// Page in the input Pdf file to which Stamp will be added
pdfFileStamp.Pages = new int[] { i };
// add the pdf file page as stamp
stamper.AddStamp(pdfFileStamp);
}
// close the stamp class instance.
stamper.Close();
// delete the file that was previously saved after its zoom factor was set to 0.88
File.Delete(@"C:\pdftest\wordfiles\07-0561_resized.pdf");