Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Ok, so I already have a small console program written (full code can be seen here) for this task. Basically, what it does, is the user grabs a CSV that is full of filenames, then chooses where to save them, and the program then runs through and creates a file based on each string in the CSV. What I have already is this:

// "newFiles" is a list of all the filenames taken from the CSV
foreach (string s in newFiles)
{
    try
    {
        StreamWriter sw;
        // eg. filename = "C:\\FolderName\\Filename.pdf"
        string fileName = folderName + "\\" + s;

        sw = File.CreateText(fileName);
        sw.WriteLine("Place holder file for Hard Copy entry");
        sw.Close();
    }
    catch (Exception e) { Console.WriteLine("File not created - " + e.Message); }
}

However, the problem is that the file name (s) is a full file name, including the extension. So in the case of files such as a .pdf, this process does not create them in the correct format. This essentially just creates a .txt file, and changes the extension. Therefore, this cannot even be opened.

If I can instead create a .pdf file that is in the correct format, I can use that file without having to replace it due to it being in the wrong format.

I was instead thinking of perhaps doing an "open - create new - save as" procedure instead, but I am not entirely sure of how I should be doing this.

share|improve this question

closed as unclear what you're asking by svick, scriptin, Snowman, Doc Brown, gnat Mar 5 at 21:16

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
I'm not sure I fully understand your question. Do you want to create a new (empty) PDF file for each selected filename? – JDT Mar 4 at 10:45
1  
As you've already suspected, just changing the extension of a .txt file to PDF doesn't make it a Portable Document File. You need to use software that actually creates PDF files, as suggested by ManoDestra's answer below. – Robert Harvey Mar 4 at 17:33
    
@JDT Yes that is the case. If I can create an empty, readable pdf, then I can use it for whenever I might want to change it, rather than having to manually create a new one and replace the old file. – Ben Mar 5 at 6:16
    
Could you put that in the original question to make it clear? I'll vote to reopen it. – JDT Mar 6 at 9:28
up vote 3 down vote accepted

You could be looking at some libraries that manage these CSV/PDF operations for you...

CSVReader: http://www.csvreader.com
SmartExcel: https://sourceforge.net/projects/smartexcel
PDFSharp: http://www.pdfsharp.com/PDFsharp
iTextSharp: https://sourceforge.net/projects/itextsharp

share|improve this answer

Something along the lines of

foreach (string s in newFiles)
{
    try
    {
        StreamWriter sw;
        string fileName 
           = Path.Combine(folderName,Path.GetFileNameWithoutExtension(s) + ".pdf);
    //....
}

should give you the filenames you want. However, I hope you are aware that changing the suffix alone is not sufficient to convert a csv file into a PDF file? For that task, you need a PDF library like iTextSharp, see this SO post, for example.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.