我正在尝试使用FreeSpire将安全的PDF转换为XPS,然后返回到PDF,然后使用iTextSharp将它们组合起来。下面是我转换各种文件的代码片段。
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转换的一个更小的规模,它的工作(见下文)
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();
我想了解为什么我要得到这个错误,以及如何修复它。
发布于 2015-08-24 03:05:19
对于您所面临的问题,将有两个解决方案。
Document
对象中获取文档,而不是在PDFDocument
中。然后可能会尝试SaveToFile
这样的东西
Document =新文档();//在document对象document.SaveToFile("Sample.pdf",FileFormat.PDF)中加载文档;第二种方法很简单,但我建议您使用第一种方法,因为很明显,像文档这样的方法比流具有更好的可转换性。由于文档有部分、段落、页面设置、文本和字体,所以需要做更好或准确的格式设置所需的所有内容。
https://stackoverflow.com/questions/31998994
复制相似问题