首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于文件位置的字符串的值为零,但存储的值表示它不是

用于文件位置的字符串的值为零,但存储的值表示它不是
EN

Stack Overflow用户
提问于 2015-08-13 21:47:37
回答 1查看 2.6K关注 0票数 10

我正在尝试使用FreeSpire将安全的PDF转换为XPS,然后返回到PDF,然后使用iTextSharp将它们组合起来。下面是我转换各种文件的代码片段。

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

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

问题是我得到了一个Value cannot be null error in doc.LoadFromFile(samplelist[f]);.I,有string path = sampleList[f];来检查samplelistf是否是空的,但它不是。我试图将samplelist[f]参数替换为名为path的变量,但它也没有执行。我测试了PDF转换的一个更小的规模,它的工作(见下文)

代码语言:javascript
运行
复制
string PDFDoc = @"C:\Users\****\Desktop\Test\Test\Test.PDF";
string XPSDoc = @"C:\Users\****\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();

我想了解为什么我要得到这个错误,以及如何修复它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-24 03:05:19

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

  1. Document对象中获取文档,而不是在PDFDocument中。然后可能会尝试SaveToFile这样的东西 Document =新文档();//在document对象document.SaveToFile("Sample.pdf",FileFormat.PDF)中加载文档;
  2. 您可以使用Stream进行类似的操作 PdfDocument doc =新的PdfDocument();//从流加载PDF文件。FileStream from_stream = File.OpenRead( Loan_listf );//确保Loan_listf是具有扩展名的文件的完整路径。doc.LoadFromStream(from_stream);//保存PDF文档。doc.SaveToFile(Loan_listf + ".pdf",FileFormat.PDF);

第二种方法很简单,但我建议您使用第一种方法,因为很明显,像文档这样的方法比流具有更好的可转换性。由于文档有部分、段落、页面设置、文本和字体,所以需要做更好或准确的格式设置所需的所有内容。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31998994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档