void pdoc_PrintPage(object sender, PrintPageEventArgs e) { Font textFont = new Font("Arial", 6); //BarcodeLib.Barcode bc = new Barcode(); FNSKUBarcode bc = new FNSKUBarcode(label.FNSKU); Image i = bc.getBarCodeImage(1); Graphics graphics = e.Graphics; // float fontHeight = font.GetHeight(); int startX = 10; int startY = 10; int Offset = 10; //Offset = Offset + 20; graphics.DrawImage(i, new Point(startX, startY)); Offset += i.Height; graphics.DrawString(label.FNSKU, textFont, new SolidBrush(Color.Black), startX, startY + Offset); Offset += (int)textFont.GetHeight() + 2; graphics.DrawString(label.Title, textFont, new SolidBrush(Color.Black), startX, startY + Offset); }
public void create(FNSkuLabel label, string condition, int copies, Double widthInches, Double heightInches) { // Create a new PDF document PdfDocument document = new PdfDocument(); document.Info.Title = label.FNSKU; int ImageHeight = 0; for (int i = 0; i < copies; i++) { // Create an empty page PdfPage page = document.AddPage(); page.Orientation = PageOrientation.Landscape; page.Height = XUnit.FromInch(heightInches); page.Width = XUnit.FromInch(widthInches); // Get an XGraphics object for drawing using (XGraphics gfx = XGraphics.FromPdfPage(page)) { using (FNSKUBarcode bc = new FNSKUBarcode(label.FNSKU)) { var barCodeImage = bc.getBarCodeImage(1); ImageHeight = barCodeImage.Height; var xOffsetBcToCenter = (page.Width - barCodeImage.Width) / 2; if (xOffsetBcToCenter < 0) { xOffsetBcToCenter = 0; } gfx.DrawImage(barCodeImage, new Point((int)xOffsetBcToCenter + 20, 2)); } string labelToFit = label.Title; bool fit = false; int fontSize = 10; var font = new XFont("Verdana", fontSize); XSize sizeText = new XSize(); while (!fit) { // Create a font sizeText = gfx.MeasureString(labelToFit, font); if (sizeText.Width < page.Width) { fit = true; } else { if (fontSize > minFontSize) { fontSize--; font = new XFont("Verdana", fontSize); } else { string[] words = labelToFit.Split(' '); if (words.Length > 1) { if (words[words.Length / 2].Length <= 0) { labelToFit = labelToFit.Remove(labelToFit.Length / 2, 2); } else { labelToFit = labelToFit.Replace(words[words.Length / 2], ""); } } else { labelToFit = labelToFit.Remove(labelToFit.Length / 2, 1); } } } } Double xOffsetToCenter = (page.Width - sizeText.Width) / 2; gfx.DrawString(labelToFit, font, XBrushes.Black, new PointF((float)xOffsetToCenter, ImageHeight + 3)); sizeText = gfx.MeasureString(label.FNSKU, font); xOffsetToCenter = (page.Width - sizeText.Width) / 2; gfx.DrawString(label.FNSKU, font, XBrushes.Black, new PointF((float)xOffsetToCenter, (float)ImageHeight + (float)sizeText.Height + 6.0f)); sizeText = gfx.MeasureString(condition, font); xOffsetToCenter = (page.Width - sizeText.Width) / 2; gfx.DrawString(condition, font, XBrushes.Black, new PointF((float)xOffsetToCenter, (float)ImageHeight + (float)sizeText.Height * 2 + 8.0f)); // Draw the text //gfx.DrawString(label.title, font, XBrushes.Black, //new XRect(i.Height+1, 0, page.Width, page.Height), //XStringFormats.Center); // Save the document... } } const string filename = "HelloWorld.pdf"; document.Save(filename); Process.Start(filename); }
public Image getImage() { int dpiX = 0; int dpiY = 0; Bitmap test = new Bitmap(100, 100); using (Graphics g = Graphics.FromImage(test)) { dpiX = (int)g.DpiX; dpiY = (int)g.DpiY; } Bitmap b = new Bitmap((int)Width * dpiX * 3, (int)Height * dpiY * 3); int barcodeHeight = 100; using (Graphics gfx = Graphics.FromImage(b)) { gfx.Clear(Color.White); // get barcode using (FNSKUBarcode bc = new FNSKUBarcode(FNSKU)) { Image barcodeImage = bc.getBarCodeImage(3); barcodeHeight = barcodeImage.Height; gfx.DrawImage(barcodeImage, new Point(0, 2)); } string labelToFit = Title; bool fit = false; int fontSize = 10; var font = new Font("Veranda", fontSize); int minFontSize = 6; SizeF sizeText = new SizeF(); while (!fit) { // Create a font sizeText = gfx.MeasureString(labelToFit, font); if (sizeText.Width < b.Width) { fit = true; } else { if (fontSize > minFontSize) { fontSize--; font = new Font("Verdana", fontSize); } else { string[] words = labelToFit.Split(' '); if (words.Length > 1) { if (words[words.Length / 2].Length <= 0) { labelToFit = labelToFit.Remove(labelToFit.Length / 2, 2); } else { labelToFit = labelToFit.Replace(words[words.Length / 2], ""); } } else { labelToFit = labelToFit.Remove(labelToFit.Length / 2, 1); } } } } Double xOffsetToCenter = (b.Width - sizeText.Width) / 2; gfx.DrawString(labelToFit, font, Brushes.Black, new PointF((float)xOffsetToCenter, barcodeHeight + 3)); sizeText = gfx.MeasureString(FNSKU, font); xOffsetToCenter = (b.Width - sizeText.Width) / 2; gfx.DrawString(FNSKU, font, Brushes.Black, new PointF((float)xOffsetToCenter, (float)barcodeHeight + (float)sizeText.Height + 6.0f)); sizeText = gfx.MeasureString(Condition, font); xOffsetToCenter = (b.Width - sizeText.Width) / 2; gfx.DrawString(Condition, font, Brushes.Black, new PointF((float)xOffsetToCenter, (float)barcodeHeight + (float)sizeText.Height * 2 + 8.0f)); } return(b); }