Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
So i have tried two different pieces of code for converting a doc to a pdf and i am getting the same error `PDF header signature not found` this is my code that i have:
 
private void Word2PDF()
        {
            //Create a new Microsoft Word application object
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
 
            //Adding dummy value because c# doesn't have optional arguments
            object oMissing = System.Reflection.Missing.Value;
 
            //Getting list of word files in specified directory
            DirectoryInfo dirInfo = new DirectoryInfo("C:\\TestFilestore\\");
            FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
            word.Visible = false;
            word.ScreenUpdating = false;
 
            foreach (FileInfo wordFile in wordFiles)
            {
                //Cast as object for word open method
                Object filename = (Object)wordFile.FullName;
 
                Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref filename, ref oMissing,
                                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                doc.Activate();
 
                object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
                object fileFormat = WdSaveFormat.wdFormatPDF;
 
                //Save document into pdf format
                doc.SaveAs(ref outputFileName,
                    ref fileFormat, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);
 
                //close the word document, but leave the word application open.
                //doc has to be cast to type_document so that it will find the correct close method.
                object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                doc = null;
            }
 
            //word has to be case to type_application so that it will find the correct quit method.
            ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
            word = null;
 
        }
 
And here is where it is breaking:
static public void CopyPages(string source, string dest)
        {
            var reader = new iTextSharp.text.pdf.PdfReader(source);
            using (FileStream fs = new FileStream(dest, FileMode.Create, FileAccess.Write, FileShare.None))
            {
 
                iTextSharp.text.Document doc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(1));
                {
                    //Use a PdfCopy to duplicate each page
                    iTextSharp.text.pdf.PdfCopy copy = new iTextSharp.text.pdf.PdfCopy(doc, fs);
                    {
                        doc.Open();
                        copy.SetLinearPageMode();
                        for (int i = 1; i <= reader.NumberOfPages; i++)
                        {
                            copy.AddPage(copy.GetImportedPage(reader, i));
                        }
                        //Reorder pages
                        //copy.ReorderPages(new int[] { 2, 1 });
                        doc.Close();
                    }
                }
            }
        }
 
This is the line that it breaks:
 var reader = new iTextSharp.text.pdf.PdfReader(source);
 
Any help will be great... pleas Smile | :) Thanks!
Posted 17-Feb-13 16:37pm
dvr2013298

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Even I know That this is an old thread... I am going to update a solution for this..
Because Today, I faced the same problem and that was found very interestingly.

I have been using IPDF Sharp since a long and have never faced this type of issue, but when today I tried to merge few files through the same code, I faced this bug,
 
Solution was pretty simple.
 
PDF file was corrupt which I was trying to pick from the directory to get merge.
 
even the above solution seems childish. but some times tiny things and tiny issues are enough to shake the heads and frustrate the developers.
 

SO I hope this will help some one , some day.
 
Smile | :)
  Permalink  
Comments
CHill60 at 23-Nov-13 7:32am
   
Might have been an oldish thread but it was still unanswered ... and you are right... sometimes it's the silly things that catch us out
VICK at 25-Nov-13 2:24am
   
Thanks CHill60. :)
Sumit Rastogi SRA at 5-Feb-14 5:39am
   
I am getting same issue -"pdf header signature not found" .But I am sure about it What pdf file I am using that is not corrupt." Please anyone can help me for this issue.
VICK at 7-Feb-14 0:08am
   
Have you tried debugging your code and found the line of code on which getting exception???
If you are using IPDF sharp and getting exception while your code tried to pick the template, than you should check the template PDF file by opening. It might have been gone corrupted and hence causing this issue to occur.So replacing it would be solving your problem.
Member 11316500 at 2 days ago
   
I am also using ItextSharp and even I am getting the same error at the line of PdfReader initialization -

using (Stream pdfStream = [HttpPostedFile from File upload control].InputStream)
{
pdfStream.Position = 0;
using (iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfStream))
{
}
}

Here the pdfStream is a Stream object created from the InputStream of FileUpload control.

The same code is running fine in console application when I am reading the data from a FileStream object as given below.

using (Stream pdfStream = new FileStream(pData.Files[0], FileMode.Open))
{
iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfStream);
}

Don't really know if reading from a posted file InputStream is an issue over FileStream or not (which any way is not possible to be converted from postedfile's inputstream). I am really going nuts..!!
The PDF file is not corrupted as the same file is getting processed very well in console application with the code shown above in the second place.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 280
1 Sergey Alexandrovich Kryukov 279
2 CPallini 205
3 Maciej Los 197
4 Afzaal Ahmad Zeeshan 160
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 17 Dec 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100