Почему мой код DrawString() не добавляет текст под логотипом? [закрыто]C#

Место общения программистов C#
Ответить
Anonymous
 Почему мой код DrawString() не добавляет текст под логотипом? [закрыто]

Сообщение Anonymous »

Документ для печатиЯ пытался добавить текст под логотипом при предварительном просмотре печати, но это не сработало. Я также пытался добавить Logo.Width, но он тоже не работает.
Строка e.Graphics.DrawString не работает, но другая информация работает хорошо; логотип добавляется правильно, но текст под логотипом не прорисовывается:
private void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
int leftMargin = e.MarginBounds.Left;
int topMargin = e.MarginBounds.Top;
int rightMargin = e.MarginBounds.Right;
int bottomMargin = e.MarginBounds.Bottom;

Font headerFont = new Font("Arial", 20, FontStyle.Bold);
Font subHeaderFont = new Font("Arial", 10, FontStyle.Regular);
Font regularFont = new Font("Arial", 10);
Font boldFont = new Font("Arial", 10, FontStyle.Bold);

int yPos = topMargin;

Graphics g = e.Graphics;

// Load an image from a file path
Image logo = Image.FromFile(Settings1.Default.LogoAddress);

// Get the page's printable area bounds.
int pageHeight = e.PageBounds.Height;
int pageWidth = e.PageBounds.Width;

// Get the image's dimensions.
int imageWidth = logo.Width;
int imageHeight = logo.Height;

// Calculate the X-coordinate for a centered image.
int xPoss = (pageWidth - imageWidth) / 2;

// Set the Y-coordinate for the top of the page.
int yPoss = 0;

// Calculate the X coordinate for horizontal centering
//float x = (e.PageBounds.Width - logo.Width) / 2;

// Set the Y coordinate for the top of the page (adjust as needed for a small top margin)
//float y = 0; // Or a small value like 10 for a slight margin
//float logoWidth = 100; // Desired width of the logo
//float logoHeight = (logo.Height / (int)logo.Width) * logoWidth; // Maintain aspect ratio

e.Graphics.DrawImage(logo, xPoss, yPoss, imageWidth, imageHeight);
e.HasMorePages = false;

// Header
string companyName = Settings1.Default.CompanyName;
string companyNameEng = Settings1.Default.CompanyNameEng;
string registrationNumber = "CR:" + Settings1.Default.CR2;
string footerAddress = Settings1.Default.Address2;
string footerPhone = "Phone:" + Settings1.Default.Phone2;

SizeF companyNameSize = e.Graphics.MeasureString(companyName, headerFont);
e.Graphics.DrawString(companyName, headerFont, Brushes.Black, leftMargin + (e.MarginBounds.Width - companyNameSize.Width) / 2, yPos);
yPos += (int)companyNameSize.Height + 2;

SizeF companyNameSize1 = e.Graphics.MeasureString(companyNameEng, headerFont);
e.Graphics.DrawString(companyNameEng, headerFont, Brushes.Black, leftMargin + (e.MarginBounds.Width - companyNameSize1.Width) / 2, yPos);
yPos += (int)companyNameSize.Height + 2;

SizeF regSize = e.Graphics.MeasureString(registrationNumber, subHeaderFont);
e.Graphics.DrawString(registrationNumber, subHeaderFont, Brushes.Black, leftMargin + (e.MarginBounds.Width - regSize.Width) / 2, yPos);
yPos += (int)regSize.Height + 15;

// Date
DateTime localDateTime = DateTime.Now;

// Customer Info
e.Graphics.DrawString($"Customer Name: {TextCustomerName.Text}", regularFont, Brushes.Black, leftMargin, yPos);
yPos += 20;
e.Graphics.DrawString($"Phone: {TextPhone.Text}", regularFont, Brushes.Black, leftMargin, yPos);
yPos += 30;

e.Graphics.DrawString($"Customer VAT: {TextVAT.Text}", regularFont, Brushes.Black, leftMargin, yPos);
yPos += 20;
e.Graphics.DrawString($"Date: {localDateTime}", regularFont, Brushes.Black, leftMargin, yPos);
yPos += 30;

e.Graphics.DrawString($"INV NO: {GenerateNextSerialNumber()}", regularFont, Brushes.Black, leftMargin, yPos);
yPos += 20;

// Table Header
int[] colWidths = { 300, 60, 60, 80, 80 };
string[] headers = { "Description", "Unit", "Qty", "Unit Price", "Amount" };
int xPos = leftMargin;

for (int i = 0; i < headers.Length; i++)
{
e.Graphics.DrawRectangle(Pens.Black, xPos, yPos, colWidths, 25);
e.Graphics.DrawString(headers, boldFont, Brushes.Black, new RectangleF(xPos + 3, yPos + 5, colWidths, 25));
xPos += colWidths;
}
yPos += 25;

// Table Rows
foreach (DataRow row in invoiceTable.Rows)
{
xPos = leftMargin;
e.Graphics.DrawRectangle(Pens.Black, xPos, yPos, colWidths[0], 25);
e.Graphics.DrawString(row["Description"].ToString(), regularFont, Brushes.Black, new RectangleF(xPos + 3, yPos + 5, colWidths[0], 25));
xPos += colWidths[0];

e.Graphics.DrawRectangle(Pens.Black, xPos, yPos, colWidths[1], 25);
e.Graphics.DrawString(row["Unit"].ToString(), regularFont, Brushes.Black, new RectangleF(xPos + 3, yPos + 5, colWidths[1], 25));
xPos += colWidths[1];

e.Graphics.DrawRectangle(Pens.Black, xPos, yPos, colWidths[2], 25);
e.Graphics.DrawString(row["Qty"].ToString(), regularFont, Brushes.Black, new RectangleF(xPos + 3, yPos + 5, colWidths[2], 25));
xPos += colWidths[2];

e.Graphics.DrawRectangle(Pens.Black, xPos, yPos, colWidths[3], 25);
e.Graphics.DrawString(string.Format("{0:C2}", row["UnitPrice"]), regularFont, Brushes.Black, new RectangleF(xPos + 3, yPos + 5, colWidths[3], 25));
xPos += colWidths[3];

e.Graphics.DrawRectangle(Pens.Black, xPos, yPos, colWidths[4], 25);
e.Graphics.DrawString(string.Format("{0:C2}", row["Amount"]), regularFont, Brushes.Black, new RectangleF(xPos + 3, yPos + 5, colWidths[4], 25));
xPos += colWidths[4];

yPos += 25;
if (yPos > bottomMargin - 200) // leave space for QR
{
e.HasMorePages = true;
return;
}
}

// Total
yPos += 10;
e.Graphics.DrawString(lblTotal.Text, boldFont, Brushes.Black, rightMargin - 190, yPos);
yPos += 40;

// Draw QR Code below invoice table & total
if (PicQRCode.Image != null)
{
int qrSize = 150;
Rectangle qrRect = new Rectangle(leftMargin, yPos, qrSize, qrSize);
e.Graphics.DrawImage(PicQRCode.Image, qrRect);
yPos += qrSize + 10;
}

// Footer
string footer = $" {footerPhone} | {footerAddress} ";
SizeF footerSize = e.Graphics.MeasureString(footer, subHeaderFont);
e.Graphics.DrawString(footer, subHeaderFont, Brushes.Black, leftMargin + (e.MarginBounds.Width - footerSize.Width) / 2, bottomMargin + 30);

e.HasMorePages = false;
}



Подробнее здесь: https://stackoverflow.com/questions/797 ... der-a-logo
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»