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
dvr2013293

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.

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

  Print Answers RSS
0 OriginalGriff 7,800
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,790


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 23 Nov 2013
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