How to set printer black and white c# code behind - c#

I have written a custom printer class.With support color printer, I set PrintDocument..DefaultPageSettings.Color = false and I also set e.PageSettings.Color =false in PrintDocument_QueryPageSettings event. I have tested with Microsoft Print to PDF but output file has still color.Sorry my english.Thanks

I use Print Dialog in my printing code and I set the printing color like this:
PrintDialog pd = new PrintDialog();
pd.PrintTicket.OutputColor = System.Printing.OutputColor.Monochrome;
But test it on a real printer

Related

How to set up and print with custom paper size in C# printdocument?

I am trying to use PrintDocument and set up the paper size to print or barcode thermal printer. Because I don't have the printer nearby I am using Microsoft Print To PDF option which appeared in Win10.
During initialization I have such code:
As you see, here I am trying to set up custom paper size for default paper size. But, I cannot specify Kind property, because it's readonly! RawKind property not helps.
As alternative I have such event. It does not help either. It correctly displays the page layout on preview, but in PDF document I observe pages printed in A4, as by default.
private void PrintDoc_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
{
PageSettings nSettings = new PageSettings();
int properWidthInHundretsOfInches = (int)(handlingClassRef.newconfig.labelParameters.barcodeLabelWidthMM * (1.0 / 25.4) * 100.0);
int properHeightInHundretsOfInches = (int)(handlingClassRef.newconfig.labelParameters.barcodeLabelHeightMM * (1.0 / 25.4) * 100.0);
nSettings.PaperSize = new PaperSize("label", (int)properWidthInHundretsOfInches, (int)properHeightInHundretsOfInches);
e.PageSettings = nSettings;
}
I am aware of question How to print with custom paper size in winforms , but I don't actually understand the answer. Should I reconfigure printer by using printer properties OS dialog? I would like rather not to require user to modify settings of of printer in one way or another. Also, I'd like to achieve appropriate result during printing to pdf exploration phase.
How to set up and print with custom paper size in C# printdocument?
Edit: using the line:
printDoc.DefaultPageSettings.PaperSize = new PaperSize("label", properWidthInHundretsOfInches, properHeightInHundretsOfInches);
did not resolve question.
Here is a result:
preview is nice and small but printed document is large and has not proper page size
You can try by initialising PaperSize class under System.Drawing.Printing and then you can specify the custom size
printDoc.DefaultPageSettings.PaperSize = new PaperSize("MyPaper", 600, 800);
I found the solution for this!
The Short Answer is :
printDocument1.DefaultPageSettings.PaperSize = new PaperSize("MyPaper", 700, 900);
Why it's printing A4 Paper Size, Not Full Report?
Because The Default Virtual pdf printer in Windows Microsoft Print To Pdf uses A4 Paper Size, you can try to change it to A5 from the Control Panel And try to print it again. You will notice that it has included more lines on the pdf output!
So don't worry, the code I have mentioned is Correct, but it depends on the printer you use. Because printers Use only Some Formatted Paper Sizes and it will not accept more pages out of the frame.
See This picture for more explanation
..
First, I was furious Because of this problem,
I thought that printpreviewDialog1 had another Printable area, and I tried to make it as exact as printdocument1, and then I noticed it's just a viewer.
After hours of research and many tries, I noticed that the printer wouldn't accept Any more lines; I was working on a Cashier report. I needed to make a long paper for the thermal printer, but when I was testing on the "print to pdf" printer, it didn't Print all the lines on preview control because it just prints to A4 size, mo more and no less!

C# Raw Print , Compress print document, reducing printing file size , Fast Print to printer

I try to print some jpeg file to my printer. but it will be very very slow printing.
I need to control print file size.
below is my try and fail
Try 1.
**PrintDocument p = new PrintDocument();
p.Print();
But why my original file is ~100 KB send print file will be grown to 1.7MB????
I just use paint send to print is only 120K or a little grown that is acceptable.**
windows paint
like this
print job list sample
Try 2.
FlowDocument doc = new FlowDocument(paragraph);
same to PrintDocument file size will be grown up but that is better than PrintDocument just case the file size.
Try 3 RawPrint
can be sent a file without growing file size to the printer but the printer does nothing.

Printing an SSRS report in Landscape mode from Windows Forms application

