How to use `CustomDocumentProperties` methods

I have a server running the DocumentEditor Import service. The code of such service is basically equal to the one posted here: https://github.com/SyncfusionExamples/EJ2-DocumentEditor-WebServices/tree/master/ASP.NET%20Core.

Now, I want to have a FileSystems CustomProperties (this one: https://help.syncfusion.com/file-formats/docio/working-with-word-document?cs-save-lang=1&cs-lang=asp.net%20core#working-with-word-document-properties), so I added this to the DocumentEditorController.cs file:
[AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
[Route("CustomProperties")]
public string CustomProperties(IFormCollection data)
{
if (data.Files.Count == 0)
return null;
Stream stream = new MemoryStream();
IFormFile file = data.Files[0];
int index = file.FileName.LastIndexOf('.');
string type = index > -1 && index < file.FileName.Length - 1 ?
file.FileName.Substring(index) : ".docx";
file.CopyTo(stream);
stream.Position = 0;

WordDocument document = WordDocument.Load(stream, GetFormatType(type.ToLower()));
document.CustomDocumentProperties.Add("genesisId", "some_random_id");
string json = Newtonsoft.Json.JsonConvert.SerializeObject(document);
document.Dispose();

return json;
}

Problem is, when I try to compile the code, I get this:
Failed to download package 'Microsoft.Extensions.Configuration.UserSecrets.2.0.0' from 'https://api.nuget.org/v3-flatcontainer/microsoft.extensions.configuration.usersecrets/2.0.0/microsoft.extensions.configuration.usersecrets.2.0.0.nupkg'.
  The download of 'https://api.nuget.org/v3-flatcontainer/microsoft.extensions.configuration.usersecrets/2.0.0/microsoft.extensions.configuration.usersecrets.2.0.0.nupkg' timed out because no data was received for 60000ms.
    Exception of type 'System.TimeoutException' was thrown.
  Failed to download package 'Microsoft.AspNetCore.Mvc.DataAnnotations.2.0.2' from 'https://api.nuget.org/v3-flatcontainer/microsoft.aspnetcore.mvc.dataannotations/2.0.2/microsoft.aspnetcore.mvc.dataannotations.2.0.2.nupkg'.
  The download of 'https://api.nuget.org/v3-flatcontainer/microsoft.aspnetcore.mvc.dataannotations/2.0.2/microsoft.aspnetcore.mvc.dataannotations.2.0.2.nupkg' timed out because no data was received for 60000ms.
    Exception of type 'System.TimeoutException' was thrown.
  Restore completed in 1.16 min for /App/EJ2APIServices.csproj.
Controllers/DocumentEditorController.cs(53,22): error CS1061: 'WordDocument' does not contain a definition for 'CustomDocumentProperties' and no accessible extension method 'CustomDocumentProperties' accepting a first argument of type 'WordDocument' could be found (are you missing a using directive or an assembly reference?) [/App/EJ2APIServices.csproj]
The command '/bin/sh -c dotnet publish -c Release -o /out' returned a non-zero code: 1
scripty ERR! script failed: '/home/guerra/projects/megazord/applications/syncfusion-service/scripts/build/index.sh'
scripty ERR! exit status: 1


Looking at the docs, I found something about Assemblies. Up to today, I have never heard of them (I'm not a .NET Core developer) so I'm not sure if I'm using them correctly.

Anyway, I got the Syncfusion.DocIO.Portable.dllSyncfusion.DocIO.PortableSyncfusion.Compression.Portable.dllSyncfusion.Compression.Portable.xmlSyncfusion.OfficeChart.Portable.dllSyncfusion.OfficeChart.Portable.xml, files from Syncfusion/Essential Studio/FileFormats/18.3.0.35/Assemblies/.NET Standard 2.0+ and put them on a Assemblies folder on src/. Then I tried to load those assemblies using Assembly.Load() but got no results (indeed, I tried passing a non-sensical string to force and error but nothing happened).

Could anyone helpo me?

5 Replies 1 reply marked as answer

HC Harini Chellappa Syncfusion Team November 19, 2020 07:22 AM UTC

Hi Henrique, 
 
Syncfusion Greetings! 
 
We have checked the provided code snippet. You are using customDocumentProperties of Syncfusion.DocIO.DLS.WordDocument in Syncfusion.EJ2.DocumentEditor.WordDocument. 
 
 
You have created the word document object of Syncfusion.EJ2.DocumentEditor.WordDocument in which there is no property/class called customDocumentProperties. Hence it throws below error. 
 
“ 'WordDocument' does not contain a definition for 'CustomDocumentProperties' and no accessible extension method 'CustomDocumentProperties' accepting a first argument of type 'WordDocument' could be found (are you missing a using directive or an assembly reference?)” 
 
Currently we don’t have support to add custom document properties as like DocIO. 
 
Can you please share your usecase requirement on using customDocumentProperties in DocumentEditor? So that we will check and update you the details accordingly. 
 
Regards, 
 
Harini C 



HG Henrique Guerra November 19, 2020 01:47 PM UTC

Hello, Harini.

I hope you are doing well!

My requirements are quite simple, indeed. I just want want to expose a route in my application that allows me to insert a given Custom Property with a given value in a given .docx file.

Since I found no sample in your Github for DocIO, I thought I could use the same project I already had (the DocumentEdtitor one) because it also has a DocIO dependency.

Reading your reply, I understand that I'm not referencing the correct DocIO package, thus I don't have the correct properties to do what I want. So, how could I import the correct dependencies in my project to have both DocumentEditor and CustomProperties APIs in the same service? Since I did no big changes to the sample DocumentEditor project in .NET Core, I believe you could guide me/implement an example using it as references.

Thank you for your reply.


HC Harini Chellappa Syncfusion Team November 20, 2020 06:00 AM UTC

Hi Henrique, 
 
To set custom properties for the word document via DocIO, you need to pass the file stream to DocIO document object and then set the properties in that document object. 
 
 
Sample code snippet 
 
//stream is the document stream in which you want to add custom properties 
Syncfusion.DocIO.DLS.WordDocument doc = new Syncfusion.DocIO.DLS.WordDocument(stream, Syncfusion.DocIO.FormatType.Docx); 
doc.CustomDocumentProperties.Add("genesisId", "12345"); 
FileStream output_stream = new FileStream(@"Data/custproperties.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite); 
doc.Save(output_stream, Syncfusion.DocIO.FormatType.Docx); 
doc.Close(); 
 
In DocumentEditor controller, we have defined the DocIO Word Document as WDocument. 
 
 
you can also initialize document object like below. 
 
WDocument doc = new WDocument(stream, WFormatType.Docx); 
doc.CustomDocumentProperties.Add("genesisId", "12345"); 
FileStream output_stream = new FileStream(@"Data/custproperties.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite); 
doc.Save(output_stream, WFormatType.Docx); 
doc.Close(); 
 
Kindly check it and let us know whether this helps. 
 
Regards, 
 
Harini C 



HG Henrique Guerra November 23, 2020 03:24 PM UTC

Hi, Harini.

I'm getting a file not found error now:


Here's the code:
[AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
[Route("AddCustomProperty")]
public FileStreamResult AddCustomProperty(IFormCollection data)
{
if (data.Files.Count == 0)
return null;

IFormFile file = data.Files[0];
int index = file.FileName.LastIndexOf('.');
string type = index > -1 && index < file.FileName.Length - 1 ?
file.FileName.Substring(index) : ".docx";

Console.WriteLine("Before sourceStreamPath definition");
Console.WriteLine(file.FileName);
FileStream sourceStreamPath = new FileStream(@"Data/eita foi (2).docx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
Console.WriteLine("After sourceStreamPath definition");
//Opens an source document from file system through constructor of WordDocument class
using (WDocument doc = new WDocument(sourceStreamPath, WFormatType.Docx))
{
//Adds the custom document properties of various data types
doc.CustomDocumentProperties.Add("genesisId", "12345");

//Saves and closes the destination document to MemoryStream
MemoryStream output_stream = new MemoryStream();
doc.Save(output_stream, WFormatType.Docx);
doc.Close();
output_stream.Position = 0;

//Download Word document in the browser
return File(output_stream, "application/msword", "Result.docx");
}
}

Marked as answer

HG Henrique Guerra November 23, 2020 09:10 PM UTC

Thank you for your help, Harini.

I finally got it to work:
AcceptVerbs("Post")]
[HttpPost]
[EnableCors("AllowAllOrigins")]
[Route("InitVersioning")]
public FileStreamResult InitVersioning(IFormCollection data)
{
if (data.Files.Count == 0)
return null;

IFormFile file = data.Files[0];
string fileExt = GetFileExt(file.FileName);
WFormatType format = GetWFormatType(fileExt);
string contentType = GetContentType(format);

string documentId = HttpContext.Request.Query["id"].ToString();

if (documentId == null)
throw new NotSupportedException("id was not passed as a parameter on querystring.");

using (WDocument doc = GetDocument(file))
{
doc.CustomDocumentProperties.Remove(DOCUMENT_ID);
doc.CustomDocumentProperties.Add(DOCUMENT_ID, documentId);

doc.CustomDocumentProperties.Remove(DOCUMENT_VERSION);
doc.CustomDocumentProperties.Add(DOCUMENT_VERSION, "1");

//Saves and closes the destination document to MemoryStream
MemoryStream output_stream = new MemoryStream();
doc.Save(output_stream, WFormatType.Docx);
doc.Close();
doc.Dispose();
output_stream.Position = 0;

//Download Word document in the browser
return File(output_stream, contentType, file.FileName);
}
}

Loader.
Up arrow icon