Hi,
I am new to Aspose, so I might be overlooking something obvious.
I have a load of Word template files (.dotx) containing Structured Document Tags that has been handed to me by a client. Using Aspose.Words 10.8.0.0 I have been attempting to write some code to replace the content of the SDTs and save the resulting output to a file.
This has worked fine when saving to .xps or .pdf. However, when I change the output fileformat to .docx and open the result in Word the content of the tags is not replaced by the content I have defined in code.
I tried to insert an SDT of my own with Word2010, ran the code again and found that the tag I put in myself was replaced fine, but the other was not. When changing the output to .doc or .xps, both tags are replaced as expected.
If I unzip the .xml-files from the .docx, I can see my values in document.xml.
Here is some sample code that generates the output (sorry, not sure how to enable syntax highlighting … ):
using Aspose.Words;
using Aspose.Words.Markup;
namespace DocumentGeneration.Tryout.Managers
{
public class SdtManager : AsposeManagerBase
{
private
const string TemplateFile = @"C:\Aspose\Test\template2.dotx";
private
const string OutputDocxFile = @"C:\Aspose\Test\output-docx.docx";
private
const string OutputDocFile = @"C:\Aspose\Test\output-doc.doc";
private
const string OutputXpsFile = @"C:\Aspose\Test\output-xps.xps";
private
const string LicenseFile = @"C:\Aspose\license\license.lic";
public SdtManager()
{
var license = new Aspose.Words.License();
license.SetLicense(LicenseFile);
}
public void InsertNameAndSave(object name)
{
var document = new Document(TemplateFile);
var tags = document.GetChildNodes(NodeType.StructuredDocumentTag, true);
foreach (var tag in tags)
{
var sdt = tag as StructuredDocumentTag;
if (sdt != null && sdt.Tag.Equals("NameTag"))
{
sdt.RemoveAllChildren();
var p = new Paragraph(document);
var r = new Run(document, name.ToString());
if (sdt.Level == MarkupLevel.Inline)
sdt.AppendChild«;
else
{
p.AppendChild«;
sdt.AppendChildº;
}
}
}
document.Save(OutputDocxFile, SaveFormat.Docx);
document.Save(OutputDocFile, SaveFormat.Doc);
document.Save(OutputXpsFile, SaveFormat.Xps);
}
}
}
using DocumentGeneration.Tryout.Managers;
namespace DSA.Facit.DocumentGeneration.Tryout.Runner
{
class Program
{
static void Main(string[] args)
{
var manager = new SdtManager();
manager.InsertNameAndSave("John Doe");
}
}
}
I have attached a stripped down version of the template I use, including the tag I inserted myself.
As far as I can tell, the only difference between the two tags is the attributes Id and PlaceHolderName having different values.
Best regards,
Christian