VintaSoft Barcode .NET SDK 14.1: Documentation for .NET developer
In This Topic
    Add vector barcode to PDF document
    In This Topic
    Example: Here is an example that shows how to add vector barcodes to pages of PDF document.

    /// <summary>
    /// Adds a vector barcode to a PDF document.
    /// </summary>
    /// <param name="pdfFilename">A path to a PDF document.</param>
    public static void AddVectorBarcodeToPdfDocument(string pdfFilename)
    {
        // create the barcode writer
        using (Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter())
        {
            // specify that Code 128 barcode must be created
            barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.DataMatrix;
            // padding size
            float padding = 5;
    
            // open PDF document
            using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename))
            {
                // for each PDF page
                for (int i = 0; i < document.Pages.Count; i++)
                {
                    // get reference to PDF page
                    Vintasoft.Imaging.Pdf.Tree.PdfPage page = document.Pages[i];
                    // use page number as the barcode value
                    barcodeWriter.Settings.Value = (i + 1).ToString();
                    // get barcode as graphic path
                    using (Vintasoft.Imaging.Drawing.IGraphicsPath barcodePath = Convert(barcodeWriter.GetBarcodeAsBarcodePath()))
                    {
                        // translate graphic path for drawing of barcode on the right-bottom corner of page
                        float barcodeWidth = barcodePath.GetBounds().Width;
                        Vintasoft.Imaging.AffineMatrix m = Vintasoft.Imaging.AffineMatrix.CreateTranslation(page.MediaBox.Right - barcodeWidth - padding, padding);
                        barcodePath.Transform(m);
    
                        // get Graphics object for drawing on PDF page
                        using (Vintasoft.Imaging.Pdf.Drawing.PdfGraphics g = page.GetGraphics())
                        {
                            Vintasoft.Imaging.Pdf.Drawing.PdfBrush brush = new Vintasoft.Imaging.Pdf.Drawing.PdfBrush(System.Drawing.Color.Black);
                            // fill barcode path on Graphics object
                            g.FillPath(brush, barcodePath);
                        }
                    }
                }
    
                // save PDF document
                string resultFileName = System.IO.Path.GetFileNameWithoutExtension(pdfFilename) + "_marked.pdf";
                document.Save(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(pdfFilename), resultFileName));
            }
        }            
    }
    
    /// <summary>
    /// Converts <see cref="Vintasoft.Barcode.BarcodePathData"/> to <see cref="Vintasoft.Imaging.Drawing.IGraphicsPath"/>.
    /// </summary>
    /// <param name="barcodePathData">The barcode path data.</param>
    private static Vintasoft.Imaging.Drawing.IGraphicsPath Convert(Vintasoft.Barcode.BarcodePathData barcodePathData)
    {
        System.Drawing.PointF[] points = Vintasoft.Barcode.GdiConverter.Convert(barcodePathData.Points);
        byte[] pointTypes = barcodePathData.GetPointTypesAsByteArray();
        return Vintasoft.Imaging.Drawing.DrawingFactory.Default.CreateGraphicsPath(new Vintasoft.Imaging.Drawing.GraphicsPathData(points, pointTypes));
    }
    
    ''' <summary>
    ''' Adds a vector barcode to a PDF document.
    ''' </summary>
    ''' <param name="pdfFilename">A path to a PDF document.</param>
    Public Shared Sub AddVectorBarcodeToPdfDocument(pdfFilename As String)
        ' create the barcode writer
        Using barcodeWriter As New Vintasoft.Barcode.BarcodeWriter()
            ' specify that Code 128 barcode must be created
            barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.DataMatrix
            ' padding size
            Dim padding As Single = 5
    
            ' open PDF document
            Using document As New Vintasoft.Imaging.Pdf.PdfDocument(pdfFilename)
                ' for each PDF page
                For i As Integer = 0 To document.Pages.Count - 1
                    ' get reference to PDF page
                    Dim page As Vintasoft.Imaging.Pdf.Tree.PdfPage = document.Pages(i)
                    ' use page number as the barcode value
                    barcodeWriter.Settings.Value = (i + 1).ToString()
                    ' get barcode as graphic path
                    Using barcodePath As Vintasoft.Imaging.Drawing.IGraphicsPath = Convert(barcodeWriter.GetBarcodeAsBarcodePath())
                        ' translate graphic path for drawing of barcode on the right-bottom corner of page
                        Dim barcodeWidth As Single = barcodePath.GetBounds().Width
                        Dim m As Vintasoft.Imaging.AffineMatrix = Vintasoft.Imaging.AffineMatrix.CreateTranslation(page.MediaBox.Right - barcodeWidth - padding, padding)
                        barcodePath.Transform(m)
    
                        ' get Graphics object for drawing on PDF page
                        Using g As Vintasoft.Imaging.Pdf.Drawing.PdfGraphics = page.GetGraphics()
                            Dim brush As New Vintasoft.Imaging.Pdf.Drawing.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 = System.IO.Path.GetFileNameWithoutExtension(pdfFilename) & "_marked.pdf"
                document.Save(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(pdfFilename), resultFileName))
            End Using
        End Using
    End Sub
    
    ''' <summary>
    ''' Converts <see cref="Vintasoft.Barcode.BarcodePathData"/> to <see cref="Vintasoft.Imaging.Drawing.IGraphicsPath"/>.
    ''' </summary>
    ''' <param name="barcodePathData">The barcode path data.</param>
    Private Shared Function Convert(barcodePathData As Vintasoft.Barcode.BarcodePathData) As Vintasoft.Imaging.Drawing.IGraphicsPath
        Dim points As System.Drawing.PointF() = Vintasoft.Barcode.GdiConverter.Convert(barcodePathData.Points)
        Dim pointTypes As Byte() = barcodePathData.GetPointTypesAsByteArray()
        Return Vintasoft.Imaging.Drawing.DrawingFactory.[Default].CreateGraphicsPath(New Vintasoft.Imaging.Drawing.GraphicsPathData(points, pointTypes))
    End Function