Check if uploaded MS Excel/MS Word is using the preformatted template

Is there a way to check if the uploaded file (MS Excel/MS Word) is using a preformatted template (MS Excel/MS Word)?

What we want is only allow file that uses preformatted template for uploading.
Thanks.
Hi,

Thanks for your inquiry.

Yes, you can check if a Word document was created using a template or not. In Aspose.Words, Document.AttachedTemplate property contains full path to the template(including the template name) using which the Word file was created. If a Word file under consideration was created without using any template, the Document.AttachedTemplate property will contain an empty string. You can check the property and accept only the files which were created using the specific template of your interest. Please have a look here to see the Document class members and description of AttachedTemplate property.

You can also use Document.BuiltInDocumentProperties.Template property to fetch the name of the template file used while creating a Word document. Document.BuiltInDocumentProperties.Template contains the template file name used while creating the file or contains "Normal.dotm" if no template was used.

Here is how you can get the above properties after loading a document using Aspose.Words for .NET:
// Load uploaded document using Aspose.Words
Document doc = new Document("uploadedDocument.docx");

// Get Attached Template path and Template name.
String csTemplatePath = doc.AttachedTemplate;
String csBuiltInTemplateName = doc.BuiltInDocumentProperties.Template;

Hope this helps. I am moving this forum thread to Aspose.Total forum and my colleagues from Aspose.Cells team will reply you shortly.

Hi,


I am a representative of Aspose.Cells team.

Regarding Excel’s template info, similarly, you may use BuiltInDocumentPropertyCollection.Template property to get/set the name of the document template.

Sample code:

Workbook wb = new Workbook(“e:\test2\Book1.xlsx”);
String tempName = wb.BuiltInDocumentProperties.Template;

Thank you.



Thank you for your reply!

Hi,


You are welcome. Please let us know any time if you have any further queries. We are always glad to help you.

Hi,


I tried some code to check the template of uploaded file.
It is working fine if I am using the sample templates in the Microsoft Word.

When I create my own word template: “Test.dotx” and do a copy called “Test-copy.dotx”,
both template name are “Test.dotx”.

However, when I edit the content of Test-copy.dotx and save it as “TestDoc.docx”,
the template name of “TestDoc.docx” is “TestDoc.docx”.

What I would like to achieve is provide a preformatted template and user can edit the content and upload the file. When they upload a file, we will check if the uploaded file is using the preformatted template.
Is it possible to achieve this?
Also, is it possible that an user can create his own template with the same name but different content and pass the checking of template?
Hi,

Thanks for your inquiry.

I think what you are looking is a way to enforce security on your template. You can add protection to individual content controls in a template to help prevent someone from deleting or editing a particular content control or group of controls, or you can help protect all of the template content with a password by enforcing restrictions on the template. You can also set template to be read only so that no one can modify it and only the documents created using the template should contain that template name. Please have a quick look here for further details if you are creating templates using Microsoft Word.

If you want to enforce the security using Aspose.Words, then I would suggest you to please have a look at how to protect the document using Aspose.Words here. Also have a look at how to access document properties using Aspose.Words here.

Yes, it is possible that a user creates his own template with same name and passes the template verification logic. Unfortunately, in this case, we can not hide template name from user.

Hope this helps. Please let us know if you have any further queries

Hi,


Thanks for your reply.

I have attached a copy of what I have done so far.
In the attachment, I use only legacy forms field. These data can be read via

// Get all the formfields
FormFieldCollection formFields = doc.Range.FormFields;

I am wondering is there a way to style the legacy form field?

If not, I would like to change the form fiields to activeX control.
Is there a way I can read the data for activex control using aspose.words?

Thank you.

Also, How do I read the image file using Aspose.Word.

I want to replace the text field for logo with image selector.