C# 用c语言打印多个文档#
我想使用C#.Net打印多个无提示文件(txt、pdf、htm等)。我尝试了各种方法,使用ShellExecute、Process、PrintDocument,但都没有成功。有一个应用程序“”正是我所需要的,有人知道它是如何工作的吗?例如,它在不打开adobe reader的情况下打印pdf文件,甚至在任务管理器adobe出现时,唯一的要求是让他的应用程序与该文件关联。谢谢你的帮助 使用下面的ShellExecute方法,我可以发送打印,但如果pdf Adobe Reader处于打开状态,则不会打开安静,文件将直接发送到打印队列C# 用c语言打印多个文档#,c#,pdf,printing,C#,Pdf,Printing,我想使用C#.Net打印多个无提示文件(txt、pdf、htm等)。我尝试了各种方法,使用ShellExecute、Process、PrintDocument,但都没有成功。有一个应用程序“”正是我所需要的,有人知道它是如何工作的吗?例如,它在不打开adobe reader的情况下打印pdf文件,甚至在任务管理器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();
}
- Visual Studio 2013,C#,Windows 8.1
某些文档类型可以被操作系统理解(例如Windows和XPS文件),并且可以在不使用任何附加软件的情况下打印,但在一般情况下没有此类解决方案。您说您尝试了各种方法,但没有解释它们是如何不起作用的。我们怎么知道你做错了什么?“无提示打印”是什么意思?那些仅仅是需求而没有显示任何效果的问题通常不会在堆栈溢出时得到很好的接受。相反,显示(在代码中)迄今为止试图解决问题的内容和当前遇到的问题,以及一个具体的、可回答的、客观的问题。是的,显示您的代码,只有我们才能理解您使用的逻辑以及失败的原因。对于我提出问题的方式,我深表歉意,我有点失望没有得到一个适合我的形状,我编辑了问题,并把我使用的代码。非常感谢。Ken谢谢你的回复,我已经在我的机器上安装了这个软件,我做了几个测试。我注意到他询问了相关的应用程序,但正如我所提到的,他在没有打开Adobe Reader的情况下打印了pdf文件,文档进入队列直接打印。老实说,如果软件开发人员说需要Acrobat打印,我倾向于相信他们……我同意你和他们的看法,但是你知道他们如何在不打开AdobeReader的情况下给人留下这样的印象吗?可能是因为他们使用了其他的应用程序,或者是某种技术,通过这些技术,你的任务管理器中根本没有显示这种印象。这几乎不是绝对正确的。