I have a Windows Forms application that has a textbox, a button, and 3 ReportViewer. The 3 ReportViewer boxes are hidden. When you enter a ShopOrder into the textbox and click on the button, it will automatically pass the Shop order value as a parameter to all 3 reports, render the report, and once the rendering is complete, render the report as EMF file, print the report.
I am using this link as a guide to print SSRS reports automatically from a Windows Forms application.
I have a few variances in my application because I am using ServerReports in my ReportViewer and not LocalReport. But after all these changes, my application prints them all out with no problems.
But the only issue I have is, I am not able to set my page orientation to Landscape, even though the orientation on my report is Landscape.
So I thought maybe I need to set the deviceInfo variable's PageWidth and PageHeight variables accordingly, so this is what the deviceInfo variable has:
string deviceInfo =
#"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>11in</PageWidth>
<PageHeight>8.5in</PageHeight>
<MarginTop>0.25in</MarginTop>
<MarginLeft>0.25in</MarginLeft>
<MarginRight>0.25in</MarginRight>
<MarginBottom>0.25in</MarginBottom>
</DeviceInfo>";
I have two Export functions: Export and ExportLandscape. The code snippet above is a part of ExportLandscape. When I call ExportLandscape, my report still prints in Portrait.
I tried just completely removing the Page setup options from my DeviceInfo variable and made it just say the OutputFormat. That didn't do it either.
Is there anything else I need to change for my report to print in Landscape? What am I missing?
It is also worth noting that, out of my 3 reports, 2 of them print in Landscape and 1 prints in Portrait. So I would really like for my application to just print it in whatever page settings the report is in. I just tried getting the report's page size and report's margins and setting those to my DeviceInfo variable as suggested here. STILL NO LUCK!!
I just tried adding a breakpoint at the Export(ReportViewer report) function and stepped through. When I get the report.ServerReport.GetDefaultPageSettings().PaperSize in the immediate window, I see this:
{[PaperSize Letter Kind=Letter Height=1100 Width=850]}
Height: 1100
Kind: Letter
PaperName: "Letter"
RawKind: 1
Width: 850
This makes me feel like even though my report is set to landscape (height = 8.5in and Width = 11in), my application does not seem to recognize it.
Important Update:
The printer I am printing to has 2 paper trays. When I print a portrait report, it takes it from the default tray with the default paper size (tray 2). But when my application sends the landscape report to print, the printer tries to get a paper out of tray 1. When I load tray 1 with the same paper that is in tray 2, it asks me to enter a width and height of the paper. The printer does not seem to understand when I tell it to print it in landscape. Or rather, the printer thinks this is some new setting that it does not know about. When I enter 11 for width and 8.5 for height, it prints landscape data on a portrait paper.
To make myself clearer, the data is getting printed with a width of 11 and height of 8.5. AKA, only 75% of the data gets printed. The rest gets pushed out of the page because the page is still being oriented in portrait.
You need to use a suitable PageSettings for the PrintDocument which is used for printing. You need to apply some changes to code of that article to be able to print in different paper size or page layout.
First you need to create a suitable PageSettings, for example if you have set the default page setting for your report to be landscape:
var report = reportViewer1.LocalReport;
var pageSettings = new PageSettings();
pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
pageSettings.Margins = report.GetDefaultPageSettings().Margins;
or if you want to create a new page setting:
var pageSettings = new PageSettings();
pageSettings.Landscape = true;
pageSettings.PaperSize = reportViewer1.PrinterSettings.PaperSizes.Cast<PaperSize>()
.Where(x => x.Kind == PaperKind.A4).First();
Then use the pageSetting when creating the deviceInfo:
string deviceInfo =
$#"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
<PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
<MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
<MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
<MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
<MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
</DeviceInfo>";
And at last, use the same pageSettings with PrintDocument:
PrintDocument printDoc = new PrintDocument();
printDoc.DefaultPageSettings = pageSettings;
I've created an extension method to make it easier to print the report easily by calling Print() or Print(PageSettings). You can find it here: Print RDLC Report without showing the ReportViewer

Barcodes printing with irregular lines

