Նշեք տեքստը PDF էջում՝ օգտագործելով տեքստային նշագրման նշումը .NET-ում

Բլոգի կատեգորիա՝ PDF.NET

02.02.2021

PDF տեքստի նշագրման նշումը թույլ է տալիս նշել տեքստը (ընդգծել, գծիկով գծել, ընդգծել, ալիքավոր ընդգծել) PDF էջում:
VintaSoft PDF .NET Plug-in-ը տրամադրում է API, որը թույլ է տալիս ծրագրային եղանակով PDF էջին ավելացնել PDF Text Markup ծանոթագրություն:
SDK-ն նաև տրամադրում է PdfTextMarkupTool տեսողական գործիքը, որը թույլ է տալիս տեսողականորեն նշել տեքստը՝ օգտագործելով PDF Text Markup annotation WinForms-ում կամ WPF պատկերի դիտման ծրագրում ցուցադրվող PDF էջում: Ավելացված նշումները լիովին համատեղելի են PDF սպեցիֆիկացիաներին համապատասխանող ցանկացած PDF դիտման հետ:


Ահա C# կոդը, որը ցույց է տալիս, թե ինչպես ծրագրային եղանակով նշել տեքստը (highlight, strikeout, underline, squiggly underline) PDF էջում՝ օգտագործելով PDF տեքստային նշագրման նշումը.
/// <summary>
/// Highlights specified text in PDF document.
/// </summary>
/// <param name="inputPdfFilename">The input PDF filename.</param>
/// <param name="outputPdfFilename">The output PDF filename.</param>
/// <param name="text">A text that must be highlighted.</param>
public static void HiglightTextInPdfDocument(string inputPdfFilename, string outputPdfFilename, string text)
{
    MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, 
        Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.Highlight, System.Drawing.Color.Orange, text);
}

/// <summary>
/// Strikeouts specified text in PDF document.
/// </summary>
/// <param name="inputPdfFilename">The input PDF filename.</param>
/// <param name="outputPdfFilename">The output PDF filename.</param>
/// <param name="text">A text that must be striked out.</param>
public static void StrikeoutTextInPdfDocument(string inputPdfFilename, string outputPdfFilename, string text)
{
    MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, 
        Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.StrikeOut, System.Drawing.Color.Red, text);
}

/// <summary>
/// Underlines specified text in PDF document.
/// </summary>
/// <param name="inputPdfFilename">The input PDF filename.</param>
/// <param name="outputPdfFilename">The output PDF filename.</param>
/// <param name="text">A text that must be underlined.</param>
public static void UnderlineTextInPdfDocument(string inputPdfFilename, string outputPdfFilename, string text)
{
    MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, 
        Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.Underline, System.Drawing.Color.Green, text);
}

/// <summary>
/// Squiggly undelines specified text in PDF document.
/// </summary>
/// <param name="inputPdfFilename">The input PDF filename.</param>
/// <param name="outputPdfFilename">The output PDF filename.</param>
/// <param name="text">A text that must be squiggly underlined.</param>
public static void SquigglyUnderlineTextInPdfDocument(string inputPdfFilename, string outputPdfFilename, string text)
{
    MarkupTextInPdfDocument(inputPdfFilename, outputPdfFilename, 
        Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType.Squiggly, System.Drawing.Color.Blue, text);
}

/// <summary>
/// Markups specified text in PDF document.
/// </summary>
/// <param name="inputPdfFilename">The input PDF filename.</param>
/// <param name="outputPdfFilename">The output PDF filename.</param>
/// <param name="color">The markup annotation color.</param>
/// <param name="annotationType">The markup annotation type.</param>
/// <param name="text">A text to markup.</param>
private static void MarkupTextInPdfDocument(
    string inputPdfFilename,
    string outputPdfFilename,
    Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType annotationType,
    System.Drawing.Color color,
    string text)
{
    // open input PDF document
    using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(inputPdfFilename))
    {
        // for each PDF page
        foreach (Vintasoft.Imaging.Pdf.Tree.PdfPage page in document.Pages)
        {
            // extract text region of PDF page
            Vintasoft.Imaging.Text.TextRegion pageTextRegion = page.ExtractTextRegion(false);

            // search and markup all specified text on PDF page
            int index = 0;
            Vintasoft.Imaging.Text.TextRegion foundText;
            while (true)
            {
                // find next text
                foundText = pageTextRegion.FindText(text, ref index, false);
                if (foundText == null)
                    break;
                index += foundText.Symbols.Length;

                // markup found text
                MarkupText(page, foundText, annotationType, color);
            }
        }

        // save PDF document to output file
        document.SaveChanges(outputPdfFilename);
    }
}

/// <summary>
/// Markups specified text region in PDF page.
/// </summary>
/// <param name="page">The page.</param>
/// <param name="textRegion">The text region to markup.</param>
/// <param name="color">The markup annotation color.</param>
/// <param name="annotationType">The markup annotation type.</param>
private static void MarkupText(
    Vintasoft.Imaging.Pdf.Tree.PdfPage page,
    Vintasoft.Imaging.Text.TextRegion textRegion,
    Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationType annotationType,
    System.Drawing.Color color)
{
    // create markup annotation of specified type
    Vintasoft.Imaging.Pdf.Tree.Annotations.PdfTextMarkupAnnotation markupAnnotation =
        new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfTextMarkupAnnotation(page, annotationType);

    // set annotation properties
    markupAnnotation.SetTextRegion(textRegion);
    markupAnnotation.Color = color;
    markupAnnotation.Title = System.Environment.UserName;
    markupAnnotation.CreationDate = System.DateTime.Now;

    // generate annotation appearance
    markupAnnotation.UpdateAppearance();

    // add annotation to annotations of PDF page
    if (page.Annotations == null)
        page.Annotations = new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationList(page.Document);
    page.Annotations.Add(markupAnnotation);
}


