Click here to Skip to main content
6,629,377 members and growing! (21,781 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

Programmatically Convert Documents to PDFs the Easy Way

By jkandiko

An easy way to create PDF documents from code
C# 2.0, Windows, .NET 1.1VS.NET2003, Dev
Posted:9 Feb 2007
Views:46,419
Bookmarked:62 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
11 votes for this article.
Popularity: 3.61 Rating: 3.47 out of 5
2 votes, 18.2%
1
1 vote, 9.1%
2
2 votes, 18.2%
3
4 votes, 36.4%
4
2 votes, 18.2%
5

Background

In my workplace, it is common for people to want documents in PDF format. The problem I was running into was that programming a PDF is no simple task. That, and I already had the documents in another format (Excel). Knowing that there were PDF print drivers, I decided to figure out how they worked. I noticed that almost all of them used Ghostscript, a program to convert PostScript to PDF format. The only problem with these print drivers was that it required user interaction. I couldn't allow this as I have to bulk print about 100 reports a month and having an end user specify where to save a file would just be too cumbersome... and I didn't like the nagging shareware graphics.

After eventually putting two and two together, I discovered that .NET could call GhostScript with the appropriate commands to generate a PDF. But my problem still lay in the fact that I didn't want to create my own printer driver. Enter virtual printers and file ports. In order for this code to work, you must create a virtual printer.

First you will want to download Ghostscript and install it.

Continue by adding a local printer and uncheck "Automatically detect and install my Plug and Play printer".

Create a new local port and enter "C:\output.ps" for the port name.

In order for GhostScript to correctly parse the PostScript, it must be set as the printer driver. You can find the printer driver for GhostScript in the GhostScript installation directory in the lib folder.

For the attached code to work, name the printer Ghostscript.

Using the Code

The attached application puts all the puzzles in place. First the application stores the current default printer. This will be used later to set the printer back.

public static string GetDefaultPrinterName(){
    PrintDocument pd = new PrintDocument();
    return pd.PrinterSettings.PrinterName;
}

Second, the Ghostscript printer that we set up earlier is set as the default so the user doesn't have to select a printer.

public static long SetDefaultPrinterName(string name){
    return SetDefaultPrinter(name);
}

Third, we call the print command on a file. The trick here for me was detecting when an Excel file was printed. I work primarily with Excel documents and needed a quick way to print out the entire workbook and not just the opened worksheet.

public static void CreatePdf(string action, string file, string directory){
    if (file.EndsWith("xls")){
        Excel.ApplicationClass excel = new Excel.ApplicationClass();
        excel.Visible = false;

        Excel.Workbook workbook = excel.Workbooks.Open(Path.Combine(directory, file),
			Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing);

        workbook.PrintOut(Type.Missing, Type.Missing, 1, false, Type.Missing, 
			false, Type.Missing, Type.Missing);

        excel.Quit();
    }else{
        Process p = new Process();
                
        p.StartInfo.FileName = file;
        p.StartInfo.Verb = action;
        p.StartInfo.WorkingDirectory = directory;
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        p.StartInfo.CreateNoWindow = true;
        Console.WriteLine("Starting process");
        p.Start();
        //p.Kill();
    }
    Console.WriteLine("Creating Pdf");
    CreatePdf(file);
}

Finally, we call the GhostScript executable, giving it the filename we want it outputted to and where to find the PostScript. In order to pass the executable a statement, we just redirect the input to a command we create and we also grab the output.

private static string CreatePdf(string fileName){
    string command = "gswin32c -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=\"" + 
			fileName + ".pdf\"  -fc:\\output.ps";

    Console.WriteLine(command);
    Process p = new Process();

    StreamWriter sw;
    StreamReader sr;
            
    ProcessStartInfo info = new ProcessStartInfo("cmd");
    info.WorkingDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
    Console.WriteLine("Current directory: " + info.WorkingDirectory);
    info.CreateNoWindow = true;
    info.UseShellExecute = false;
            
    info.RedirectStandardInput = true;
    info.RedirectStandardOutput = true;
            
    p.StartInfo = info;
    p.Start();
            
    sw = p.StandardInput;
    sr = p.StandardOutput;
    sw.AutoFlush = true;
            
    sw.WriteLine(command);
            
    sw.Close();
            
    string ret = sr.ReadToEnd();

    Console.WriteLine(ret);
    return ret;
}

After GhostScript runs, we just set the printer back to the previous default and the user is none the wiser.

try{
    Printers.SetDefaultPrinterName(currentPrinterName);
}catch{}

This application can be used for quick PDF creation, but mostly it is for developers looking for an example of quickly creating PDFs without the hassle of programming in the PDF SDK. When running this application, place the gswin32c executable in the same directory. For further information on optimizing PDFs and doing all sorts of other crazy things, look into the GhostScript documentation and change the parameters specified. Have fun!

Points of Interest

The neat thing about this application is that it requires no extra work for the developer to format the document into a PDF. The way the document appears on the screen in its native application is how it is printed to PDF.

Things to Remember

Make sure to set up a virtual printer. If you modify where the file port goes, make sure to update the code.

Make sure to place the gswin32c in the same directory as the executing application.

It also looks like GhostScript needs to be installed on the machine. Find the 8.54 version here.

History

  • 9th February, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

jkandiko


Member

Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 47 (Total in Forum: 47) (Refresh)FirstPrevNext
Questiongenerates Output.ps file PinmemberLekha_J10:25 27 Mar '09  
GeneralXLVIEW executes but no PS File generated PinmemberMember 304588414:38 12 Mar '09  
GeneralRe: XLVIEW executes but no PS File generated Pinmemberjkandiko4:29 13 Mar '09  
GeneralThanks Pinmemberthohoff3:36 4 Feb '09  
GeneralHow to pass the arguments to the console application Pinmembergeetha naidu19:27 9 Jul '08  
GeneralURgent: Pdf generation Pinmembergeetha naidu20:20 8 Jul '08  
Questionwhat kind of manufacturer and printer I must chose in step "Install Printer Software" of "Add Printer Wizad"? Pinmembertaihip18:16 22 Nov '07  
AnswerRe: what kind of manufacturer and printer I must chose in step "Install Printer Software" of "Add Printer Wizad"? Pinmemberjkandiko4:52 27 Nov '07  
QuestionRe: what kind of manufacturer and printer I must chose in step "Install Printer Software" of "Add Printer Wizad"? Pinmembertaihip15:24 27 Nov '07  
AnswerRe: what kind of manufacturer and printer I must chose in step "Install Printer Software" of "Add Printer Wizad"? Pinmemberjkandiko4:32 28 Nov '07  
GeneralRe: what kind of manufacturer and printer I must chose in step "Install Printer Software" of "Add Printer Wizad"? Pinmembertaihip15:27 28 Nov '07  
Questionproblem with the executions the aplication Pinmemberradkeck212:35 4 Sep '07  
AnswerRe: problem with the executions the aplication Pinmemberjkandiko12:37 4 Sep '07  
GeneralRe: problem with the executions the aplication Pinmemberradkeck212:44 4 Sep '07  
GeneralRe: problem with the executions the aplication Pinmemberradkeck212:45 4 Sep '07  
GeneralRe: problem with the executions the aplication Pinmemberjkandiko12:47 4 Sep '07  
GeneralRe: problem with the executions the aplication Pinmemberradkeck212:55 4 Sep '07  
GeneralRe: problem with the executions the aplication Pinmemberradkeck222:11 4 Sep '07  
GeneralRe: problem with the executions the aplication Pinmemberjkandiko4:48 5 Sep '07  
GeneralHi, Urgent. I want to save excel sheets as PDF Files Pinmemberpravin parmar8:54 1 Sep '07  
GeneralRe: Hi, Urgent. I want to save excel sheets as PDF Files Pinmemberjkandiko4:36 4 Sep '07  
GeneralWord Document Extension Pinmemberskeynan8:19 24 Mar '07  
GeneralRe: Word Document Extension Pinmemberjkandiko5:30 26 Mar '07  
GeneralHow can we give file to be converted Pinmemberakjal19:20 5 Mar '07  
GeneralRe: How can we give file to be converted Pinmemberjkandiko4:37 6 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Feb 2007
Editor: Deeksha Shenoy
Copyright 2007 by jkandiko
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project