我正在尝试直接在打印机上打印pdf文件,它支持postscript。不幸的是,打印文档被缩放(比adobe reader的打印小一点,缩放模式 - “实际大小”)。有没有办法打印它没有缩放?这是一个我用来打印的代码:
[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);
}
}
顺便说一下 - 当我这样打印时,我可以管理打印机页边距吗?
谢谢,巴托斯
PDF和PostScript不是一回事!仅仅因为您的打印机“支持PostScript”并不意味着它可以打印PDF文件。
在这种情况下,它看起来确实像您的打印机支持PDF打印。为缩放输出而考虑的原因有两个:
1)PDF文件的介质尺寸与打印机中的介质不匹配。
2)打印机具有不可打印的边距。为了确保打印整个PDF文件(如果它包含直到介质边缘的绘图操作),打印机将缩放PDF以使PDF介质适合打印介质的可打印区域。
您可以根据打印机中的介质检查PDF页面的介质尺寸。如果他们匹配那么你知道它不是那个。
解决方案完全取决于打印机的功能。您可以告诉它不要缩放输出(如果您确定PDF的标记区域将适合打印页面的可打印区域)。如果没有,那么您唯一的解决方案是使用较小的媒体重制PDF文件。
您还可以尝试在PDF文件中设置一个小于打印介质的CropBox,然后查看您的打印机是否会打印未缩放的PDF。
感谢@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