VintaSoft Barcode .NET SDK v10.0
In This Topic
    Add vector barcode to PDF document in WPF
    In This Topic

    Example: Here is an example that shows how to add vector barcodes to pages of PDF document.

    ' Important: You need Vintasoft.WpfBarcode.dll, Vintasoft.Imaging.dll,
       ' Vintasoft.Imaging.Wpf.dll, Vintasoft.Imaging.Pdf.dll assemblies
    ' to run this code.
    Public Shared Sub MarkPDF(pdfFilename As String)
            ' create the barcode writer
            Dim barcodeWriter As New BarcodeWriter()
            ' specify that Code 128 barcode must be created
            barcodeWriter.Settings.Barcode = BarcodeType.DataMatrix
            ' padding size
            Dim padding As Single = 5
    
            ' open PDF document
            Using document As New PdfDocument(pdfFilename)
                    ' for each PDF page
                    For i As Integer = 0 To document.Pages.Count - 1
                            ' get reference to PDF page
                            Dim page As PdfPage = document.Pages(i)
                            ' use page number as the barcode value
                            barcodeWriter.Settings.Value = (i + 1).ToString()
                            ' get barcode as graphic path
                            Dim barcodePathGeometry As System.Windows.Media.PathGeometry = barcodeWriter.GetBarcodeAsPathGeometry()
                            Using barcodePath As GraphicsPath = WpfObjectConverter.CreateDrawingGraphicsPath(barcodePathGeometry)
                                    ' translate graphic path for drawing of barcode on the right-bottom corner of page
                                    Using m As New Matrix()
                                            Dim barcodeWidth As Single = barcodePath.GetBounds().Width
                                            m.Translate(page.MediaBox.Right - barcodeWidth - padding, padding)
                                            barcodePath.Transform(m)
                                    End Using
                                    ' get Graphics object for drawing on PDF page
                                    Using g As PdfGraphics = page.GetGraphics()
                                            Dim brush As New PdfBrush(System.Drawing.Color.Black)
                                            ' fill barcode path on Graphics object
                                            g.FillPath(brush, barcodePath)
                                    End Using
                            End Using
                    Next
    
                    ' save PDF document
                    Dim resultFileName As String = Path.GetFileNameWithoutExtension(pdfFilename) & "_marked.pdf"
                    document.Save(Path.Combine(Path.GetDirectoryName(pdfFilename), resultFileName))
            End Using
    End Sub
                  
    
    // Important: You need Vintasoft.WpfBarcode.dll, Vintasoft.Imaging.dll,
    // Vintasoft.Imaging.Wpf.dll, Vintasoft.Imaging.Pdf.dll assemblies
    // to run this code.
    public static void MarkPDF(string pdfFilename)
    {
        // create the barcode writer
        BarcodeWriter barcodeWriter = new BarcodeWriter();
        // specify that Code 128 barcode must be created
        barcodeWriter.Settings.Barcode = BarcodeType.DataMatrix;
        // padding size
        float padding = 5;
    
        // open PDF document
        using (PdfDocument document = new PdfDocument(pdfFilename))
        {
            // for each PDF page
            for (int i = 0; i < document.Pages.Count; i++)
            {
                // get reference to PDF page
                PdfPage page = document.Pages[i];
                // use page number as the barcode value
                barcodeWriter.Settings.Value = (i + 1).ToString();
                // get barcode as graphic path
                System.Windows.Media.PathGeometry barcodePathGeometry = barcodeWriter.GetBarcodeAsPathGeometry();
                using (GraphicsPath barcodePath = WpfObjectConverter.CreateDrawingGraphicsPath(barcodePathGeometry))
                {
                    // translate graphic path for drawing of barcode on the right-bottom corner of page
                    using (Matrix m = new Matrix())
                    {
                        float barcodeWidth = barcodePath.GetBounds().Width;
                        m.Translate(page.MediaBox.Right - barcodeWidth - padding, padding);
                        barcodePath.Transform(m);
                    }
                    // get Graphics object for drawing on PDF page
                    using (PdfGraphics g = page.GetGraphics())
                    {
                        PdfBrush brush = new PdfBrush(System.Drawing.Color.Black);
                        // fill barcode path on Graphics object
                        g.FillPath(brush, barcodePath);
                    }
                }
            }
    
            // save PDF document
            string resultFileName = Path.GetFileNameWithoutExtension(pdfFilename) + "_marked.pdf";
            document.Save(Path.Combine(Path.GetDirectoryName(pdfFilename), resultFileName));
        }
    }