原創|使用教程|編輯:龔雪|2016-03-17 16:44:49.000|閱讀 980 次
概述:本次教程將向您展示如何使用Barcode Professional SDK for .NET在PDF文檔中插入EAN/UPC復合型條形碼,2D PDF417條形碼和GS1線性條形碼。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
通過Barcode Professional SDK生成的條形碼具有高分辨率以使其獲得更利于輸出打印的質量。
以下為PDF文檔插入條形碼前后的圖像:
Dim pdfFile As String = "C:\Temp\SampleDoc.pdf" Dim pdfFileOut As String = "C:\Temp\SampleDoc_barcode.pdf" Dim dpi As Integer = 300 '1. Open the PDF file using a PdfReader Dim pdfReader As New iTextSharp.text.pdf.PdfReader(pdfFile) '2. Create a PdfStamper object for inserting or stamping the barcodes... Dim pdfStamper As New iTextSharp.text.pdf.PdfStamper(pdfReader, new System.IO.FileStream(pdfFileOut, System.IO.FileMode.Create, System.IO.FileAccess.Write)) '3. Get the page content from the PDF file by creating a PdfContentByte object Dim pdfPage As iTextSharp.text.pdf.PdfContentByte = pdfStamper.GetOverContent(1) Dim pageWidth As Single = pdfReader.GetPageSize(1).Width Dim pageHeight As Single = pdfReader.GetPageSize(1).Height '4. Generate the barcode images using Barcode Professional SDK '4.1. Generate the UPC-A CCB composite barcode. Using bc1 As New Neodynamic.SDK.Barcode.BarcodeProfessional() 'set the desired barcode symbology e.g. UPC-A CCB bc1.Symbology = Neodynamic.SDK.Barcode.Symbology.UpcACCB 'set the value to encode e.g. Primary Data = 01234567890 and Secondary Data = 991234-abcd bc1.Code = "01234567890|991234-abcd" 'set the barcode unit and sizes... bc1.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch bc1.BarWidth = 0.013 'the narrow bar width a.k.a. X value bc1.BarHeight = 1 'the height of the barcode bars bc1.GuardBarHeight = bc1.BarHeight + 5 * bc1.BarWidth 'the height of the guard bars only available for EAN/UPC barcodes bc1.QuietZoneWidth = 9 * bc1.BarWidth 'the quiet zone for UPC-A is 9 times the BarWidth value per GS1 spec 'Generate the barcode image content for inserting it into the PDF page 'For high quality, you can generated the barcode at, for instance, 300 dpi Dim barcodeImage1 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bc1.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi)) 'set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch) 'the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!! barcodeImage1.SetAbsolutePosition(pageWidth - 4.25F * 72F + ((3.25F - (barcodeImage1.Width / dpi))/2) * 72F , pageHeight - ((barcodeImage1.Height / dpi) * 72F) - (2.5F * 72F)) 'scale the image based on the image dpi barcodeImage1.ScalePercent(72F / CSng(dpi) * 100F) 'add the image object to the pdf page pdfPage.AddImage(barcodeImage1) End Using '4.2. Generate the 2D Micro PDF417 barcode. Using bc2 As New Neodynamic.SDK.Barcode.BarcodeProfessional() 'set the desired barcode symbology e.g. Micro PDF417 bc2.Symbology = Neodynamic.SDK.Barcode.Symbology.MicroPdf417 'set the value to encode e.g. ABCDE-1234567890-PDF bc2.Code = "ABCDE-1234567890-PDF" 'set the barcode unit and sizes... bc2.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch bc2.BarWidth = 0.01 'the narrow bar width a.k.a. X value bc2.BarRatio = 3 'in MicroPdf417 each bars' heigh in a row is calculated as BarWidth * BarRatio 'Generate the barcode image content for inserting it into the PDF page 'For high quality, you can generated the barcode at, for instance, 300 dpi Dim barcodeImage2 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bc2.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi)) 'set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch) 'the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!! barcodeImage2.SetAbsolutePosition(72F + ((3.25F - (barcodeImage2.Width / dpi)) / 2) * 72F, pageHeight - ((barcodeImage2.Height / dpi) * 72F) - (6F * 72F)); 'scale the image based on the image dpi barcodeImage2.ScalePercent(72F / CSng(dpi) * 100F) 'add the image object to the pdf page pdfPage.AddImage(barcodeImage2) End Using '4.3. Generate the GS1 DataBar-14 Stacked barcode. Using bc3 As New Neodynamic.SDK.Barcode.BarcodeProfessional() 'set the desired barcode symbology e.g. DataBar-14 stacked bc3.Symbology = Neodynamic.SDK.Barcode.Symbology.GS1DataBar14Stacked 'set the value to encode e.g. 0061414199999 bc3.Code = "0061414199999" 'set the barcode unit and sizes... bc3.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch bc3.BarWidth = 0.0208 'the narrow bar width a.k.a. X value 'Generate the barcode image content for inserting it into the PDF page 'For high quality, you can generated the barcode at, for instance, 300 dpi Dim barcodeImage3 As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(bc3.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi)) 'set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch) 'the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!! barcodeImage3.SetAbsolutePosition(pageWidth - 4.25F * 72F + ((3.25F - (barcodeImage3.Width / dpi)) / 2) * 72F, pageHeight - ((barcodeImage3.Height / dpi) * 72F) - (6f * 72F)); 'scale the image based on the image dpi barcodeImage3.ScalePercent(72F / CSng(dpi) * 100F) 'add the image object to the pdf page pdfPage.AddImage(barcodeImage3) End Using 'close PdfStamper pdfStamper.Close() 'close PdfReader pdfReader.Close()
string pdfFile = @"C:\Temp\SampleDoc.pdf"; string pdfFileOut = @"C:\Temp\SampleDoc_barcode.pdf"; int dpi = 300; //1. Open the PDF file using a PdfReader iTextSharp.text.pdf.PdfReader pdfReader = new iTextSharp.text.pdf.PdfReader(pdfFile); //2. Create a PdfStamper object for inserting or stamping the barcodes... iTextSharp.text.pdf.PdfStamper pdfStamper = new iTextSharp.text.pdf.PdfStamper(pdfReader, new System.IO.FileStream(pdfFileOut, System.IO.FileMode.Create, System.IO.FileAccess.Write)); //3. Get the page content from the PDF file by creating a PdfContentByte object iTextSharp.text.pdf.PdfContentByte pdfPage = pdfStamper.GetOverContent(1); float pageWidth = pdfReader.GetPageSize(1).Width; float pageHeight = pdfReader.GetPageSize(1).Height; //4. Generate the barcode images using Barcode Professional SDK //4.1. Generate the UPC-A CCB composite barcode. using (Neodynamic.SDK.Barcode.BarcodeProfessional bc1 = new Neodynamic.SDK.Barcode.BarcodeProfessional()) { //set the desired barcode symbology e.g. UPC-A CCB bc1.Symbology = Neodynamic.SDK.Barcode.Symbology.UpcACCB; //set the value to encode e.g. Primary Data = 01234567890 and Secondary Data = 991234-abcd bc1.Code = "01234567890|991234-abcd"; //set the barcode unit and sizes... bc1.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch; bc1.BarWidth = 0.013; //the narrow bar width a.k.a. X value bc1.BarHeight = 1; //the height of the barcode bars bc1.GuardBarHeight = bc1.BarHeight + 5 * bc1.BarWidth; //the height of the guard bars only available for EAN/UPC barcodes bc1.QuietZoneWidth = 9 * bc1.BarWidth; //the quiet zone for UPC-A is 9 times the BarWidth value per GS1 spec //Generate the barcode image content for inserting it into the PDF page //For high quality, you can generated the barcode at, for instance, 300 dpi iTextSharp.text.Image barcodeImage1 = iTextSharp.text.Image.GetInstance(bc1.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi)); //set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch) //the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!! barcodeImage1.SetAbsolutePosition(pageWidth - 4.25f * 72f + ((3.25f - (barcodeImage1.Width / dpi))/2) * 72f , pageHeight - ((barcodeImage1.Height / dpi) * 72f) - (2.5f * 72f)); //scale the image based on the image dpi barcodeImage1.ScalePercent(72f / (float)dpi * 100f); //add the image object to the pdf page pdfPage.AddImage(barcodeImage1); } //4.2. Generate the 2D Micro PDF417 barcode. using (Neodynamic.SDK.Barcode.BarcodeProfessional bc2 = new Neodynamic.SDK.Barcode.BarcodeProfessional()) { //set the desired barcode symbology e.g. Micro PDF417 bc2.Symbology = Neodynamic.SDK.Barcode.Symbology.MicroPdf417; //set the value to encode e.g. ABCDE-1234567890-PDF bc2.Code = "ABCDE-1234567890-PDF"; //set the barcode unit and sizes... bc2.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch; bc2.BarWidth = 0.01; //the narrow bar width a.k.a. X value bc2.BarRatio = 3; //in MicroPdf417 each bars' heigh in a row is calculated as BarWidth * BarRatio //Generate the barcode image content for inserting it into the PDF page //For high quality, you can generated the barcode at, for instance, 300 dpi iTextSharp.text.Image barcodeImage2 = iTextSharp.text.Image.GetInstance(bc2.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi)); //set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch) //the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!! barcodeImage2.SetAbsolutePosition(72f + ((3.25f - (barcodeImage2.Width / dpi)) / 2) * 72f, pageHeight - ((barcodeImage2.Height / dpi) * 72f) - (6f * 72f)); //scale the image based on the image dpi barcodeImage2.ScalePercent(72f / (float)dpi * 100f); //add the image object to the pdf page pdfPage.AddImage(barcodeImage2); } //4.3. Generate the GS1 DataBar-14 Stacked barcode. using (Neodynamic.SDK.Barcode.BarcodeProfessional bc3 = new Neodynamic.SDK.Barcode.BarcodeProfessional()) { //set the desired barcode symbology e.g. DataBar-14 stacked bc3.Symbology = Neodynamic.SDK.Barcode.Symbology.GS1DataBar14Stacked; //set the value to encode e.g. 0061414199999 bc3.Code = "0061414199999"; //set the barcode unit and sizes... bc3.BarcodeUnit = Neodynamic.SDK.Barcode.BarcodeUnit.Inch; bc3.BarWidth = 0.0208; //the narrow bar width a.k.a. X value //Generate the barcode image content for inserting it into the PDF page //For high quality, you can generated the barcode at, for instance, 300 dpi iTextSharp.text.Image barcodeImage3 = iTextSharp.text.Image.GetInstance(bc3.GetBarcodeImage(System.Drawing.Imaging.ImageFormat.Png, dpi)); //set the position of the barcode image. PDF uses Points as unit (1 point = 1/72 inch) //the starting point (x=0, y=0) in the PDF coord is the bottom-left corner!!! barcodeImage3.SetAbsolutePosition(pageWidth - 4.25f * 72f + ((3.25f - (barcodeImage3.Width / dpi)) / 2) * 72f, pageHeight - ((barcodeImage3.Height / dpi) * 72f) - (6f * 72f)); //scale the image based on the image dpi barcodeImage3.ScalePercent(72f / (float)dpi * 100f); //add the image object to the pdf page pdfPage.AddImage(barcodeImage3); } //close PdfStamper pdfStamper.Close(); //close PdfReader pdfReader.Close();
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn