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.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…


ASP.NETASP.NET CoreDocument Viewer

High-Performance Text Replacement in Large DOCX Files using C# .NET

Learn how to efficiently replace text in large DOCX files using C# .NET and the ServerTextControl component from Text Control. This article demonstrates the performance benefits of using the…


ASP.NETASP.NET CoreDocument Viewer

Document Viewer 33.2.1 Released: New Event and Bug Fixes

This service pack includes important bug fixes and improvements to enhance the stability and performance of the Document Viewer. In addition, a new event has been introduced to provide developers…


ASP.NETAccessibilityASP.NET Core

Upcoming Support for PDF/UA Compliance and Tagged PDF Generation in Version 34.0

We are happy to announce that version 34.0 will support PDF/UA compliance and the creation of tagged PDF documents. This significant update demonstrates our ongoing commitment to accessibility by…