SetHdevmode似乎不起作用 [英] SetHdevmode does not seem to be working
问题描述
我不熟悉使用C#打印。 我汇集了从几个地方收集的代码,但是遇到了很多困难。我试图使用PdfiumViewer打印PDF而无需用户干预。 我已设法在bin文件
中保存打印机设置,并且能够验证bin文件中的内容是否正确。 但是,当我尝试使用bin文件设置打印机设置时,打印机未使用这些设置。 以下是代码可以有人请告诉我我可能做错了什么?
I am new to printing in C#. I have put together code gathered from several places but am having lots of difficulties. I am trying to use the PdfiumViewer to print PDFs without user intervention. I have managed to save printer setting in bin files and have been able to verify that what is in the bin file is correct. However when I try to set the printer setting using the bin files the printer is not using the settings. Below is the code can someone please tell me what I maybe doing wrong?
              // pdf路径和文件名
              var path = txtPDF.Text;
             
              //使用文档加载PdfiumViewer
             使用(var document = PdfDocument.Load(path))
                  {
//pdf path and file name
var path = txtPDF.Text;
//load PdfiumViewer with document
using (var document = PdfDocument.Load(path))
{
                  //使用PdfiumViewer创建打印文档
                 使用(var printDocument = document.CreatePrintDocument())
                      {
$
                          //将bin文件复制到数组中$
                          byte [] bytearray = File.ReadAllBytes(NameAndPathOfBinFile);
                          //创建指向"保存bin文件的数组"的指针将被存储
                          var pDevMode = Marshal.AllocHGlobal(bytearray.Length);
                          //复制"bin文件数组"到指针位置
                          Marshal.Copy(bytearray,0,pDevMode,bytearray.Length);
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //设置加载的"bin文件数组"的iPdfiumViewer devmode指针< b $ b &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; printDocument.PrinterSettings.SetHdevmode(pDevMode);
$
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //获取设置的打印机名称
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; printDocument.PrinterSettings.PrinterName = printerName;
$
$
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //这只是显示对话框,以便我可以验证设置,并且我已验证设置是否正确
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; int value = AdvancedDocumentProperties(IntPtr.Zero,IntPtr.Zero,printerName,pDevMode,pDevMode);
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //打印文件
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; printDocument.Print();
$
$
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }
//CREATE print document using PdfiumViewer
using (var printDocument = document.CreatePrintDocument())
{
//copy bin file into array
byte[] bytearray = File.ReadAllBytes(NameAndPathOfBinFile);
//create pointer to where the "array holding the bin file" will be stored
var pDevMode = Marshal.AllocHGlobal(bytearray.Length);
//copy "bin file array" to pointer location
Marshal.Copy(bytearray, 0, pDevMode, bytearray.Length);
//set point the iPdfiumViewer devmode pointer of loaded "bin file array"
printDocument.PrinterSettings.SetHdevmode(pDevMode);
//get set printer name
printDocument.PrinterSettings.PrinterName = printerName;
//This just shows the dialog box to so I can verify the settings and I have verified that the setting are correct
int value = AdvancedDocumentProperties(IntPtr.Zero, IntPtr.Zero, printerName, pDevMode, pDevMode);
//print the document
printDocument.Print();
}
}
}
推荐答案
Hi Palford,
Hi Palford,
感谢您发布此处。
对于您的问题,请检查您的DEVMODE结构。
For your question, please check your DEVMODE structure .
请参阅以下链接。
Please refer to the link below.
最好的问候,
Wendy
这篇关于SetHdevmode似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!