Products Technologies Demo Docs Blog Support Company

X15: Inserting Client-Side Images using JavaScript

Using JavaScript API enhancements, client-side images can be added to documents. This article shows how to add a client-side image using a file input form element.

X15: Inserting Client-Side Images using JavaScript

Using JavaScript API enhancements, client-side images can be added to documents. All supported image types can be added as Base64 encoded strings and inserted at character position, anchored to paragraphs or fixed on pages.

The TXTextControl.ImageCollection class provides methods to add images from Base64 data into the document. The following methods can be used to add images:

Method Description
addAnchored Inserts a new image which is anchored to the specified text position.
addAnchoredAtLocation Inserts a new image which is anchored to the specified text position.
addAtFixedPosition Inserts a new image which has a fixed geometrical position in the document.
addInline Inserts an image inline, which means that it is treated in the text like a single character.

Consider the following ASP.NET MVC view that consists of a simple HTML file input element and a TextControl:

@using TXTextControl.Web.MVC
@using TXTextControl.Web

<input type="file" id="file" name="file" enctype="multipart/form-data" />

@Html.TXTextControl().TextControl(settings =>
{
    
}).Render()

The following JavaScript code reads the content of an opened local image, converts it to a Base64 encoded string and finally adds the image to the document:

document.getElementById('file').addEventListener('change', readFile, false);

function readFile(evt) {
    var files = evt.target.files;
    var file = files[0];
    var reader = new FileReader();

    reader.onload = function (event) {
        var binaryString =  btoa(event.target.result);
        TXTextControl.images.addInline(binaryString, -1);
    }

    reader.readAsBinaryString(file);
}

In the following sample, that can be downloaded directly from our GitHub account, an additional ribbon menu item is added that opens a dialog box to add local images.

Adding local images

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • Javascript: ImageCollection.addAnchored Method
  • Javascript: ImageCollection.addAnchoredAtLocation Method
  • Javascript: ImageCollection.addAtFixedPosition Method
  • Javascript: ImageCollection.addInline Method
  • TXTextControl.ImageCollection Class

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2015 or better
  • TX Text Control .NET Server X15 (trial sufficient)

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETForm FieldsHTML5

X15: Adding MS Word Compatible Fields and Form Elements to TXTextControl.Web

This article explains how to insert MS Word compatible form fields and form elements to documents.


ASP.NETASP.NET CorePDF/UA

PDF/UA vs. PDF/A-3a: Which Format Should You Use for Your Business Application?

In this blog post, we will explore the differences between PDF/UA and PDF/A-3a, helping you choose the right format for your business needs. We will discuss the key features, benefits, and use…


ASP.NETASP.NET CorePDF/UA

Validating PDF/UA Documents in .NET C#

Creating accessible and compliant PDF documents is becoming an increasingly important requirement across industries. In this blog post, we explore how to validate PDF/UA documents using Text…


ASP.NETWindows FormsWPF

Sneak Peek: TX Text Control 34.0 Coming November 2025

We are excited to announce the upcoming release of TX Text Control 34.0, scheduled for November 2025. This update brings a host of new features and improvements to enhance your document processing…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…