c# – 错误:值不能为空

前端之家收集整理的这篇文章主要介绍了c# – 错误:值不能为空前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图将安全的PDF转换为XPS并使用FreeSpire返回到PDF,然后使用iTextSharp进行组合.以下是我的代码片段,用于转换各种文件.
  1. char[] delimiter = { '\\' };
  2. string WorkDir = @"C:\Users\rwong\Desktop\PDF\Test";
  3. Directory.SetCurrentDirectory(WorkDir);
  4. string[] SubWorkDir = Directory.GetDirectories(WorkDir);
  5. //convert items to PDF
  6. foreach (string subdir in SubWorkDir)
  7. {
  8. string[] Loan_list = Directory.GetFiles(subdir);
  9. for (int f = 0; f < Loan_list.Length - 1; f++)
  10. {
  11. if (Loan_list[f].EndsWith(".doc") || Loan_list[f].EndsWith(".DOC"))
  12. {
  13. Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
  14. doc.LoadFromFile(Loan_list[f],FileFormat.DOC);
  15. doc.SaveToFile((Path.ChangeExtension(Loan_list[f],".pdf")),FileFormat.PDF);
  16. doc.Close();
  17. }
  18. . //other extension cases
  19. .
  20. .
  21. else if (Loan_list[f].EndsWith(".pdf") || Loan_list[f].EndsWith(".PDF"))
  22. {
  23. PdfReader reader = new PdfReader(Loan_list[f]);
  24. bool PDFCheck = reader.IsOpenedWithFullPermissions;
  25. reader.Close();
  26. if (PDFCheck)
  27. {
  28. Console.WriteLine("{0}\\Full Permisions",Loan_list[f]);
  29. reader.Close();
  30. }
  31. else
  32. {
  33. Console.WriteLine("{0}\\Secured",Loan_list[f]);
  34. Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
  35. string path = Loan_List[f];
  36. doc.LoadFromFile(Loan_list[f]);
  37. doc.SaveToFile((Path.ChangeExtension(Loan_list[f],".xps")),FileFormat.XPS);
  38. doc.Close();
  39.  
  40. Spire.Pdf.PdfDocument doc2 = new Spire.Pdf.PdfDocument();
  41. doc2.LoadFromFile((Path.ChangeExtension(Loan_list[f],FileFormat.XPS);
  42. doc2.SaveToFile(Loan_list[f],FileFormat.PDF);
  43. doc2.Close();
  44. }

问题是我得到一个值不能为null的错误在doc.LoadFromFile(Loan_list [f]);我有字符串path = Loan_list [f];检查Loan_list [f]是否为空,但不是.我试图用名为path的变量替换Loan_list [f]参数,但是它也不会执行.我在较小规模的测试PDF转换工作(见下文)

  1. string PDFDoc = @"C:\Users\rwong\Desktop\Test\Test\Test.PDF";
  2. string XPSDoc = @"C:\Users\rwong\Desktop\Test\Test\Test.xps";
  3.  
  4. //Convert PDF file to XPS file
  5. PdfDocument doc = new PdfDocument();
  6. doc.LoadFromFile(PDFDoc);
  7. doc.SaveToFile(XPSDoc,FileFormat.XPS);
  8. doc.Close();
  9.  
  10. //Convert XPS file to PDF
  11. PdfDocument doc2 = new PdfDocument();
  12. doc2.LoadFromFile(XPSDoc,FileFormat.XPS);
  13. doc2.SaveToFile(PDFDoc,FileFormat.PDF);
  14. doc2.Close();

我想了解为什么我收到这个错误以及如何解决它.

解决方法

您将面临的问题将有2种解决方案.

>获取不在PDF文档中的文档对象中的文档.然后可能会尝试SaveToFile这样的东西

  1. Document document = new Document();
  2. //Load a Document in document Object
  3. document.SaveToFile("Sample.pdf",FileFormat.PDF);

>你可以使用Stream来做同样的事情

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

第二种方法是容易的,但是我建议您使用第一种方法,因为明显的原因,如文档将提供比流更好的可转换性.由于文档具有部分,段落,页面设置,文本,字体,需要做更好或精确的格式化所需的所有内容.

猜你在找的C#相关文章