Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This gist exceeds the recommended number of files (~10). To access all files, please clone this gist.
This Gist contains CSharp code snippets for examples of Aspose.Words for .NET.
using Aspose.Words;
using System;
namespace AsposeWordsDockerTest
{
class Program
{
static void Main(string[] args)
{
// Create document and save it in all available formats.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello from Aspose.Words!!!");
foreach (SaveFormat sf in Enum.GetValues(typeof(SaveFormat)))
{
if (sf != SaveFormat.Unknown)
{
try
{
doc.Save(string.Format("out{0}", FileFormatUtil.SaveFormatToExtension(sf)), sf);
Console.WriteLine("Saving {0}\t\t[OK]", sf);
}
catch
{
Console.WriteLine("Saving {0}\t\t[FAILED]", sf);
}
}
}
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "BubbleChart.docx";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetContracts(), "contracts");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "template_cleanup.docx";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
engine.Options = ReportBuildOptions.RemoveEmptyParagraphs;
engine.BuildReport(doc, Common.GetManagers(), "managers");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "BulletedList.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetClients(), "clients");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public class PointData
{
public string Time { get; set; }
public int Flow { get; set; }
public int Rainfall { get; set; }
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
List<PointData> data = new List<PointData>()
{
new PointData { Time = "12:00:00 AM", Flow = 10, Rainfall = 2 },
new PointData { Time = "01:00:00 AM", Flow = 15, Rainfall = 4 },
new PointData { Time = "02:00:00 AM", Flow = 23, Rainfall = 7 }
};
List<string> seriesNames = new List<string>
{
"Flow",
"Rainfall"
};
Document doc = new Document(dataDir + "ChartTemplate.docx");
ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, new object[] { data, seriesNames }, new string[] { "data", "seriesNames" });
doc.Save(dataDir + "ChartTemplate_Out.docx");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "ChartWithFilteringGroupingOrdering.docx";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetContracts(), "contracts");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public class Client
{
public String Name { get; set; }
public String Country { get; set; }
public String LocalAddress { get; set; }
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
foreach (Manager manager in GetManagers())
{
foreach (Contract contract in manager.Contracts)
yield return contract.Client;
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
foreach (Manager manager in GetManagers())
{
foreach (Contract contract in manager.Contracts)
yield return contract;
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
IEnumerator<Manager> managers = GetManagers().GetEnumerator();
managers.MoveNext();
return managers.Current;
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Manager manager = new Manager { Name = "John Smith", Age = 36, Photo = Photo() };
manager.Contracts = new Contract[]
{
new Contract { Client = new Client { Name = "A Company", Country= "Australia", LocalAddress = "219-241 Cleveland St STRAWBERRY HILLS NSW 1427" }, Manager = manager, Price = 1200000, Date = new DateTime(2015, 1, 1) },
new Contract { Client = new Client { Name = "B Ltd.", Country= "Brazil", LocalAddress = "Avenida João Jorge, 112, ap. 31 Vila Industrial Campinas - SP 13035-680"}, Manager = manager, Price = 750000, Date = new DateTime(2015, 4, 1) },
new Contract { Client = new Client { Name = "C & D", Country= "Canada", LocalAddress = "101-3485 RUE DE LA MONTAGNE MONTRÉAL (QUÉBEC) H3G 2A6" }, Manager = manager, Price = 350000, Date = new DateTime(2015, 7, 1) }
};
yield return manager;
manager = new Manager { Name = "Tony Anderson", Age = 37, Photo = Photo() };
manager.Contracts = new Contract[]
{
new Contract { Client = new Client { Name = "E Corp.", LocalAddress = "445 Mount Eden Road Mount Eden Auckland 1024" }, Manager = manager, Price = 650000, Date = new DateTime(2015, 2, 1) },
new Contract { Client = new Client { Name = "F & Partners", LocalAddress = "20 Greens Road Tuahiwi Kaiapoi 7691 " }, Manager = manager, Price = 550000, Date = new DateTime(2015, 8, 1) },
};
yield return manager;
manager = new Manager { Name = "July James", Age = 38, Photo = Photo() };
manager.Contracts = new Contract[]
{
new Contract { Client = new Client { Name = "G & Co.", Country= "Greece", LocalAddress = "Karkisias 6 GR-111 42 ATHINA GRÉCE" }, Manager = manager, Price = 350000, Date = new DateTime(2015, 2, 1) },
new Contract { Client = new Client { Name = "H Group", Country= "Hungary", LocalAddress = "Budapest Fiktív utca 82., IV. em./28.2806" }, Manager = manager, Price = 250000, Date = new DateTime(2015, 5, 1) },
new Contract { Client = new Client { Name = "I & Sons", LocalAddress ="43 Vogel Street Roslyn Palmerston North 4414" }, Manager = manager, Price = 100000, Date = new DateTime(2015, 7, 1) },
new Contract { Client = new Client { Name = "J Ent." , Country= "Japan", LocalAddress = "Hakusan 4-Chōme 3-2 Bunkyō-ku, TŌKYŌ 112-0001 Japan"}, Manager = manager, Price = 100000, Date = new DateTime(2015, 8, 1) }
};
yield return manager;
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
// Load the photo and read all bytes.
byte[] imgdata = System.IO.File.ReadAllBytes(dataDir + "photo.png");
return imgdata;
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "CommonList.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetManagers(), "managers");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "CommonMasterDetail.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetManagers(), "managers");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "ConditionalBlock.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetClients(), "clients");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public class Contract
{
public Manager Manager { get; set; }
public Client Client { get; set; }
public float Price { get; set; }
public DateTime Date { get; set; }
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "HelloWorld.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create an instance of sender class to set it's properties.
Sender sender = new Sender { Name = "LINQ Reporting Engine", Message = "Hello World" };
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, sender, "sender");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "InParagraphList.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetClients(), "clients");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "InTableAlternateContent.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetContracts(), "contracts");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "InTableList.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetManagers(), "managers");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "InTableMasterDetail.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetManagers(), "managers");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "InTableWithFilteringGroupingSorting.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetContracts(), "contracts");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public class Manager
{
public String Name { get; set; }
public int Age { get; set; }
public byte[] Photo { get; set; }
public IEnumerable<Contract> Contracts { get; set; }
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "MulticoloredNumberedList.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetClients(), "clients");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "NumberedList.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetClients(), "clients");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "PieChart.docx";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetManagers(), "managers");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "ScatterChart.docx";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetContracts(), "contracts");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public class Sender
{
public String Name { get; set; }
public String Message { get; set; }
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, new object());
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LINQ();
string fileName = "SingleRow.doc";
// Load the template document.
Document doc = new Document(dataDir + fileName);
// Load the photo and read all bytes.
byte[] imgdata = System.IO.File.ReadAllBytes(dataDir + "photo.png");
// Create a Reporting Engine.
ReportingEngine engine = new ReportingEngine();
// Execute the build report.
engine.BuildReport(doc, Common.GetManager(), "manager");
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the finished document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
Document doc = new Document(dataDir + "Test File (doc).doc");
foreach (DigitalSignature signature in doc.DigitalSignatures)
{
Console.WriteLine("*** Signature Found ***");
Console.WriteLine("Is valid: " + signature.IsValid);
Console.WriteLine("Reason for signing: " + signature.Comments); // This property is available in MS Word documents only.
Console.WriteLine("Time of signing: " + signature.SignTime);
Console.WriteLine("Subject name: " + signature.CertificateHolder.Certificate.SubjectName.Name);
Console.WriteLine("Issuer name: " + signature.CertificateHolder.Certificate.IssuerName.Name);
Console.WriteLine();
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
string supportedDir = dataDir + "OutSupported";
string unknownDir = dataDir + "OutUnknown";
string encryptedDir = dataDir + "OutEncrypted";
string pre97Dir = dataDir + "OutPre97";
// Create the directories if they do not already exist
if (Directory.Exists(supportedDir) == false)
Directory.CreateDirectory(supportedDir);
if (Directory.Exists(unknownDir) == false)
Directory.CreateDirectory(unknownDir);
if (Directory.Exists(encryptedDir) == false)
Directory.CreateDirectory(encryptedDir);
if (Directory.Exists(pre97Dir) == false)
Directory.CreateDirectory(pre97Dir);
string[] fileList = Directory.GetFiles(dataDir);
// Loop through all found files.
foreach (string fileName in fileList)
{
// Extract and display the file name without the path.
string nameOnly = Path.GetFileName(fileName);
Console.Write(nameOnly);
// Check the file format and move the file to the appropriate folder.
FileFormatInfo info = FileFormatUtil.DetectFileFormat(fileName);
// Display the document type.
switch (info.LoadFormat)
{
case LoadFormat.Doc:
Console.WriteLine("\tMicrosoft Word 97-2003 document.");
break;
case LoadFormat.Dot:
Console.WriteLine("\tMicrosoft Word 97-2003 template.");
break;
case LoadFormat.Docx:
Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Free Document.");
break;
case LoadFormat.Docm:
Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Enabled Document.");
break;
case LoadFormat.Dotx:
Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Free Template.");
break;
case LoadFormat.Dotm:
Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Enabled Template.");
break;
case LoadFormat.FlatOpc:
Console.WriteLine("\tFlat OPC document.");
break;
case LoadFormat.Rtf:
Console.WriteLine("\tRTF format.");
break;
case LoadFormat.WordML:
Console.WriteLine("\tMicrosoft Word 2003 WordprocessingML format.");
break;
case LoadFormat.Html:
Console.WriteLine("\tHTML format.");
break;
case LoadFormat.Mhtml:
Console.WriteLine("\tMHTML (Web archive) format.");
break;
case LoadFormat.Odt:
Console.WriteLine("\tOpenDocument Text.");
break;
case LoadFormat.Ott:
Console.WriteLine("\tOpenDocument Text Template.");
break;
case LoadFormat.DocPreWord60:
Console.WriteLine("\tMS Word 6 or Word 95 format.");
break;
case LoadFormat.Unknown:
default:
Console.WriteLine("\tUnknown format.");
break;
}
// Now copy the document into the appropriate folder.
if (info.IsEncrypted)
{
Console.WriteLine("\tAn encrypted document.");
File.Copy(fileName, Path.Combine(encryptedDir, nameOnly), true);
}
else
{
switch (info.LoadFormat)
{
case LoadFormat.DocPreWord60:
File.Copy(fileName, Path.Combine(pre97Dir, nameOnly), true);
break;
case LoadFormat.Unknown:
File.Copy(fileName, Path.Combine(unknownDir, nameOnly), true);
break;
default:
File.Copy(fileName, Path.Combine(supportedDir, nameOnly), true);
break;
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Check the file format and move the file to the appropriate folder.
FileFormatInfo info = FileFormatUtil.DetectFileFormat(fileName);
// Display the document type.
switch (info.LoadFormat)
{
case LoadFormat.Doc:
Console.WriteLine("\tMicrosoft Word 97-2003 document.");
break;
case LoadFormat.Dot:
Console.WriteLine("\tMicrosoft Word 97-2003 template.");
break;
case LoadFormat.Docx:
Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Free Document.");
break;
case LoadFormat.Docm:
Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Enabled Document.");
break;
case LoadFormat.Dotx:
Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Free Template.");
break;
case LoadFormat.Dotm:
Console.WriteLine("\tOffice Open XML WordprocessingML Macro-Enabled Template.");
break;
case LoadFormat.FlatOpc:
Console.WriteLine("\tFlat OPC document.");
break;
case LoadFormat.Rtf:
Console.WriteLine("\tRTF format.");
break;
case LoadFormat.WordML:
Console.WriteLine("\tMicrosoft Word 2003 WordprocessingML format.");
break;
case LoadFormat.Html:
Console.WriteLine("\tHTML format.");
break;
case LoadFormat.Mhtml:
Console.WriteLine("\tMHTML (Web archive) format.");
break;
case LoadFormat.Odt:
Console.WriteLine("\tOpenDocument Text.");
break;
case LoadFormat.Ott:
Console.WriteLine("\tOpenDocument Text Template.");
break;
case LoadFormat.DocPreWord60:
Console.WriteLine("\tMS Word 6 or Word 95 format.");
break;
case LoadFormat.Unknown:
default:
Console.WriteLine("\tUnknown format.");
break;
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
string[] fileList = Directory.GetFiles(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document from disk.
Document doc = new Document(dataDir + "Test File (doc).doc");
// Create a new memory stream.
MemoryStream outStream = new MemoryStream();
// Save the document to stream.
doc.Save(outStream, SaveFormat.Docx);
// Convert the document to byte form.
byte[] docBytes = outStream.ToArray();
// The bytes are now ready to be stored/transmitted.
// Now reverse the steps to load the bytes back into a document object.
MemoryStream inStream = new MemoryStream(docBytes);
// Load the stream into a new document object.
Document loadDoc = new Document(inStream);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document from disk.
Document doc = new Document(dataDir + "Document.EpubConversion.docx");
// Create a new instance of HtmlSaveOptions. This object allows us to set options that control
// How the output document is saved.
HtmlSaveOptions saveOptions =
new HtmlSaveOptions();
// Specify the desired encoding.
saveOptions.Encoding = System.Text.Encoding.UTF8;
// Specify at what elements to split the internal HTML at. This creates a new HTML within the EPUB
// which allows you to limit the size of each HTML part. This is useful for readers which cannot read
// HTML files greater than a certain size e.g 300kb.
saveOptions.DocumentSplitCriteria = DocumentSplitCriteria.HeadingParagraph;
// Specify that we want to export document properties.
saveOptions.ExportDocumentProperties = true;
// Specify that we want to save in EPUB format.
saveOptions.SaveFormat = SaveFormat.Epub;
// Export the document as an EPUB file.
doc.Save(dataDir + "Document.EpubConversion_out.epub", saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public static void ConvertDocumentToEPUBUsingDefaultSaveOption()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document from disk.
Document doc = new Document(dataDir + "Document.EpubConversion.doc");
// Save the document in EPUB format.
doc.Save(dataDir + "Document.EpubConversionUsingDefaultSaveOption_out.epub");
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Load the document from disk.
Document doc = new Document(dataDir + "Test File (doc).docx");
HtmlSaveOptions options = new HtmlSaveOptions();
// HtmlSaveOptions.ExportRoundtripInformation property specifies
// Whether to write the roundtrip information when saving to HTML, MHTML or EPUB.
// Default value is true for HTML and false for MHTML and EPUB.
options.ExportRoundtripInformation = true;
doc.Save(dataDir + "ExportRoundtripInformation_out.html", options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Open a Word document
Document doc = new Document(dataDir + "Test File (doc).docx");
HtmlSaveOptions options = new HtmlSaveOptions();
// Split a document into smaller parts, in this instance split by heading
options.DocumentSplitCriteria = DocumentSplitCriteria.HeadingParagraph;
// Save the output file
doc.Save(dataDir + "SplitDocumentByHeadings_out.html", options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
HtmlSaveOptions options = new HtmlSaveOptions();
options.DocumentSplitCriteria = DocumentSplitCriteria.HeadingParagraph;
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document from disk.
Document doc = new Document(dataDir + "Test File (doc).docx");
HtmlSaveOptions options = new HtmlSaveOptions();
// HtmlSaveOptions.ExportRoundtripInformation property specifies
// Whether to write the roundtrip information when saving to HTML, MHTML or EPUB.
// Default value is true for HTML and false for MHTML and EPUB.
options.ExportRoundtripInformation = true;
doc.Save(dataDir + "ExportRoundtripInformation_out.html", options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document into Aspose.Words.
Document doc = new Document(dataDir + "Test File (docx).docx");
// Save into a memory stream in MHTML format.
Stream stream = new MemoryStream();
doc.Save(stream, SaveFormat.Mhtml);
// Rewind the stream to the beginning so Aspose.Email can read it.
stream.Position = 0;
// Create an Aspose.Network MIME email message from the stream.
MailMessage message = MailMessage.Load(stream, new MhtmlLoadOptions());
message.From = "your_from@email.com";
message.To = "your_to@email.com";
message.Subject = "Aspose.Words + Aspose.Email MHTML Test Message";
// Send the message using Aspose.Email
SmtpClient client = new SmtpClient();
client.Host = "your_smtp.com";
client.Send(message);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Load the document from disk.
Document doc = new Document(dataDir + "Test File (docx).docx");
PclSaveOptions saveOptions = new PclSaveOptions();
saveOptions.SaveFormat = SaveFormat.Pcl;
saveOptions.RasterizeTransformedElements = false;
// Export the document as an PCL file.
doc.Save(dataDir + "Document.PclConversion_out.pcl", saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Initialize a Document.
Document doc = new Document();
// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello World!");
dataDir = dataDir + "CreateDocument_out.docx";
// Save the document to disk.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// The path to the document which is to be processed.
string filePath = dataDir + "Document.Signed.docx";
FileFormatInfo info = FileFormatUtil.DetectFileFormat(filePath);
if (info.HasDigitalSignature)
{
Console.WriteLine("Document {0} has digital signatures, they will be lost if you open/save this document with Aspose.Words.", Path.GetFileName(filePath));
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Create a simple document from scratch.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Test Signed PDF.");
// Load the certificate from disk.
// The other constructor overloads can be used to load certificates from different locations.
X509Certificate2 cert = new X509Certificate2(
dataDir + "signature.pfx", "signature");
// Pass the certificate and details to the save options class to sign with.
PdfSaveOptions options = new PdfSaveOptions();
options.DigitalSignatureDetails = new PdfDigitalSignatureDetails();
dataDir = dataDir + "Document.Signed_out.pdf";
// Save the document as PDF.
doc.Save(dataDir, options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
using System.Security.Cryptography.X509Certificates;
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Create a simple document from scratch.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Test Signed PDF.");
PdfSaveOptions options = new PdfSaveOptions();
options.DigitalSignatureDetails = new PdfDigitalSignatureDetails(
CertificateHolder.Create(dataDir + "CioSrv1.pfx", "cinD96..arellA"),"reason","location",DateTime.Now);
doc.Save(dataDir + @"DigitallySignedPdfUsingCertificateHolder.Signed_out.pdf", options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Load the document from disk.
Document doc = new Document(dataDir + "Rendering.doc");
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.DisplayDocTitle = true;
// Save the document in PDF format.
doc.Save(dataDir + "DisplayDocTitleInWindowTitlebar.pdf", saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Load the document from disk.
Document doc = new Document(dataDir + "Rendering.docx");
// Save the document in PDF format.
doc.Save(dataDir + "SaveDoc2Pdf.pdf");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public static void PdfRenderWarnings(string dataDir)
{
// Load the document from disk.
Document doc = new Document(dataDir + "PdfRenderWarnings.doc");
// Set a SaveOptions object to not emulate raster operations.
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.MetafileRenderingOptions = new MetafileRenderingOptions
{
EmulateRasterOperations = false,
RenderingMode = MetafileRenderingMode.VectorWithFallback
};
// If Aspose.Words cannot correctly render some of the metafile records to vector graphics then Aspose.Words renders this metafile to a bitmap.
HandleDocumentWarnings callback = new HandleDocumentWarnings();
doc.WarningCallback = callback;
doc.Save(dataDir + "PdfRenderWarnings.pdf", saveOptions);
// While the file saves successfully, rendering warnings that occurred during saving are collected here.
foreach (WarningInfo warningInfo in callback.mWarnings)
{
Console.WriteLine(warningInfo.Description);
}
}
public class HandleDocumentWarnings : IWarningCallback
{
/// <summary>
/// Our callback only needs to implement the "Warning" method. This method is called whenever there is a
/// potential issue during document processing. The callback can be set to listen for warnings generated during document
/// load and/or document save.
/// </summary>
public void Warning(WarningInfo info)
{
//For now type of warnings about unsupported metafile records changed from DataLoss/UnexpectedContent to MinorFormattingLoss.
if (info.WarningType == WarningType.MinorFormattingLoss)
{
Console.WriteLine("Unsupported operation: " + info.Description);
mWarnings.Warning(info);
}
}
public WarningInfoCollection mWarnings = new WarningInfoCollection();
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public class HandleDocumentWarnings : IWarningCallback
{
/// <summary>
/// Our callback only needs to implement the "Warning" method. This method is called whenever there is a
/// potential issue during document processing. The callback can be set to listen for warnings generated during document
/// load and/or document save.
/// </summary>
public void Warning(WarningInfo info)
{
//For now type of warnings about unsupported metafile records changed from DataLoss/UnexpectedContent to MinorFormattingLoss.
if (info.WarningType == WarningType.MinorFormattingLoss)
{
Console.WriteLine("Unsupported operation: " + info.Description);
mWarnings.Warning(info);
}
}
public WarningInfoCollection mWarnings = new WarningInfoCollection();
}
public static void RenderMetafileToBitmap(string dataDir)
{
// Load the document from disk.
Document doc = new Document(dataDir + "PdfRenderWarnings.doc");
MetafileRenderingOptions metafileRenderingOptions =
new MetafileRenderingOptions
{
EmulateRasterOperations = false,
RenderingMode = MetafileRenderingMode.VectorWithFallback
};
// If Aspose.Words cannot correctly render some of the metafile records to vector graphics then Aspose.Words renders this metafile to a bitmap.
HandleDocumentWarnings callback = new HandleDocumentWarnings();
doc.WarningCallback = callback;
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.MetafileRenderingOptions = metafileRenderingOptions;
doc.Save(dataDir + "PdfSaveOptions.HandleRasterWarnings.pdf", saveOptions);
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
string fileName = "Document.docx";
Document doc = new Document(dataDir + fileName);
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.ExportFontResources = true;
saveOptions.ExportFontsAsBase64 = true;
dataDir = dataDir + "ExportFontsAsBase64_out.html";
doc.Save(dataDir, saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
string fileName = "Document.docx";
Document doc = new Document(dataDir + fileName);
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.CssStyleSheetType = CssStyleSheetType.External;
saveOptions.ExportFontResources = true;
saveOptions.ResourceFolder = dataDir + @"\Resources";
saveOptions.ResourceFolderAlias = "http://example.com/resources";
doc.Save(dataDir + "ExportResourcesUsingHtmlSaveOptions.html", saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public class ImageLoadingWithCredentialsHandler : IResourceLoadingCallback
{
public ImageLoadingWithCredentialsHandler()
{
mWebClient = new WebClient();
}
public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
{
if (args.ResourceType == ResourceType.Image)
{
Uri uri = new Uri(args.Uri);
if (uri.Host == "www.aspose.com")
mWebClient.Credentials = new NetworkCredential("User1", "akjdlsfkjs");
else
mWebClient.Credentials = new NetworkCredential("SomeOtherUserID", "wiurlnlvs");
// Download the bytes from the location referenced by the URI.
byte[] imageBytes = mWebClient.DownloadData(args.Uri);
args.SetData(imageBytes);
return ResourceLoadingAction.UserProvided;
}
else
{
return ResourceLoadingAction.Default;
}
}
private WebClient mWebClient;
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Create Document and DocumentBuilder.
// The builder makes it simple to add content to the document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Read the image from file, ensure it is disposed.
using (Image image = Image.FromFile(inputFileName))
{
// Find which dimension the frames in this image represent. For example
// The frames of a BMP or TIFF are "page dimension" whereas frames of a GIF image are "time dimension".
FrameDimension dimension = new FrameDimension(image.FrameDimensionsList[0]);
// Get the number of frames in the image.
int framesCount = image.GetFrameCount(dimension);
// Loop through all frames.
for (int frameIdx = 0; frameIdx < framesCount; frameIdx++)
{
// Insert a section break before each new page, in case of a multi-frame TIFF.
if (frameIdx != 0)
builder.InsertBreak(BreakType.SectionBreakNewPage);
// Select active frame.
image.SelectActiveFrame(dimension, frameIdx);
// We want the size of the page to be the same as the size of the image.
// Convert pixels to points to size the page to the actual image size.
PageSetup ps = builder.PageSetup;
ps.PageWidth = ConvertUtil.PixelToPoint(image.Width, image.HorizontalResolution);
ps.PageHeight = ConvertUtil.PixelToPoint(image.Height, image.VerticalResolution);
// Insert the image into the document and position it at the top left corner of the page.
builder.InsertImage(
image,
RelativeHorizontalPosition.Page,
0,
RelativeVerticalPosition.Page,
0,
ps.PageWidth,
ps.PageHeight,
WrapType.None);
}
}
// Save the document to PDF.
doc.Save(outputFileName);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
ConvertImageToPdf(dataDir + "Test.jpg", dataDir + "TestJpg_out.pdf");
ConvertImageToPdf(dataDir + "Test.png", dataDir + "TestPng_out.pdf");
ConvertImageToPdf(dataDir + "Test.wmf", dataDir + "TestWmf_out.pdf");
ConvertImageToPdf(dataDir + "Test.tiff", dataDir + "TestTif_out.pdf");
ConvertImageToPdf(dataDir + "Test.gif", dataDir + "TestGif_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
LoadOptions options = new LoadOptions();
options.AnnotationsAtBlockLevel = true;
Document doc = new Document(dataDir + "AnnotationsAtBlockLevel.docx", options);
DocumentBuilder builder = new DocumentBuilder(doc);
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChildNodes(NodeType.StructuredDocumentTag, true)[0];
BookmarkStart start = builder.StartBookmark("bm");
BookmarkEnd end = builder.EndBookmark("bm");
sdt.ParentNode.InsertBefore(start, sdt);
sdt.ParentNode.InsertAfter(end, sdt);
//Save the document into DOCX
doc.Save(dataDir + "AnnotationsAtBlockLevel_out.docx", SaveFormat.Docx);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
LoadOptions lo = new LoadOptions();
lo.ConvertShapeToOfficeMath = true;
// Specify load option to use previous default behaviour i.e. convert math shapes to office math ojects on loading stage.
Document doc = new Document(dataDir + @"OfficeMath.docx", lo);
//Save the document into DOCX
doc.Save(dataDir + "ConvertShapeToOfficeMath_out.docx", SaveFormat.Docx);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public class DocumentLoadingWarningCallback : IWarningCallback
{
public void Warning(WarningInfo info)
{
// Prints warnings and their details as they arise during document loading.
Console.WriteLine($"WARNING: {info.WarningType}, source: {info.Source}");
Console.WriteLine($"\tDescription: {info.Description}");
}
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
private class HtmlLinkedResourceLoadingCallback : IResourceLoadingCallback
{
public ResourceLoadingAction ResourceLoading(ResourceLoadingArgs args)
{
switch (args.ResourceType)
{
case ResourceType.CssStyleSheet:
{
Console.WriteLine($"External CSS Stylesheet found upon loading: {args.OriginalUri}");
// CSS file will don't used in the document
return ResourceLoadingAction.Skip;
}
case ResourceType.Image:
{
// Replaces all images with a substitute
const string newImageFilename = "Logo.jpg";
Console.WriteLine($"\tImage will be substituted with: {newImageFilename}");
Image newImage = Image.FromFile(RunExamples.GetDataDir_QuickStart() + newImageFilename);
ImageConverter converter = new ImageConverter();
byte[] imageBytes = (byte[])converter.ConvertTo(newImage, typeof(byte[]));
args.SetData(imageBytes);
// New images will be used instead of presented in the document
return ResourceLoadingAction.UserProvided;
}
case ResourceType.Document:
{
Console.WriteLine($"External document found upon loading: {args.OriginalUri}");
// Will be used as usual
return ResourceLoadingAction.Default;
}
default:
throw new InvalidOperationException("Unexpected ResourceType value.");
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + @"encrypted.odt", new Aspose.Words.LoadOptions("password"));
doc.Save(dataDir + "out.odt", new OdtSaveOptions("newpassword"));
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Set the Encoding attribute in a LoadOptions object to override the automatically chosen encoding with the one we know to be correct
LoadOptions loadOptions = new LoadOptions { Encoding = Encoding.UTF7 };
Document doc = new Document(dataDir + "Encoded in UTF-7.txt", loadOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Create a new LoadOptions object and set its ResourceLoadingCallback attribute as an instance of our IResourceLoadingCallback implementation
LoadOptions loadOptions = new LoadOptions { ResourceLoadingCallback = new HtmlLinkedResourceLoadingCallback() };
// When we open an Html document, external resources such as references to CSS stylesheet files and external images
// will be handled in a custom manner by the loading callback as the document is loaded
Document doc = new Document(dataDir + "Images.html", loadOptions);
doc.Save(dataDir + "Document.LoadOptionsCallback_out.pdf");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
LoadOptions lo = new LoadOptions();
//Update the fields with the dirty attribute
lo.UpdateDirtyFields = true;
//Load the Word document
Document doc = new Document(dataDir + @"input.docx", lo);
//Save the document into DOCX
doc.Save(dataDir + "output.docx", SaveFormat.Docx);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Create a new LoadOptions object and set its WarningCallback property.
LoadOptions loadOptions = new LoadOptions { WarningCallback = new DocumentLoadingWarningCallback() };
Document doc = new Document(dataDir + "document.docx", loadOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Create a new LoadOptions object, which will load documents according to MS Word 2019 specification by default.
LoadOptions loadOptions = new LoadOptions();
// Change the loading version to Microsoft Word 2010.
loadOptions.MswVersion = MsWordVersion.Word2010;
Document doc = new Document(dataDir + "document.docx", loadOptions);
doc.Save(dataDir + "Word2003_out.docx");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
LoadOptions lo = new LoadOptions();
lo.TempFolder = @"C:\TempFolder\";
Document doc = new Document(dataDir + "document.docx", lo);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
FileFormatInfo info = FileFormatUtil.DetectFileFormat(dataDir + @"encrypted.odt");
Console.WriteLine(info.IsEncrypted);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public static void DeleteFromDatabase(string fileName, OleDbConnection mConnection)
{
// Create the SQL command.
string commandString = "DELETE * FROM Documents WHERE FileName='" + fileName + "'";
OleDbCommand command = new OleDbCommand(commandString, mConnection);
// Delete the record.
command.ExecuteNonQuery();
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
string dbName = "";
// Open a database connection.
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + RunExamples.GetDataDir_Database() + dbName;
OleDbConnection mConnection = new OleDbConnection(connString);
mConnection.Open();
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Store the document to the database.
StoreToDatabase(doc, mConnection);
// Read the document from the database and store the file to disk.
Document dbDoc = ReadFromDatabase(fileName, mConnection);
// Save the retrieved document to disk.
string newFileName = Path.GetFileNameWithoutExtension(fileName) + " from DB" + Path.GetExtension(fileName);
dbDoc.Save(dataDir + newFileName);
// Delete the document from the database.
DeleteFromDatabase(fileName, mConnection);
// Close the connection to the database.
mConnection.Close();
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public static Document ReadFromDatabase(string fileName, OleDbConnection mConnection)
{
// Create the SQL command.
string commandString = "SELECT * FROM Documents WHERE FileName='" + fileName + "'";
OleDbCommand command = new OleDbCommand(commandString, mConnection);
// Create the data adapter.
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
// Fill the results from the database into a DataTable.
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
// Check there was a matching record found from the database and throw an exception if no record was found.
if (dataTable.Rows.Count == 0)
throw new ArgumentException(string.Format("Could not find any record matching the document \"{0}\" in the database.", fileName));
// The document is stored in byte form in the FileContent column.
// Retrieve these bytes of the first matching record to a new buffer.
byte[] buffer = (byte[])dataTable.Rows[0]["FileContent"];
// Wrap the bytes from the buffer into a new MemoryStream object.
MemoryStream newStream = new MemoryStream(buffer);
// Read the document from the stream.
Document doc = new Document(newStream);
// Return the retrieved document.
return doc;
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
public static void StoreToDatabase(Document doc, OleDbConnection mConnection)
{
// Save the document to a MemoryStream object.
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.Docx);
// Get the filename from the document.
string fileName = Path.GetFileName(doc.OriginalFileName);
// Create the SQL command.
string commandString = "INSERT INTO Documents (FileName, FileContent) VALUES('" + fileName + "', @Docx)";
OleDbCommand command = new OleDbCommand(commandString, mConnection);
// Add the @Docx parameter.
command.Parameters.AddWithValue("Docx", stream.ToArray());
// Write the document to the database.
command.ExecuteNonQuery();
}
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
HtmlLoadOptions lo = new HtmlLoadOptions();
lo.PreferredControlType = HtmlControlType.StructuredDocumentTag;
//Load the HTML document
Document doc = new Document(dataDir + @"input.html", lo);
//Save the HTML document into DOCX
doc.Save(dataDir + "output.docx", SaveFormat.Docx);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_QuickStart();
string fileName = "Document.docx";
// Load the document from the absolute path on disk.
Document doc = new Document(dataDir + fileName);
dataDir = dataDir + RunExamples.GetOutputFilePath(fileName);
// Save the document as DOCX document.
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_QuickStart();
string fileName = "Document.docx";
// Load the document from the absolute path on disk.
Document doc = new Document(dataDir + fileName);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_QuickStart();
string fileName = "Document.docx";
// Open the stream. Read only access is enough for Aspose.Words to load a document.
Stream stream = File.OpenRead(dataDir + fileName);
// Load the entire document into memory.
Document doc = new Document(stream);
// You can close the stream now, it is no longer needed because the document is in memory.
stream.Close();
// ... do something with the document
// Convert the document to a different format and save to stream.
MemoryStream dstStream = new MemoryStream();
doc.Save(dstStream, SaveFormat.Rtf);
// Rewind the stream position back to zero so it is ready for the next reader.
dstStream.Position = 0;
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_QuickStart();
string fileName = "Document.docx";
// Open the stream. Read only access is enough for Aspose.Words to load a document.
Stream stream = File.OpenRead(dataDir + fileName);
// Load the entire document into memory.
Document doc = new Document(stream);
// You can close the stream now, it is no longer needed because the document is in memory.
stream.Close();
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// The encoding of the text file is automatically detected.
Document doc = new Document(dataDir + "LoadTxt.txt");
// Save as any Aspose.Words supported format, such as DOCX.
dataDir = dataDir + "LoadTxt_out.docx";
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
// Loads encrypted document.
Document doc = new Document(dataDir + "LoadEncrypted.docx", new LoadOptions("aspose"));
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
Document doc = new Document(dataDir + "Document.doc");
dataDir = dataDir + "Document.ConvertToTxt_out.txt";
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Here is an SVG image: ");
builder.InsertHtml(
@"<svg height='210' width='500'>
<polygon points='100,10 40,198 190,78 10,78 160,198'
style='fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;' />
</svg> ");
HtmlSaveOptions options = new HtmlSaveOptions();
options.MetafileFormat = HtmlMetafileFormat.Svg;
dataDir = dataDir + "ExportSVGinHTML_out.html";
doc.Save(dataDir, options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.docx");
HtmlSaveOptions options = new HtmlSaveOptions();
options.MetafileFormat = HtmlMetafileFormat.EmfOrWmf;
dataDir = dataDir + "SaveHtmlWithMetafileFormat_out.html";
doc.Save(dataDir, options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.docx");
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.CssStyleSheetType = CssStyleSheetType.External;
saveOptions.CssClassNamePrefix = "pfx_";
dataDir = dataDir + "CssClassNamePrefix_out.html";
doc.Save(dataDir, saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
var doc = new Aspose.Words.Document(dataDir + "CidUrls.docx");
Aspose.Words.Saving.HtmlSaveOptions saveOptions = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Mhtml);
saveOptions.PrettyFormat = true;
saveOptions.ExportCidUrlsForMhtmlResources = true;
saveOptions.SaveFormat = SaveFormat.Mhtml;
dataDir = dataDir + "SetExportCidUrlsForMhtmlResources_out.mhtml";
doc.Save(dataDir, saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Test File (docx).docx");
Aspose.Words.Saving.HtmlSaveOptions saveOptions = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Html);
saveOptions.PrettyFormat = true;
saveOptions.ResolveFontNames = true;
dataDir = dataDir + "ResolveFontNames_out.html";
doc.Save(dataDir, saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Load the document from disk.
Document doc = new Document(dataDir + "Test File (doc).doc");
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
options.UseTargetMachineFonts = true;
dataDir = dataDir + "UseFontFromTargetMachine_out.html";
// Save the document to disk.
doc.Save(dataDir, options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// Load the document from disk.
Document doc = new Document(dataDir + "Test File (doc).doc");
HtmlFixedSaveOptions options = new HtmlFixedSaveOptions();
//Setting this property to true restores the old behavior (separate files) for compatibility with legacy code.
//Default value is false.
//All CSS rules are written into single file "styles.css
options.SaveFontFaceCssSeparately = false;
dataDir = dataDir + "WriteAllCSSrulesinSingleFile_out.html";
// Save the document to disk.
doc.Save(dataDir, options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
Document doc = new Document(dataDir + "Document.docx");
dataDir = dataDir + "Report_out.docx";
// If this method overload is causing a compiler error then you are using the Client Profile DLL whereas
// The Aspose.Words .NET 2.0 DLL must be used instead.
doc.Save(dataDir);
DocumentBuilder builder = new DocumentBuilder();
// Create a new table with two cells.
builder.InsertCell();
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("Cell1");
builder.InsertCell();
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("Cell2");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
// Makes all paragraphs inside table to be aligned to Left.
saveOptions.TableContentAlignment = TableContentAlignment.Left;
builder.Document.Save(dataDir + "left.md", saveOptions);
// Makes all paragraphs inside table to be aligned to Right.
saveOptions.TableContentAlignment = TableContentAlignment.Right;
builder.Document.Save(dataDir + "right.md", saveOptions);
// Makes all paragraphs inside table to be aligned to Center.
saveOptions.TableContentAlignment = TableContentAlignment.Center;
builder.Document.Save(dataDir + "center.md", saveOptions);
// Makes all paragraphs inside table to be aligned automatically.
// The alignment in this case will be taken from the first paragraph in corresponding table column.
saveOptions.TableContentAlignment = TableContentAlignment.Auto;
builder.Document.Save(dataDir + "auto.md", saveOptions);
DocumentBuilder builder = new DocumentBuilder();
builder.Writeln("Some text!");
// specify MarkDownSaveOptions
MarkdownSaveOptions saveOptions = (MarkdownSaveOptions)SaveOptions.CreateSaveOptions(SaveFormat.Markdown);
builder.Document.Save(dataDir + "TestDocument.md", saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
string fileName = "TestFile RenderShape.docx";
Document doc = new Document(dataDir + fileName);
// This is the directory we want the exported images to be saved to.
string imagesDir = Path.Combine(dataDir, "Images");
// The folder specified needs to exist and should be empty.
if (Directory.Exists(imagesDir))
Directory.Delete(imagesDir, true);
Directory.CreateDirectory(imagesDir);
// Set an option to export form fields as plain text, not as HTML input elements.
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html);
options.ExportTextInputFormFieldAsText = true;
options.ImagesFolder = imagesDir;
dataDir = dataDir + "Document.SaveWithOptions_out.html";
doc.Save(dataDir, options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.doc");
DocSaveOptions saveOptions = new DocSaveOptions();
saveOptions.AlwaysCompressMetafiles = false;
doc.Save(dataDir + "SmallMetafilesUncompressed.doc", saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.docx");
DocSaveOptions docSaveOptions = new DocSaveOptions();
docSaveOptions.Password = "password";
dataDir = dataDir + "Document.Password_out.docx";
doc.Save(dataDir, docSaveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "in.doc");
DocSaveOptions saveOptions = (DocSaveOptions)SaveOptions.CreateSaveOptions(SaveFormat.Doc);
saveOptions.SavePictureBullet = false;
doc.Save(dataDir + "out.doc", saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.doc");
OoxmlSaveOptions ooxmlSaveOptions = new OoxmlSaveOptions();
ooxmlSaveOptions.Password = "password";
dataDir = dataDir + "Document.Password_out.docx";
doc.Save(dataDir, ooxmlSaveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.doc");
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.FlatOpc);
so.KeepLegacyControlChars = true;
dataDir = dataDir + "Document_out.docx";
// Save the document to disk.
doc.Save(dataDir, so);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.doc");
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.Docx);
so.CompressionLevel = CompressionLevel.SuperFast;
// Save the document to disk.
doc.Save(dataDir + "SetCompressionLevel_out.docx", so);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.doc");
// Set Word2016 version for document
doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2016);
//Set the Strict compliance level.
OoxmlSaveOptions ooxmlSaveOptions = new OoxmlSaveOptions
{
Compliance = OoxmlCompliance.Iso29500_2008_Strict,
SaveFormat = SaveFormat.Docx
};
dataDir = dataDir + "Document.Iso29500_2008_Strict_out.docx";
doc.Save(dataDir, ooxmlSaveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.docx");
OoxmlSaveOptions ooxmlSaveOptions = new OoxmlSaveOptions();
ooxmlSaveOptions.UpdateLastSavedTimeProperty = true;
dataDir = dataDir + "UpdateLastSavedTimeProperty_out.docx";
// Save the document to disk.
doc.Save(dataDir, ooxmlSaveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
RtfLoadOptions loadOptions = new RtfLoadOptions();
loadOptions.RecognizeUtf8Text = true;
Document doc = new Document(dataDir + "Utf8Text.rtf", loadOptions);
dataDir = dataDir + "RecognizeUtf8Text_out.rtf";
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Input.docx");
TxtSaveOptions saveOptions = new TxtSaveOptions();
saveOptions.AddBidiMarks = true;
dataDir = dataDir + "Document.AddBidiMarks_out.txt";
doc.Save(dataDir, saveOptions);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc1 = new Document("input_document");
doc1.Save(dataDir + "output1.txt");
Document doc2 = new Document("input_document");
TxtSaveOptions options = new TxtSaveOptions();
doc2.Save(dataDir + "output2.txt", options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
TxtLoadOptions loadOptions = new TxtLoadOptions();
loadOptions.DetectNumberingWithWhitespaces = false;
Document doc = new Document(dataDir + "LoadTxt.txt", loadOptions);
dataDir = dataDir + "DetectNumberingWithWhitespaces_out.docx";
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
TxtLoadOptions loadOptions = new TxtLoadOptions();
loadOptions.DocumentDirection = DocumentDirection.Auto;
Document doc = new Document(dataDir + "arabic.txt", loadOptions);
Paragraph paragraph = doc.FirstSection.Body.FirstParagraph;
Console.WriteLine(paragraph.ParagraphFormat.Bidi);
dataDir = dataDir + "DocumentDirection_out.docx";
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "TxtExportHeadersFootersMode.docx");
TxtSaveOptions options = new TxtSaveOptions();
options.SaveFormat = SaveFormat.Text;
// All headers and footers are placed at the very end of the output document.
options.ExportHeadersFootersMode = TxtExportHeadersFootersMode.AllAtEnd;
doc.Save(dataDir + "outputFileNameA.txt", options);
// Only primary headers and footers are exported at the beginning and end of each section.
options.ExportHeadersFootersMode = TxtExportHeadersFootersMode.PrimaryOnly;
doc.Save(dataDir + "outputFileNameB.txt", options);
// No headers and footers are exported.
options.ExportHeadersFootersMode = TxtExportHeadersFootersMode.None;
doc.Save(dataDir + "outputFileNameC.txt", options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
TxtLoadOptions loadOptions = new TxtLoadOptions();
loadOptions.LeadingSpacesOptions = TxtLeadingSpacesOptions.Trim;
loadOptions.TrailingSpacesOptions = TxtTrailingSpacesOptions.Trim;
Document doc = new Document(dataDir + "LoadTxt.txt", loadOptions);
dataDir = dataDir + "HandleSpacesOptions_out.docx";
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.doc");
dataDir = dataDir + "Document.ConvertToTxt_out.txt";
doc.Save(dataDir);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document("input_document");
TxtSaveOptions options = new TxtSaveOptions();
options.ListIndentation.Count = 3;
options.ListIndentation.Character = ' ';
doc.Save(dataDir + "output.txt", options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document("input_document");
TxtSaveOptions options = new TxtSaveOptions();
options.ListIndentation.Count = 1;
options.ListIndentation.Character = '\t';
doc.Save(dataDir + "output.txt", options);
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "VbaProject_source.docm");
VbaProject project = doc.VbaProject;
Document destDoc = new Document();
destDoc.VbaProject = new VbaProject();
// Clone a single module.
VbaModule copyModule = doc.VbaProject.Modules["Module1"].Clone();
destDoc.VbaProject.Modules.Add(copyModule);
destDoc.Save(dataDir + "output.docm");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "VbaProject_source.docm");
VbaProject project = doc.VbaProject;
Document destDoc = new Document();
// Clone the whole project.
destDoc.VbaProject = doc.VbaProject.Clone();
destDoc.Save(dataDir + "output.docm");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document();
// Create a new VBA project.
VbaProject project = new VbaProject();
project.Name = "AsposeProject";
doc.VbaProject = project;
// Create a new module and specify a macro source code.
VbaModule module = new VbaModule();
module.Name = "AsposeModule";
module.Type = VbaModuleType.ProceduralModule;
module.SourceCode = "New source code";
// Add module to the VBA project.
doc.VbaProject.Modules.Add(module);
doc.Save(dataDir + "VbaProject_out.docm");
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "test.docm");
VbaProject project = doc.VbaProject;
const string newSourceCode = "Test change source code";
// Choose a module, and set a new source code.
project.Modules[0].SourceCode = newSourceCode;
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET
Document doc = new Document(dataDir + "Document.dot");
if (doc.VbaProject != null)
{
foreach (VbaModule module in doc.VbaProject.Modules)
{
Console.WriteLine(module.SourceCode);
}