U tekstualnom modu jedino sto mozes da menjas je da ukljucis/iskljucis kondenzovan mod i u oba slucaja broj karaktera koji moze da stane u jedan red je uvek fiksan i zavisi od stampaca (i papira). Ti u sustini samo treba da obratis paznju da ti duzina stringa ne prelazi maksimalnu duzinu koja moze da se odstampa a na kraju svakog reda saljes ESC sekvencu za linefeed i carriagereturn.
Da ti malo olaksam posao, saljem ti moju klasu za rad sa matricnim stampacima kao i primer kako koristiti.
Prvo ide klasa:
Code:
using System;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
namespace DarkMan
{
public class MatrixPrinter
{
public static Boolean DisableLatinCharacters = false;
public static MatrixPrinter OpenPrinter(PrinterSettings printerSettings, string documentName)
{
MatrixPrinter printer = new MatrixPrinter();
if(printer.Open(printerSettings, documentName)) return printer;
return null;
}
public static MatrixPrinter OpenPrinter(string szPrinterName, string documentName)
{
MatrixPrinter printer = new MatrixPrinter();
if(printer.Open(szPrinterName, documentName)) return printer;
return null;
}
private IntPtr hPrinter = IntPtr.Zero;
private DOCINFOA docinfo = null;
public MatrixPrinter()
{
this.hPrinter = IntPtr.Zero;
this.docinfo = null;
}
~MatrixPrinter()
{
Close();
}
public Boolean Open(string szPrinterName, string documentName)
{
hPrinter = new IntPtr(0);
docinfo = new DOCINFOA();
docinfo.pDocName = documentName;
docinfo.pDataType = "RAW";
// Open the printer.
if(OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)) {
// Start a document.
if(StartDocPrinter(hPrinter, 1, docinfo)) return true;
ClosePrinter(hPrinter);
}
hPrinter = IntPtr.Zero;
docinfo = null;
return false;
}
public Boolean Open(PrinterSettings printerSettings, string documentName)
{
return Open(printerSettings.PrinterName, documentName);
}
public void Close()
{
if(hPrinter != IntPtr.Zero) {
EndDocPrinter(hPrinter);
ClosePrinter(hPrinter);
}
hPrinter = IntPtr.Zero;
docinfo = null;
}
public bool WriteByte(byte value)
{
if(hPrinter == IntPtr.Zero) return false;
// Allocate some unmanaged memory for one byte.
IntPtr pByte = Marshal.AllocCoTaskMem(1);
Marshal.WriteByte(pByte, value);
// Send the unmanaged byte to the printer.
int dwWritten = 0;
bool bSuccess = WritePrinter(hPrinter, pByte, 1, out dwWritten);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pByte);
return bSuccess;
}
public bool WriteBytes(byte[] buffer)
{
if(hPrinter == IntPtr.Zero) return false;
int nLength = Convert.ToInt32(buffer.Length);
// Allocate some unmanaged memory for those bytes.
IntPtr pBytes = Marshal.AllocCoTaskMem(nLength);
Marshal.Copy(buffer, 0, pBytes, nLength);
// Send the unmanaged bytes to the printer
int dwWritten = 0;
bool bSuccess = WritePrinter(hPrinter, pBytes, nLength, out dwWritten);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pBytes);
return bSuccess;
}
public bool Write(String text, Boolean ProcessLatinCharacters)
{
if(hPrinter == IntPtr.Zero) return false;
return WriteBytes(MatrixPrinter.GetBytes(text, ProcessLatinCharacters));
}
public bool Write(String text)
{
return Write(text, false);
}
public bool Write(char ch, Boolean ProcessLatinCharacters)
{
if(hPrinter == IntPtr.Zero) return false;
return WriteBytes(MatrixPrinter.GetBytes(ch, ProcessLatinCharacters));
}
public bool Write(char ch)
{
return Write(ch, false);
}
public bool WriteLine(String text, Boolean ProcessLatinCharacters)
{
if(hPrinter == IntPtr.Zero) return false;
return WriteBytes(MatrixPrinter.GetBytes(text + (char)EscapeCodes.LineFeed + (char)EscapeCodes.CarriageReturn, ProcessLatinCharacters));
}
public bool WriteLine(String text)
{
return WriteLine(text, false);
}
public bool EnableCondensedMode(Boolean Enable)
{
return WriteEscapeCode(Enable ? EscapeCodes.SelectCondensedMode : EscapeCodes.CancelCondensedMode);
}
public bool FormFeed()
{
return WriteEscapeCode(EscapeCodes.FormFeed);
}
public void SetPageLengthInLines(int lines)
{
lines = Math.Min(Math.Max(lines, 1), 127);
WriteEscapeCode(EscapeCodes.EscapeCode);
WriteByte((byte)'C');
WriteByte((byte)lines);
}
public void SetPageLengthInInches(int inches)
{
inches = Math.Min(Math.Max(inches, 1), 22);
WriteEscapeCode(EscapeCodes.EscapeCode);
WriteByte((byte)'C');
WriteEscapeCode(EscapeCodes.NUL);
WriteByte((byte)inches);
}
private static byte[] GetBytes(String text, Boolean ProcessLatinCharacters)
{
if(ProcessLatinCharacters) {
if(DisableLatinCharacters) {
text = text.Replace('Š', 'S');
text = text.Replace('š', 's');
text = text.Replace('Č', 'C');
text = text.Replace('č', 'c');
text = text.Replace('Ć', 'C');
text = text.Replace('ć', 'c');
text = text.Replace('Ž', 'Z');
text = text.Replace('ž', 'z');
text = text.Replace('Đ', 'D');
text = text.Replace('đ', 'd');
} else {
text = text.Replace('Š', (char)230);
text = text.Replace('š', (char)231);
text = text.Replace('Č', (char)172);
text = text.Replace('č', (char)159);
text = text.Replace('Ć', (char)143);
text = text.Replace('ć', (char)134);
text = text.Replace('Ž', (char)189);
text = text.Replace('ž', (char)190);
text = text.Replace('Đ', (char)209);
text = text.Replace('đ', (char)208);
}
byte[] temp = System.Text.Encoding.Unicode.GetBytes(text);
byte[] buffer = new byte[temp.Length / 2];
for(int i = 0, j = 0; i < temp.Length; i += 2, j++) buffer[j] = temp[i];
return buffer;
}
return System.Text.Encoding.ASCII.GetBytes(text);
}
private static byte[] GetBytes(char ch, Boolean ProcessLatinCharacters)
{
if(ProcessLatinCharacters) {
if(DisableLatinCharacters) {
if(ch == 'Š') ch = 'S';
if(ch == 'š') ch = 's';
if(ch == 'Č') ch = 'C';
if(ch == 'č') ch = 'c';
if(ch == 'Ć') ch = 'C';
if(ch == 'ć') ch = 'c';
if(ch == 'Ž') ch = 'Z';
if(ch == 'ž') ch = 'z';
if(ch == 'Đ') ch = 'D';
if(ch == 'đ') ch = 'd';
} else {
if(ch == 'Š') ch = (char)230;
if(ch == 'š') ch = (char)231;
if(ch == 'Č') ch = (char)172;
if(ch == 'č') ch = (char)159;
if(ch == 'Ć') ch = (char)143;
if(ch == 'ć') ch = (char)134;
if(ch == 'Ž') ch = (char)189;
if(ch == 'ž') ch = (char)190;
if(ch == 'Đ') ch = (char)209;
if(ch == 'đ') ch = (char)208;
}
byte[] temp = System.Text.Encoding.Unicode.GetBytes(new char[] { ch });
byte[] buffer = new byte[temp.Length / 2];
for(int i = 0, j = 0; i < temp.Length; i += 2, j++) buffer[j] = temp[i];
return buffer;
}
return System.Text.Encoding.ASCII.GetBytes(new char[] { ch });
}
public bool WriteEscapeCode(EscapeCodes code)
{
if(hPrinter == IntPtr.Zero) return false;
return WriteByte((byte)code);
}
public enum EscapeCodes: byte
{
NUL = 0x00,
EscapeCode = 0x1B,
LineFeed = 0x0A,
FormFeed = 0x0C,
CarriageReturn = 0x0D,
SelectCondensedMode = 0x0F,
CancelCondensedMode = 0x12
}
public enum MatrixPrinterCommandType
{
PrinterOperation_Beeper,
PrinterOperation_SelectPrinter,
PrinterOperation_DeselectPrinter,
PrinterOperation_TurnCutSheetFeederControlOff,
PrinterOperation_TurnCutSheetFeederControlOn,
PrinterOperation_DisablePaperOutSensor,
PrinterOperation_EnablePaperOutSensor,
PrinterOperation_SelectUnidirectionalModeForOneLine,
PrinterOperation_InitializePrinter,
PrinterOperation_CancelUnidirectionalMode,
PrinterOperation_SelectUnidirectionalMode,
PrinterOperation_TurnHalfSpeedModeOff,
PrinterOperation_TurnHalfSpeedModeOn,
PrinterOperation_SkipOverPerforation,
PrinterOperation_CancelSkipOverPerforation,
PrintingStyle_Pica16DoubleStrike,
PrintingStyle_Elite32DoubleWide,
PrintingStyle_Condensed64Italic,
PrintingStyle_Emphasized128Underline,
PrintingStyle_SelectNLQRomanFont,
PrintingStyle_SelectNLQSansSerifFont,
PrintingStyle_SelectDraftMode,
PrintingStyle_SelectNLQMode,
PrintSizeAndCharacterWidth_SelectDoubleWidthForOneLine,
PrintSizeAndCharacterWidth_SelectCondensedMode,
PrintSizeAndCharacterWidth_CancelCondensedMode,
PrintSizeAndCharacterWidth_CancelOneLineDoubleWidthMode,
PrintSizeAndCharacterWidth_DoubleWidthForOneLineESC,
PrintSizeAndCharacterWidth_SelectCondensedModeESC,
PrintSizeAndCharacterWidth_SelectEliteWidth12cpi,
PrintSizeAndCharacterWidth_SelectPicaWidth10cpi,
PrintSizeAndCharacterWidth_CancelDoubleWidthMode,
PrintSizeAndCharacterWidth_SelectDoubleWidthMode,
PrintEnhancement_CancelUnderlining,
PrintEnhancement_SelectUnderlining,
PrintEnhancement_SelectBold,
PrintEnhancement_CancelBold,
PrintEnhancement_SelectDoubleStrikeMode,
PrintEnhancement_CancelDoubleStrikeMode,
PrintEnhancement_SelectSuperscript,
PrintEnhancement_SelectSubscript,
PrintEnhancement_CancelSuperscriptSubscript,
CharacterSets_SelectItalicMode,
CharacterSets_CancelItalicMode,
CharacterSets_EnablePrintingOfExtendedCharacters,
CharacterSets_DisablePrintingOfExtendedCharacters,
CharacterSets_International_USA,
CharacterSets_International_France,
CharacterSets_International_Germany,
CharacterSets_International_UnitedKingdom,
CharacterSets_International_DenmarkI,
CharacterSets_International_Sweden,
CharacterSets_International_Italy,
CharacterSets_International_Spain,
CharacterSets_International_Japan,
CharacterSets_International_Norway,
CharacterSets_International_DenmarkII,
CharacterSets_International_SpainII,
CharacterSets_International_LatinAmerica,
CharacterSets_SelectItalicCharacterSet,
CharacterSets_SelectEpsonCharacterSet,
CharacterSets_Table_PC850_Multilingual,
CharacterSets_Table_PC851_Greek,
CharacterSets_Table_PC853_Turkish,
CharacterSets_Table_PC855_Cyrillic,
CharacterSets_Table_PC860_Portuguese,
CharacterSets_Table_PC863_CanadianFrench,
CharacterSets_Table_PC865_Norwegian,
CharacterSets_Table_PC852_EasternEurope,
CharacterSets_Table_PC857_Turkish,
CharacterSets_Table_PC862_Hebrew,
CharacterSets_Table_PC864_Arabic,
CharacterSets_Table_PC866_Russian,
}
private class MatrixPrinterCommand
{
public MatrixPrinterCommandType commandType;
public string command;
public MatrixPrinterCommand(MatrixPrinterCommandType commandType, string command)
{
this.commandType = commandType;
this.command = command.Replace(" ", "");
for(int i = 0; i < characterTable.Length; i++)
this.command = this.command.Replace(characterTable[i], characterTableCode[i]);
}
private String[] characterTable = new String[] {
"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL",
"BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"
};
private String[] characterTableCode = new String[] {
"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06", "\x07",
"\x08", "\x09", "\x0A", "\x0B", "\x0C", "\x0D", "\x0E", "\x0F",
"\x10", "\x11", "\x12", "\x13", "\x14", "\x15", "\x16", "\x17",
"\x18", "\x19", "\x1A", "\x1B", "\x1C", "\x1D", "\x1E", "\x1F",
};
}
private MatrixPrinterCommand[] commands = new MatrixPrinterCommand[] {
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_Beeper, "BEL"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_SelectPrinter, "DC1"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_DeselectPrinter, "DC3"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_TurnCutSheetFeederControlOff, "ESC EM 0"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_TurnCutSheetFeederControlOn, "ESC EM 4"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_DisablePaperOutSensor, "ESC 8"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_EnablePaperOutSensor, "ESC 9"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_SelectUnidirectionalModeForOneLine, "ESC <"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_InitializePrinter, "ESC @"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_CancelUnidirectionalMode, "ESC U 0"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_SelectUnidirectionalMode, "ESC U 1"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_TurnHalfSpeedModeOff, "ESC s 0"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_TurnHalfSpeedModeOn, "ESC s 1"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_SkipOverPerforation, "ESC N 1"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrinterOperation_CancelSkipOverPerforation, "ESC O"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintingStyle_Pica16DoubleStrike, "ESC ! 0"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintingStyle_Elite32DoubleWide, "ESC ! 1"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintingStyle_Condensed64Italic, "ESC ! 4"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintingStyle_Emphasized128Underline, "ESC ! 8"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintingStyle_SelectNLQRomanFont, "ESC k 0"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintingStyle_SelectNLQSansSerifFont, "ESC k 1"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintingStyle_SelectDraftMode, "ESC x 0"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintingStyle_SelectNLQMode, "ESC x 1"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintSizeAndCharacterWidth_SelectDoubleWidthForOneLine, "SO"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintSizeAndCharacterWidth_SelectCondensedMode, "SI"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintSizeAndCharacterWidth_CancelCondensedMode, "DC2"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintSizeAndCharacterWidth_CancelOneLineDoubleWidthMode, "DC4"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintSizeAndCharacterWidth_DoubleWidthForOneLineESC, "ESC SO"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintSizeAndCharacterWidth_SelectCondensedModeESC, "ESC SI"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintSizeAndCharacterWidth_SelectEliteWidth12cpi, "ESC M"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintSizeAndCharacterWidth_SelectPicaWidth10cpi, "ESC P"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintSizeAndCharacterWidth_CancelDoubleWidthMode, "ESC W 0"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintSizeAndCharacterWidth_SelectDoubleWidthMode, "ESC W 1"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintEnhancement_CancelUnderlining, "ESC - 0"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintEnhancement_SelectUnderlining, "ESC - 1"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintEnhancement_SelectBold, "ESC E"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintEnhancement_CancelBold, "ESC F"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintEnhancement_SelectDoubleStrikeMode, "ESC G"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintEnhancement_CancelDoubleStrikeMode, "ESC H"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintEnhancement_SelectSuperscript, "ESC S 0"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintEnhancement_SelectSubscript, "ESC S 1"),
new MatrixPrinterCommand(MatrixPrinterCommandType.PrintEnhancement_CancelSuperscriptSubscript, "ESC T"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_SelectItalicMode, "ESC 4"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_CancelItalicMode, "ESC 5"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_EnablePrintingOfExtendedCharacters, "ESC 6"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_DisablePrintingOfExtendedCharacters, "ESC 7"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_USA, "ESC R \x00"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_France, "ESC R \x01"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_Germany, "ESC R \x02"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_UnitedKingdom, "ESC R \x03"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_DenmarkI, "ESC R \x04"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_Sweden, "ESC R \x05"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_Italy, "ESC R \x06"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_Spain, "ESC R \x07"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_Japan, "ESC R \x08"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_Norway, "ESC R \x09"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_DenmarkII, "ESC R \x0A"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_SpainII, "ESC R \x0B"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_International_LatinAmerica, "ESC R \x0C"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_SelectItalicCharacterSet, "ESC t NUL"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_SelectEpsonCharacterSet, "ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC850_Multilingual, "ESC ( t ETX NUL SOH \x03 NUL ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC851_Greek, "ESC ( t ETX NUL SOH \x04 NUL ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC853_Turkish, "ESC ( t ETX NUL SOH \x05 NUL ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC855_Cyrillic, "ESC ( t ETX NUL SOH \x06 NUL ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC860_Portuguese, "ESC ( t ETX NUL SOH \x07 NUL ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC863_CanadianFrench, "ESC ( t ETX NUL SOH \x08 NUL ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC865_Norwegian, "ESC ( t ETX NUL SOH \x09 NUL ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC852_EasternEurope, "ESC ( t ETX NUL SOH \x0A NUL ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC857_Turkish, "ESC ( t ETX NUL SOH \x0B NUL ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC862_Hebrew, "ESC ( t ETX NUL SOH \x0C NUL ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC864_Arabic, "ESC ( t ETX NUL SOH \x0D NUL ESC t SOH"),
new MatrixPrinterCommand(MatrixPrinterCommandType.CharacterSets_Table_PC866_Russian, "ESC ( t ETX NUL SOH \x0E NUL ESC t SOH"),
};
public void SendPrinterCommand(MatrixPrinterCommandType commandType)
{
foreach(MatrixPrinterCommand command in commands)
if(command.commandType == commandType) this.Write(command.command);
}
////////////////////////////////////////////////////////////////////////////////
//
// RawPrinterHelper http://support.microsoft.com/?kbid=322091
//
////////////////////////////////////////////////////////////////////////////////
// Structure and API declarions:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
}
}
A sada jedan primer:
Code:
foreach(string name in PrinterSettings.InstalledPrinters) {
Console.WriteLine(name);
}
MatrixPrinter printer = MatrixPrinter.OpenPrinter(PrinterSettings.InstalledPrinters[4], "Moj dokument");
if(printer != null) {
printer.SendPrinterCommand(MatrixPrinter.MatrixPrinterCommandType.CharacterSets_EnablePrintingOfExtendedCharacters);
printer.SendPrinterCommand(MatrixPrinter.MatrixPrinterCommandType.CharacterSets_Table_PC852_EasternEurope);
printer.SetPageLengthInInches(12);
printer.EnableCondensedMode(false);
printer.Write("Proba ", true);
printer.WriteLine("Proba šđčćŠĐČĆ", true);
printer.FormFeed();
printer.Close();
}
Metoda OpenPrinter ce inicijalizovati novu instancu klase (ako bas hoces mozes i rucno inicijalizovati sa new pa otvoriti sa Open i proslediti naziv stampaca). Ovoj metodi saljes naziv stampaca na koji se stampa i prosledjujes naziv dokumenta (ovaj naziv ce se pojaviti u printer queue).
Prve dve komande ukljucuju mogucnost stampanja nasih karaktera.
SetPageLengthInInches - ovom metodom mozes menjati velicinu papira, default je 12", kada stampas na perforiranom A4 papiru, stane do 70 redova teksta, ako od 70-reda posaljes
FormFeed stampac ce se pravilno pozicionirati na sledecu stranu. U slucaju da na primer stampas virmane koji su 4", ovom metodom podesi na 4" i posle svakog FormFeed-a stampac ce pravilno odmeravati po 4" papira.
EnableCondensedMode - ovom metodom mozes ukljucivati/iskljucivati kondenzovan mod, mislim da za LX-300 kada nije ukljucen kondenzovan mod stane oko 80 karaktera u jedan red a kada se ukljuci onda stane oko 130 karaktera, na onom velikom A3 stampacu (sto je tezak nekih 50kg) mislim da moze da stane oko 120 karaktera kada nije ukljucen kondenzovan mod na A4 stranicu (ima uzi font) a jos vise kada je ukljucen, na POS stampace ne znam koliko moze da stane. Odradi test, posalji string dovoljne velicine da pokrije celu liniju pa izbroj rucno koliko moze da stane u oba moda pa se time rukovodi.
Write i
WriteLine - ovim metodama mozes da saljes stringove na stampu. WriteLine automatski posle stampe string salje LineFeed, tj prelazi u novi red. Drugi parametar je opcion i ako ga izostavis smatra se da je false. Ako ostavis da je false onda se nasi karakteri nece stampati pravilno, ovim paramterom se vrsi mapiranje ovih karaktera na sa ASCII tabele na tabelu matricnog stampaca.
FormFeed - saljes ovu komandu da se zapocne stampa na sledecoj strani. Kod A4 formata maksimalno mozes da odstampas 70 redova, ako zapocnes 71 i zatim posaljes FormFeed preskocice celu stranicu je si u stvari ti vec zapoceo sledecu stranicu. Ne moras da stampas prazne redove do 70-tog, slobodno posalji FormFeed u bilo kom trenutnku.