Идея динамического заполнения отчета RDLC элементами в виде дерева ( готово), а затем отправить его на автоматическую печать. Здесь все становится сложнее. Размер отчета установлен на размер POS по умолчанию (от 80 мм до 3276 мм).
Идея состоит в том, чтобы начать с высоты (нужно поэкспериментировать, какое значение подходит лучше всего)
А затем начните добавлять данные в зависимости от количества элементов, агрегированных в отчет, чтобы это было примерно так
Код: Выделить всё
double paperWidthMm = report_imprime_comanda.LocalReport.GetDefaultPageSettings().PaperSize.Width;
double totalHeight = 30;
foreach (TreeNode parentNode in parentNodes)
{
if (parentNode.Tag is Comanda comandaMain)
{
double itemHeight = GetItemHeight(comandaMain);
totalHeight += itemHeight;
filteredComandas.Add(comandaMain);
}
}
// Dynamically set the page height based on total content height
int paperWidthHundredthsOfInch = (int)(paperWidthMm / 25.4 * 100);
int paperHeightHundredthsOfInch = (int)(totalHeight / 25.4 * 100);
PageSettings pageSettings = new PageSettings
{
PaperSize = new PaperSize("Custom", paperWidthHundredthsOfInch, paperHeightHundredthsOfInch)
};
MessageBox.Show("Paper Size: " + pageSettings, "Tamaño papel", MessageBoxButtons.OK, MessageBoxIcon.Warning);
report_imprime_comanda.SetPageSettings(pageSettings);
// Bind filtered data to the report
report_imprime_comanda.LocalReport.DataSources.Clear();
report_imprime_comanda.LocalReport.DataSources.Add(new ReportDataSource("Comandas", filteredComandas));
// Refresh the report to apply the changes
report_imprime_comanda.RefreshReport();
PrintPOS printPOS = new PrintPOS();
// Render and print the report
printPOS.PrintReportToPrinter(report_imprime_comanda.LocalReport, printerName);
Код: Выделить всё
private double GetItemHeight(Comanda item)
{
// Base height for each comanda item
double baseHeight = 30;
// Additional height for each child note
double noteHeight = 15;
// Calculate total height
double totalHeight = baseHeight;
// Split the notes by ';' to count them
if (!string.IsNullOrEmpty(item.NOTAS))
{
string[] notes = item.NOTAS.Split(';');
totalHeight += notes.Length * noteHeight;
}
return totalHeight;
}
Код: Выделить всё
public void PrintReportToPrinter(LocalReport report, string printerName)
{
// Configure print settings for the POS printer
PrinterSettings printerSettings = new PrinterSettings
{
PrinterName = printerName,
Copies = 1,
PrintToFile = false,
};
// Create a PrintDocument object
PrintDocument printDoc = new PrintDocument
{
PrinterSettings = printerSettings
};
printDoc.PrintPage += (sender, args) =>
{
// Render the report to an image
Warning[] warnings;
string[] streamIds;
string mimeType;
string encoding;
string extension;
byte[] bytes = report.Render(
"Image", null, out mimeType, out encoding, out extension,
out streamIds, out warnings);
using (System.Drawing.Image image = System.Drawing.Image.FromStream(new System.IO.MemoryStream(bytes)))
{
args.Graphics.DrawImage(image, args.MarginBounds);
args.HasMorePages = false;
}
};
// Print the document
printDoc.Print();
}
}
Любая помощь приветствуется.
Заранее спасибо!
Я жду для печати динамических данных непосредственно на POS-принтере.
Подробнее здесь: https://stackoverflow.com/questions/787 ... t-printing