我试图打印pdf文件直接在打印机上,这支持postscript。不幸的是,打印的文档是缩放的(比来自adobe阅读器的打印要小一些,具有缩放模式--“实际大小”)。有没有办法不用缩放就打印出来?下面是我用来打印的代码:
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall, ThrowOnUnmappableChar = true, BestFitMapping = false)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall, ThrowOnUnmappableChar = true, BestFitMapping = false)]
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Interoperability", "CA1401:PInvokesShouldNotBeVisible"), 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);
public static bool SendFileToPrinter(string pdfFileName, string printerName, string name)
{
try
{
bool success = false;
using (FileStream fs = new FileStream(pdfFileName, FileMode.Open))
{
int nLength = Convert.ToInt32(fs.Length);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = new Byte[fs.Length];
bytes = br.ReadBytes(nLength);
IntPtr ptrUnmanagedBytes = new IntPtr(0);
ptrUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
Marshal.Copy(bytes, 0, ptrUnmanagedBytes, nLength);
success = SendBytesToPrinter(printerName, ptrUnmanagedBytes, nLength, name);
Marshal.FreeCoTaskMem(ptrUnmanagedBytes);
}
return success;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount, string name)
{
try
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool success = false; // Assume failure unless you specifically succeed.
di.pDocName = name;
di.pDataType = "RAW";
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
if (StartDocPrinter(hPrinter, 1, di))
{
if (StartPagePrinter(hPrinter))
{
success = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
if (success == false)
{
dwError = Marshal.GetLastWin32Error();
}
return success;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}顺便说一句,当我那样打印的时候,我能管理打印机的边距吗?
谢谢,巴托兹
发布于 2018-03-05 13:57:12
谢谢你的建议@KenS,我终于找到了一个解决办法,为什么PDF没有正确打印。默认的PDF缩放有一个问题,这是为了使打印适合页面。解决这一问题的方法是设置缩放:
/ViewerPreferences<</PrintScaling/None>> 例如,在PDF文件中:
对象之前:
2 0 obj
<<
/Type/Catalog
/Pages 3
>>
endobj对象在以下之后:
2 0 obj
<<
/Type/Catalog
/ViewerPreferences<</PrintScaling/None>>
/Pages 3
>>
endobj发布于 2018-03-01 12:32:14
PDF和PostScript不是一回事!仅仅因为你的打印机‘支持PostScript’并不意味着它可以打印PDF文件。
在这种情况下,看起来您的打印机确实支持PDF打印。对于扩展输出有两个可能的原因:
1) PDF文件的媒体大小与打印机中的媒体不匹配。
( 2)打印机具有不可打印的边距。为了确保完整的PDF文件被打印出来(如果它包含直到媒体边缘的绘图操作),打印机对PDF进行缩放,以便PDF媒体适合印刷媒体的可打印区域。
您可以对照打印机中的媒体检查PDF页的媒体大小。如果他们匹配的话,那你就知道不是这样的。
解决方案完全取决于打印机的功能。您可以告诉它不要缩放输出(如果您确信PDF的标记区域将适合打印页面的可打印区域)。如果没有,那么您的唯一解决方案是使用更小的媒体重新生成PDF文件。
您还可以尝试在PDF文件中设置一个比打印媒体更小的CropBox,并查看您的打印机是否会打印不缩放的PDF。
https://stackoverflow.com/questions/49046431
复制相似问题