【问题标题】:Error: Value cannot be null错误:值不能为空
【发布时间】:2015-11-07 01:21:17
【问题描述】:

我正在尝试使用 FreeSpire 将受保护的 PDF 转换为 XPS,然后再转换回 PDF,然后使用 iTextSharp 将它们合并。下面是我的代码 sn-p 用于转换各种文件。

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();
              }

问题是我在doc.LoadFromFile(Loan_list[f]); 中得到了一个Value cannot be null error。我有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();

我想了解我收到此错误的原因以及如何解决它。

【问题讨论】:

  • 在这一行string WorkDir = @"C:\Users\rwong\Desktop\PDF\Test"; 尝试将代码更改为以下string WorkDir = @"C:\Users\rwong\Desktop\PDF\Test\"; 看看是否可以解决问题
  • 不,它没有任何区别。我什至尝试在 LoadFromFile(Loan_list[f], FileFormat.PDF) 中添加另一个参数,但没有骰子
  • 你试过没有FileFormat.DOC参数吗?您是否尝试将文字路径放在第一个参数中?如果你做了其中一件事,它会起作用吗?
  • 抱歉,我不明白为什么FileFormat.Doc 会很重要,如果这不是我所关心的,但我还是尝试了它,但它没有用。文字字符串也不起作用。编辑:两个建议都给了我同样的错误。
  • 如果将文档的密码作为第二个参数传递呢?

标签: c# pdf-generation itextsharp xpsdocument spire.doc


【解决方案1】:

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

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

    Document document = new Document();
    //Load a Document in document Object
    document.SaveToFile("Sample.pdf", FileFormat.PDF);
    
  2. 你可以用 Stream 来做同样的事情

    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);
    

第二种方法很简单,但我建议您使用第一种方法,因为显而易见的原因是文档比流具有更好的可转换性。由于文档包含部分、段落、页面设置、文本、字体,因此需要进行更好或更准确的格式设置。

【讨论】:

  • 感谢您的意见!尝试使用您的第一个解决方案时,我没有SaveToFile 作为选项。你知道为什么吗?
  • 我相信link 会帮助你
猜你喜欢
  • 2014-01-30
  • 2017-12-16
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 2020-09-24
  • 2020-11-16
  • 2011-12-08
相关资源
最近更新 更多