Exemple #1
0
        void DoPrint()
        {
            try
            {
                btnPrint.Enabled = false;
                if (!Directory.Exists("PDF"))
                {
                    Directory.CreateDirectory("PDF");
                }
                var pdf = new iTextSharp.text.Document(new iTextSharp.text.Rectangle(468, 288f));
                //var pdfFile = Application.StartupPath + @"c:\temp\temp100.pdf";
                var fileName = DateTime.Now.ToString("yyyyMMddHHmmss");
                var pdfFile  = @"PDF\" + fileName + @".pdf";

                //var ms = new MemoryStream();

                PdfWriter writer = PdfWriter.GetInstance(pdf, new FileStream(pdfFile, FileMode.Create));
                //PdfWriter pdfWriter = PdfWriter.GetInstance(pdf, ms);

                pdf.Open();

                //var qrBitmap = ZXingNetBarcodeHelper.GenerateQRCode("1234567890", 25, 25);
                //var txm = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromHbitmap(qrBitmap.GetHbitmap()), ImageFormat.Bmp);
                ////txm.ScaleAbsoluteHeight(40);
                //txm.SetAbsolutePosition(200, 200);
                //pdf.Add(txm);
                ////get the day of the year
                //int dayOfYear = DateTime.Now.DayOfYear;
                //var bitmapBarcode1 = ZXingNetBarcodeHelper.GenerateCode128(string.Format("2S{0}{1}9001",DateTime.Now.Year,DateTime.Now.DayOfYear), 25, 15);
                //var txm1 = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromHbitmap(bitmapBarcode1.GetHbitmap()), ImageFormat.Bmp);
                ////txm.ScaleAbsoluteHeight(40);
                //txm1.SetAbsolutePosition(100, 200);
                //pdf.Add(txm1);

                PdfContentByte cb = writer.DirectContent;
                cb.SetLineWidth(0.6f);
                cb.SetColorStroke(BaseColor.BLACK);

                cb.MoveTo(218, 5);
                cb.LineTo(218, 80);
                cb.Stroke();

                cb.MoveTo(5, 80);
                cb.LineTo(463, 80);
                cb.Stroke();

                cb.MoveTo(280, 80);
                cb.LineTo(280, 285);
                cb.Stroke();

                cb.MoveTo(5, 145);
                cb.LineTo(463, 145);
                cb.Stroke();

                cb.MoveTo(5, 210);
                cb.LineTo(463, 210);
                cb.Stroke();

                cb.MoveTo(280, 174);
                cb.LineTo(463, 174);
                cb.Stroke();

                cb.MoveTo(140, 210);
                cb.LineTo(140, 285);
                cb.Stroke();

                //remove the rect
                //cb.Rectangle(1, 1, pdf.PageSize.Width - 2, pdf.PageSize.Height - 2);
                //cb.Stroke();

                var ltc      = tbLotTrace.Text;
                var lpu      = tbLICPlate.Text;
                var custPN   = tbCustPart.Text;
                var po       = tbCustPO.Text;
                var rev      = tbREV.Text;
                var qty      = tbQTY.Text;
                var asn      = tbASN.Text;
                var partDesc = tbPartDesc.Text;
                var custPO   = tbCustPO.Text;

                List <string> shiptos = new List <string>();
                for (int i = 0; i < 5; i++)
                {
                    if (tbShipTo.Lines.Count() < i + 1)
                    {
                        shiptos.Add("");
                    }
                    else
                    {
                        shiptos.Add(tbShipTo.Lines[i]);
                    }
                }

                var bitmapBarcode1 = ZXingNetBarcodeHelper.GenerateCode128("1J" + lpu, 25, 25);
                var txm1           = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromHbitmap(bitmapBarcode1.GetHbitmap()), ImageFormat.Bmp);
                //txm.ScaleAbsoluteHeight(40);
                txm1.SetAbsolutePosition(15, 10);
                pdf.Add(txm1);

                var bitmapBarcode2 = ZXingNetBarcodeHelper.GenerateCode128("1T" + ltc, 25, 25);
                var txm2           = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromHbitmap(bitmapBarcode2.GetHbitmap()), ImageFormat.Bmp);
                //txm.ScaleAbsoluteHeight(40);
                txm2.SetAbsolutePosition(250, 24);
                txm2.ScaleAbsoluteWidth(200);
                pdf.Add(txm2);

                var bitmapBarcode3 = ZXingNetBarcodeHelper.GenerateCode128("K" + po, 25, 25);
                var txm3           = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromHbitmap(bitmapBarcode3.GetHbitmap()), ImageFormat.Bmp);
                //txm.ScaleAbsoluteHeight(40);
                txm3.SetAbsolutePosition(40, 90);
                //txm3.ScaleAbsoluteWidth(200);
                pdf.Add(txm3);

                var bitmapBarcode4 = ZXingNetBarcodeHelper.GenerateCode128("Q" + qty, 25, 25);
                var txm4           = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromHbitmap(bitmapBarcode4.GetHbitmap()), ImageFormat.Bmp);
                //txm4.ScaleAbsoluteHeight(40);
                txm4.SetAbsolutePosition(330, 111);
                //txm4.ScaleAbsoluteWidth(200);
                pdf.Add(txm4);

                var bitmapBarcode5 = ZXingNetBarcodeHelper.GenerateCode128("P" + custPN, 25, 25);
                var txm5           = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromHbitmap(bitmapBarcode5.GetHbitmap()), ImageFormat.Bmp);
                //txm5.ScaleAbsoluteHeight(40);
                txm5.SetAbsolutePosition(40, 150);
                //txm5.ScaleAbsoluteWidth(200);
                pdf.Add(txm5);

                var bitmapBarcode6 = ZXingNetBarcodeHelper.GenerateCode128("2S" + asn, 25, 25);
                var txm6           = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromHbitmap(bitmapBarcode6.GetHbitmap()), ImageFormat.Bmp);
                //txm6.ScaleAbsoluteHeight(40);
                txm6.SetAbsolutePosition(305, 238);
                //txm5.ScaleAbsoluteWidth(200);
                pdf.Add(txm6);

                var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

                cb.BeginText();
                cb.SetFontAndSize(baseFont, 16);
                cb.SetColorFill(BaseColor.BLACK);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", lpu), 15, 40, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", ltc), 250, 10, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", po), 40, 120, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", qty), 355, 95, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", custPN), 40, 180, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", rev), 360, 155, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", asn), 305, 222, 0);
                cb.EndText();

                cb.BeginText();
                cb.SetFontAndSize(baseFont, 8);
                cb.SetColorFill(BaseColor.BLACK);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("UNIT(1J):"), 10, 60, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("LIC PLATE-"), 10, 70, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("(1T):"), 220, 40, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("CODE"), 220, 50, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("TRACE"), 220, 60, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("LOT"), 220, 70, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("CUST"), 10, 135, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("PO#"), 10, 125, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("(K):"), 10, 115, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("QTY(Q):"), 285, 90, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("CUST PART"), 10, 199, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("(P):"), 10, 189, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("REV"), 285, 163, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("LEVEL:"), 285, 153, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("PART"), 285, 199, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("DESC:"), 285, 189, 0);

                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("ASN ID:"), 285, 275, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("(2S)"), 285, 267, 0);

                cb.EndText();

                cb.BeginText();
                cb.SetFontAndSize(baseFont, 7);
                cb.SetColorFill(BaseColor.BLACK);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("FROM:"), 10, 275, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("BizLink"), 60, 265, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("NO1.Industrial Zone,FengHuangVillage,"), 12, 255, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("FuYong Town,BaoAn District,ShenZhen"), 12, 245, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("City,GuangDong Province, China"), 12, 235, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("49693A"), 55, 225, 0);

                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("TO:"), 145, 275, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", shiptos[0]), 150, 265, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", shiptos[1]), 150, 255, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", shiptos[2]), 150, 245, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", shiptos[3]), 150, 235, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, string.Format("{0}", shiptos[4]), 150, 225, 0);

                cb.EndText();

                //verify position
                //cb.Rectangle(320,135,135,50);
                //cb.SetColorStroke(BaseColor.RED);
                //cb.Stroke();

                ColumnText ct = new ColumnText(cb);
                Phrase     p  = new Phrase(string.Format("{0}", partDesc), new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12));
                ct.SetSimpleColumn(p, 320, 170, 455, 210, 14, Element.ALIGN_LEFT);
                ct.Go();

                pdf.Close();

                Wait(1);
                axAcroPDF1.setShowToolbar(false);
                if (axAcroPDF1.LoadFile(pdfFile))
                {
                    axAcroPDF1.printAll();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                btnPrint.Enabled = true;
            }
        }
Exemple #2
0
        private void btnPrint_Click(object sender, EventArgs e)
        {
            var bmp = ZXingNetBarcodeHelper.GenerateDataMatrix("24-002424-05-01,YYWKSIXXXXXX", 100, 100);

            bmp.Save(@"c:\temp\test.bmp", ImageFormat.Bmp);
        }