VintaSoft Barcode .NET SDK v11.0
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.

    ' Important: You need Vintasoft.Barcode.dll, Vintasoft.Imaging.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
    Using barcodePath As GraphicsPath = barcodeWriter.GetBarcodeAsGraphicsPath()
            ' 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(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
                  
    
    // The project, which uses this code, must have references to the following assemblies:
    // - Vintasoft.Barcode
    
    /// <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
        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 (System.Drawing.Drawing2D.GraphicsPath barcodePath = barcodeWriter.GetBarcodeAsGraphicsPath())
                {
                    // translate graphic path for drawing of barcode on the right-bottom corner of page
                    using (System.Drawing.Drawing2D.Matrix m = new System.Drawing.Drawing2D.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 (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));
        }
    }