我想使用C#.Net打印无声多个文件(txt,pdf,htm,...)。我尝试了各种方法,我使用了ShellExecute,Process,PrintDocument,都没有成功。有一个应用程序" http://www.print-conductor.com/download/"这正是我需要的,有没有人知道它是如何工作的?例如,它打印pdf文件而不打开adobe reader,即使在任务管理器adobe中也没有出现,唯一的要求就是让他的应用程序与文件相关联。感谢任何帮助。
使用下面的ShellExecute方法,我可以发送打印,但是在pdf的情况下Adobe Reader打开,安静是他没有打开,文件要直接发送到打印队列。
[DllImport("shell32.dll", EntryPoint = "ShellExecute")]
private static extern IntPtr ShellExecute(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters,
string lpDirectory, ShowCommands nShowCmd);
public bool Print(string fullPath)
{
var result = false;
try
{
var resultPrint = ShellExecute(IntPtr.Zero, "Print", fullPath, "", "", ShowCommands.SwShowminimized).ToInt32();
if (resultPrint > 32) result = true;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
return result;
}
我对提出问题第一部分的方式表示道歉,我对未能完成项目表示不满,我已经3周没有了,原谅我。
另一次尝试不成功,该文件出现在打印队列中,但它打印为空白:
public class RawPrinterHelper
{
// Structure and API declarions:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
// SendBytesToPrinter()
// When the function is given a printer name and an unmanaged array
// of bytes, the function sends those bytes to the print queue.
// Returns true on success, false on failure.
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.
di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";
// Open the printer.
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
// Start a document.
if (StartDocPrinter(hPrinter, 1, di))
{
// Start a page.
if (StartPagePrinter(hPrinter))
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}
public static bool SendFileToPrinter(string szPrinterName, string szFileName)
{
// Open the file.
FileStream fs = new FileStream(szFileName, FileMode.Open);
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(fs);
// Dim an array of bytes big enough to hold the file's contents.
Byte[] bytes = new Byte[fs.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;
nLength = Convert.ToInt32(fs.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes(nLength);
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}
public static bool SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
// How many characters are in the string?
dwCount = szString.Length;
// Assume that the printer is expecting ANSI text, and then convert
// the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
}
class Program
{
private static void Main(string[] args)
{
var defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();
RawPrinterHelper.SendFileToPrinter(defaultPrintQueue.FullName, @"C:\Temp\Teste.pdf");
}
}
使用以下选项,Adobe Reader也会打开并保持打开状态:
public void Print(string fullPath, string printerName)
{
Process process = new Process
{
StartInfo =
{
FileName = fullPath,
UseShellExecute = true,
Verb = "printto",
Arguments = "\"" + printerName + "\""
}
};
process.Start();
}
答案 0 :(得分:2)
如果你看一下Print Conductor'它如何运作'链接说:
" Print Conductor的特殊功能是自动将文件发送到其他程序进行打印。
...
需要Adobe Reader或Adobe Acrobat才能打印PDF文件。"
事实上它 打开Acrobat(或至少是与PDF文件相关的应用程序)以打印它们。我担心要打印任意文档类型,您需要一个了解如何阅读文档以及如何呈现文档的应用程序。
操作系统可以理解某些文档类型(例如Windows和XPS文件),无需任何其他软件即可打印,但在一般情况下,没有这样的解决方案。