I'm printing pages via GTK# using a PrintOperation object like this:
PrintOperation print = new PrintOperation();
print.BeginPrint += new BeginPrintHandler(OnBeginPrint);
print.DrawPage += new DrawPageHandler(OnDrawPage);
print.EndPrint += new EndPrintHandler(OnEndPrint);
print.Run(PrintOperationAction.Print, null);
Result: My page is silently printed on the systems default printer.
So far so good.
If I replace the last line with
print.Run(PrintOperationAction.PrintDialog, null);
I'm able to choose a printer via default printer selection dialog and the page is printed on the selected printer.
What I'd like to do now is to print the document silently on another (= non default) printer without choosing it manually from the dialog (my application should print in background without user interaction).
I know the printers name in my application, but how can I set the printer that my pages are printed on this "non default" printer without user interaction?
Again I found the answer myself:
PrintSettings settings = new PrintSettings();
settings.Printer = "MY SECONDARY PRINTER";
PrintOperation print = new PrintOperation();
print.PrintSettings = settings;
print.BeginPrint += new BeginPrintHandler(OnBeginPrint);
print.DrawPage += new DrawPageHandler(OnDrawPage);
print.EndPrint += new EndPrintHandler(OnEndPrint);
print.Run(PrintOperationAction.Print, null);
Related
My project needs to print a simple string object to a printer of choice using something like the WPF System.Windows.Controls.PrintDialog.
I created a standard System.Drawing.Printing.PrintDocument but it does not have a DocumentPaginator that is required for the PrintDocument() method belonging to the PrintDialog class.
This seems like a trivial task to print a string to a printer with a dialog to show printer choices, but is turning out significantly more difficult. Please Help!
Since you're using WPF, instead of using the PrintDocument, just create a FlowDocument. I think that's a lot easier.
var dlg = new PrintDialog();
dlg.PageRangeSelection = PageRangeSelection.AllPages;
dlg.UserPageRangeEnabled = false;
if(dlg.ShowDialog() == true)
{
var doc = new FlowDocument();
// For normal A4&/letter pages, the defaults will print in two columns
doc.ColumnWidth = dlg.PrintableAreaWidth;
doc.Blocks.Add(new Paragraph(new Run("My arbitrary long string to print.\nNewlines work. Pages will break as needed.")));
dlg.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator, "Simple document");
}
I am currently creating a new page from code to use as my startpage for a sitedefinition which I also create from code..however.. even if I publish the newly created startpage I always end up with the following message in the CMS UI:
"This content is in English. It does not exist in svenska. Would you like to translate it now?"
How can I "Translate" the page from programmatically and then publish it as well? I haven't been able to find anything related to this here or while googling it.
You need to use the CreateLanguageBranch available in the IContentRepository.
In my example below Swedish is the default language on the site
var parent = ContentReference.RootPage;
IContentRepository contentRepository =
EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
StartpagePage startpage = contentRepository.GetDefault<StartpagePage>(parent);
startpage.PageName = "Teststartsida";
startpage.Title = "Teststartsida";
// this will create a startpage in the default language, Swedish in my case,
// use SaveAction.Publish and save the page into a new variable
var createdPage = contentRepository.Save(startpage,
EPiServer.DataAccess.SaveAction.Publish,
AccessLevel.NoAccess);
// invoke CreateLanguageBranch with LanguageSelector
var startpageLanguageBranch =
contentRepository.CreateLanguageBranch<StartpagePage>(createdPage,
new LanguageSelector("en"));
startpageLanguageBranch.PageName = "Start page test";
startpageLanguageBranch.Title = "Start page test";
// this will create a languagebranch in the language stated with the LanguageSelector.
// Use SaveAction.Save
contentRepository.Save(startpageLanguageBranch,
EPiServer.DataAccess.SaveAction.Save,
AccessLevel.NoAccess);
I've seen many posts with regards to setting printer tray in c# for word document. I need a solution for Excel.
A better solution, if possible, for any document. Some kind of method i can pass a file path and the tray.
EDIT
So far I've tried the following but no visible changes have been made in the printer settings.
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = #"\\localhost\HP-4515n";
var dps = ps.DefaultPageSettings;
dps.PaperSource.RawKind = 260;
OR
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = #"\\localhost\HP-4515n";
PaperSource psrc = new PaperSource();
psrc.RawKind = 260;
psrc.SourceName = "unknown";
dps.PaperSource = psrc;
EDIT 2
I'm hardcoding RawKind since the tray somehow does not show in the papersources.
And currently when i print eg. Excel document i show the PrinterDialog, get the name of the selected printer and pass it to interop Excel active printer property. But now i need to print mass of documents and i need to set the selected printer and it's property specially the tray programmatically.
#sysboard, I see from the MSDN page on the PrinterSettings class that the DefaultPageSettings property does not have a set method, only a get method. I am not sure that this is accessible from external classes...You might look into the PageSettings class as it looks like it has an overloaded constructor that allows you to pass a specified printer, and it has a set method on PaperSource.
You can get the available papersources using the folowing code :
PrintDocument printDoc1 = new PrintDocument();
List<PaperSource> psList = new List<PaperSource>();
PaperSource pkSource;
for (int i = 0; i < printDoc1.PrinterSettings.PaperSources.Count; i++)
{
pkSource = printDoc1.PrinterSettings.PaperSources[i];
psList.Add(pkSource);
}
Now present these options to the user and get the input as to which paper source to use, say its the first one, you can do :
PrintDocument doc = new PrintDocument();
doc.DefaultPageSettings.PaperSource = psList[0];
doc.Print();
Why you are hardcoding psrc.RawKind = 260;
To set a RawKind for a Paper Source there is PaperSourceKind enum available. try the following code
PrintDocument doc = new PrintDocument();
PaperSource pSource = new PaperSource();
pSource.RawKind = (int)PaperSourceKind.Middle;
doc.DefaultPageSettings.PaperSource = pSource;
I'm trying to print labels with a dynamic content. The print works fine but the problem is that the printing itself (the font) is 90 degrees twisted. It looks like this:
But it should look like this:
I cannot change the settings of the printer because other labels do print correct. So I think it must be something in the code. You can watch the C# code here:
System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
printerSettings.PrinterName = #"\\server\printer";
printerSettings.Copies = Convert.ToInt16((Convert.ToInt16(row.Cells["Counter"].Value.ToString()) - 1));
System.Drawing.Printing.PrintController standardPrintController = new System.Drawing.Printing.StandardPrintController();
Telerik.Reporting.Processing.ReportProcessor reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
reportProcessor.PrintController = standardPrintController;
Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource();
instanceReportSource.ReportDocument = myReport;
reportProcessor.PrintReport(instanceReportSource, printerSettings);
Does anyone know such a problem or a possible solution?
Suggestion very appreciated :)
I've had the same problem once, but never used Telerik for anything. I was trying to print a XPS file with the PrintServer object, and came up with this solution to rotate the print.
Basicly I change the page orientation of the queue before I print for the user, and then change it back afterwards.
It might be usefull to you as well.
PrintServer ps = new PrintServer("\\\\somePrintServer");
var queue = ps.GetPrintQueue("printerShareName");
var oldOrientation = queue.UserPrintTicket.PageOrientation;
queue.UserPrintTicket.PageOrientation = PageOrientation.Landscape;
//print job here
queue.UserPrintTicket.PageOrientation = oldOrientation;
Is there a way to switch printer trays during a print job? I've been asked to put together a pick/pack slip program. They want the inventory pick slip to be printed on a sheet of colored paper, the pack slips to be on white paper, and they want it properly collated (pick, pack, pack, pack, pack; pick, pack, pack, pack, pack; ...).
I found some other threads on setting default trays, but didn't find anything on alternating trays during the job. Maybe I'm not searching on the right thing.
Don't know if it makes a difference, but our printer is an HP 3015n and the clients will be both XP and Win 7 Pro.
You can try something like this you have to reference System.Drawing.dll from the projects --> Reference--> Add
//Namespace: System.Drawing.Printing
//Assembly: System.Drawing (in System.Drawing.dll)
PrintDocument printDoc = new PrintDocument();
PaperSize oPS = new PaperSize();
oPS.RawKind = (int)PaperKind.A4;
PaperSource oPSource = new PaperSource();
oPSource.RawKind = (int) PaperSourceKind.Upper;
printDoc.PrinterSettings = new PrinterSettings();
printDoc.PrinterSettings.PrinterName = sPrinterName;
printDoc.DefaultPageSettings.PaperSize = oPS;
printDoc.DefaultPageSettings.PaperSource = oPSource;
printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage);
printDoc.Print();
printDoc.Dispose();
To my knowledge no - you have to submit 2 jobs on a queue that basically only you use.
You can change printer tray with this code.
string _paperSource = "TRAY 2"; // Printer Tray
string _paperName = "8x17"; // Printer paper name
//Tested code comment. The commented code was the one I tested, but when
//I was writing the post I realized that could be done with less code.
//PaperSize pSize = new PaperSize() //Tested code :)
//PaperSource pSource = new PaperSource(); //Tested code :)
/// Find selected paperSource and paperName.
foreach (PaperSource _pSource in printDoc.PrinterSettings.PaperSources)
{
if (_pSource.SourceName.ToUpper() == _paperSource.ToUpper())
{
printDoc.DefaultPageSettings.PaperSource = _pSource;
//pSource = _pSource; //Tested code :)
break;
}
}
foreach (PaperSize _pSize in printDoc.PrinterSettings.PaperSizes)
{
if (_pSize.PaperName.ToUpper() == _paperName.ToUpper())
{
printDoc.DefaultPageSettings.PaperSize = _pSize;
//pSize = _pSize; //Tested code :)
break;
}
}
//printDoc.DefaultPageSettings.PaperSize = pSize; //Tested code :)
//printDoc.DefaultPageSettings.PaperSource = pSource; //Tested code :)