Код: Выделить всё
public class ProductCategory
{
private int iD = -1;
private int parentID = -1;
private string catName = string.Empty;
private List
childCategories = new List();
}
Моя функция печати следующая:
private void CreateDocument()
{
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = printDocument1;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
pageNumber++;
PrintPageNumber(pageNumber, e);
yPos = e.MarginBounds.Top;
if (pageNumber == 1)
{
PrintCompanyInfo(e);
}
for (int j = catIndex; j < orderedCategories.Count; j++)
{
categoryFontSize = categoryDefaultFontSize;
yPos += mainCategoryPaddingY;
DoCategoryRecursive(orderedCategories[j], e);
catIndex = j;
}
}
private int catIndex = 0;
private void PrintCompanyInfo(PrintPageEventArgs e)
{
// Create image.
Image newImage = Image.FromFile(companyLogo);
// Create coordinates for upper-left corner of image.
int x = e.MarginBounds.Left;
int y = e.MarginBounds.Top;
// Draw image to screen.
e.Graphics.DrawImage(newImage, x, y, photoWidth, photoHeight);
e.Graphics.DrawString("Leo InnoTech (Pty) Ltd", new Font("Calibri", 28, FontStyle.Regular), Brushes.Black, new Point(350, 100));
e.Graphics.DrawString("Registration Number: 2024 / 378584 / 07", new Font("Arial", 9, FontStyle.Bold), Brushes.Black, new Point(410, 160));
e.Graphics.DrawString("Phone:", new Font("Calibri", 14, FontStyle.Regular), Brushes.Black, new Point(410, 210));
e.Graphics.DrawString("0611 864 142", new Font("Calibri", 14, FontStyle.Regular), Brushes.Black, new Point(490, 210));
e.Graphics.DrawString("Email Address:", new Font("Calibri", 14, FontStyle.Regular), Brushes.Black, new Point(350, 235));
e.Graphics.DrawString("leoinnotechsa@gmail.com", new Font("Calibri", 14, FontStyle.Regular), Brushes.Blue, new Point(490, 235));
yPos = y + photoHeight + productPaddingY;
}
private void PrintPageNumber(int pageNr, PrintPageEventArgs e)
{
int x = e.MarginBounds.Right;
int y = e.MarginBounds.Bottom;
e.Graphics.DrawString(pageNr.ToString(), new Font("Calibri", 14, FontStyle.Regular), Brushes.Black, new Point(x, y));
}
private void DoCategoryRecursive(ProductCategory cat, PrintPageEventArgs e)
{
// Load all Products for cat
List products = Products_Manager_SqlServer.Load_Products_ByCategoryID_Controller(connectionString, cat.ID);
if (products.Count > 0 || cat.ChildCategories.Count > 0)
{
if (yPos > e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
// Add category name to document
e.Graphics.DrawString(cat.CatName, new Font("Calibri", categoryFontSize, FontStyle.Bold), Brushes.Black, new Point(xPos, yPos));
PrintProducts(products, e);
if (cat.ChildCategories.Count > 0)
{
foreach (ProductCategory child in cat.ChildCategories)
{
yPos += minorCategoryPaddingY;
categoryFontSize -= fontSizeDecrement;
DoCategoryRecursive(child, e);
}
}
else
{
return;
}
}
else
{
return;
}
}
private void PrintProducts(List products, PrintPageEventArgs e)
{
string photoPath = defaultPhotoPath;
Image newImage = null;
foreach (Product product in products)
{
yPos += productPaddingY;
if (yPos + photoHeight > e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
// Get primary Photo
List photos = ProductPhotos_Manager_SqlServer.Load_PhotoPrimary_ByProduct_Controller(connectionString, product.ID);
if (photos.Count > 0)
{
photoPath = photos[0].Photo;
}
// Create image.
newImage = Image.FromFile(photoPath);
// Draw image to document.
e.Graphics.DrawImage(newImage, xPos + photoOffsetX, yPos, photoWidth, photoHeight);
yPos += photoHeight;
if (yPos > e.MarginBounds.Bottom)
{
e.HasMorePages = true;
return;
}
}
}
< /code>
Я был бы признателен за толку в правильном направлении. Или есть лучший способ справиться с этим, чем с рекурсивными функциями?>
Подробнее здесь: https://stackoverflow.com/questions/796 ... e-function
Мобильная версия