找不到PDF标题签名 - 对此真的很困惑 [英] PDF header signature not found - Really confused about this

查看:82
本文介绍了找不到PDF标题签名 - 对此真的很困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我尝试了两个不同的代码片段,用于将doc转换为pdf,我得到了相同的错误`PDF header signature not found`这是我的代码:



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 :) Thanks!

推荐答案

即使我知道这是一个老线程...我将为此更新解决方案..

因为今天,我遇到了同样的问题,发现非常有趣。



我一直在使用 IPDF Sharp 从来没有遇到过这种类型的问题,但是今天我尝试通过相同的代码合并几个文件,我遇到了这个bug,



解决方案很简单。



PDF文件已损坏,我试图从目录中选择以获得合并。



即使上述解决方案看起来也很幼稚。但有时微小的事情和微小的问题足以动摇头脑并让开发人员感到沮丧。





我希望这会有所帮助一天,有一天。



:)
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.

:)


这篇关于找不到PDF标题签名 - 对此真的很困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