I want to set the scaling ratio for a POS-5802DD printer in C#, before sending it to printer from crystal report,
Printer printer = new Printer(); // printer name
printer.ScalingRatio = 2;
printer.Connect();
printer.PrintText("Hello, world!", FontSize.Huge);
printer.Disconnect();
I want this seting in c# code.
Related
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!
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
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
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.
I am creating Shipping Label using iTextSharp.
What I am doing is Creating a Label in PDF so I can format it in any way I want and then send it to my THERMAL PRINTER.
My problem is, My labels are of size 4x6 (standard shipping label). These are the labels which we see on UPS & Fedex Packages. How Can i make my PDF exactly fit within 4x6 inches? currently It is printing in regular A4 document.
I am using following:
Dim document As New Document()
document.SetPageSize(PageSize.A4_LANDSCAPE)
Set a Custom Page Size:
Dim pgSize As New iTextSharp.text.Rectangle(myWidth, myHeight)
Dim doc As New iTextSharp.text.Document(pgSize, leftMargin, rightMargin, topMargin, bottomMargin)
iTextSharp uses 72 pixels per inch, so if you know the height and width of your desired page size in inches, just multiply those numbers by 72 to get myWidth and myHeight.
https://stackoverflow.com/a/2503476/102937
I would recommend producing raw printer language. Thermal bar code printers all have a native language. Languages such as ZPLII (Zebra Printer Language 2) or DPL (Datamax Printer Language). You can build them as a string and pass them directly to the printer. Searching the printer manufactures website you can quickly find the printer language manual for the printer you are using.
The great advantage to this method is control and speed. As Zebras and Datamax printers do not actually care about a page size you can focus on rendering the data you want in the size and orientation you want.
You may also be able to take advantage of some of the extra logic that the printers have. This is especially useful for serialized tags with sequential numbering. A single string sent to the printer can produce dozens to hundreds of labels. If you are going to do a lot of thermal bar code printing I strongly recommend understanding the power these printers contain in their native languages.
To Set document size use like this:-
Document doc = new Document(new iTextSharp.text.Rectangle(295f, 420f), 0f, 0f, 0f, 0f);
PdfWriter.GetInstance(doc, Response.OutputStream);
doc.Open();
-----------
-----
---------
For font here is code:-
iTextSharp.text.Font myFont1 = new iTextSharp.text.Font() { Size = 4.5f };
PdfPTable header1 = new PdfPTable(2);
header1.AddCell(new PdfPCell(new Phrase("", myFont1 )) { UseAscender = true, PaddingTop = 0, Border = 0, HorizontalAlignment = 0 });
i have just added other property's for you information future use.
happy coding!!