You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过OpenXML设置Word文档的CoreFileProperties并写入Tag字段

解决方案:通过OpenXML关联数据库ID到Word/PDF,追踪邮件发送记录

1. 给Word文档添加数据库ID标记(同步到PDF Keywords)

在填充Word书签数据完成后,通过OpenXML SDK设置文档的核心属性Keywords为数据库ID(Word的该属性会自动同步为PDF的Keywords字段):

// 假设已创建/打开WordprocessingDocument实例doc
CoreFilePropertiesPart coreProps = doc.CoreFilePropertiesPart;
// 若核心属性部分不存在则创建
if (coreProps == null)
{
    coreProps = doc.AddCoreFilePropertiesPart();
    coreProps.Properties = new Properties();
}

// 将数据库ID写入Keywords,建议加前缀避免歧义
string dbIdTag = $"OrderID:{yourDatabaseId}";
coreProps.Properties.Keywords = new Keywords(dbIdTag);
coreProps.Properties.Save();

// 保存Word文档
doc.Save();

2. 生成PDF并确保属性同步

无论你使用Microsoft.Office.Interop.Word的SaveAs方法还是其他OpenXML转换工具,只要Word文档的Keywords属性已设置,生成的PDF会自动继承该值作为自身的Keywords属性。

3. 从PDF提取ID关联数据库记录

邮件发送完成后,读取PDF的Keywords字段解析出数据库ID,即可关联对应记录并更新邮件发送时间:

示例(使用PdfSharp库)

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

// 读取目标PDF
using (PdfDocument pdfDoc = PdfReader.Open("generated.pdf", PdfDocumentOpenMode.ReadOnly))
{
    string pdfKeywords = pdfDoc.Info.Keywords;
    if (!string.IsNullOrWhiteSpace(pdfKeywords) && pdfKeywords.StartsWith("OrderID:"))
    {
        // 解析数据库ID
        if (int.TryParse(pdfKeywords.Substring("OrderID:".Length), out int targetId))
        {
            // 执行数据库操作:更新targetId对应的邮件发送时间
            UpdateEmailSendTime(targetId, DateTime.Now);
        }
    }
}

关键注意事项

  • 给ID添加前缀(如OrderID:):避免与其他可能的Keywords内容冲突,确保解析准确
  • 异常处理:需处理数据库ID为空、PDF Keywords读取失败、ID解析失败等边界情况
  • 转换工具验证:若使用第三方PDF转换工具,需确认其支持传递Word核心属性到PDF的Keywords字段

内容的提问来源于stack exchange,提问作者user3121062

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 15:27:12