feat(附件签章): 修复附件标识与签章定位
build.yaml / build (push) Successful in 48s

This commit is contained in:
xiaoqidun committed 2026-10-10 17:03:06 +08:00
1 parent 07eb44c037
commit 341ad7cd7e
4 files changed
+72 -27

No files matched your search

+24 -2
View File
@@ -52,7 +52,15 @@ func (e *Editor) AddAttachment(name string, data []byte) (string, error) {
return "", err return "", err
} }
} }
id := strconv.Itoa(maximum + 1) attachments, err := base.reader.Attachments()
if err != nil {
return "", err
}
used := make(map[string]bool, len(attachments))
for _, attachment := range attachments {
used[attachment.ID] = true
}
id, maximum := nextAttachmentID(used, maximum)
file := e.packageName("Attachments/Attachment_" + id) file := e.packageName("Attachments/Attachment_" + id)
attrs := ofdAttrs{} attrs := ofdAttrs{}
attrs.add("ID", id) attrs.add("ID", id)
@@ -121,10 +129,24 @@ func (e *Editor) AddAttachment(name string, data []byte) (string, error) {
if err := e.commitAnnotationParts(base, parts); err != nil { if err := e.commitAnnotationParts(base, parts); err != nil {
return "", err return "", err
} }
e.maxID, e.source.idsReady = maximum+1, true e.maxID, e.source.idsReady = maximum, true
return id, nil return id, nil
} }
// nextAttachmentID 分配以字母开头且不与已有附件重复的标识
// 入参: used 已用附件标识, maximum 当前编号上限
// 返回: string 附件标识, int 更新后的编号上限
func nextAttachmentID(used map[string]bool, maximum int) (string, int) {
for {
maximum++
id := "a" + strconv.Itoa(maximum)
if !used[id] {
used[id] = true
return id, maximum
}
}
}
// RenameAttachment 修改附件显示名称,保留格式、内容及扩展字段,相同名称不产生撤销记录 // RenameAttachment 修改附件显示名称,保留格式、内容及扩展字段,相同名称不产生撤销记录
// 入参: id 附件标识, name 显示名称 // 入参: id 附件标识, name 显示名称
// 返回: error 名称或附件结构错误 // 返回: error 名称或附件结构错误
+38 -21
View File
@@ -55,6 +55,7 @@ type editorPageImport struct {
templateParts map[string][]byte templateParts map[string][]byte
defaultCS string defaultCS string
attachments map[string][]byte attachments map[string][]byte
attachmentIDs map[string]bool
copyPage bool copyPage bool
strictLinks bool strictLinks bool
progress editorProgress progress editorProgress
@@ -957,10 +958,14 @@ func (m *editorPageImport) encode(entry editorImportEntry, node *editorXML) ([]b
var err error var err error
switch key { switch key {
case "ID": case "ID":
if name == "Signature" || name == "StampAnnot" { if name == "Attachment" {
value, err = m.attachmentID(value)
} else if name == "Signature" || name == "StampAnnot" {
value = name + ":" + value value = name + ":" + value
value = m.id(value)
} else {
value = m.id(value)
} }
value = m.id(value)
case "PageID": case "PageID":
if !m.copyPage || m.pages[editorImportKey(value)] { if !m.copyPage || m.pages[editorImportKey(value)] {
value, err = m.reference(value) value, err = m.reference(value)
@@ -1173,25 +1178,11 @@ func (m *editorPageImport) annotations() ([]byte, error) {
// 入参: id 来源附件标识 // 入参: id 来源附件标识
// 返回: string 新标识, error 错误信息 // 返回: string 新标识, error 错误信息
func (m *editorPageImport) attachment(id string) (string, error) { func (m *editorPageImport) attachment(id string) (string, error) {
key := editorImportKey(id)
if m.copyPage { if m.copyPage {
if mapped := m.ids[key]; mapped != "" {
return mapped, nil
}
attachments, err := m.reader.Attachments()
if err != nil {
return "", err
}
for _, attachment := range attachments {
if editorImportKey(attachment.ID) == key {
m.ids[key] = attachment.ID
return attachment.ID, nil
}
}
return id, nil return id, nil
} }
if _, exists := m.attachments[key]; exists { if _, exists := m.attachments[id]; exists {
return m.id(id), nil return m.attachmentID(id)
} }
name := m.reader.ResPath(m.reader.OFD.DocBody[m.reader.documentIndex].DocRoot) name := m.reader.ResPath(m.reader.OFD.DocBody[m.reader.documentIndex].DocRoot)
if m.doc.Attachments.Path != "" { if m.doc.Attachments.Path != "" {
@@ -1210,15 +1201,41 @@ func (m *editorPageImport) attachment(id string) (string, error) {
} }
if root != nil { if root != nil {
for _, node := range root.children { for _, node := range root.children {
if node.name.Local == "Attachment" && editorImportKey(node.attr("ID")) == key { if node.name.Local == "Attachment" && node.attr("ID") == id {
m.attachments[key], err = m.encode(editorImportEntry{name: name, data: data}, node) m.attachments[id], err = m.encode(editorImportEntry{name: name, data: data}, node)
return m.id(id), err if err != nil {
return "", err
}
return m.attachmentID(id)
} }
} }
} }
return "", fmt.Errorf("attachment %q not found", id) return "", fmt.Errorf("attachment %q not found", id)
} }
// attachmentID 为来源附件分配独立的文本标识,避免与数值型资源标识混淆
// 入参: id 来源附件标识
// 返回: string 目标附件标识, error 附件索引错误
func (m *editorPageImport) attachmentID(id string) (string, error) {
key := "Attachment:" + id
if mapped := m.ids[key]; mapped != "" {
return mapped, nil
}
if m.attachmentIDs == nil {
attachments, err := m.target.Attachments()
if err != nil {
return "", err
}
m.attachmentIDs = make(map[string]bool, len(attachments))
for _, attachment := range attachments {
m.attachmentIDs[attachment.ID] = true
}
}
mapped, maximum := nextAttachmentID(m.attachmentIDs, m.maximum)
m.maximum, m.ids[key] = maximum, mapped
return mapped, nil
}
// signatures 复制所选页面的签章及原始验证凭据,过滤其他页面的签章位置 // signatures 复制所选页面的签章及原始验证凭据,过滤其他页面的签章位置
// 返回: []byte 签名索引条目, error 错误信息 // 返回: []byte 签名索引条目, error 错误信息
func (m *editorPageImport) signatures() ([]byte, error) { func (m *editorPageImport) signatures() ([]byte, error) {
+7 -2
View File
@@ -126,7 +126,7 @@ func (r *Reader) SignatureStampPositions(stamps []SignatureStamp) ([]SignatureSt
pages := signaturePageNumbers(doc) pages := signaturePageNumbers(doc)
positions := make([]SignatureStampPosition, 0, len(stamps)) positions := make([]SignatureStampPosition, 0, len(stamps))
for _, stamp := range stamps { for _, stamp := range stamps {
page, ok := pages[stamp.PageRef] page, ok := resourceValue(pages, stamp.PageRef)
if !ok { if !ok {
return nil, fmt.Errorf("signature stamp page not found: %s", stamp.PageRef) return nil, fmt.Errorf("signature stamp page not found: %s", stamp.PageRef)
} }
@@ -183,6 +183,7 @@ func (r *Reader) parseSignatures(doc *Document) error {
if err := xml.NewDecoder(f).Decode(&signatures); err != nil { if err := xml.NewDecoder(f).Decode(&signatures); err != nil {
return err return err
} }
pages := signaturePageNumbers(doc)
for _, sigRef := range signatures.List { for _, sigRef := range signatures.List {
func(sigRef Signature) { func(sigRef Signature) {
sigPath := resolveResourcePath(sigListPath, "", sigRef.BaseLoc) sigPath := resolveResourcePath(sigListPath, "", sigRef.BaseLoc)
@@ -217,7 +218,11 @@ func (r *Reader) parseSignatures(doc *Document) error {
return return
} }
for _, annot := range sigFile.SignedInfo.StampAnnot { for _, annot := range sigFile.SignedInfo.StampAnnot {
pageID := annot.PageRef page, ok := resourceValue(pages, annot.PageRef)
if !ok {
continue
}
pageID := doc.Pages.Page[page-1].ID
bbox, err := parseSignatureStampBox(annot.Boundary) bbox, err := parseSignatureStampBox(annot.Boundary)
if err != nil { if err != nil {
continue continue
+3 -2
View File
@@ -98,10 +98,11 @@ func signatureWriteStamps(r *Reader, stamps []SignatureStamp, seal bool) ([]Sign
if len(result) != 0 { if len(result) != 0 {
pages := make(map[string]bool) pages := make(map[string]bool)
for _, page := range doc.Pages.Page { for _, page := range doc.Pages.Page {
if page.ID == "" || pages[page.ID] { id := editorResourceID(page.ID)
if id == "" || pages[id] {
return nil, fmt.Errorf("ambiguous stamp page ID: %s", page.ID) return nil, fmt.Errorf("ambiguous stamp page ID: %s", page.ID)
} }
pages[page.ID] = true pages[id] = true
} }
} }
ids := make(map[string]bool) ids := make(map[string]bool)