Save as DOCX and PDF
Converting a document with TX Text Control takes two calls: load it in one format and save it in another. In a Windows Forms application, the document comes either from the editor, where the user wrote it, or from a file that the user never sees. Each case below is a button handler: exporting the editor's document as DOCX and PDF, converting a Word file in the background, saving to byte arrays and creating a PDF/A copy.
Prerequisites
- The application from Create Your First Windows Forms Application, with a
TextControlnamedtextControl1onForm1. - For each section, a
Buttonon the form. Double-click it in the designer to create itsClickhandler. - The Professional or Enterprise edition for DOC, DOCX and PDF, and the Enterprise edition for PDF/A. See Editions and Feature Availability.
Form1.cs needs these using directives:
using System.IO;
using TXTextControl;
Export the Editor's Document
Save always writes the complete document, in the format you pass. Two calls save it as DOCX and as PDF:
private void exportButton_Click(object sender, EventArgs e)
{
string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// Save the complete document in both formats
textControl1.Save(Path.Combine(folder, "document.docx"), StreamType.WordprocessingML);
textControl1.Save(Path.Combine(folder, "document.pdf"), StreamType.AdobePDF);
MessageBox.Show("Saved document.docx and document.pdf in " + folder);
}
StreamType.WordprocessingML is the DOCX format and StreamType.AdobePDF the PDF format. An existing file is overwritten. To let the user choose the file name and to add a title or a password to the PDF, see Create a PDF.
Convert a File Without the Editor
A ServerTextControl converts documents without touching the one in the editor. The following handler lets the user pick a Word 97-2003 document and saves it as DOCX and PDF next to the original:
private void convertButton_Click(object sender, EventArgs e)
{
using (OpenFileDialog dialog = new OpenFileDialog())
{
dialog.Filter = "Word 97-2003 documents (*.doc)|*.doc";
if (dialog.ShowDialog() != DialogResult.OK)
{
return;
}
using (ServerTextControl tx = new ServerTextControl())
{
// Initialize the new instance before using it
tx.Create();
tx.Load(dialog.FileName, StreamType.MSWord);
// Save next to the source file, with the same name
tx.Save(Path.ChangeExtension(dialog.FileName, ".docx"), StreamType.WordprocessingML);
tx.Save(Path.ChangeExtension(dialog.FileName, ".pdf"), StreamType.AdobePDF);
}
}
}
The code works as follows:
- Create initializes the
ServerTextControl, and theusingstatement disposes it when the conversion is done. - Load reads the file.
StreamType.MSWordis the binary Word format, DOC. - ServerTextControl.Save writes it twice, once in each format.
The same pattern converts any format TX Text Control loads into any format it saves. Supported File Formats lists them.
Save to Byte Arrays
A document that goes into a database or an email attachment does not need a file: with a BinaryStreamType such as WordprocessingML or AdobePDF, Save writes it to a byte array. Load and Save Without a Dialog Box shows the code.
Create a PDF/A Copy in the Background
To save the editor's document in another format without changing the editor's settings, hand a copy to a ServerTextControl. The following handler creates a PDF/A document, the format for long-term archiving:
private void archiveButton_Click(object sender, EventArgs e)
{
// Copy the editor's document in the TX Text Control format
byte[] document;
textControl1.Save(out document, BinaryStreamType.InternalUnicodeFormat);
using (ServerTextControl tx = new ServerTextControl())
{
tx.Create();
// PDF/A embeds its fonts: use only fonts that can be embedded
tx.FontSettings.EmbeddableFontsOnly = true;
tx.Load(document, BinaryStreamType.InternalUnicodeFormat);
tx.Save("archive.pdf", StreamType.AdobePDFA,
new SaveSettings() { DocumentTitle = "Archived Document" });
}
}
The code works as follows:
BinaryStreamType.InternalUnicodeFormatis the TX Text Control format. The copy in theServerTextControlis the same document as in the editor.EmbeddableFontsOnlyandStreamType.AdobePDFAare set on the copy, so the editor keeps its fonts and settings. What they do is explained in Create a PDF/A Document.- SaveSettings adds the title to the document.
Next Steps
- Print a Document: print with or without the editor.
- Create a PDF: document properties, the Save As dialog, passwords and permissions.
- Saving and Converting Documents: all save and conversion options in detail.
- ServerTextControl: when a desktop application uses the component without a user interface.