Example - Creating PDF Document

TX Text Control .NET Server for ASP.NET includes a powerful PDF engine to create PDF documents in a very fast and reliable manner.

This demo shows how to convert a DOCX document to PDF and how to set various document access permissions using the SaveSettings object.

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\pdf.aspx

Example description

1. Check some of the checkboxes and type in a security password for the created Adobe PDF document.

2. Click the button Create PDF.

On clicking the button, a document is loaded into an instance of ServerTextControl. A new set of DocumentAccessPermissions is created that defines the access permissions of the PDF document. Based on the selected checkboxes, these settings are adjusted and passed to the Save method in the SaveSettings parameter.

Finally, the created PDF document is returned as a file download to the browser.

[C#]
protected void btnCreate_Click(object sender, EventArgs e)
{
	// create a new instance of ServerTextControl
	using (TXTextControl.ServerTextControl tx = 
		new TXTextControl.ServerTextControl())
	{
		tx.Create();

		// load the sample document
		tx.Load(Server.MapPath("/documents/sample.docx"), 
			TXTextControl.StreamType.WordprocessingML);

		TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings();

		// set the document access permissions based on the checkboxes
		TXTextControl.DocumentAccessPermissions permissions = 
			new TXTextControl.DocumentAccessPermissions();


		if (cbAllowHighLevelPrinting.Checked == true)
			permissions = 
				TXTextControl.DocumentAccessPermissions.AllowHighLevelPrinting;

		if (cbAllowDocumentAssembly.Checked == true)
			permissions = permissions | 
				TXTextControl.DocumentAccessPermissions.AllowHighLevelPrinting;

		if (cbAllowExtractContents.Checked == true)
			permissions = permissions | 
				TXTextControl.DocumentAccessPermissions.AllowExtractContents;

		if (cbAllowGeneralEditing.Checked == true)
			permissions = permissions | 
				TXTextControl.DocumentAccessPermissions.AllowGeneralEditing;

		saveSettings.DocumentAccessPermissions = permissions;

		saveSettings.MasterPassword = tbPassword.Text;
		saveSettings.UserPassword = tbPassword.Text;

		// save the document as PDF in a byte array
		byte[] data;
		tx.Save(out data, TXTextControl.BinaryStreamType.AdobePDF, saveSettings);

		// return the document to the browser for download
		Response.Clear();
		Response.AddHeader("content-disposition",
			String.Format("attachment;filename={0}", "txtextcontrol.pdf"));
		Response.ContentType = "application/pdf";
		Response.BinaryWrite(data);
		Response.End();
	}
}
[Visual Basic]
Protected Sub btnCreate_Click(sender As Object, e As EventArgs)
	' create a new instance of ServerTextControl
	Using tx As New TXTextControl.ServerTextControl()
		tx.Create()

		' load the sample document
		tx.Load(Server.MapPath("/documents/sample.docx"), TXTextControl.StreamType.WordprocessingML)

		Dim saveSettings As New TXTextControl.SaveSettings()

		' set the document access permissions based on the checkboxes
		Dim permissions As New TXTextControl.DocumentAccessPermissions()


		If cbAllowHighLevelPrinting.Checked = True Then
			permissions = TXTextControl.DocumentAccessPermissions.AllowHighLevelPrinting
		End If

		If cbAllowDocumentAssembly.Checked = True Then
			permissions = permissions Or TXTextControl.DocumentAccessPermissions.AllowHighLevelPrinting
		End If

		If cbAllowExtractContents.Checked = True Then
			permissions = permissions Or TXTextControl.DocumentAccessPermissions.AllowExtractContents
		End If

		If cbAllowGeneralEditing.Checked = True Then
			permissions = permissions Or TXTextControl.DocumentAccessPermissions.AllowGeneralEditing
		End If

		saveSettings.DocumentAccessPermissions = permissions

		saveSettings.MasterPassword = tbPassword.Text
		saveSettings.UserPassword = tbPassword.Text

		' save the document as PDF in a byte array
		Dim data As Byte()
		tx.Save(data, TXTextControl.BinaryStreamType.AdobePDF, saveSettings)

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