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 path and file name
var path = txtPDF.Text;
//load PdfiumViewer with document
using (var document = PdfDocument.Load(path))
{
//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();
}
}
}