VintaSoft Barcode .NET SDK v10.0
GetBarcodeAsGraphicsPath Method (BarcodeWriter)
Example 




Vintasoft.Barcode Namespace > BarcodeWriter Class : GetBarcodeAsGraphicsPath Method
Returns barcode as System.Drawing.Drawing2D.GraphicsPath.
Syntax
'Declaration
 
Public Function GetBarcodeAsGraphicsPath() As GraphicsPath
'Usage
 
Dim instance As BarcodeWriter
Dim value As GraphicsPath
 
value = instance.GetBarcodeAsGraphicsPath()
public GraphicsPath GetBarcodeAsGraphicsPath()
public: GraphicsPath* GetBarcodeAsGraphicsPath(); 
public:
GraphicsPath^ GetBarcodeAsGraphicsPath(); 

Return Value

Remarks
This method ignores values of properties:
Example
This example shows how to add barcode in vector form to PDF document.
Imports System.Drawing   
Imports System.Drawing.Drawing2D   
Imports System.IO   
Imports Vintasoft.Barcode   
Imports Vintasoft.Imaging.Pdf   
Imports Vintasoft.Imaging.Pdf.Drawing   
Imports Vintasoft.Imaging.Pdf.Tree   
   
''' <summary>   
''' This test shows how to draw barcodes on each page of PDF document.   
''' Barcode writer generated barcode as graphics path.   
''' </summary>   
''' <remarks>   
''' <b>Important:</b> Vintasoft.Barcode.dll, Vintasoft.Imaging.dll and Vintasoft.Imaging.Pdf.dll    
''' assemblies are necessary for executing this code.   
''' </remarks>   
Class GetBarcodeAsGraphicsPathExample   
   
	''' <summary>   
	''' Runs the test.   
	''' </summary>   
	Public Shared Sub Test()   
		Dim barcodeValue As String = "ABCDEF1234ABCDEF1234AB"   
   
		' draw barcode on each page of PDF document, barcode size is 4 millimeters,   
		' recommend print quality: 600dpi, recommend scan quality : 400-600dpi   
		DrawBarcodeOnPdfPages("test.pdf", 4, barcodeValue)   
   
		' draw barcode on each page of PDF document, barcode size is 10 millimeters,   
		' recommend print quality: 300-600dpi, recommend scan quality : 150dpi   
		DrawBarcodeOnPdfPages("test.pdf", 10, barcodeValue)   
	End Sub   
   
	''' <summary>   
	''' Draws barcode on each page of PDF document.   
	''' </summary>   
	Public Shared Sub DrawBarcodeOnPdfPages(pdfFilename As String, barcodeSizeInMillimeters As Single, barcodeValue As String)   
		' X padding - 20mm   
		Dim xPadding As Single = TraslateMillimetersToPdfPageUserSpace(20)   
		' Y padding - 15mm   
		Dim yPadding As Single = TraslateMillimetersToPdfPageUserSpace(15)   
   
		' translate barcode size, in millimeters, to the PdfPage UserSpace units   
		Dim barcodeSize As Single = TraslateMillimetersToPdfPageUserSpace(barcodeSizeInMillimeters)   
   
		' create the barcode writer   
		Dim barcodeWriter As New BarcodeWriter()   
   
		' specify that writer must generate DataMatrix barcode   
		barcodeWriter.Settings.Barcode = BarcodeType.DataMatrix   
   
		' open PDF document   
		Using document As New PdfDocument(pdfFilename)   
			' for each PDF page   
			For i As Integer = 0 To document.Pages.Count - 1   
				Dim page As PdfPage = document.Pages(i)   
   
				' set the barcode value   
				barcodeWriter.Settings.Value = barcodeValue   
   
				' generate barcode as graphics path   
				Using barcodePath As GraphicsPath = barcodeWriter.GetBarcodeAsGraphicsPath()   
					' transform barcode path   
					Using m As New Matrix()   
						' translate barcode to right-top corner of page   
						Dim x As Single = page.MediaBox.Right - barcodeSize - xPadding   
						Dim y As Single = page.MediaBox.Bottom - barcodeSize - yPadding   
						m.Translate(x, y)   
   
						' gets generated barcode size   
						Dim genaratedBarcodeSize As Single = barcodePath.GetBounds().Height   
   
						' scale barcode    
						Dim scale As Single = barcodeSize / genaratedBarcodeSize   
						m.Scale(scale, scale)   
   
						barcodePath.Transform(m)   
					End Using   
   
					' fill barcode path   
					Using g As PdfGraphics = page.GetGraphics()   
						Dim brush As New PdfBrush(Color.Black)   
						g.FillPath(brush, barcodePath)   
					End Using   
				End Using   
			Next   
   
			Dim filePosfix As String = String.Format("_marked{0}mm.pdf", barcodeSizeInMillimeters)   
			Dim outPdfFilename As String = Path.GetFileNameWithoutExtension(pdfFilename) & filePosfix   
   
			' save PDF document   
			document.Save(outPdfFilename)   
		End Using   
	End Sub   
   
	''' <summary>   
	''' Translates value from millimeters to the PdfPage UserSpace units (72 DPI).   
	''' </summary>   
	Private Shared Function TraslateMillimetersToPdfPageUserSpace(value As Single) As Single   
		Return ((value / 10F) / 2.54F * 72F)   
	End Function   
   
End Class
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using Vintasoft.Barcode;
using Vintasoft.Imaging.Pdf;
using Vintasoft.Imaging.Pdf.Drawing;
using Vintasoft.Imaging.Pdf.Tree;
            
/// <summary>
/// This test shows how to draw barcodes on each page of PDF document.
/// Barcode writer generated barcode as graphics path.
/// </summary>
/// <remarks>
/// <b>Important:</b> Vintasoft.Barcode.dll, Vintasoft.Imaging.dll and Vintasoft.Imaging.Pdf.dll 
/// assemblies are necessary for executing this code.
/// </remarks>
class GetBarcodeAsGraphicsPathExample
{
            
    /// <summary>
    /// Runs the test.
    /// </summary>
    public static void Test()
    {
        string barcodeValue = "ABCDEF1234ABCDEF1234AB";
            
        // draw barcode on each page of PDF document, barcode size is 4 millimeters,
        // recommend print quality: 600dpi, recommend scan quality : 400-600dpi
        DrawBarcodeOnPdfPages("test.pdf", 4, barcodeValue);
            
        // draw barcode on each page of PDF document, barcode size is 10 millimeters,
        // recommend print quality: 300-600dpi, recommend scan quality : 150dpi
        DrawBarcodeOnPdfPages("test.pdf", 10, barcodeValue);
    }
    
    /// <summary>
    /// Draws barcode on each page of PDF document.
    /// </summary>
    public static void DrawBarcodeOnPdfPages(
        string pdfFilename,
        float barcodeSizeInMillimeters,
        string barcodeValue)
    {
        // X padding - 20mm
        float xPadding = TraslateMillimetersToPdfPageUserSpace(20);
        // Y padding - 15mm
        float yPadding = TraslateMillimetersToPdfPageUserSpace(15);
            
        // translate barcode size, in millimeters, to the PdfPage UserSpace units
        float barcodeSize = TraslateMillimetersToPdfPageUserSpace(barcodeSizeInMillimeters);
            
        // create the barcode writer
        BarcodeWriter barcodeWriter = new BarcodeWriter();
            
        // specify that writer must generate DataMatrix barcode
        barcodeWriter.Settings.Barcode = BarcodeType.DataMatrix;
            
        // open PDF document
        using (PdfDocument document = new PdfDocument(pdfFilename))
        {
            // for each PDF page
            for (int i = 0; i < document.Pages.Count; i++)
            {
                PdfPage page = document.Pages[i];
            
                // set the barcode value
                barcodeWriter.Settings.Value = barcodeValue;
            
                // generate barcode as graphics path
                using (GraphicsPath barcodePath = barcodeWriter.GetBarcodeAsGraphicsPath())
                {
                    // transform barcode path
                    using (Matrix m = new Matrix())
                    {
                        // translate barcode to right-top corner of page
                        float x = page.MediaBox.Right - barcodeSize - xPadding;
                        float y = page.MediaBox.Bottom - barcodeSize - yPadding;
                        m.Translate(x, y);
            
                        // gets generated barcode size
                        float genaratedBarcodeSize = barcodePath.GetBounds().Height;
            
                        // scale barcode 
                        float scale = barcodeSize / genaratedBarcodeSize;
                        m.Scale(scale, scale);
            
                        barcodePath.Transform(m);
                    }
            
                    // fill barcode path
                    using (PdfGraphics g = page.GetGraphics())
                    {
                        PdfBrush brush = new PdfBrush(Color.Black);
                        g.FillPath(brush, barcodePath);
                    }
                }
            }
            
            string filePosfix = string.Format("_marked{0}mm.pdf", barcodeSizeInMillimeters);
            string outPdfFilename = Path.GetFileNameWithoutExtension(pdfFilename) + filePosfix;
            
            // save PDF document
            document.Save(outPdfFilename);
        }
    }
            
    /// <summary>
    /// Translates value from millimeters to the PdfPage UserSpace units (72 DPI).
    /// </summary>
    private static float TraslateMillimetersToPdfPageUserSpace(float value)
    {
        return ((value / 10f) / 2.54f * 72f);
    }
            
}
Requirements

Target Platforms: Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

See Also

Reference

BarcodeWriter Class
BarcodeWriter Members