diff --git a/ofdgo_editor_attachment.go b/ofdgo_editor_attachment.go index a73cd82..0273c96 100644 --- a/ofdgo_editor_attachment.go +++ b/ofdgo_editor_attachment.go @@ -52,7 +52,15 @@ func (e *Editor) AddAttachment(name string, data []byte) (string, error) { 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) attrs := ofdAttrs{} 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 { return "", err } - e.maxID, e.source.idsReady = maximum+1, true + e.maxID, e.source.idsReady = maximum, true 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 修改附件显示名称,保留格式、内容及扩展字段,相同名称不产生撤销记录 // 入参: id 附件标识, name 显示名称 // 返回: error 名称或附件结构错误 diff --git a/ofdgo_editor_import.go b/ofdgo_editor_import.go index 0a96273..37a591e 100644 --- a/ofdgo_editor_import.go +++ b/ofdgo_editor_import.go @@ -55,6 +55,7 @@ type editorPageImport struct { templateParts map[string][]byte defaultCS string attachments map[string][]byte + attachmentIDs map[string]bool copyPage bool strictLinks bool progress editorProgress @@ -957,10 +958,14 @@ func (m *editorPageImport) encode(entry editorImportEntry, node *editorXML) ([]b var err error switch key { 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 = m.id(value) + } else { + value = m.id(value) } - value = m.id(value) case "PageID": if !m.copyPage || m.pages[editorImportKey(value)] { value, err = m.reference(value) @@ -1173,25 +1178,11 @@ func (m *editorPageImport) annotations() ([]byte, error) { // 入参: id 来源附件标识 // 返回: string 新标识, error 错误信息 func (m *editorPageImport) attachment(id string) (string, error) { - key := editorImportKey(id) 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 } - if _, exists := m.attachments[key]; exists { - return m.id(id), nil + if _, exists := m.attachments[id]; exists { + return m.attachmentID(id) } name := m.reader.ResPath(m.reader.OFD.DocBody[m.reader.documentIndex].DocRoot) if m.doc.Attachments.Path != "" { @@ -1210,15 +1201,41 @@ func (m *editorPageImport) attachment(id string) (string, error) { } if root != nil { for _, node := range root.children { - if node.name.Local == "Attachment" && editorImportKey(node.attr("ID")) == key { - m.attachments[key], err = m.encode(editorImportEntry{name: name, data: data}, node) - return m.id(id), err + if node.name.Local == "Attachment" && node.attr("ID") == id { + m.attachments[id], err = m.encode(editorImportEntry{name: name, data: data}, node) + if err != nil { + return "", err + } + return m.attachmentID(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 复制所选页面的签章及原始验证凭据,过滤其他页面的签章位置 // 返回: []byte 签名索引条目, error 错误信息 func (m *editorPageImport) signatures() ([]byte, error) { diff --git a/ofdgo_sign.go b/ofdgo_sign.go index 77ce315..a61565d 100644 --- a/ofdgo_sign.go +++ b/ofdgo_sign.go @@ -126,7 +126,7 @@ func (r *Reader) SignatureStampPositions(stamps []SignatureStamp) ([]SignatureSt pages := signaturePageNumbers(doc) positions := make([]SignatureStampPosition, 0, len(stamps)) for _, stamp := range stamps { - page, ok := pages[stamp.PageRef] + page, ok := resourceValue(pages, stamp.PageRef) if !ok { 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 { return err } + pages := signaturePageNumbers(doc) for _, sigRef := range signatures.List { func(sigRef Signature) { sigPath := resolveResourcePath(sigListPath, "", sigRef.BaseLoc) @@ -217,7 +218,11 @@ func (r *Reader) parseSignatures(doc *Document) error { return } 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) if err != nil { continue diff --git a/ofdgo_sign_write_stamp.go b/ofdgo_sign_write_stamp.go index bc99579..e551f6f 100644 --- a/ofdgo_sign_write_stamp.go +++ b/ofdgo_sign_write_stamp.go @@ -98,10 +98,11 @@ func signatureWriteStamps(r *Reader, stamps []SignatureStamp, seal bool) ([]Sign if len(result) != 0 { pages := make(map[string]bool) 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) } - pages[page.ID] = true + pages[id] = true } } ids := make(map[string]bool)