GetBarcodeAsBarcodePath() Метод (BarcodeWriter)
Этот метод игнорирует значения свойств:
Этот C#/VB. NET code показывает, как добавить штрих-код в векторной форме в PDF документ.
Imports System.IO
Imports Vintasoft.Barcode
Imports Vintasoft.Imaging
Imports Vintasoft.Imaging.Drawing
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
Using 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 IGraphicsPath = Convert(barcodeWriter.GetBarcodeAsBarcodePath())
' transform barcode path
Dim m As New AffineMatrix()
' 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.TranslatePrepend(x, y)
' gets generated barcode size
Dim genaratedBarcodeSize As Single = barcodePath.GetBounds().Height
' scale barcode
Dim scale As Single = barcodeSize / genaratedBarcodeSize
m.ScalePrepend(scale, scale)
barcodePath.Transform(m)
' fill barcode path
Using g As PdfGraphics = page.GetGraphics()
Dim brush As New PdfBrush(System.Drawing.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 Using
End Sub
''' <summary>
''' Converts <see cref="BarcodePathData"/> to <see cref="IGraphicsPath"/>.
''' </summary>
''' <param name="barcodePathData">The barcode path data.</param>
Private Shared Function Convert(barcodePathData As BarcodePathData) As IGraphicsPath
Dim points As System.Drawing.PointF() = Vintasoft.Barcode.GdiConverter.Convert(barcodePathData.Points)
Dim pointTypes As Byte() = barcodePathData.GetPointTypesAsByteArray()
Return DrawingFactory.[Default].CreateGraphicsPath(New GraphicsPathData(points, pointTypes))
End Function
''' <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.IO;
using Vintasoft.Barcode;
using Vintasoft.Imaging;
using Vintasoft.Imaging.Drawing;
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
using (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 (IGraphicsPath barcodePath = Convert(barcodeWriter.GetBarcodeAsBarcodePath()))
{
// transform barcode path
AffineMatrix m = new AffineMatrix();
// translate barcode to right-top corner of page
float x = page.MediaBox.Right - barcodeSize - xPadding;
float y = page.MediaBox.Bottom - barcodeSize - yPadding;
m.TranslatePrepend(x, y);
// gets generated barcode size
float genaratedBarcodeSize = barcodePath.GetBounds().Height;
// scale barcode
float scale = barcodeSize / genaratedBarcodeSize;
m.ScalePrepend(scale, scale);
barcodePath.Transform(m);
// fill barcode path
using (PdfGraphics g = page.GetGraphics())
{
PdfBrush brush = new PdfBrush(System.Drawing.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>
/// Converts <see cref="BarcodePathData"/> to <see cref="IGraphicsPath"/>.
/// </summary>
/// <param name="barcodePathData">The barcode path data.</param>
private static IGraphicsPath Convert(BarcodePathData barcodePathData)
{
System.Drawing.PointF[] points = Vintasoft.Barcode.GdiConverter.Convert(barcodePathData.Points);
byte[] pointTypes = barcodePathData.GetPointTypesAsByteArray();
return DrawingFactory.Default.CreateGraphicsPath(new GraphicsPathData(points, pointTypes));
}
/// <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);
}
}
Целевые платформы: .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5