Ահա C# կոդը, որը ցույց է տալիս, թե ինչպես տեսողականորեն նշել տեքստը WinForms պատկերի դիտման մեջ ցուցադրվող PDF էջում.
/// <summary>
/// Sets the PdfTextMarkupTool as current visual tool in specified image viewer.
/// </summary>
/// <param name="imageViewer">The image viewer.</param>
public static void SetPdfTextMarkupTool(Vintasoft.Imaging.UI.ImageViewer imageViewer)
{
    // create an instance of the PdfTextMarkupTool class
    Vintasoft.Imaging.Pdf.UI.PdfTextMarkupTool textMarkupTool = new Vintasoft.Imaging.Pdf.UI.PdfTextMarkupTool();

    // set text markup mode to "Highlight"
    textMarkupTool.MarkupMode = Vintasoft.Imaging.Pdf.UI.PdfTextMarkupToolMode.Highlight;

    // set color of markup annotation to "Orange"
    textMarkupTool.HighlightColor = System.Drawing.Color.Orange;

    // subscribe to the MarkupAnnotationCreated event
    textMarkupTool.MarkupAnnotationCreated += TextMarkupTool_MarkupAnnotationCreated;

    // get TextSelectionTool from image viewer
    Vintasoft.Imaging.UI.VisualTools.TextSelectionTool textSelectionTool = GetTextSelectionTool(imageViewer);

    // set the PdfTextMarkupTool+TextSelectionTool as visual tool of the image viewer
    imageViewer.VisualTool = new Vintasoft.Imaging.UI.VisualTools.CompositeVisualTool(textMarkupTool, textSelectionTool);
}

/// <summary>
/// Returns the text selection tool for specified image viewer.
/// </summary>
/// <param name="imageViewer">The image viewer.</param>
/// <returns>The text selection tool.</returns>
private static Vintasoft.Imaging.UI.VisualTools.TextSelectionTool GetTextSelectionTool(Vintasoft.Imaging.UI.ImageViewer imageViewer)
{
    // search a TextSelectionTool in visual tools of the image viewer
    Vintasoft.Imaging.UI.VisualTools.TextSelectionTool textSelectionTool =
        Vintasoft.Imaging.UI.VisualTools.VisualTool.FindVisualTool<Vintasoft.Imaging.UI.VisualTools.TextSelectionTool>(imageViewer.VisualTool);
    if (textSelectionTool != null)
        return textSelectionTool;

    // create an instance of text selection tool
    return new Vintasoft.Imaging.UI.VisualTools.TextSelectionTool();
}

/// <summary>
/// Handles the MarkupAnnotationCreated event of the PdfTextMarkupTool visual tool.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="Vintasoft.Imaging.Pdf.UI.PdfTextMarkupAnnotationEventArgs" /> instance containing the event data.</param>
private static void TextMarkupTool_MarkupAnnotationCreated(object sender, Vintasoft.Imaging.Pdf.UI.PdfTextMarkupAnnotationEventArgs e)
{
    // set user name as title of text markup annotation
    e.MarkupAnnotation.Title = System.Environment.UserName;
}



VintaSoft Imaging .NET SDK տեղադրման փաթեթը ներառում է նաև PDF Editor Demo և WPF PDF Editor Demo հավելվածները, որոնք թույլ են տալիս փորձարկել տեքստի նշագրման գործառույթը (ընդգծում, գծիկ, ընդգծում, ծալքավոր ընդգծում) PDF էջում՝ առանց որևէ կոդավորման:

Vintasoft PDF խմբագրիչի ցուցադրական գործիքագոտու միջոցով տեքստը նշելու և նշելու համար անհրաժեշտ է.
Ահա էկրանի նկար, որը ցույց է տալիս, թե ինչպես ընտրել և նշել տեքստը PDF էջում՝ օգտագործելով Vintasoft PDF խմբագրիչի ցուցադրական գործիքակազմը. Vintasoft PDF խմբագրիչի ցուցադրական տարբերակ. Ընտրեք և նշեք տեքստը PDF էջում՝ օգտագործելով գործիքակազմը



Ընտրված տեքստը Vintasoft PDF խմբագրիչի ցուցադրական տարբերակում համատեքստային ցանկի միջոցով նշելու համար անհրաժեշտ է.
Ահա էկրանի նկար, որը ցույց է տալիս, թե ինչպես նշել ընտրված տեքստը PDF էջում՝ օգտագործելով Vintasoft PDF խմբագրիչի ցուցադրական տարբերակի համատեքստային ցանկը. Vintasoft PDF խմբագրիչի ցուցադրական տարբերակ. Նշեք ընտրված տեքստը PDF էջում՝ օգտագործելով համատեքստային ցանկը