嗨,我需要类似于热敏打印的打印票的帮助
Hi, I need help for print ticket similar to thermal print
我有一个嵌入了 iTextSharp 函数的 C# class,但我只能输出已知格式的 PDF。
这是代码:
public class TicketPDF
{
public TicketPDF()
{
myDocument.AddAuthor("Xauro sistemas");
myDocument.AddCreator("José Donoso Moscoso");
myDocument.AddTitle("Venta");
}
PdfWriter writer = null;
PdfContentByte cb = null;
ArrayList headerLines = new ArrayList();
ArrayList subHeaderLines = new ArrayList();
ArrayList items = new ArrayList();
ArrayList totales = new ArrayList();
ArrayList footerLines = new ArrayList();
private string headerImage = "";
bool _DrawItemHeaders = true;
int count = 0;
string path = "";
string file_name = "";
int maxChar = 40;
int maxCharDescription = 20;
int imageHeight = 0;
float leftMargin = 0;
float topMargin = 5;
static int fontSize = 7;
static BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);
static Font font = new Font(bfCourier, fontSize, Font.NORMAL, BaseColor.BLACK);
Document myDocument = new Document(PageSize.LETTER); //Aqui se ponen todos los objetos
string line = "";
#region Properties
public String Path
{
get { return path; }
set { path = value; }
}
public String FileName
{
get { return file_name; }
set { file_name = value; }
}
public String FullFileName
{
get { return (String.Format("{0}{1}", path, file_name)); }
}
public String HeaderImage
{
get { return headerImage; }
set { if (headerImage != value) headerImage = value; }
}
public int MaxChar
{
get { return maxChar; }
set { if (value != maxChar) maxChar = value; }
}
public bool DrawItemHeaders
{
set { _DrawItemHeaders = value; }
}
public int MaxCharDescription
{
get { return maxCharDescription; }
set { if (value != maxCharDescription) maxCharDescription = value; }
}
public int FontSize
{
get { return fontSize; }
set { if (value != fontSize) fontSize = value; }
}
public Font FontName
{
get { return font; }
set { if (value != font) font = value; }
}
#endregion
public void AddHeaderLine(string line)
{
headerLines.Add(line);
}
public void AddSubHeaderLine(string line)
{
subHeaderLines.Add(line);
}
public void AddItem(string cantidad, string item, string price)
{
TicketOrderItem newItem = new TicketOrderItem('?');
items.Add(newItem.GenerateItem(cantidad, item, price));
}
public void AddTotal(string name, string price)
{
TicketOrderTotal newTotal = new TicketOrderTotal('?');
totales.Add(newTotal.GenerateTotal(name, price));
}
public void AddFooterLine(string line)
{
footerLines.Add(line);
}
private string AlignRightText(int length)
{
string espacios = "";
int spaces = maxChar - length;
for (int x = 0; x < spaces; x++)
espacios += " ";
return espacios;
}
private string DottedLine()
{
string dotted = "";
for (int x = 0; x < maxChar; x++)
dotted += "=";
return dotted;
}
public bool Print()
{
try
{
//aqui para generar el PDF
writer = PdfWriter.GetInstance(myDocument, new FileStream(path + file_name, FileMode.Create));
myDocument.Open();
cb = writer.DirectContent;
cb.SetFontAndSize(font.BaseFont, fontSize);
cb.BeginText();
DrawImage();
DrawHeader();
DrawSubHeader();
DrawItems();
DrawTotales();
DrawFooter();
cb.EndText();
myDocument.Close();
return true;
}
catch (Exception ex)
{
throw (ex);
}
}
private float YPosition()
{
return (myDocument.PageSize.Height -
(topMargin + (count * font.CalculatedSize + imageHeight)));
}
private void DrawImage()
{
try
{
if ((headerImage != null) && (headerImage != ""))
{
if (File.Exists(headerImage))
{
Image logo = Image.GetInstance(headerImage);
double height = ((double)logo.Height / 58) * 15;
imageHeight = (int)Math.Round(height) + 3;
logo.SetAbsolutePosition(0,
myDocument.PageSize.Height - imageHeight);
logo.ScaleToFit(logo.Width, imageHeight);
myDocument.Add(logo);
}
}
}
catch (Exception ex) { throw (ex); }
}
private void DrawHeader()
{
try
{
foreach (string header in headerLines)
{
if (header.Length > maxChar)
{
int currentChar = 0;
int headerLength = header.Length;
while (headerLength > maxChar)
{
line = header.Substring(currentChar, maxChar);
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
currentChar += maxChar;
headerLength -= maxChar;
}
line = header;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line.Substring(currentChar, line.Length - currentChar));
count++;
}
else
{
line = header;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
}
}
DrawEspacio();
}
catch (Exception ex) { throw (ex); }
}
private void DrawSubHeader()
{
try
{
line = DottedLine();
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
DrawEspacio();
foreach (string subHeader in subHeaderLines)
{
if (subHeader.Length > maxChar)
{
int currentChar = 0;
int subHeaderLength = subHeader.Length;
while (subHeaderLength > maxChar)
{
line = subHeader;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line.Substring(currentChar, maxChar));
count++;
currentChar += maxChar;
subHeaderLength -= maxChar;
}
line = subHeader;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line.Substring(currentChar, line.Length - currentChar));
count++;
line = DottedLine();
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
DrawEspacio();
}
else
{
line = subHeader;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
line = DottedLine();
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
}
}
DrawEspacio();
}
catch (Exception ex) { throw (ex); }
}
private void DrawItems()
{
TicketOrderItem ordIt = new TicketOrderItem('?');
if (_DrawItemHeaders)
{
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText("CANT DESCRIPCION IMPORTE");
}
count++;
DrawEspacio();
foreach (string item in items)
{
line = ordIt.GetItemCantidad(item);
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
line = ordIt.GetItemPrice(item);
line = AlignRightText(line.Length) + line;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
string name = ordIt.GetItemName(item);
leftMargin = 0;
if (name.Length > maxCharDescription)
{
int currentChar = 0;
int itemLength = name.Length;
while (itemLength > maxCharDescription)
{
line = ordIt.GetItemName(item);
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(" " + line.Substring(currentChar, maxCharDescription));
count++;
currentChar += maxCharDescription;
itemLength -= maxCharDescription;
}
line = ordIt.GetItemName(item);
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(" " + line.Substring(currentChar,
maxCharDescription));
count++;
}
else
{
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(" " + ordIt.GetItemName(item));
count++;
}
}
leftMargin = 0;
DrawEspacio();
line = DottedLine();
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
DrawEspacio();
}
private void DrawTotales()
{
TicketOrderTotal ordTot = new TicketOrderTotal('?');
foreach (string total in totales)
{
line = ordTot.GetTotalCantidad(total);
line = AlignRightText(line.Length) + line;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
leftMargin = 0;
line = "" + ordTot.GetTotalName(total);
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
}
leftMargin = 0;
DrawEspacio();
DrawEspacio();
}
private void DrawFooter()
{
foreach (string footer in footerLines)
{
if (footer.Length > maxChar)
{
int currentChar = 0;
int footerLength = footer.Length;
while (footerLength > maxChar)
{
line = footer;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line.Substring(currentChar, maxChar));
count++;
currentChar += maxChar;
footerLength -= maxChar;
}
line = footer;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line.Substring(currentChar, maxChar));
count++;
}
else
{
line = footer;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
}
}
leftMargin = 0;
DrawEspacio();
}
private void DrawEspacio()
{
line = "";
cb.SetTextMatrix(leftMargin, YPosition());
cb.SetFontAndSize(font.BaseFont, fontSize);
cb.ShowText(line);
count++;
}
}
public class TicketOrderItem
{
char[] delimitador = new char[] { '?' };
public TicketOrderItem(char delimit)
{
delimitador = new char[] { delimit };
}
public string GetItemCantidad(string TicketOrderItem)
{
string[] delimitado = TicketOrderItem.Split(delimitador);
return delimitado[0];
}
public string GetItemName(string TicketOrderItem)
{
string[] delimitado = TicketOrderItem.Split(delimitador);
return delimitado[1];
}
public string GetItemPrice(string TicketOrderItem)
{
string[] delimitado = TicketOrderItem.Split(delimitador);
return delimitado[2];
}
public string GenerateItem(string cantidad,
string itemName, string price)
{
return cantidad + delimitador[0] +
itemName + delimitador[0] + price;
}
}
public class TicketOrderTotal
{
char[] delimitador = new char[] { '?' };
public TicketOrderTotal(char delimit)
{
delimitador = new char[] { delimit };
}
public string GetTotalName(string totalItem)
{
string[] delimitado = totalItem.Split(delimitador);
return delimitado[0];
}
public string GetTotalCantidad(string totalItem)
{
string[] delimitado = totalItem.Split(delimitador);
return delimitado[1];
}
public string GenerateTotal(string totalName,
string price)
{
return totalName + delimitador[0] + price;
}
}
用法如下:
public static void CrearBoleta(DTE dte, string pathOut = null)
{
TicketPDF boleta = new TicketPDF();
boleta.Path = Path.GetDirectoryName(pathOut)+"\";
boleta.FileName = $"{Path.GetFileNameWithoutExtension(pathOut)}.PDF";
boleta.HeaderImage = null;
boleta.AddHeaderLine("TICKET_HEADER_01");
boleta.AddHeaderLine("TICKET_HEADER_02");
boleta.AddHeaderLine("TICKET_HEADER_03");
boleta.AddHeaderLine("TICKET_HEADER_04");
boleta.AddHeaderLine("TICKET_HEADER_05");
boleta.AddSubHeaderLine("Dato 1");
boleta.AddSubHeaderLine("Dato 2");
boleta.AddSubHeaderLine("Dato 3");
boleta.AddSubHeaderLine("Dato 4");
boleta.AddSubHeaderLine("Dato 5");
int Cantidad = 1;
string Producto = "Stacarbone 100";
int Total = 1500;
// Detalles
boleta.AddItem( Cantidad.ToString(), Producto.Substring(0, 11), Total.ToString() );
boleta.Print();
}
并生成以下 Letter 格式的 PDF:
但是我需要生成一个类似于这个的 80mm 格式的 PDF,有谁知道如何让我的 class 生成这种格式?
我在我的项目中使用 iTextSharp 5.5.13.3。
问候和感谢
有一个以矩形作为参数的文档构造函数
public Document(Rectangle pageSize)
想必你可以传入任何你喜欢的尺寸
这里是矩形https://github.com/itext/itextsharp/blob/develop/src/core/iTextSharp/text/Rectangle.cs
我有一个嵌入了 iTextSharp 函数的 C# class,但我只能输出已知格式的 PDF。
这是代码:
public class TicketPDF
{
public TicketPDF()
{
myDocument.AddAuthor("Xauro sistemas");
myDocument.AddCreator("José Donoso Moscoso");
myDocument.AddTitle("Venta");
}
PdfWriter writer = null;
PdfContentByte cb = null;
ArrayList headerLines = new ArrayList();
ArrayList subHeaderLines = new ArrayList();
ArrayList items = new ArrayList();
ArrayList totales = new ArrayList();
ArrayList footerLines = new ArrayList();
private string headerImage = "";
bool _DrawItemHeaders = true;
int count = 0;
string path = "";
string file_name = "";
int maxChar = 40;
int maxCharDescription = 20;
int imageHeight = 0;
float leftMargin = 0;
float topMargin = 5;
static int fontSize = 7;
static BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);
static Font font = new Font(bfCourier, fontSize, Font.NORMAL, BaseColor.BLACK);
Document myDocument = new Document(PageSize.LETTER); //Aqui se ponen todos los objetos
string line = "";
#region Properties
public String Path
{
get { return path; }
set { path = value; }
}
public String FileName
{
get { return file_name; }
set { file_name = value; }
}
public String FullFileName
{
get { return (String.Format("{0}{1}", path, file_name)); }
}
public String HeaderImage
{
get { return headerImage; }
set { if (headerImage != value) headerImage = value; }
}
public int MaxChar
{
get { return maxChar; }
set { if (value != maxChar) maxChar = value; }
}
public bool DrawItemHeaders
{
set { _DrawItemHeaders = value; }
}
public int MaxCharDescription
{
get { return maxCharDescription; }
set { if (value != maxCharDescription) maxCharDescription = value; }
}
public int FontSize
{
get { return fontSize; }
set { if (value != fontSize) fontSize = value; }
}
public Font FontName
{
get { return font; }
set { if (value != font) font = value; }
}
#endregion
public void AddHeaderLine(string line)
{
headerLines.Add(line);
}
public void AddSubHeaderLine(string line)
{
subHeaderLines.Add(line);
}
public void AddItem(string cantidad, string item, string price)
{
TicketOrderItem newItem = new TicketOrderItem('?');
items.Add(newItem.GenerateItem(cantidad, item, price));
}
public void AddTotal(string name, string price)
{
TicketOrderTotal newTotal = new TicketOrderTotal('?');
totales.Add(newTotal.GenerateTotal(name, price));
}
public void AddFooterLine(string line)
{
footerLines.Add(line);
}
private string AlignRightText(int length)
{
string espacios = "";
int spaces = maxChar - length;
for (int x = 0; x < spaces; x++)
espacios += " ";
return espacios;
}
private string DottedLine()
{
string dotted = "";
for (int x = 0; x < maxChar; x++)
dotted += "=";
return dotted;
}
public bool Print()
{
try
{
//aqui para generar el PDF
writer = PdfWriter.GetInstance(myDocument, new FileStream(path + file_name, FileMode.Create));
myDocument.Open();
cb = writer.DirectContent;
cb.SetFontAndSize(font.BaseFont, fontSize);
cb.BeginText();
DrawImage();
DrawHeader();
DrawSubHeader();
DrawItems();
DrawTotales();
DrawFooter();
cb.EndText();
myDocument.Close();
return true;
}
catch (Exception ex)
{
throw (ex);
}
}
private float YPosition()
{
return (myDocument.PageSize.Height -
(topMargin + (count * font.CalculatedSize + imageHeight)));
}
private void DrawImage()
{
try
{
if ((headerImage != null) && (headerImage != ""))
{
if (File.Exists(headerImage))
{
Image logo = Image.GetInstance(headerImage);
double height = ((double)logo.Height / 58) * 15;
imageHeight = (int)Math.Round(height) + 3;
logo.SetAbsolutePosition(0,
myDocument.PageSize.Height - imageHeight);
logo.ScaleToFit(logo.Width, imageHeight);
myDocument.Add(logo);
}
}
}
catch (Exception ex) { throw (ex); }
}
private void DrawHeader()
{
try
{
foreach (string header in headerLines)
{
if (header.Length > maxChar)
{
int currentChar = 0;
int headerLength = header.Length;
while (headerLength > maxChar)
{
line = header.Substring(currentChar, maxChar);
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
currentChar += maxChar;
headerLength -= maxChar;
}
line = header;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line.Substring(currentChar, line.Length - currentChar));
count++;
}
else
{
line = header;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
}
}
DrawEspacio();
}
catch (Exception ex) { throw (ex); }
}
private void DrawSubHeader()
{
try
{
line = DottedLine();
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
DrawEspacio();
foreach (string subHeader in subHeaderLines)
{
if (subHeader.Length > maxChar)
{
int currentChar = 0;
int subHeaderLength = subHeader.Length;
while (subHeaderLength > maxChar)
{
line = subHeader;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line.Substring(currentChar, maxChar));
count++;
currentChar += maxChar;
subHeaderLength -= maxChar;
}
line = subHeader;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line.Substring(currentChar, line.Length - currentChar));
count++;
line = DottedLine();
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
DrawEspacio();
}
else
{
line = subHeader;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
line = DottedLine();
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
}
}
DrawEspacio();
}
catch (Exception ex) { throw (ex); }
}
private void DrawItems()
{
TicketOrderItem ordIt = new TicketOrderItem('?');
if (_DrawItemHeaders)
{
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText("CANT DESCRIPCION IMPORTE");
}
count++;
DrawEspacio();
foreach (string item in items)
{
line = ordIt.GetItemCantidad(item);
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
line = ordIt.GetItemPrice(item);
line = AlignRightText(line.Length) + line;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
string name = ordIt.GetItemName(item);
leftMargin = 0;
if (name.Length > maxCharDescription)
{
int currentChar = 0;
int itemLength = name.Length;
while (itemLength > maxCharDescription)
{
line = ordIt.GetItemName(item);
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(" " + line.Substring(currentChar, maxCharDescription));
count++;
currentChar += maxCharDescription;
itemLength -= maxCharDescription;
}
line = ordIt.GetItemName(item);
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(" " + line.Substring(currentChar,
maxCharDescription));
count++;
}
else
{
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(" " + ordIt.GetItemName(item));
count++;
}
}
leftMargin = 0;
DrawEspacio();
line = DottedLine();
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
DrawEspacio();
}
private void DrawTotales()
{
TicketOrderTotal ordTot = new TicketOrderTotal('?');
foreach (string total in totales)
{
line = ordTot.GetTotalCantidad(total);
line = AlignRightText(line.Length) + line;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
leftMargin = 0;
line = "" + ordTot.GetTotalName(total);
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
}
leftMargin = 0;
DrawEspacio();
DrawEspacio();
}
private void DrawFooter()
{
foreach (string footer in footerLines)
{
if (footer.Length > maxChar)
{
int currentChar = 0;
int footerLength = footer.Length;
while (footerLength > maxChar)
{
line = footer;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line.Substring(currentChar, maxChar));
count++;
currentChar += maxChar;
footerLength -= maxChar;
}
line = footer;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line.Substring(currentChar, maxChar));
count++;
}
else
{
line = footer;
cb.SetTextMatrix(leftMargin, YPosition());
cb.ShowText(line);
count++;
}
}
leftMargin = 0;
DrawEspacio();
}
private void DrawEspacio()
{
line = "";
cb.SetTextMatrix(leftMargin, YPosition());
cb.SetFontAndSize(font.BaseFont, fontSize);
cb.ShowText(line);
count++;
}
}
public class TicketOrderItem
{
char[] delimitador = new char[] { '?' };
public TicketOrderItem(char delimit)
{
delimitador = new char[] { delimit };
}
public string GetItemCantidad(string TicketOrderItem)
{
string[] delimitado = TicketOrderItem.Split(delimitador);
return delimitado[0];
}
public string GetItemName(string TicketOrderItem)
{
string[] delimitado = TicketOrderItem.Split(delimitador);
return delimitado[1];
}
public string GetItemPrice(string TicketOrderItem)
{
string[] delimitado = TicketOrderItem.Split(delimitador);
return delimitado[2];
}
public string GenerateItem(string cantidad,
string itemName, string price)
{
return cantidad + delimitador[0] +
itemName + delimitador[0] + price;
}
}
public class TicketOrderTotal
{
char[] delimitador = new char[] { '?' };
public TicketOrderTotal(char delimit)
{
delimitador = new char[] { delimit };
}
public string GetTotalName(string totalItem)
{
string[] delimitado = totalItem.Split(delimitador);
return delimitado[0];
}
public string GetTotalCantidad(string totalItem)
{
string[] delimitado = totalItem.Split(delimitador);
return delimitado[1];
}
public string GenerateTotal(string totalName,
string price)
{
return totalName + delimitador[0] + price;
}
}
用法如下:
public static void CrearBoleta(DTE dte, string pathOut = null)
{
TicketPDF boleta = new TicketPDF();
boleta.Path = Path.GetDirectoryName(pathOut)+"\";
boleta.FileName = $"{Path.GetFileNameWithoutExtension(pathOut)}.PDF";
boleta.HeaderImage = null;
boleta.AddHeaderLine("TICKET_HEADER_01");
boleta.AddHeaderLine("TICKET_HEADER_02");
boleta.AddHeaderLine("TICKET_HEADER_03");
boleta.AddHeaderLine("TICKET_HEADER_04");
boleta.AddHeaderLine("TICKET_HEADER_05");
boleta.AddSubHeaderLine("Dato 1");
boleta.AddSubHeaderLine("Dato 2");
boleta.AddSubHeaderLine("Dato 3");
boleta.AddSubHeaderLine("Dato 4");
boleta.AddSubHeaderLine("Dato 5");
int Cantidad = 1;
string Producto = "Stacarbone 100";
int Total = 1500;
// Detalles
boleta.AddItem( Cantidad.ToString(), Producto.Substring(0, 11), Total.ToString() );
boleta.Print();
}
并生成以下 Letter 格式的 PDF:
但是我需要生成一个类似于这个的 80mm 格式的 PDF,有谁知道如何让我的 class 生成这种格式?
我在我的项目中使用 iTextSharp 5.5.13.3。
问候和感谢
有一个以矩形作为参数的文档构造函数
public Document(Rectangle pageSize)
想必你可以传入任何你喜欢的尺寸
这里是矩形https://github.com/itext/itextsharp/blob/develop/src/core/iTextSharp/text/Rectangle.cs