使用C#winforms从文件夹打印多个PDF文件的代码 [英] Code For Printing Multiple PDF Files From Folder using C# winforms

查看:55
本文介绍了使用C#winforms从文件夹打印多个PDF文件的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们

i需要一个代码才能从文件夹中打印多个PDF文件而无需使用c#Winforms打开文件。



请某人帮忙我.......

hi friends
i need a Code For Printing Multiple PDF Files From Folder Without opening the Files using c# Winforms.

pls someone help me.......

推荐答案

以下代码将搜索给定目录中的所有文件,使用方法调用PrintPDFs自动将文件发送到运行此代码的机器上的默认打印机。



它将使用adobe的能力静默打印PDF,它仍然会打开文件,但我也是包含名为FindAndKillProcess的代码,它将关闭adobe。



如果不花费更多精密组件,这将是......可能至少......其中一个你会有更简单的选择。



如果你看一下itextsharp或pdfsharp,有选项打印PDF,这就是我最终的方式,但代码下面应该可以工作。



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.

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


我使用了iTextSharp,其他代码来自其他人



I used iTextSharp for this, other codes where from others

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


这篇关于使用C#winforms从文件夹打印多个PDF文件的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