A client asked me to build an inventory solution for them where they'd print barcode labels for all office equipment to keep track of them in various ways.
They gave me a Citizen CL-S621 printer (203x203 dpi resolution) to use for testing and after (the nightmare that was) configuring its drivers to print and fitting everything to the nonstandard labels they gave me to test, the biggest problem I'm still running into is that the printer is having trouble printing some bars in a straight line and instead prints them in dashed/dotted forms.
The C# Code below shows the basics of how I build the barcodes using this library :
public void CreateTheBarcode(string StringToEncode)
{
Barcode b = new Barcode();
b.LabelFont = new Font("Sample Bar Code Font", 24, FontStyle.Bold);
b.IncludeLabel = true;
b.Encode(BarcodeLib.TYPE.CODE128, StringToEncode, Color.Black, Color.White, 730, 140);
b.SaveImage(#"C:\temp\Barcodes\"+StringToEncode+".png",SaveTypes.PNG);
Print(#"C:\temp\Barcodes\"+StringToEncode+".png");
}
public static void Print(string FilePath)
{
Process printJob = new Process();
printJob.StartInfo.FileName = FilePath;
printJob.StartInfo.UseShellExecute = true;
printJob.StartInfo.Verb = "printto";
printJob.StartInfo.CreateNoWindow = false;
printJob.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
printJob.StartInfo.Arguments = "\"" + "Citizen CL-S621" + "\"";
printJob.StartInfo.WorkingDirectory = Path.GetDirectoryName(FilePath);
printJob.Start();
}
The hardcoded width and height are the best approximation I found of a good image size the printer would accept and accurately print onto the labels since I had to measure their size by ruler and between printer offsets and eye-measurement accuracy issues getting a size that worked proved troublesome.
In any case, I generated some barcodes and the images are crisp, clear and seem pretty good:
The lines are straight and clearly defined, the text is sharp, everything seems fine;
but when I go to print them I get:
Some of the bars print straight and clear while some have irregular edges and some are just irregular dot/slash/curved patterns. The text in all of them suffers the same issue. I have tried different font sizes and supposedly barcode-friendly fonts and the issue persists. The problem persists if I remove the text labels.
It strikes me as an image rasterization issue but I am not fully sure, nor do I know how to fix that.
I'm not yet sure if a scanner can read these or not, I receive one tomorrow, but any tips on what I may be doing wrong would be appreciated.
These printer settings should help:
Select the printer in Device and Printers > Printing Preferences > Graphics
Set Dithering to None
The printer is antialiasing your images. This is an endemic problem with barcode fonts. You have to either set your printer to not antialias (try Settings/Printer/Printing Preferences/Color Mode/Monochrome) or you have to set the image DPI to be identical to the printer DPI (typically 300 DPI).
If your printer renders to a rectangle and not to a DPI, you have to size your image so the image pixels are an integer multiple of the printer pixels of the same rectangle.
It's difficult to propose a solution to your problem because you have too many moving parts; you start with a font that goes to an image that goes to a file that goes to a shell command that goes to an application you haven't told us about that goes to a printer. Any one of those steps could anti-alias your image.
My suggestion is to remove these intermediate parts. What's calling CreateTheBarcode? An application you wrote? Try having that application print to the printer directly using .NET System.Drawing.Printing.PrintDocument. You can't do that and have to use the mystery application UseShellExecute calls? Study that application, maybe there is some way you can send printer commands instead of going through all that font to image to file rigamarole; many label printers have ways to output text like “test 123” directly as barcodes.
Edit: Maybe what you are attempting is flat-out impossible. What are the dimensions of the label you are printing to? Your “More numbers” example is over 500 px across and your printer is only 200 DPI. If the labels are narrower than 500/200 ≅ 2.5 inches there is no way the bars will fit even if you print at 203 DPI.
Same problem and this worked perfectly.
Option 1: Adjust Dithering Settings
Click the Windows button.
Go to Control Panel.
Go to Devices and Printers.
Right-click preferred printer.
Click "Printer Preferences".
Go to "Dithering" tab and select "None".
Click apply.

Label Printing From C# - Document Size Is Too Large For Printer

I'm developing an application which must print labels. The label printer i'm using is a Brother QL-570. The label width is 66mm and the length of the labels needs to be approximately 45mm. The problem I'm having is that I am unable to configure the application to actually print the labels. Everytime I do so I receive a warning stating that the document size is too large for the printer. No matter what size I attempt to change the PrintDocument size to I always receieve a warning stating that the document is 90mm x 29mm and is too large for the label printer.
Here's just one of my attempts:
private PrintDocument label;
label = new PrintDocument();
PaperSize pS = new PaperSize("Custom Size", 212, 67);
label.DefaultPageSettings.PaperSize = pS;
label.PrinterSettings.PrinterName = "Brother QL-570";
label.PrinterSettings.DefaultPageSettings.PaperSize = pS;
label.PrintPage += new PrintPageEventHandler(label_PrintPage);
private void label_PrintPage(object sender, PrintPageEventArgs e)
{
SolidBrush brush = new SolidBrush(Color.Black);
Font header = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Bold);
e.Graphics.DrawString("Hello World", header, brush, 30, 30);
}
Has anyone any idea where I'm going wrong? I think I may be setting up the paper size for both the document and the printer incorrectly. I've tried numerous other paper sizes and to no avail.
Thanks For Any Help.
The problem was not a programming problem but a printer configuration problem. By default the printer was configured to use a different paper size other than the one I was using. Thanks for all your help.
Thanks. Your comment helped me with a similar issue. It appears that these Brother label printers plainly ignore the PageSettings properties.. once you have it working try putting some wild values in there. Completely ignored - crazy! Set a large enough print area and ensure that you are drawing within the right rectangle and you'll be fine.

Categories

Resources