Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends
i need a Code For Printing Multiple PDF Files From Folder Without opening the Files using c# Winforms.

pls someone help me.......
Posted
Updated 12-Jun-16 11:59am
Comments
OriginalGriff 22-Nov-12 3:30am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
In order to print the file, some application needs to open it...it may be yours, it may be a PDF reader. But something has to, or it can't get at the data to format it. What are you trying to achieve?
Use the "Improve question" widget to edit your question and provide better information.
CHAITANYA KIRAN KASANI 5-Dec-12 7:17am    
Is This Is Not A Question? But I Get The Answer SuccessFully.
stefbauer 22-Nov-12 11:34am    
found something similar in microsoft forums:
link
CHAITANYA KIRAN KASANI 23-Nov-12 3:40am    
thanQ stef for ur suggest....i will refer it
[no name] 22-Nov-12 21:57pm    
I have a try but I must load the document in the Form to have the contents and then print. Maybe you need C# PDF Viewer Components to realize.

The following code will search for all files in a give directory, use a method call PrintPDFs to automatically send the file to the default printer on the machine that this code is run on.

It will use adobe's ability to silently print a PDF, it will still open the files but i've also included code called FindAndKillProcess which will close adobe.

Without spending tons for a more sophisticated component this is going to be...probably at least...one of the easier options you'll have.

If you look at itextsharp or pdfsharp there are options to print the PDF that way which is the way i ended up going but the code below should work.

C#
string[] files = Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
    PrintPDFs(file);
}

public static Boolean PrintPDFs(string pdfFileName)
{
    try
    {
        Process proc = new Process();
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        proc.StartInfo.Verb = "print";

        //Get application path will get default application for given file type ("pdf")
        //This will allow you to not care if its adobe reader 10 or adobe acrobat.
        proc.StartInfo.FileName = IOUtils.GetApplicationPath(pdfFileName, "pdf");
        proc.StartInfo.Arguments = String.Format(@"/p /h {0}", pdfFileName);
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.CreateNoWindow = true;

        proc.Start();
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        if (proc.HasExited == false)
        {
            proc.WaitForExit(10000);
        }

        proc.EnableRaisingEvents = true;

        proc.Close();
        FindAndKillProcess("AcroRd32");
        return true;
    }
    catch
    {
        return false;
    }
}


public static bool FindAndKillProcess(string name)
{
    foreach (Process clsProcess in Process.GetProcesses())
    {
        if (clsProcess.ProcessName.StartsWith(name))
        {
            clsProcess.Kill();
            return true;
        }
    }
    return false;
}
 
Share this answer
 
Comments
CHAITANYA KIRAN KASANI 23-Nov-12 3:16am    
ThanQ for ur code.
But the Error-"The name 'IOUtils' does not exist in the current context"...How To Use The Class IOUtils.
David_Wimbley 23-Nov-12 10:10am    
all that is is a method that will dynamically find the default application for pdf's. You can just hard code "C:\program files\adobe\pdf\acrd32.exe" or whatever your PDF application is and it should compile then.
CHAITANYA KIRAN KASANI 24-Nov-12 5:32am    
ThanQ Plyswthsqurles .....its working....Thanks A Lot
David_Wimbley 26-Nov-12 18:52pm    
Glad to hear and happy to help!
CHAITANYA KIRAN KASANI 30-Nov-12 23:32pm    
Hi Good Day...
i Have one bug from the above code....
i had took

string path1= method1();//it is string method that returns string
string path2= method2();
string path3= method3();
string path4= method4();
string path5= method5();
string path6= method6();
//Here method1(),method2()...method6() are string methods That returns //string.Each String Have Different File Path
string[] files = { path1,path2,path3,path4,path5,path6 };
Here It Is Printing Alternatively By Printing One Path PDF File and Leaving Another Path PDF File..But While in Debug Mode It Is Printing All The PDF Files Successfully.
pls Help Me...
I used iTextSharp for this, other codes where from others

C#
private void CombineMultiplePDFs(string[] sourceDir, string targetPath)
        
        {
            try
            {
                // step 1: creation of a document-object
                Document document = new Document();
                // step 2: we create a writer that listens to the document
                PdfCopy writer = new PdfCopy(document, new FileStream(targetPath, FileMode.Create));
                if (writer == null)
                {
                    return;
                }
                // step 3: we open the document
                document.Open();
                foreach (string fileName in sourceDir)
                {
                    //first we validate if pdf file is valid
                         
                    string extension = Path.GetExtension(fileName);
                    if (extension == ".pdf")
                    {
                        if (IsValidPdf(fileName) != true)
                        {
                            
                        }
                        else
                        {
                            // we create a reader for a certain document
                            //PdfReader.unethicalreading = true;
                            //unlockPdf(fileName);
                            PdfReader reader = new PdfReader(fileName);
                            rtbMessage.Text += "\n" + Path.GetFileName(fileName) + "  -   Added!";
                            PdfReader.unethicalreading = true;
                            
                            reader.ConsolidateNamedDestinations();
                            // step 4: we add content
                            for (int i = 1; i <= reader.NumberOfPages; i++)
                            {
                                PdfImportedPage page = writer.GetImportedPage(reader, i);
                                writer.AddPage(page);
                            }
                            PRAcroForm form = reader.AcroForm;
                            if (form != null)
                            {
                                writer.AddDocument(reader);
                            }
                            reader.Close();
                        }
                    }
                }
                // step 5: we close the document and writer                
                writer.Close();
                document.Close();
                
                rtbMessage.Text += "\n" + "\n"+ "Merge completed  - " + Path.GetFileName(targetPath) + " file created!";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private bool IsValidPdf(string filepath)
        {
            bool Ret = true;
            PdfReader reader = null;
            try
            {
                reader = new PdfReader(filepath);
            }
            catch
            {
                Ret = false;
            }
            return Ret;
        }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900