2015-08-13 125 views
10

我试图将安全的PDF转换为XPS并使用FreeSpire返回到PDF,然后使用iTextSharp将它们组合在一起。以下是我用于转换各种文件的代码片段。错误:值不能为空

char[] delimiter = { '\\' }; 
string WorkDir = @"C:\Users\rwong\Desktop\PDF\Test"; 
Directory.SetCurrentDirectory(WorkDir); 
string[] SubWorkDir = Directory.GetDirectories(WorkDir); 
//convert items to PDF 
foreach (string subdir in SubWorkDir) 
{ 
    string[] Loan_list = Directory.GetFiles(subdir); 
    for (int f = 0; f < Loan_list.Length - 1; f++) 
    { 
     if (Loan_list[f].EndsWith(".doc") || Loan_list[f].EndsWith(".DOC")) 
     { 
      Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument(); 
      doc.LoadFromFile(Loan_list[f], FileFormat.DOC); 
      doc.SaveToFile((Path.ChangeExtension(Loan_list[f],".pdf")), FileFormat.PDF); 
      doc.Close(); 
     } 
     . //other extension cases 
     . 
     . 
     else if (Loan_list[f].EndsWith(".pdf") || Loan_list[f].EndsWith(".PDF")) 
     { 
      PdfReader reader = new PdfReader(Loan_list[f]); 
      bool PDFCheck = reader.IsOpenedWithFullPermissions; 
      reader.Close(); 
      if (PDFCheck) 
      { 
       Console.WriteLine("{0}\\Full Permisions", Loan_list[f]); 
       reader.Close(); 
      } 
      else 
      { 
       Console.WriteLine("{0}\\Secured", Loan_list[f]); 
       Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument(); 
       string path = Loan_List[f]; 
       doc.LoadFromFile(Loan_list[f]); 
       doc.SaveToFile((Path.ChangeExtension(Loan_list[f], ".xps")), FileFormat.XPS); 
       doc.Close(); 

       Spire.Pdf.PdfDocument doc2 = new Spire.Pdf.PdfDocument(); 
       doc2.LoadFromFile((Path.ChangeExtension(Loan_list[f], ".xps")), FileFormat.XPS); 
       doc2.SaveToFile(Loan_list[f], FileFormat.PDF); 
       doc2.Close(); 
       } 

的问题是,我得到一个Value cannot be null errordoc.LoadFromFile(Loan_list[f]);。我有string path = Loan_list[f];检查Loan_list [F]是空的,但事实并非如此。我试图用名为path的变量替换Loan_list[f]参数,但它也不会去。我测试规模较小但它的工作的PDF转换(见下文)

string PDFDoc = @"C:\Users\rwong\Desktop\Test\Test\Test.PDF"; 
string XPSDoc = @"C:\Users\rwong\Desktop\Test\Test\Test.xps"; 

//Convert PDF file to XPS file 
PdfDocument doc = new PdfDocument(); 
doc.LoadFromFile(PDFDoc); 
doc.SaveToFile(XPSDoc, FileFormat.XPS); 
doc.Close(); 

//Convert XPS file to PDF 
PdfDocument doc2 = new PdfDocument(); 
doc2.LoadFromFile(XPSDoc, FileFormat.XPS); 
doc2.SaveToFile(PDFDoc, FileFormat.PDF); 
doc2.Close(); 

我想知道为什么我收到此错误,以及如何解决它。

+0

'string WorkDir = @“C:\ Users \ rwong \ Desktop \ PDF \ Test”;'尝试将代码更改为以下内容 'string WorkDir = @“C:\ Users \ rwong \ Desktop \ PDF \ Test \“;'看看是否可以解决问题 – MethodMan

+0

不是,它没有什么区别。我甚至尝试在LoadFromFile(Loan_list [f],FileFormat.PDF)中添加另一个参数,但没有骰子 – LampPost

+0

您是否尝试过不使用FileFormat.DOC参数?你有没有尝试把第一个参数的文字路径?如果你做了这些事情之一,它会起作用吗? –

回答

4

对于您面临的问题,会有2种解决方案。

  1. 获取文档Document对象不在PDFDocument。然后可能会尝试SaveToFile像这样的事情

    Document document = new Document(); 
    //Load a Document in document Object 
    document.SaveToFile("Sample.pdf", FileFormat.PDF); 
    
  2. 可以使用流为同一像这样

    PdfDocument doc = new PdfDocument(); 
    //Load PDF file from stream. 
    FileStream from_stream = File.OpenRead(Loan_list[f]); 
    //Make sure the Loan_list[f] is the complete path of the file with extension. 
    doc.LoadFromStream(from_stream); 
    //Save the PDF document. 
    doc.SaveToFile(Loan_list[f] + ".pdf",FileFormat.PDF); 
    

第二种方法是最简单的一个,但我会建议你由于显而易见的原因使用第一个,比如文档会给出比流更好的可转换性。由于该文档具有部分,段落,页面设置,文本,字体等所有需要做的更好或更精确的格式要求。

+0

感谢您的输入!在尝试使用第一个解决方案时,我没有'SaveToFile'作为选项。你知道为什么吗? – LampPost

+0

我确信这[链接](http://www.e-iceblue.com/Knowledgebase/Spire.Doc/Spire.Doc-Program-Guide/Conversion.html)会帮助你 –