के साथ गतिशील पेपर ऊंचाई मैं थर्मल प्रिंटर पर पृष्ठों का आकार बदलने में एक समस्या में भाग गया। मैंने इस उत्तर से शुरू किया है: https://stackoverflow.com/a/27165167/1030464 और अब मेरे पास कोड है जिसे मैंने नीचे चिपकाया है।.NET प्रिंट डॉक्यूमेंट
यह अच्छी तरह से काम करता है, हालांकि मैं पृष्ठ की आकार की गणना और सेट करता हूं, ऐसा लगता है कि यह हर बार एक पूर्ण ए 4 आकार का पृष्ठ प्रिंट करता है। (मैं सैम 4 एस एलिक्स II और माइक्रोसॉफ्ट पीडीएफ प्रिंटर पर परीक्षण कर रहा हूं) - यह एक बड़ी समस्या है, क्योंकि इसे अक्सर 5-6 लाइन लंबे टेक्स्ट स्निपेट प्रिंट करने की आवश्यकता होती है।
मुझे एकाधिक थर्मल प्रिंटर का समर्थन करने की आवश्यकता है और मुझे केवल बुनियादी कार्यों की आवश्यकता है (इसलिए पेपर जाम आदि जैसे सिग्नल प्राप्त करने की आवश्यकता नहीं है) इसलिए मैंने पीओएस के बजाय विंडोज प्रिंटर ड्राइवर के साथ जाने का फैसला किया। नेट एक।
मैं पाठ की ऊंचाई की गणना करता हूं और पेपर के अनुसार आकार का आकार देता हूं, हालांकि इसका आउटपुट पेपर आकार पर कोई प्रभाव नहीं पड़ता है। क्या किसी के पास इस मुद्दे का समाधान है?
आप, बहुत ज्यादा
public int Print(DatabaseConnector dc)
{
try {
// Set up PrintDocument
PrintDocument recordDoc = new PrintDocument();
recordDoc.DocumentName = "PrintTask ID "+id.ToString();
recordDoc.PrintPage += new PrintPageEventHandler(PrintTask.PrintReceiptPage); // Filling in the stuff
// Print Controller
StandardPrintController spc = new StandardPrintController();
recordDoc.PrintController = spc; // This hides popup
// Printer Settings
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = dc.ReadSetting("PrinterName");
recordDoc.PrinterSettings = ps;
recordDoc.Print();
// Clean up
recordDoc.Dispose();
}
catch (Exception exc)
{
((MainForm)Application.OpenForms[0]).msg(exc.Message);
}
return 1; // ignore this
}
private static void PrintReceiptPage(object sender, PrintPageEventArgs e)
{
try {
// Read settings
DatabaseConnector db = new DatabaseConnector();
PrintTask pt = db.ReadTask();
float x = float.Parse(db.ReadSetting("PaperMarginFromLeft"));
float y = float.Parse(db.ReadSetting("PaperMarginFromTop"));
float width = float.Parse(db.ReadSetting("PaperWidth"));
float height = 0F;
string text;
// Set up font
Font drawFont1 = new Font(db.ReadSetting("PrintFont"), Int32.Parse(db.ReadSetting("PrintFontSize")), FontStyle.Regular);
SolidBrush drawBrush = new SolidBrush(Color.Black);
// Set format of string.
StringFormat drawFormatLeft = new StringFormat();
drawFormatLeft.Alignment = StringAlignment.Near;
// Draw string to screen.
text = pt.getData();
e.Graphics.DrawString(text, drawFont1, drawBrush, new RectangleF(x, y, width, height), drawFormatLeft);
// calculate text size
SizeF textSize = e.Graphics.MeasureString(text, drawFont1);
y += textSize.Height;
// Set page size - has no effect
e.HasMorePages = false;
float inchHeight = PrintTask.PixelsToInchY(y, e.Graphics);
PaperSize originalPaperSize = e.PageSettings.PaperSize;
PaperSize scaledSize = new PaperSize("Custom", originalPaperSize.Width, (int)Math.Ceiling(inchHeight * 100));
e.PageSettings.PaperSize = scaledSize;
e.PageSettings.PrinterSettings.DefaultPageSettings.PaperSize = scaledSize;
}
catch (Exception exc)
{
((MainForm)Application.OpenForms[0]).msg(exc.Message);
}
}
public static float PixelsToInchX(float n, Graphics graphics)
{
return n * graphics.DpiX/300;
}
public static float PixelsToInchY(float n, Graphics graphics)
{
return n * graphics.DpiY/300;
}
थर्मल प्रिंटर पृष्ठों का उपयोग नहीं करते हैं, उनके पास एक सतत पेपर रोल होता है। PrintDocument कक्षा का उपयोग लगभग कभी सही नहीं है। आकार के कारण नहीं, यह आमतौर पर बहुत धीमी है। पीओएस सही दृष्टिकोण है। –