Example - Loading and Saving

TX Text Control supports various industry standard document formats such as DOC, DOCX, RTF, HTML, PDF and XML. All of these document formats can be created, converted or modified.

This sample shows how documents can be loaded and saved using ServerTextControl. When clicking on Load DOCX document, a DOCX file is loaded into an instance of ServerTextControl and saved into a byte array to be loaded into the DocumentViewer. The document is converted to PDF when the second button Download as PDF is clicked.

This example is part the Sample Explorer project which includes all samples in one. The sources for this sample project can be found here:

Language Project Location
C# %USERPROFILE%\Documents\TX Text Control 24.0.NET Server for ASP.NET\Samples\ASP.NET\CSharp\Sample Explorer\
VB.NET %USERPROFILE%\Documents\TX Text Control 24.0.NET Server for ASP.NET\Samples\ASP.NET\VB.NET\Sample Explorer\

You will find the source code and the ASPX page of this sample in the Solution structure tree under: Basics\loadsave.aspx

Example description

1. Click the button Load DOCX document in order to load a sample document into the ASP.NET DocumentViewer.

The used DocumentViewer is part of TX Text Control .NET Server for ASP.NET and can be used to view documents in read-only mode. The DOCX document is loaded into an instance of ServerTextControl. To show the variety of saving options, this demo saves the document as a byte array into a memory variable which is then loaded into the DocumentViewer.

[C#]
protected void Button1_Click(object sender, EventArgs e)
{
	// create a new instance of ServerTextControl
	using (TXTextControl.ServerTextControl tx =
		new TXTextControl.ServerTextControl())
	{
		// load the document
		tx.Create();
		tx.Load(Server.MapPath("/documents/sample.docx"), 
			TXTextControl.StreamType.WordprocessingML);

		// save the document in the internal format to a byte array
		byte[] data;
		tx.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);

		// load the byte array into the read-only DocumentViewer
		DocumentViewer1.LoadDocumentFromMemory(data, 
			TXTextControl.DocumentServer.FileFormat.InternalUnicodeFormat);
	}

	btn_DownloadDocument.Visible = true;
}
[Visual Basic]
Protected Sub Button1_Click(sender As Object, e As EventArgs)
	' create a new instance of ServerTextControl
	Using tx As New TXTextControl.ServerTextControl()
		' load the document
		tx.Create()
		tx.Load(Server.MapPath("/documents/sample.docx"), TXTextControl.StreamType.WordprocessingML)

		' save the document in the internal format to a byte array
		Dim data As Byte()
		tx.Save(data, TXTextControl.BinaryStreamType.InternalUnicodeFormat)

		' load the byte array into the read-only DocumentViewer
		DocumentViewer1.LoadDocumentFromMemory(data, TXTextControl.DocumentServer.FileFormat.InternalUnicodeFormat)
	End Using

	btn_DownloadDocument.Visible = True
End Sub

2. Click the button Download as PDF to download the loaded document as a PDF document.

The document is again loaded into an instance of ServerTextControl and then saved to memory into a byte array in the Adobe PDF format. This byte array is returned to the browser as a file download.

[C#]
protected void Button2_Click(object sender, EventArgs e)
{
	byte[] data;

	// create a new instance of ServerTextControl and load the document
	using (TXTextControl.ServerTextControl tx = 
		new TXTextControl.ServerTextControl())
	{
		tx.Create();
		tx.Load(Server.MapPath("/documents/sample.docx"), 
			TXTextControl.StreamType.WordprocessingML);

		// save the document as PDF into a byte array
		tx.Save(out data, TXTextControl.BinaryStreamType.AdobePDF);
	}

	// return the document to the browser for download
	Response.Clear();
	Response.AddHeader("content-disposition", 
		String.Format("attachment;filename={0}", "created_by_txtextcontrol.pdf"));
	Response.ContentType = "application/pdf";
	Response.BinaryWrite(data);
	Response.End();
}
[Visual Basic]
Protected Sub Button2_Click(sender As Object, e As EventArgs)
	Dim data As Byte()

	' create a new instance of ServerTextControl and load the document
	Using tx As New TXTextControl.ServerTextControl()
		tx.Create()
		tx.Load(Server.MapPath("/documents/sample.docx"), TXTextControl.StreamType.WordprocessingML)

		' save the document as PDF into a byte array
		tx.Save(data, TXTextControl.BinaryStreamType.AdobePDF)
	End Using

	' return the document to the browser for download
	Response.Clear()
	Response.AddHeader("content-disposition", [String].Format("attachment;filename={0}", "created_by_txtextcontrol.pdf"))
	Response.ContentType = "application/pdf"
	Response.BinaryWrite(data)
	Response.[End]()
End Sub