Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
4.00/5 (1 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:

C#
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:
C#
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:
C#
var reader = new iTextSharp.text.pdf.PdfReader(source);


Any help will be great... pleas :) Thanks!
Posted
Updated 22-Nov-16 21:59pm
Comments
syed2109 21-Dec-17 10:57am    
refer https://stackoverflow.com/a/12363773/2089963

1 solution

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.

:)
 
Share this answer
 
Comments
CHill60 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 25-Nov-13 2:24am    
Thanks CHill60. :)
Sumit Rastogi SRA 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 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 17-Dec-14 1:38am    
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900