Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Oct 13, 2014 8:58 pm

Hey,

I would like to know if it is possible to convert all doc and docx files (don't know the names) from one folder and transfer them to the other folder as a PDF format. One doc = one PDF and so on.

I used the following code (please see below).
I get error (NullReferenceException was unhandled) on:
doc.SaveToFile(fileName, FileFormat.PDF);

Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Spire.Doc;


namespace Convert
{
    class Program
    {
        static void Main(string[] args)
        {
            string dir = @"C:\Source\";
            string des = @"C:\Destination\";

            string[] docs = Directory.GetFiles(dir, "*.txt");
            string[] docxs = Directory.GetFiles(dir, "*.rtf");


            foreach (string fileName in docs)
            {
                Document doc = new Document();

                var newPath = Path.ChangeExtension(fileName, "pdf");
                var savePath = Path.Combine(des, Path.GetFileName(newPath));
                doc.SaveToFile(fileName, FileFormat.PDF);
             


            }
            foreach (string fileName in docxs)
            {
                Document docx = new Document();
                var newPath = Path.ChangeExtension(fileName, "pdf");
                var savePath = Path.Combine(des, Path.GetFileName(newPath));
                docx.SaveToFile(fileName, FileFormat.PDF);
             
            }
        }
    }
}



Thank you for any help.

Lukasz1989rr
 
Posts: 1
Joined: Mon Oct 13, 2014 7:07 pm

Tue Oct 14, 2014 2:16 am

Hello,

Thanks for your inquiry.
Absolutely, our product supports the feature to to convert all doc and docx files from one folder and transfer them to the other folder as a PDF format. I have modify your codes accordingly for your reference.
Code: Select all
        string dir = @"C:\Source\";
        string des = @"C:\Destination\";
        string[] docs = Directory.GetFiles(dir, "*.txt");
        string[] docxs = Directory.GetFiles(dir, "*.rtf");
        foreach (string fileName in docs)
        {
            Document doc = new Document();
            using(TextReader tr=new StreamReader(fileName))
            {
                doc.LoadText(tr);
            }
           
            var newPath = Path.ChangeExtension(fileName, "pdf");
            var savePath = Path.Combine(des, Path.GetFileName(newPath));
            doc.SaveToFile(savePath, FileFormat.PDF);

        }
        foreach (string fileName in docxs)
        {
            Document docx = new Document();
            docx.LoadFromFile(fileName);
            var newPath = Path.ChangeExtension(fileName, "pdf");
            var savePath = Path.Combine(des, Path.GetFileName(newPath));
            docx.SaveToFile(savePath, FileFormat.PDF);

        }

If there are any questions, welcome to get it back to us.
Sincerely,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Thu Oct 16, 2014 9:24 am

Hello,

Has the issue been resolved? Could you please give us some feedback if convenience?

If there are any questions, welcome to get it back to us.

Thanks,
Gary
E-iceblue support team
User avatar

Gary.zhang
 
Posts: 1380
Joined: Thu Apr 04, 2013 1:30 am

Wed Mar 13, 2019 2:32 am

i got the same problem. And the answer u posted is only getting 1 file from the folder. And a problem on saving it on the NEW FOLDER

Fool
 
Posts: 1
Joined: Wed Mar 13, 2019 2:24 am

Wed Mar 13, 2019 10:05 am

Hello Fool,

Thanks for your post.
Sorry that I am not quite sure your requirement by “And the answer u posted is only getting 1 file from the folder”, please provide more details about your requirement to help us better understand. Then we will look into it accordingly. Besides, as for your encountered problem, please provide your full testing code as well as your input files to help us do a further investigation. You could send them to us via email (support@e-iceblue.com). Thanks in advance.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Mon Mar 18, 2019 10:01 am

Hello Fool,

Greetings from E-iceblue.
Could you let us know how is the issue going? Thanks in advance for your valuable feedback and time.

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1261
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.Doc