In an App Service running on Azure I need to replace mail merge fields in a word/docx-document with content.
As I understand interop can't be used because it needs word to be installed.
So how do I replace mail merge fields on Azure in a c# app service?
Maybe one can use the OpenXML SDK for this? But how?
[Update]
OpenXML worked, I created the following helper class to replace the mailmerge content:
public static void DocXReplaceMergeFields(Stream docStream, Dictionary<string, string> placeholder)
{
using (var docXml = WordprocessingDocument.Open(docStream, true))
{
//docXml.ChangeDocumentType(WordprocessingDocumentType.Document);
foreach (var run in docXml.MainDocumentPart.Document.Descendants<Run>())
{
foreach (var text in run.Descendants<Text>().Where(a => a.Text.StartsWith("«") && a.Text.EndsWith("»")))
{
var propertyName = text.Text.Substring(1, text.Text.Length - 2);
if (placeholder.TryGetValue(propertyName, out var propertyValue))
text.Text = propertyValue;
}
}
var settingsPart = docXml.MainDocumentPart.GetPartsOfType<DocumentSettingsPart>().First();
var oxeSettings = settingsPart.Settings.Where(a => a.LocalName == "mailMerge").FirstOrDefault();
if (oxeSettings != null)
{
settingsPart.Settings.RemoveChild(oxeSettings);
settingsPart.Settings.Save();
}
docXml.MainDocumentPart.Document.Save();
}
}
You can give a try using Open XML SDK:
https://learn.microsoft.com/en-us/office/open-xml/word-processing
If that doesn't work for some reason, try doing the same using an Azure Logic App:
https://medium.com/plumsail/create-complex-excel-and-word-documents-from-templates-in-microsoft-flow-azure-logic-apps-and-794334e59f0f
Related
We are trying to extract data from a checkbox using the Form recognizer. We have a custom model where we extract 4 fields. All fields get extracted except one ("has_observations").
We used the analyze tab on fott-2-1.azurewebsites.net and it shows correctly for all the files we are trying to do OCR on.
private static FormField GetField(this RecognizedFormCollection forms, string fieldName)
{
FormField field = null;
foreach (RecognizedForm form in forms)
{
if (form.Fields.ContainsKey(fieldName) && form.Fields[fieldName] != null)
{
field = form.Fields[fieldName];
logger.LogWarning("values is=" + field.ValueData.ToString());
break;
}
}
return field;
}
The field is "Azure.AI.FormRecognizer.Models.FormField" for "has_observations" label everytime and we have no idea how to fix it.
Which version of FormRecognizer SDK are you using? 3.1.1? You maybe calling different version from website vs from SDK.
If you are using 4.0.0-beta.X, by default it calls 2022-01-30-preview version of FormRecognizer APIs (see https://azuresdkdocs.blob.core.windows.net/$web/dotnet/Azure.AI.FormRecognizer/4.0.0-beta.3/index.html), but based on the screenshot you are using 2.1. So, API results maybe different between versions.
A few other troubleshooting options to see exact URLs and responses for http traffic from FormRecognizer SDK:
Logging with the Azure SDK for .NET, I.e. line below starts printing to the console each request/response.
var listener = AzureEventSourceListener.CreateConsoleLogger(EventLevel.Informational);
Some information will be redacted in logs. Changes to ClientOptions below will show more data (see details in Azure SDK diagnostics):
var frOptions = new FormRecognizerClientOptions() { Diagnostics = { IsLoggingContentEnabled = true, LoggedHeaderNames = { "*" }, LoggedQueryParameters = { "*" }}};
var client = new FormRecognizerClient(new Uri(endpoint), credential, frOptions);
I'm trying to get out all embedded resource files from a solution using Roslyn and MSBuild Api.
private async Task<Document> CheckConstForLocalization(Document document, LocalDeclarationStatementSyntax localDeclaration,
CancellationToken cancellationToken)
{
foreach (var project in document.Project.Solution.Projects)
{
foreach (var sourceDoc in project.AdditionalDocuments)
{
if (false == sourceDoc.Name.EndsWith(".cs"))
{
Debug.WriteLine(sourceDoc.Name);
}
}
foreach (var sourceDoc in project.Documents)
{
if (false == sourceDoc.Name.EndsWith(".cs"))
{
Debug.WriteLine(sourceDoc.Name);
}
}
}
var newRoot = await document.GetSyntaxRootAsync(cancellationToken);
// Return document with transformed tree.
return document.WithSyntaxRoot(newRoot);
}
When I modify my resource files to be AdditionFiles, I can get them through the project AdditionalDocuments. However I would like to be able to grab these with out doing so. The file does not appear in Documents or Additional Documents
How can I find Resx files without modifying their attributes?
I've figured out a way to find the designer files, I get the associated C# Document names by iterating over the csproj file and getting the embedded resources.
public const string LAST_GENERATED_TAG = "LastGenOutput";
public const string RESX_FILE_EXTENSION = ".resx";
public List<string> GetResourceDesignerInfo(Project project)
{
XDocument xmldoc = XDocument.Load(project.FilePath);
XNamespace msbuild = "http://schemas.microsoft.com/developer/msbuild/2003";
var resxFiles = new List<string>();
foreach (var resource in xmldoc.Descendants(msbuild + "EmbeddedResource"))
{
string includePath = resource.Attribute("Include").Value;
var includeExtension = Path.GetExtension(includePath);
if (0 == string.Compare(includeExtension, RESX_FILE_EXTENSION, StringComparison.OrdinalIgnoreCase))
{
var outputTag = resource.Elements(msbuild + LAST_GENERATED_TAG).FirstOrDefault();
if (null != outputTag)
{
resxFiles.Add(outputTag.Value);
}
}
}
return resxFiles;
}
You can't, at the moment. It isn't supported by the API. (I was researching this just yesterday.)
There's a feature request to support it which you might like to support and subscribe to, but I don't believe there's any way of doing this at the moment.
My understanding is that Visual Studio hooks into MSBuild more tightly than Roslyn's support does at the moment. (See the issue I raised about <Deterministic> for another example.)
I know there are several docx creation/modification approaches in C#
OpenXML, DocX API, etc.
I've figured out how to enable tracked changes using Open XML, just haven't figured out to lock the tracked changes (a feature in word 2013).
This code works fine for me:
static void Main(string[] args)
{
using (var document = WordprocessingDocument.Open(#"D:\DocTest\Test1.docx", true))
{
AddTrackingLock(document);
}
}
private static void AddTrackingLock(WordprocessingDocument document)
{
var documentSettings = document.MainDocumentPart.DocumentSettingsPart;
var documentProtection = documentSettings
.Settings
.FirstOrDefault(it =>
it is DocumentProtection &&
(it as DocumentProtection).Edit == DocumentProtectionValues.TrackedChanges)
as DocumentProtection;
if (documentProtection == null)
{
var documentProtectionElement = new DocumentProtection();
documentProtectionElement.Edit = DocumentProtectionValues.TrackedChanges;
documentProtectionElement.Enforcement = OnOffValue.FromBoolean(true);
documentSettings.Settings.AppendChild(documentProtectionElement);
}
else
{
documentProtection.Enforcement = OnOffValue.FromBoolean(true);
}
}
Have a look at How to set the editing restrictions in Word using Open XML SDK 2.0
I don't have any working code for you, but the password hash needs to be stored in
DocumentProtection.Hash.
Hope that this hint helps moving you along!
I am creating a wcf library that needs to read xml files as pass the elements through the service that will be using in my windows 8 app.
Question is I first created a WCF application using the same code and it worked fine but now using the library the xml files are returning as NULL.
{
var doc = XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("/TestCodes.xml"));
var testCodes = doc.Descendants("TextInfo");
return testCodes.Select(item =>
{
var xElement = item.Element("Value");
var element = item.Element("Description");
if (element != null)
return xElement != null ? new TestCodes()
{
Value = xElement.Value,
Description = element.Value
} : null;
return null;
}).ToList();
}
Is there another way to read in an xml files in a library that differs from the wcf application? Unfortunately I have to use the WCF Library so no way around that part.
Any help would be great.
Is it possible to read the publisher name of the currently running ClickOnce application (the one you set at Project Properties -> Publish -> Options -> Publisher name in Visual Studio)?
The reason why I need it is to run another instance of the currently running application as described in this article and pass parameters to it.
Of course I do know my application's publisher name, but if I hard code it and later on I decide to change my publisher's name I will most likely forget to update this piece of code.
Here is another option. Note that it will only get the publisher name for the currently running application, which is all I need.
I'm not sure if this is the safest way to parse the XML.
public static string GetPublisher()
{
XDocument xDocument;
using (MemoryStream memoryStream = new MemoryStream(AppDomain.CurrentDomain.ActivationContext.DeploymentManifestBytes))
using (XmlTextReader xmlTextReader = new XmlTextReader(memoryStream))
{
xDocument = XDocument.Load(xmlTextReader);
}
var description = xDocument.Root.Elements().Where(e => e.Name.LocalName == "description").First();
var publisher = description.Attributes().Where(a => a.Name.LocalName == "publisher").First();
return publisher.Value;
}
You would think this would be trivial, but I don't see anything in the framework that gives you this info.
If you want a hack, you can get the publisher from the registry.
Disclaimer - Code is ugly and untested...
...
var publisher = GetPublisher("My App Name");
...
public static string GetPublisher(string application)
{
using (var key = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Uninstall"))
{
var appKey = key.GetSubKeyNames().FirstOrDefault(x => GetValue(key, x, "DisplayName") == application);
if (appKey == null) { return null; }
return GetValue(key, appKey, "Publisher");
}
}
private static string GetValue(RegistryKey key, string app, string value)
{
using (var subKey = key.OpenSubKey(app))
{
if (!subKey.GetValueNames().Contains(value)) { return null; }
return subKey.GetValue(value).ToString();
}
}
If you find a better solution, please follow-up.
I dont know about ClickOnce, but normally, you can read the assembly-info using the System.Reflection framework:
public string AssemblyCompany
{
get
{
object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
if (attributes.Length == 0)
{
return "";
}
return ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
Unfortunately, theres no "publisher" custom-attribute, just throwing this out as a possible work-around