public void Thumbnail() { //ExStart //ExFor:BuiltInDocumentProperties.Thumbnail //ExFor:DocumentProperty.ToByteArray //ExSummary:Shows how to append a thumbnail to an Epub document. // Create a blank document and add some text with a DocumentBuilder Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.Writeln("Hello world!"); // The thumbnail property resides in a document's built in properties, but is used exclusively by Epub e-book documents BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; // Load an image from our file system into a byte array byte[] thumbnailBytes = File.ReadAllBytes(ImageDir + "Logo.jpg"); // Set the value of the Thumbnail property to the array from above properties.Thumbnail = thumbnailBytes; // Our thumbnail should be visible at the start of the document, before the text we added doc.Save(ArtifactsDir + "Properties.Thumbnail.epub"); // We can also extract a thumbnail property into a byte array and then into the local file system like this DocumentProperty thumbnail = doc.BuiltInDocumentProperties["Thumbnail"]; File.WriteAllBytes(ArtifactsDir + "Properties.Thumbnail.gif", thumbnail.ToByteArray()); //ExEnd using (FileStream imgStream = new FileStream(ArtifactsDir + "Properties.Thumbnail.gif", FileMode.Open)) { TestUtil.VerifyImage(400, 400, imgStream); } }
public void Thumbnail() { //ExStart //ExFor:BuiltInDocumentProperties.Thumbnail //ExFor:DocumentProperty.ToByteArray //ExSummary:Shows how to add a thumbnail to a document that we save as an Epub. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.Writeln("Hello world!"); // If we save a document, whose "Thumbnail" property contains image data that we added, as an Epub, // a reader that opens that document may display the image before the first page. BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; byte[] thumbnailBytes = File.ReadAllBytes(ImageDir + "Logo.jpg"); properties.Thumbnail = thumbnailBytes; doc.Save(ArtifactsDir + "DocumentProperties.Thumbnail.epub"); // We can extract a document's thumbnail image and save it to the local file system. DocumentProperty thumbnail = doc.BuiltInDocumentProperties["Thumbnail"]; File.WriteAllBytes(ArtifactsDir + "DocumentProperties.Thumbnail.gif", thumbnail.ToByteArray()); //ExEnd using (FileStream imgStream = new FileStream(ArtifactsDir + "DocumentProperties.Thumbnail.gif", FileMode.Open)) { TestUtil.VerifyImage(400, 400, imgStream); } }
public void Description() { //ExStart //ExFor:BuiltInDocumentProperties.Author //ExFor:BuiltInDocumentProperties.Category //ExFor:BuiltInDocumentProperties.Comments //ExFor:BuiltInDocumentProperties.Keywords //ExFor:BuiltInDocumentProperties.Subject //ExFor:BuiltInDocumentProperties.Title //ExSummary:Shows how to work with document properties in the "Description" category. // Create a blank document Document doc = new Document(); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; // Set the values of some descriptive properties // These are metadata that can be glanced at without opening the document in the "Details" or "Content" folder views in Windows Explorer // The "Details" view has columns dedicated to these properties // Fields such as AUTHOR, SUBJECT, TITLE etc. can be used to display these values inside the document properties.Author = "John Doe"; properties.Title = "John's Document"; properties.Subject = "My subject"; properties.Category = "My category"; properties.Comments = $"This is {properties.Author}'s document about {properties.Subject}"; // Tags can be used as keywords and are separated by semicolons properties.Keywords = "Tag 1; Tag 2; Tag 3"; // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Description doc.Save(ArtifactsDir + "Properties.Description.docx"); //ExEnd }
public void HyperlinkBase() { //ExStart //ExFor:BuiltInDocumentProperties.HyperlinkBase //ExSummary:Shows how to store the base part of a hyperlink in the document's properties. // Create a blank document and a DocumentBuilder Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a relative hyperlink to "Document.docx", which will open that document when clicked on builder.InsertHyperlink("Relative hyperlink", "Document.docx", false); // If we don't have a "Document.docx" in the same folder as the document we are about to save, we will end up with a broken link Assert.False(File.Exists(ArtifactsDir + "Document.docx")); doc.Save(ArtifactsDir + "Properties.HyperlinkBase.BrokenLink.docx"); // We could keep prepending something like "C:\users\...\data" to every hyperlink we place to remedy this // Alternatively, if we know that all our linked files will come from the same folder, // we could set a base hyperlink in the document properties, keeping our hyperlinks short BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; Assert.True(File.Exists(MyDir + "Document.docx")); properties.HyperlinkBase = MyDir; doc.Save(ArtifactsDir + "Properties.HyperlinkBase.WorkingLink.docx"); //ExEnd }
[Test] //ExSkip public void Content() { Document doc = new Document(MyDir + "Paragraphs.docx"); BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount property shows the page count in real time and its value can be assigned to the Pages property // The "Pages" property stores the page count of the document. Assert.AreEqual(6, properties.Pages); // The "Words", "Characters", and "CharactersWithSpaces" built-in properties also display various document statistics, // but we need to call the "UpdateWordCount" method on the whole document before we can expect them to contain accurate values. Assert.AreEqual(1054, properties.Words); //ExSkip Assert.AreEqual(6009, properties.Characters); //ExSkip Assert.AreEqual(7049, properties.CharactersWithSpaces); //ExSkip doc.UpdateWordCount(); Assert.AreEqual(1035, properties.Words); Assert.AreEqual(6026, properties.Characters); Assert.AreEqual(7041, properties.CharactersWithSpaces); // Count the number of lines in the document, and then assign the result to the "Lines" built-in property. LineCounter lineCounter = new LineCounter(doc); properties.Lines = lineCounter.GetLineCount(); Assert.AreEqual(142, properties.Lines); // Assign the number of Paragraph nodes in the document to the "Paragraphs" built-in property. properties.Paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).Count; Assert.AreEqual(29, properties.Paragraphs); // Get an estimate of the file size of our document via the "Bytes" built-in property. Assert.AreEqual(20310, properties.Bytes); // Set a different template for our document, and then update the "Template" built-in property manually to reflect this change. doc.AttachedTemplate = MyDir + "Business brochure.dotx"; Assert.AreEqual("Normal", properties.Template); properties.Template = doc.AttachedTemplate; // "ContentStatus" is a descriptive built-in property. properties.ContentStatus = "Draft"; // Upon saving, the "ContentType" built-in property will contain the MIME type of the output save format. Assert.AreEqual(string.Empty, properties.ContentType); // If the document contains links, and they are all up to date, we can set the "LinksUpToDate" property to "true". Assert.False(properties.LinksUpToDate); doc.Save(ArtifactsDir + "DocumentProperties.Content.docx"); TestContent(new Document(ArtifactsDir + "DocumentProperties.Content.docx")); //ExSkip }
public void Origin() { //ExStart //ExFor:BuiltInDocumentProperties.Company //ExFor:BuiltInDocumentProperties.CreatedTime //ExFor:BuiltInDocumentProperties.LastPrinted //ExFor:BuiltInDocumentProperties.LastSavedBy //ExFor:BuiltInDocumentProperties.LastSavedTime //ExFor:BuiltInDocumentProperties.Manager //ExFor:BuiltInDocumentProperties.NameOfApplication //ExFor:BuiltInDocumentProperties.RevisionNumber //ExFor:BuiltInDocumentProperties.Template //ExFor:BuiltInDocumentProperties.TotalEditingTime //ExFor:BuiltInDocumentProperties.Version //ExSummary:Shows how to work with document properties in the "Origin" category. // Open a document that we have created and edited using Microsoft Word. Document doc = new Document(MyDir + "Properties.docx"); BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; // The following built-in properties contain information regarding the creation and editing of this document. // We can right-click this document in Windows Explorer and find // these properties via "Properties" -> "Details" -> "Origin" category. // Fields such as PRINTDATE and EDITTIME can display these values in the document body. Console.WriteLine($"Created using {properties.NameOfApplication}, on {properties.CreatedTime}"); Console.WriteLine($"Minutes spent editing: {properties.TotalEditingTime}"); Console.WriteLine($"Date/time last printed: {properties.LastPrinted}"); Console.WriteLine($"Template document: {properties.Template}"); // We can also change the values of built-in properties. properties.Company = "Doe Ltd."; properties.Manager = "Jane Doe"; properties.Version = 5; properties.RevisionNumber++; // Microsoft Word updates the following properties automatically when we save the document. // To use these properties with Aspose.Words, we will need to set values for them manually. properties.LastSavedBy = "John Doe"; properties.LastSavedTime = DateTime.Now; // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details" -> "Origin". doc.Save(ArtifactsDir + "DocumentProperties.Origin.docx"); //ExEnd properties = new Document(ArtifactsDir + "DocumentProperties.Origin.docx").BuiltInDocumentProperties; Assert.AreEqual("Doe Ltd.", properties.Company); Assert.AreEqual(new DateTime(2006, 4, 25, 10, 10, 0), properties.CreatedTime); Assert.AreEqual(new DateTime(2019, 4, 21, 10, 0, 0), properties.LastPrinted); Assert.AreEqual("John Doe", properties.LastSavedBy); TestUtil.VerifyDate(DateTime.Now, properties.LastSavedTime, TimeSpan.FromSeconds(5)); Assert.AreEqual("Jane Doe", properties.Manager); Assert.AreEqual("Microsoft Office Word", properties.NameOfApplication); Assert.AreEqual(12, properties.RevisionNumber); Assert.AreEqual("Normal", properties.Template); Assert.AreEqual(8, properties.TotalEditingTime); Assert.AreEqual(786432, properties.Version); }
[Test] //ExSkip public void Content() { // Open a document with a couple paragraphs of content Document doc = new Document(MyDir + "Properties.Content.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; // By using built in properties, // we can treat document statistics such as word/page/character counts as metadata that can be glanced at without opening the document // These properties are accessed by right-clicking the file in Windows Explorer and navigating to Properties > Details > Content // If we want to display this data inside the document, we can use fields such as NUMPAGES, NUMWORDS, NUMCHARS etc. // Also, these values can also be viewed in Microsoft Word by navigating File > Properties > Advanced Properties > Statistics // Page count: The PageCount attribute shows the page count in real time and its value can be assigned to the Pages property properties.Pages = doc.PageCount; Assert.AreEqual(2, properties.Pages); // Word count: The UpdateWordCount() automatically assigns the real time word/character counts to the respective built in properties doc.UpdateWordCount(); Assert.AreEqual(198, properties.Words); Assert.AreEqual(1114, properties.Characters); Assert.AreEqual(1310, properties.CharactersWithSpaces); // Line count: Count the lines in a document and assign value to the Lines property\ LineCounter lineCounter = new LineCounter(doc); properties.Lines = lineCounter.GetLineCount(); Assert.AreEqual(14, properties.Lines); // Paragraph count: Assign the size of the count of child Paragraph-nodes to the Paragraphs built in property properties.Paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).Count; Assert.AreEqual(2, properties.Paragraphs); // Bytes: Use a stream to find out the real file size of our document and assign it to the Property using (MemoryStream stream = new MemoryStream()) { doc.Save(stream, SaveFormat.Docx); properties.Bytes = (int)stream.Length; Assert.AreEqual(10870, properties.Bytes); } // Template: The Template attribute can reflect the filename of the attached template document doc.AttachedTemplate = MyDir + "Document.BusinessBrochureTemplate.dot"; Assert.AreEqual("Normal", properties.Template); properties.Template = doc.AttachedTemplate; // Content status: This is a descriptive field properties.ContentStatus = "Draft"; // Content type: Upon saving, any value we assign to this field will be overwritten by the MIME type of the output save format Assert.AreEqual("", properties.ContentType); // If the document contains links and they are all up to date, we can set this to true Assert.False(properties.LinksUpToDate); doc.Save(ArtifactsDir + "Properties.Content.docx"); }
public void Origin() { //ExStart //ExFor:BuiltInDocumentProperties.Company //ExFor:BuiltInDocumentProperties.CreatedTime //ExFor:BuiltInDocumentProperties.LastPrinted //ExFor:BuiltInDocumentProperties.LastSavedBy //ExFor:BuiltInDocumentProperties.LastSavedTime //ExFor:BuiltInDocumentProperties.Manager //ExFor:BuiltInDocumentProperties.NameOfApplication //ExFor:BuiltInDocumentProperties.RevisionNumber //ExFor:BuiltInDocumentProperties.Template //ExFor:BuiltInDocumentProperties.TotalEditingTime //ExFor:BuiltInDocumentProperties.Version //ExSummary:Shows how to work with document properties in the "Origin" category. Document doc = new Document(MyDir + "Properties.docx"); // The properties we will work with are members of the BuiltInDocumentProperties attribute BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; // Since this document has been edited and printed in the past, values generated by Microsoft Word will appear here // These values can be glanced at by right clicking the file in Windows Explorer, without actually opening the document // Fields such as PRINTDATE, EDITTIME etc. can display these values inside the document Console.WriteLine($"Created using {properties.NameOfApplication}, on {properties.CreatedTime}"); Console.WriteLine($"Minutes spent editing: {properties.TotalEditingTime}"); Console.WriteLine($"Date/time last printed: {properties.LastPrinted}"); Console.WriteLine($"Template document: {properties.Template}"); // We can set these properties ourselves properties.Company = "Doe Ltd."; properties.Manager = "Jane Doe"; properties.Version = 5; properties.RevisionNumber++; // If we plan on programmatically saving the document, we may record some details like this properties.LastSavedBy = "John Doe"; properties.LastSavedTime = DateTime.Now; // When right clicking the document file in Windows Explorer, these properties are found in Properties > Details > Origin doc.Save(ArtifactsDir + "Properties.Origin.docx"); //ExEnd properties = new Document(ArtifactsDir + "Properties.Origin.docx").BuiltInDocumentProperties; Assert.AreEqual("Doe Ltd.", properties.Company); Assert.AreEqual(new DateTime(2006, 4, 25, 10, 10, 0), properties.CreatedTime); Assert.AreEqual(new DateTime(2019, 4, 21, 10, 0, 0), properties.LastPrinted); Assert.AreEqual("John Doe", properties.LastSavedBy); TestUtil.VerifyDate(DateTime.Now, properties.LastSavedTime, TimeSpan.FromSeconds(5)); Assert.AreEqual("Jane Doe", properties.Manager); Assert.AreEqual("Microsoft Office Word", properties.NameOfApplication); Assert.AreEqual(12, properties.RevisionNumber); Assert.AreEqual("Normal", properties.Template); Assert.AreEqual(8, properties.TotalEditingTime); Assert.AreEqual(786432, properties.Version); }
//ExEnd private void TestContent(Document doc) { BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; Assert.AreEqual(6, properties.Pages); Assert.AreEqual(1035, properties.Words); Assert.AreEqual(6026, properties.Characters); Assert.AreEqual(7041, properties.CharactersWithSpaces); Assert.AreEqual(142, properties.Lines); Assert.AreEqual(29, properties.Paragraphs); Assert.AreEqual(15500, properties.Bytes, 200); Assert.AreEqual(MyDir.Replace("\\\\", "\\") + "Business brochure.dotx", properties.Template); Assert.AreEqual("Draft", properties.ContentStatus); Assert.AreEqual(string.Empty, properties.ContentType); Assert.False(properties.LinksUpToDate); }
public void HyperlinkBase() { //ExStart //ExFor:BuiltInDocumentProperties.HyperlinkBase //ExSummary:Shows how to store the base part of a hyperlink in the document's properties. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a relative hyperlink to a document in the local file system named "Document.docx". // Clicking on the link in Microsoft Word will open the designated document, if it is available. builder.InsertHyperlink("Relative hyperlink", "Document.docx", false); // This link is relative. If there is no "Document.docx" in the same folder // as the document that contains this link, the link will be broken. Assert.False(File.Exists(ArtifactsDir + "Document.docx")); doc.Save(ArtifactsDir + "DocumentProperties.HyperlinkBase.BrokenLink.docx"); // The document we are trying to link to is in a different directory to the one we are planning to save the document in. // We could fix links like this by putting an absolute filename in each one. // Alternatively, we could provide a base link that every hyperlink with a relative filename // will prepend to its link when we click on it. BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; properties.HyperlinkBase = MyDir; Assert.True(File.Exists(properties.HyperlinkBase + ((FieldHyperlink)doc.Range.Fields[0]).Address)); doc.Save(ArtifactsDir + "DocumentProperties.HyperlinkBase.WorkingLink.docx"); //ExEnd doc = new Document(ArtifactsDir + "DocumentProperties.HyperlinkBase.BrokenLink.docx"); properties = doc.BuiltInDocumentProperties; Assert.AreEqual(string.Empty, properties.HyperlinkBase); doc = new Document(ArtifactsDir + "DocumentProperties.HyperlinkBase.WorkingLink.docx"); properties = doc.BuiltInDocumentProperties; Assert.AreEqual(MyDir, properties.HyperlinkBase); Assert.True(File.Exists(properties.HyperlinkBase + ((FieldHyperlink)doc.Range.Fields[0]).Address)); }
public void Description() { //ExStart //ExFor:BuiltInDocumentProperties.Author //ExFor:BuiltInDocumentProperties.Category //ExFor:BuiltInDocumentProperties.Comments //ExFor:BuiltInDocumentProperties.Keywords //ExFor:BuiltInDocumentProperties.Subject //ExFor:BuiltInDocumentProperties.Title //ExSummary:Shows how to work with built-in document properties in the "Description" category. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); BuiltInDocumentProperties properties = doc.BuiltInDocumentProperties; // Below are four built-in document properties that have fields that can display their values in the document body. // 1 - "Author" property, which we can display using an AUTHOR field: properties.Author = "John Doe"; builder.Write("Author:\t"); builder.InsertField(FieldType.FieldAuthor, true); // 2 - "Title" property, which we can display using a TITLE field: properties.Title = "John's Document"; builder.Write("\nDoc title:\t"); builder.InsertField(FieldType.FieldTitle, true); // 3 - "Subject" property, which we can display using a SUBJECT field: properties.Subject = "My subject"; builder.Write("\nSubject:\t"); builder.InsertField(FieldType.FieldSubject, true); // 4 - "Comments" property, which we can display using a COMMENTS field: properties.Comments = $"This is {properties.Author}'s document about {properties.Subject}"; builder.Write("\nComments:\t\""); builder.InsertField(FieldType.FieldComments, true); builder.Write("\""); // The "Category" built-in property does not have a field that can display its value. properties.Category = "My category"; // We can set multiple keywords for a document by separating the string value of the "Keywords" property with semicolons. properties.Keywords = "Tag 1; Tag 2; Tag 3"; // We can right-click this document in Windows Explorer and find these properties in "Properties" -> "Details". // The "Author" built-in property is in the "Origin" group, and the others are in the "Description" group. doc.Save(ArtifactsDir + "DocumentProperties.Description.docx"); //ExEnd doc = new Document(ArtifactsDir + "DocumentProperties.Description.docx"); properties = doc.BuiltInDocumentProperties; Assert.AreEqual("John Doe", properties.Author); Assert.AreEqual("My category", properties.Category); Assert.AreEqual($"This is {properties.Author}'s document about {properties.Subject}", properties.Comments); Assert.AreEqual("Tag 1; Tag 2; Tag 3", properties.Keywords); Assert.AreEqual("My subject", properties.Subject); Assert.AreEqual("John's Document", properties.Title); Assert.AreEqual("Author:\t\u0013 AUTHOR \u0014John Doe\u0015\r" + "Doc title:\t\u0013 TITLE \u0014John's Document\u0015\r" + "Subject:\t\u0013 SUBJECT \u0014My subject\u0015\r" + "Comments:\t\"\u0013 COMMENTS \u0014This is John Doe's document about My subject\u0015\"", doc.GetText().Trim()); }