|
| 1 | +--- |
| 2 | +title: Adding a Barcode to a PDF Document using PdfProcessing and the WinForms BarcodeView |
| 3 | +description: Learn how to generate a barcode and incorporate it into a PDF document using Telerik products. |
| 4 | +type: how-to |
| 5 | +page_title: How to Add a Barcode to a PDF with PdfProcessing and the WinForms BarcodeView |
| 6 | +slug: add-barcode-to-pdf-telerik |
| 7 | +tags: radpdfprocessing, document processing, barcode, pdf, telerik reporting, winforms, barcodeview |
| 8 | +res_type: kb |
| 9 | +ticketid: 1657503 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 2024.2.426| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +Learn how to generate a PDF document and add a barcode to it. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## Solution |
| 25 | + |
| 26 | +To add a barcode to a PDF document, consider using the [WinForms BarcodeView](https://docs.telerik.com/devtools/winforms/controls/barcodeview/overview): |
| 27 | + |
| 28 | +1\. First, [generate an image of the barcode](https://docs.telerik.com/devtools/winforms/controls/barcodeview/how-to/export-to-image) |
| 29 | + |
| 30 | +2\. Then, add the [image to the PDF document]({%slug pdf-from-images-with-radfixeddocumenteditor%}). Here is a sample code snippet: |
| 31 | + |
| 32 | + ```csharp |
| 33 | + Telerik.WinControls.UI.Barcode.QRCode qrCode1 = new Telerik.WinControls.UI.Barcode.QRCode(); |
| 34 | + RadBarcodeView radBarcodeView = new RadBarcodeView(); |
| 35 | + radBarcodeView.BindingContext = new BindingContext(); |
| 36 | + qrCode1.Version = 1; |
| 37 | + radBarcodeView.Symbology = qrCode1; |
| 38 | + radBarcodeView.Text = "radBarcodeView1"; |
| 39 | + radBarcodeView.Value = "1234567"; |
| 40 | + radBarcodeView.Invalidate(); |
| 41 | + System.Drawing.Image img = radBarcodeView.ExportToImage(200, 200); |
| 42 | + string imageFilePath = "barcodeImage.png"; |
| 43 | + img.Save(imageFilePath, System.Drawing.Imaging.ImageFormat.Png); |
| 44 | + |
| 45 | + RadFixedDocument fixedDocument = new RadFixedDocument(); |
| 46 | + RadFixedDocumentEditor documentEditor = new RadFixedDocumentEditor(fixedDocument); |
| 47 | + FileStream fileStream = new FileStream(imageFilePath, FileMode.Open); |
| 48 | + Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource _imageSource = new Telerik.Windows.Documents.Fixed.Model.Resources.ImageSource(fileStream); |
| 49 | + documentEditor.InsertImageInline(_imageSource); |
| 50 | + documentEditor.InsertLineBreak(); |
| 51 | + documentEditor.Dispose(); |
| 52 | + PdfFormatProvider provider = new PdfFormatProvider(); |
| 53 | + string outputFilePath = "output.pdf"; |
| 54 | + File.Delete(outputFilePath); |
| 55 | + using (Stream output = File.OpenWrite(outputFilePath)) |
| 56 | + { |
| 57 | + provider.Export(fixedDocument, output); |
| 58 | + } |
| 59 | + Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 60 | + ``` |
| 61 | + |
| 62 | +## Notes |
| 63 | + |
| 64 | +- The WinForms BarcodeView method is suitable for applications where a barcode image can be generated and saved before adding it to the PDF: [Generating a Bar Code Image outside WinForms](https://docs.telerik.com/devtools/winforms/knowledge-base/gridview-generating-barcode-image-non-winforms). |
| 65 | + |
| 66 | +## See Also |
| 67 | + |
| 68 | +- [RadPdfProcessing Documentation]({%slug radpdfprocessing-overview%}) |
| 69 | +- [WinForms BarcodeView](https://docs.telerik.com/devtools/winforms/controls/barcodeview/overview) |
| 70 | +- [Exporting BarcodeView to Image](https://docs.telerik.com/devtools/winforms/controls/barcodeview/how-to/export-to-image) |
| 71 | +- [Generating a Barcode Image outside WinForms](https://docs.telerik.com/devtools/winforms/knowledge-base/gridview-generating-barcode-image-non-winforms) |
| 72 | + |
0 commit comments