다음은 시험해 볼 수있는 방법입니다. 템플릿 문서에 다음과 같은 콘텐츠가 있다고 가정 해 보겠습니다.
소셜 미디어 : [FacebookPage1] | [TwitterPage1] | [GooglePlusPage1] | [LinkedInPage1]
는이 경우 당신은 하이퍼 링크와 함께 그 자리를 대체하기 위해 다음과 같은 사용할 수 있습니다
:
// Create collection of "placeholder -> link" pairs.
var linkData = new Dictionary<string, string>()
{
["FacebookPage1"] = "https://www.facebook.com",
["TwitterPage1"] = "https://twitter.com",
["GooglePlusPage1"] = "https://plus.google.com",
["LinkedInPage1"] = "https://www.linkedin.com"
};
// Create placeholder regex, the pattern for texts between square brackets.
Regex placeholderRegex = new Regex(@"\[(.*?)\]", RegexOptions.Compiled);
// Load template document.
DocumentModel document = DocumentModel.Load("Template.docx");
// Search for placeholders in the document.
foreach (ContentRange placeholder in document.Content.Find(placeholderRegex).Reverse())
{
string name = placeholder.ToString().Trim('[', ']');
string link;
// Replace placeholder with Hyperlink element.
if (linkData.TryGetValue(name, out link))
placeholder.Set(new Hyperlink(document, link, name).Content);
}
// Save document.
document.Save("Output.docx");
은 다음
은 결과 "Output.docx"파일입니다
위의 코드는 GemBox.Document을 사용하여 DOCX 파일을 조작하는데, Free and Professional versions입니다.
이것은 비영리 단체에서 일하는 것을 제외하고는 훌륭하게 작동합니다. 따라서 현재 라이선스를 구매할 여력이 없습니다. 고맙습니다.하지만 무료 옵션을 계속 찾아야합니다. – JW12689