VintaSoft Imaging .NET SDK 15.1: Документация для .NET разработчика
В этом разделе
PDF: Добавление поля для штрих-кода в интерактивную форму PDF
В этом разделе
Вот C#/VB.NET код, который демонстрирует, как создать поле штрих-кода и добавить его в интерактивную форму PDF документа:
class PdfInteractiveFormBarcodeFieldExample
{
    /// <summary>
    /// Creates a PDF document with barcode field.
    /// </summary>
    /// <param name="filename">The filename.</param>
    /// <param name="barcodeSymbology">The barcode symbology.</param>
    public static void CreateDocumentWithBarcodeField(string filename,
        Vintasoft.Imaging.Pdf.Tree.InteractiveForms.BarcodeSymbologyType barcodeSymbology)
    {
        // create PDF document
        using (Vintasoft.Imaging.Pdf.PdfDocument document = 
            new Vintasoft.Imaging.Pdf.PdfDocument(
                new Vintasoft.Imaging.Pdf.PdfFormat("1.7", false, true)))
        {
            // create interactive form in PDF document
            document.InteractiveForm = 
                new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfDocumentInteractiveForm(document);
            document.InteractiveForm.CalculationOrder =
                new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormFieldList(document);

            // create an empty page
            Vintasoft.Imaging.Pdf.Tree.PdfPage page = new Vintasoft.Imaging.Pdf.Tree.PdfPage(
                document, Vintasoft.Imaging.PaperSizeKind.A4);
            page.Annotations = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(document);
            // add page to the document
            document.Pages.Add(page);


            float width = 200;
            float height = 25;
            // create a rectangle that defines the field position on PDF page
            System.Drawing.RectangleF rect = new System.Drawing.RectangleF(
                (page.Size.Width - width) / 2,
                page.Size.Height - height * 8,
                width, height);

            // create text field (barcode value)
            Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextField textField = 
                new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextField(
                    document, "TextField", rect);
            textField.TextValue = "test";
            textField.DefaultValue = textField.Value;
            // set the text default appearance
            textField.SetTextDefaultAppearance(
                document.FontManager.GetStandardFont(Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman), 
                12, System.Drawing.Color.Black);
            // set the appearance characteristics
            textField.Annotation.AppearanceCharacteristics =
                new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationAppearanceCharacteristics(document);
            textField.Annotation.AppearanceCharacteristics.BorderColor = System.Drawing.Color.Gray;
            textField.Annotation.BorderStyle = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyle(document);
            textField.Annotation.BorderStyle.Style = Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyleType.Beveled;
            document.InteractiveForm.AddField(textField, page);


            width = 200;
            height = 150;
            // create a rectangle that defines the field position on PDF page
            rect = new System.Drawing.RectangleF(
                (page.Size.Width - width) / 2,
                ((page.Size.Height - height) / 3) * 2,
                width, height);

            // create a barcode field
            Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormBarcodeField barcodeField = 
                new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormBarcodeField(
                    document, "BarcodeField", barcodeSymbology, rect);

            // set barcode field value
            barcodeField.TextValue = textField.TextValue;

            // set the barcode single module width to 0.02 inch
            barcodeField.ModuleWidth = 0.02f;

            // add the text field to the interactive form of document
            document.InteractiveForm.Fields.Add(barcodeField);

            // add annotation, associated with the text field, to the page
            page.Annotations.Add(barcodeField.Annotation);

            // create the calculate action of barcode value
            Vintasoft.Imaging.Pdf.Tree.PdfJavaScriptAction calculateValueAction = 
                new Vintasoft.Imaging.Pdf.Tree.PdfJavaScriptAction(document,
                    @"event.value = this.getField('TextField').value;");

            // add the barcode field to the calculated fields (calcualtion order)
            // of the document interactive form fields
            barcodeField.AdditionalActions = 
                new Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormFieldAdditionalActions(document);
            barcodeField.AdditionalActions.Calculate = calculateValueAction;
            document.InteractiveForm.CalculationOrder.Add(barcodeField);

            // update (create) field appearances
            document.InteractiveForm.UpdateAppearances();

            // save the document
            document.Save(filename);
        }
    }
}
Class PdfInteractiveFormBarcodeFieldExample
    ''' <summary>
    ''' Creates a PDF document with barcode field.
    ''' </summary>
    ''' <param name="filename">The filename.</param>
    ''' <param name="barcodeSymbology">The barcode symbology.</param>
    Public Shared Sub CreateDocumentWithBarcodeField(filename As String, barcodeSymbology As Vintasoft.Imaging.Pdf.Tree.InteractiveForms.BarcodeSymbologyType)
        ' create PDF document
        Using document As New Vintasoft.Imaging.Pdf.PdfDocument(New Vintasoft.Imaging.Pdf.PdfFormat("1.7", False, True))
            ' create interactive form in PDF document
            document.InteractiveForm = New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfDocumentInteractiveForm(document)
            document.InteractiveForm.CalculationOrder = New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormFieldList(document)

            ' create an empty page
            Dim page As New Vintasoft.Imaging.Pdf.Tree.PdfPage(document, Vintasoft.Imaging.PaperSizeKind.A4)
            page.Annotations = New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(document)
            ' add page to the document
            document.Pages.Add(page)


            Dim width As Single = 200
            Dim height As Single = 25
            ' create a rectangle that defines the field position on PDF page
            Dim rect As New System.Drawing.RectangleF((page.Size.Width - width) / 2, page.Size.Height - height * 8, width, height)

            ' create text field (barcode value)
            Dim textField As New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormTextField(document, "TextField", rect)
            textField.TextValue = "test"
            textField.DefaultValue = textField.Value
            ' set the text default appearance
            textField.SetTextDefaultAppearance(document.FontManager.GetStandardFont(Vintasoft.Imaging.Pdf.Tree.Fonts.PdfStandardFontType.TimesRoman), 12, System.Drawing.Color.Black)
            ' set the appearance characteristics
            textField.Annotation.AppearanceCharacteristics = New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationAppearanceCharacteristics(document)
            textField.Annotation.AppearanceCharacteristics.BorderColor = System.Drawing.Color.Gray
            textField.Annotation.BorderStyle = New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyle(document)
            textField.Annotation.BorderStyle.Style = Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationBorderStyleType.Beveled
            document.InteractiveForm.AddField(textField, page)


            width = 200
            height = 150
            ' create a rectangle that defines the field position on PDF page
            rect = New System.Drawing.RectangleF((page.Size.Width - width) / 2, ((page.Size.Height - height) / 3) * 2, width, height)

            ' create a barcode field
            Dim barcodeField As New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormBarcodeField(document, "BarcodeField", barcodeSymbology, rect)

            ' set barcode field value
            barcodeField.TextValue = textField.TextValue

            ' set the barcode single module width to 0.02 inch
            barcodeField.ModuleWidth = 0.02F

            ' add the text field to the interactive form of document
            document.InteractiveForm.Fields.Add(barcodeField)

            ' add annotation, associated with the text field, to the page
            page.Annotations.Add(barcodeField.Annotation)

            ' create the calculate action of barcode value
            Dim calculateValueAction As New Vintasoft.Imaging.Pdf.Tree.PdfJavaScriptAction(document, "event.value = this.getField('TextField').value;")

            ' add the barcode field to the calculated fields (calcualtion order)
            ' of the document interactive form fields
            barcodeField.AdditionalActions = New Vintasoft.Imaging.Pdf.Tree.InteractiveForms.PdfInteractiveFormFieldAdditionalActions(document)
            barcodeField.AdditionalActions.Calculate = calculateValueAction
            document.InteractiveForm.CalculationOrder.Add(barcodeField)

            ' update (create) field appearances
            document.InteractiveForm.UpdateAppearances()

            ' save the document
            document.Save(filename)
        End Using
    End Sub
End Class