10

I'm trying to convert secured PDFs to XPS and back to PDF using FreeSpire and then combine them using iTextSharp. Below is my code snippet for converting various files.

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

The issue is I get a Value cannot be null error in doc.LoadFromFile(samplelist[f]);.I have the string path = sampleList[f]; to check if samplelist[f] was empty but it was not. I tried to replace the samplelist[f] parameter with the variable named path but it also does not go though. I tested the PDF conversion on a smaller scale it it worked (see below)

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

I would like to understand why I am getting this error and how to fix it.

LampPost
  • 856
  • 11
  • 30
  • in this line `string WorkDir = @"C:\Users\rwong\Desktop\PDF\Test";` try changing the code to the following `string WorkDir = @"C:\Users\rwong\Desktop\PDF\Test\";` and see if that corrects the problem – MethodMan Aug 13 '15 at 21:54
  • Nope, it did not make a difference. I even tried adding in the other parameter in LoadFromFile(Loan_list[f], FileFormat.PDF) but no dice – LampPost Aug 13 '15 at 21:58
  • Have you tried it without `FileFormat.DOC` parameter ? Have you tried putting the literal path inside the first parameter ? Does it work if you did one of those things ? – Orel Eraki Aug 13 '15 at 22:01
  • Sorry, I don't understand why the `FileFormat.Doc` would matter if that is not what I am concerned about but I tried it anyways and it did not work. Also the literal string did not work. Edit: Both suggestions gave me the same error. – LampPost Aug 13 '15 at 22:07
  • What if you pass the document's password as the second parameter? – Chris Haas Aug 14 '15 at 13:39
  • I do not have the password. The trick to bypass the password is to convert the PDF to XPS and then back to PDF. I have singled out the PDF that was giving me issues but I was able to convert it using a smaller version of the script. I am lead to believe that something in my code doesn't allow the PDF to be loaded. – LampPost Aug 14 '15 at 13:44
  • What version of Spire do you use? – Backs Aug 19 '15 at 03:29
  • I am currently using Spire 3.2 – LampPost Aug 19 '15 at 12:50
  • Try to update to new version https://www.nuget.org/packages/Spire.PDF/ it seemes, they fixed some exceptions, maybe it's your case – Backs Aug 20 '15 at 03:13
  • Ah I should have been more clear. I am using their free software called FreeSpire.Pdf that does not seem most up to date compared to their Spire.PDF. I'm going to guess that at this point, this is a package problem, and I am doing nothing wrong. My only concern is that if I do buy the package and I still get the same error, that would suck. – LampPost Aug 20 '15 at 12:32

1 Answers1

5

There would be 2 solutions for the problem you are facing.

  1. Get the Document in the Document Object not in PDFDocument. And then probably try to SaveToFile Something like this

    Document document = new Document();
    //Load a Document in document Object
    document.SaveToFile("Sample.pdf", FileFormat.PDF);
    
  2. You can use Stream for the same something like this

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

Second approach is the easy one, but I would recommend you to use the first one as for obvious reasons like document will give better convertability than stream. Since the document have section, paragraph, page setup, text, fonts everything which need to be required to do a better or exact formatting required.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
  • Thank you for your input! When trying to use your first solution I do not have `SaveToFile` as an option. Would you know why? – LampPost Aug 24 '15 at 13:07
  • I am sure this [link](http://www.e-iceblue.com/Knowledgebase/Spire.Doc/Spire.Doc-Program-Guide/Conversion.html) will gonna help you – Mohit S Aug 25 '15 at 01:56