【发布时间】: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