Skip to content

OFD 文档附件 ​

本节介绍 OFD 文档附件的枚举、读写、权限控制、电子凭证识别与附件目录管理。附件通过 OFDDoc 上的 Attachments 集合或文档级快捷接口访问;单个附件由 Attachment 表示,其 GetID() 可供大纲 SetAttachmentID 或 GotoAAction::SetAttachmentID 关联,以实现点击打开嵌入附件(见 注释与动作、书签、大纲与目录)。取得 OFDDoc 请参阅 OFD 文档与页面。

能力概览 ​

能力说明
附件集合无集合时须先 CreateAttachments
集合操作GetAttachmentCount、AddAttachment、RemoveAttachment、 RemoveAllAttachments、GetAttachmentByID
附件实体SetFile / GetFile;GetID、GetAttachmentInfo
元数据name、format、usage、creation_date、modification_date
阅读器权限Visible、Deletable、Editable、Exportable
文档级快捷按索引/名称访问或一步添加文件附件
电子凭证GetElectronicBillsCount、GetElectronicBillsType、 ExportElectronicBillsFile
附件目录CreateSubDirectory、AddAttachment(attach_id)、 GetAttachment、RemoveAttachmentByID
目录扩展信息XML 格式的目录扩展描述
持久化附件变更后须包层保存

类与方法细节见接口文档。

获取附件并读取元数据 ​

文档若尚无附件集合,GetAttachments() 可能为空,需调用 CreateAttachments()。遍历时可读取 AttachmentInfo、GetID()(供 GotoA / 大纲关联)及权限状态。

以下示例演示枚举附件并读取元数据。

c
#include "fs_ofddoc_c.h"
#include "fs_ofdattachment_c.h"

void ListAttachments(FS_OFDDOC_HANDLE doc) {
  FS_OFDATTACHMENTS_HANDLE attachments = NULL;
  FS_BOOL empty = FALSE;
  int count = 0;
  int i = 0;

  FSDK_OFDDoc_OFDIsEmpty(doc, &empty);
  if (empty) {
    return;
  }

  FSDK_OFDDoc_OFDGetAttachments(doc, &attachments);
  FSDK_OFDAttachments_IsEmpty(attachments, &empty);
  if (empty) {
    FSDK_OFDDoc_OFDCreateAttachments(doc, &attachments);
  }
  FSDK_OFDAttachments_IsEmpty(attachments, &empty);
  if (empty) {
    return;
  }

  FSDK_OFDAttachments_GetAttachmentCount(attachments, &count);
  for (i = 0; i < count; ++i) {
    FS_OFDATTACHMENT_HANDLE att = NULL;
    FSOFDAttachmentInfo info;
    FS_UINT32 id = 0;
    FS_BOOL visible = FALSE;
    FS_BOOL exportable = FALSE;

    FSDK_OFDAttachments_GetAttachment(attachments, i, &att);
    if (!att) {
      continue;
    }

    memset(&info, 0, sizeof(info));
    FSDK_OFDAttachment_GetAttachmentInfo(att, &info);
    FSDK_OFDAttachment_GetID(att, &id);
    FSDK_OFDAttachment_IsVisible(att, &visible);
    FSDK_OFDAttachment_IsExportable(att, &exportable);
  }
}
c++
#include "ofd/fs_ofdpackage.h"
#include "ofd/fs_ofddoc.h"
#include "ofd/fs_ofdattachment.h"

using namespace foxit;
using namespace foxit::ofd;

void ListAttachments(OFDDoc& doc) {
  if (doc.IsEmpty()) {
    return;
  }

  Attachments attachments = doc.GetAttachments();
  if (attachments.IsEmpty()) {
    attachments = doc.CreateAttachments();
  }
  if (attachments.IsEmpty()) {
    return;
  }

  int count = attachments.GetAttachmentCount();
  for (int i = 0; i < count; ++i) {
    Attachment att = attachments.GetAttachment(i);
    if (att.IsEmpty()) {
      continue;
    }

    // 读取元数据与权限
    AttachmentInfo info = att.GetAttachmentInfo();
    uint32 id = att.GetID();
    bool visible = att.IsVisible();
    bool exportable = att.IsExportable();
    (void)info;
    (void)id;
    (void)visible;
    (void)exportable;
  }

  // 文档级快捷:按名称查找
  Attachment by_name = doc.GetAttachmentByName("output.ofd");
  (void)by_name;
}
java
import com.foxit.sdk.ofd.Attachment;
import com.foxit.sdk.ofd.AttachmentInfo;
import com.foxit.sdk.ofd.Attachments;
import com.foxit.sdk.ofd.OFDDoc;

public class OFDListAttachmentsExample {
    public static void listAttachments(OFDDoc doc) throws Exception {
        if (doc.isEmpty()) {
            return;
        }

        Attachments attachments = doc.getAttachments();
        if (attachments.isEmpty()) {
            attachments = doc.createAttachments();
        }
        if (attachments.isEmpty()) {
            return;
        }

        int count = attachments.getAttachmentCount();
        for (int i = 0; i < count; i++) {
            Attachment att = attachments.getAttachment(i);
            if (att.isEmpty()) {
                continue;
            }

            // 读取元数据与权限
            AttachmentInfo info = att.getAttachmentInfo();
            int id = att.getID();
            boolean visible = att.isVisible();
            boolean exportable = att.isExportable();
        }

        // 文档级快捷:按名称查找
        Attachment byName = doc.getAttachmentByName("output.ofd");
    }
}
csharp
using foxit.ofd;

void ListAttachments(OFDDoc doc)
{
    if (doc.IsEmpty())
    {
        return;
    }

    Attachments attachments = doc.GetAttachments();
    if (attachments.IsEmpty())
    {
        attachments = doc.CreateAttachments();
    }
    if (attachments.IsEmpty())
    {
        return;
    }

    int count = attachments.GetAttachmentCount();
    for (int i = 0; i < count; ++i)
    {
        using (Attachment att = attachments.GetAttachment(i))
        {
            if (att.IsEmpty())
            {
                continue;
            }

            AttachmentInfo info = att.GetAttachmentInfo();
            uint id = att.GetID();
            bool visible = att.IsVisible();
            bool exportable = att.IsExportable();
        }
    }
}
python
from FoxitPDFSDKPython3 import *

def list_attachments(doc):
    if doc.IsEmpty():
        return

    attachments = doc.GetAttachments()
    if attachments.IsEmpty():
        attachments = doc.CreateAttachments()
    if attachments.IsEmpty():
        return

    for i in range(attachments.GetAttachmentCount()):
        att = attachments.GetAttachment(i)
        if att.IsEmpty():
            continue
        info = att.GetAttachmentInfo()
        attachment_id = att.GetID()  # 可供 GotoA / Outline.SetAttachmentID 使用
        visible = att.IsVisible()
        exportable = att.IsExportable()
go
package main

import "foxit.com/fsdk"

func ListAttachments(doc fsdk.OFDDoc) {
	if doc.IsEmpty() {
		return
	}

	attachments := doc.GetAttachments()
	if attachments.IsEmpty() {
		attachments = doc.CreateAttachments()
	}
	if attachments.IsEmpty() {
		return
	}

	count := attachments.GetAttachmentCount()
	for i := 0; i < count; i++ {
		att := attachments.GetAttachment(i)
		if att.IsEmpty() {
			continue
		}
		info := att.GetAttachmentInfo()
		_ = att.GetID()
		_ = att.IsVisible()
		_ = att.IsExportable()
		fsdk.DeleteOFDAttachmentInfo(info)
	}
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");

function listAttachments(doc) {
  if (doc.IsEmpty()) {
    return;
  }

  let attachments = doc.GetAttachments();
  if (attachments.IsEmpty()) {
    attachments = doc.CreateAttachments();
  }
  if (attachments.IsEmpty()) {
    return;
  }

  const count = attachments.GetAttachmentCount();
  for (let i = 0; i < count; i++) {
    const att = attachments.GetAttachment(i);
    if (att.IsEmpty()) {
      continue;
    }
    const info = att.GetAttachmentInfo();
    const id = att.GetID();
    const visible = att.IsVisible();
    const exportable = att.IsExportable();
  }
}

添加附件并设置属性 ​

典型流程:AddAttachment() 取得空附件 → 设置 AttachmentInfo 或各字段 → SetFile 写入内容 → 可选配置权限。

c
#include "fs_ofddoc_c.h"
#include "fs_ofdattachment_c.h"

FS_BOOL AddAttachmentFromFile(FS_OFDDOC_HANDLE doc, const wchar_t* file_path) {
  FS_OFDATTACHMENTS_HANDLE attachments = NULL;
  FS_OFDATTACHMENT_HANDLE att = NULL;
  FS_BOOL empty = FALSE;
  FS_BOOL result = FALSE;
  FSOFDDateTime creation_date;

  FSDK_OFDDoc_OFDIsEmpty(doc, &empty);
  if (empty) {
    return FALSE;
  }

  FSDK_OFDDoc_OFDGetAttachments(doc, &attachments);
  FSDK_OFDAttachments_IsEmpty(attachments, &empty);
  if (empty) {
    FSDK_OFDDoc_OFDCreateAttachments(doc, &attachments);
  }
  FSDK_OFDAttachments_IsEmpty(attachments, &empty);
  if (empty) {
    return FALSE;
  }

  FSDK_OFDAttachments_AddAttachment(attachments, &att);
  if (!att) {
    return FALSE;
  }

  memset(&creation_date, 0, sizeof(creation_date));
  creation_date.year = 2026;
  creation_date.month = 4;
  creation_date.day = 17;
  creation_date.hour = 10;

  FSDK_OFDAttachment_SetName(att, "sample.pdf");
  FSDK_OFDAttachment_SetFormat(att, "pdf");
  FSDK_OFDAttachment_SetUsage(att, "attachment");
  FSDK_OFDAttachment_SetCreationDate(att, creation_date);
  FSDK_OFDAttachment_SetModDate(att, creation_date);
  FSDK_OFDAttachment_SetModifier(att, "Foxit SDK");
  FSDK_OFDAttachment_SetDescription(att, "Sample attachment");
  FSDK_OFDAttachment_EnableVisible(att, TRUE);
  FSDK_OFDAttachment_EnableDeletable(att, TRUE);
  FSDK_OFDAttachment_EnableEditable(att, TRUE);
  FSDK_OFDAttachment_EnableExportable(att, TRUE);

  FSDK_OFDAttachment_SetFile0(att, file_path, "sample.pdf", &result);
  return result;
}
c++
#include "ofd/fs_ofdattachment.h"

using namespace foxit;
using namespace foxit::ofd;

bool AddAttachmentFromFile(OFDDoc& doc, const char* file_path) {
  if (doc.IsEmpty()) {
    return false;
  }

  Attachments attachments = doc.GetAttachments();
  if (attachments.IsEmpty()) {
    attachments = doc.CreateAttachments();
  }
  if (attachments.IsEmpty()) {
    return false;
  }

  Attachment att = attachments.AddAttachment();
  if (att.IsEmpty()) {
    return false;
  }

  DateTime creation_date = {};
  creation_date.year = 2026;
  creation_date.month = 4;
  creation_date.day = 17;
  creation_date.hour = 10;
  creation_date.minute = 0;
  creation_date.second = 0;
  creation_date.milliseconds = 0;
  creation_date.utc_hour_offset = 0;
  creation_date.utc_minute_offset = 0;
  DateTime mod_date = creation_date;
  
  AttachmentInfo info("sample.pdf", "pdf", "attachment", creation_date, mod_date);
  att.SetAttachmentInfo(info);
  att.SetModifier("Foxit SDK");
  att.SetDescription("Sample attachment");

  // 阅读器侧权限
  att.EnableVisible(true);
  att.EnableDeletable(true);
  att.EnableEditable(true);
  att.EnableExportable(true);

  // 从本地文件写入附件内容
  return att.SetFile(file_path, "sample.pdf");
}
java
import com.foxit.sdk.common.DateTime;
import com.foxit.sdk.ofd.Attachment;
import com.foxit.sdk.ofd.Attachments;
import com.foxit.sdk.ofd.OFDDoc;

public class OFDAddAttachmentExample {
    public static boolean addAttachmentFromFile(OFDDoc doc, String filePath)
            throws Exception {
        if (doc.isEmpty()) {
            return false;
        }

        Attachments attachments = doc.getAttachments();
        if (attachments.isEmpty()) {
            attachments = doc.createAttachments();
        }
        if (attachments.isEmpty()) {
            return false;
        }

        Attachment att = attachments.addAttachment();
        if (att.isEmpty()) {
            return false;
        }

        DateTime creationDate = new DateTime(2026, 4, 17, 10, 0, 0, 0, (short) 0, 0);
        DateTime modDate = new DateTime(creationDate);

        att.setName("sample.pdf");
        att.setFormat("pdf");
        att.setUsage("attachment");
        att.setCreationDate(creationDate);
        att.setModDate(modDate);
        att.setModifier("Foxit SDK");
        att.setDescription("Sample attachment");

        // 阅读器侧权限
        att.enableVisible(true);
        att.enableDeletable(true);
        att.enableEditable(true);
        att.enableExportable(true);

        // 从本地文件写入附件内容
        return att.setFile(filePath, "sample.pdf");
    }
}
csharp
using foxit.common;
using foxit.ofd;

bool AddAttachmentFromFile(OFDDoc doc, string filePath)
{
    if (doc.IsEmpty())
    {
        return false;
    }

    Attachments attachments = doc.GetAttachments();
    if (attachments.IsEmpty())
    {
        attachments = doc.CreateAttachments();
    }
    if (attachments.IsEmpty())
    {
        return false;
    }

    using (Attachment att = attachments.AddAttachment())
    {
        if (att.IsEmpty())
        {
            return false;
        }

        DateTime creationDate = new DateTime();
        creationDate.year = 2026;
        creationDate.month = 4;
        creationDate.day = 17;
        creationDate.hour = 10;

        att.SetName("sample.pdf");
        att.SetFormat("pdf");
        att.SetUsage("attachment");
        att.SetCreationDate(creationDate);
        att.SetModDate(creationDate);
        att.SetModifier("Foxit SDK");
        att.SetDescription("Sample attachment");
        att.EnableVisible(true);
        att.EnableDeletable(true);
        att.EnableEditable(true);
        att.EnableExportable(true);

        return att.SetFile(filePath, "sample.pdf");
    }
}
python
from FoxitPDFSDKPython3 import *

def add_attachment_from_file(doc, file_path):
    if doc.IsEmpty():
        return False

    attachments = doc.GetAttachments()
    if attachments.IsEmpty():
        attachments = doc.CreateAttachments()
    if attachments.IsEmpty():
        return False

    att = attachments.AddAttachment()
    if att.IsEmpty():
        return False

    att.SetName("sample.pdf")
    att.SetFormat("pdf")
    att.SetUsage("attachment")
    att.SetModifier("Foxit SDK")
    att.SetDescription("Sample attachment")
    att.EnableVisible(True)
    att.EnableDeletable(True)
    att.EnableEditable(True)
    att.EnableExportable(True)
    return att.SetFile(file_path, "sample.pdf")
go
package main

import "foxit.com/fsdk"

func AddAttachmentFromFile(doc fsdk.OFDDoc, filePath string) bool {
	if doc.IsEmpty() {
		return false
	}

	attachments := doc.GetAttachments()
	if attachments.IsEmpty() {
		attachments = doc.CreateAttachments()
	}
	if attachments.IsEmpty() {
		return false
	}

	att := attachments.AddAttachment()
	if att.IsEmpty() {
		return false
	}

	creationDate := fsdk.NewDateTime(2026, 4, 17, 10, 0, 0, 0, 0, 0)
	modDate := fsdk.NewDateTime(2026, 4, 17, 10, 0, 0, 0, 0, 0)
	defer fsdk.DeleteDateTime(creationDate)
	defer fsdk.DeleteDateTime(modDate)

	att.SetName("sample.pdf")
	att.SetFormat("pdf")
	att.SetUsage("attachment")
	att.SetCreationDate(creationDate)
	att.SetModDate(modDate)
	att.SetModifier("Foxit SDK")
	att.SetDescription("Sample attachment")
	att.EnableVisible(true)
	att.EnableDeletable(true)
	att.EnableEditable(true)
	att.EnableExportable(true)
	return att.SetFile(filePath, "sample.pdf")
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");

function addAttachmentFromFile(doc, filePath) {
  if (doc.IsEmpty()) {
    return false;
  }

  let attachments = doc.GetAttachments();
  if (attachments.IsEmpty()) {
    attachments = doc.CreateAttachments();
  }
  if (attachments.IsEmpty()) {
    return false;
  }

  const att = attachments.AddAttachment();
  if (att.IsEmpty()) {
    return false;
  }

  const creationDate = new FSDK.DateTime(2026, 4, 17, 10, 0, 0, 0, 0, 0);
  const modDate = new FSDK.DateTime(2026, 4, 17, 10, 0, 0, 0, 0, 0);

  att.SetName("sample.pdf");
  att.SetFormat("pdf");
  att.SetUsage("attachment");
  att.SetCreationDate(creationDate);
  att.SetModDate(modDate);
  att.SetModifier("Foxit SDK");
  att.SetDescription("Sample attachment");
  att.EnableVisible(true);
  att.EnableDeletable(true);
  att.EnableEditable(true);
  att.EnableExportable(true);
  return att.SetFile(filePath, "sample.pdf");
}

导出与删除附件 ​

导出可使用 Attachment::GetFile(路径或 WriterCallback),或 OFDDoc::ExportAttachment 按索引导出。删除使用 Attachments::RemoveAttachment 或 RemoveAllAttachments。

c
#include "fs_ofddoc_c.h"
#include "fs_ofdattachment_c.h"

FS_BOOL ExportAndRemoveFirstAttachment(FS_OFDDOC_HANDLE doc, const wchar_t* export_path) {
  FS_OFDATTACHMENTS_HANDLE attachments = NULL;
  FS_OFDATTACHMENT_HANDLE att = NULL;
  FS_BOOL empty = FALSE;
  FS_BOOL result = FALSE;
  int count = 0;

  FSDK_OFDDoc_OFDIsEmpty(doc, &empty);
  if (empty) {
    return FALSE;
  }

  FSDK_OFDDoc_OFDGetAttachments(doc, &attachments);
  FSDK_OFDAttachments_IsEmpty(attachments, &empty);
  if (empty) {
    return FALSE;
  }
  FSDK_OFDAttachments_GetAttachmentCount(attachments, &count);
  if (count == 0) {
    return FALSE;
  }

  FSDK_OFDAttachments_GetAttachment(attachments, 0, &att);
  FSDK_OFDAttachment_GetFile0(att, export_path, &result);
  if (!result) {
    return FALSE;
  }

  FSDK_OFDAttachments_RemoveAttachment(attachments, 0, &result);
  return result;
}
c++
#include "ofd/fs_ofddoc.h"
#include "ofd/fs_ofdattachment.h"

using namespace foxit;
using namespace foxit::ofd;

bool ExportAndRemoveFirstAttachment(OFDDoc& doc, const wchar_t* export_path) {
  if (doc.IsEmpty()) {
    return false;
  }

  Attachments attachments = doc.GetAttachments();
  if (attachments.IsEmpty() || attachments.GetAttachmentCount() == 0) {
    return false;
  }

  // 方式一:Attachment 对象导出
  Attachment att = attachments.GetAttachment(0);
  if (!att.GetFile(WString(export_path))) {
    return false;
  }

  // 方式二:文档级按索引导出(等价快捷接口)
  // doc.ExportAttachment(0, export_path);

  return attachments.RemoveAttachment(0);
}
java
import com.foxit.sdk.ofd.Attachment;
import com.foxit.sdk.ofd.Attachments;
import com.foxit.sdk.ofd.OFDDoc;

public class OFDExportAttachmentExample {
    public static boolean exportAndRemoveFirstAttachment(OFDDoc doc,
            String exportPath) throws Exception {
        if (doc.isEmpty()) {
            return false;
        }

        Attachments attachments = doc.getAttachments();
        if (attachments.isEmpty() || attachments.getAttachmentCount() == 0) {
            return false;
        }

        // 方式一:Attachment 对象导出
        Attachment att = attachments.getAttachment(0);
        if (!att.getFile(exportPath)) {
            return false;
        }

        // 方式二:文档级按索引导出
        // doc.exportAttachment(0, exportPath);

        return attachments.removeAttachment(0);
    }
}
csharp
using foxit.ofd;

bool ExportAndRemoveFirstAttachment(OFDDoc doc, string exportPath)
{
    if (doc.IsEmpty())
    {
        return false;
    }

    Attachments attachments = doc.GetAttachments();
    if (attachments.IsEmpty() || attachments.GetAttachmentCount() == 0)
    {
        return false;
    }

    using (Attachment att = attachments.GetAttachment(0))
    {
        if (!att.GetFile(exportPath))
        {
            return false;
        }
    }

    return attachments.RemoveAttachment(0);
}
python
from FoxitPDFSDKPython3 import *

def export_and_remove_first_attachment(doc, export_path):
    if doc.IsEmpty():
        return False

    attachments = doc.GetAttachments()
    if attachments.IsEmpty() or attachments.GetAttachmentCount() == 0:
        return False

    att = attachments.GetAttachment(0)
    if not att.GetFile(export_path):
        return False
    return attachments.RemoveAttachment(0)
go
package main

import "foxit.com/fsdk"

func ExportAndRemoveFirstAttachment(doc fsdk.OFDDoc, exportPath string) bool {
	if doc.IsEmpty() {
		return false
	}

	attachments := doc.GetAttachments()
	if attachments.IsEmpty() || attachments.GetAttachmentCount() == 0 {
		return false
	}

	att := attachments.GetAttachment(0)
	if !att.GetFile(exportPath) {
		return false
	}
	return attachments.RemoveAttachment(0)
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");

function exportAndRemoveFirstAttachment(doc, exportPath) {
  if (doc.IsEmpty()) {
    return false;
  }

  const attachments = doc.GetAttachments();
  if (attachments.IsEmpty() || attachments.GetAttachmentCount() === 0) {
    return false;
  }

  const att = attachments.GetAttachment(0);
  if (!att.GetFile(exportPath)) {
    return false;
  }
  return attachments.RemoveAttachment(0);
}

电子凭证 ​

OFD 可将增值税发票、银行回单等电子凭证作为特殊附件嵌入。通过 Attachments::GetElectronicBillsCount 获取数量,GetElectronicBillsType 识别类型,ExportElectronicBillsFile 导出对应文件。

常见 ElectronicBillsType 包括:e_ElectronicInvoiceOrdIssuer(增值税电子普通发票-开票方)、e_ElectronicBkerIssuer(银行电子回单-开具方)、e_ElectronicEinvOrd(数电票普通发票)等;完整枚举见接口文档。

c
#include "fs_ofddoc_c.h"
#include "fs_ofdattachment_c.h"

FS_BOOL ExportElectronicBills(FS_OFDDOC_HANDLE doc, const char* output_dir) {
  FS_OFDATTACHMENTS_HANDLE attachments = NULL;
  FS_BOOL empty = FALSE;
  int bills_count = 0;
  int i = 0;
  char out_path[260];

  FSDK_OFDDoc_OFDIsEmpty(doc, &empty);
  if (empty) {
    return FALSE;
  }

  FSDK_OFDDoc_OFDGetAttachments(doc, &attachments);
  FSDK_OFDAttachments_IsEmpty(attachments, &empty);
  if (empty) {
    return FALSE;
  }

  FSDK_OFDAttachments_GetElectronicBillsCount(attachments, &bills_count);
  for (i = 0; i < bills_count; ++i) {
    int type = 0;
    FS_BOOL result = FALSE;
    FSDK_OFDAttachments_GetElectronicBillsType(attachments, i, &type);
    {
      int written = snprintf(out_path, sizeof(out_path), "%sbill_%d.ofd", output_dir, i);
      if (written < 0 || written >= (int)sizeof(out_path)) {
        return FALSE;
      }
    }
    FSDK_OFDAttachments_ExportElectronicBillsFile(attachments, i, out_path, &result);
    if (!result) {
      return FALSE;
    }
  }
  return TRUE;
}
c++
#include "ofd/fs_ofdattachment.h"

using namespace foxit;
using namespace foxit::ofd;

bool ExportElectronicBills(OFDDoc& doc, const char* output_dir) {
  if (doc.IsEmpty()) {
    return false;
  }

  Attachments attachments = doc.GetAttachments();
  if (attachments.IsEmpty()) {
    return false;
  }

  int bills_count = attachments.GetElectronicBillsCount();
  for (int i = 0; i < bills_count; ++i) {
    // 先识别类型再处理
    Attachments::ElectronicBillsType type = attachments.GetElectronicBillsType(i);
    (void)type;

    String out_path;
    out_path.Format("%sbill_%d.ofd", output_dir, i);
    if (!attachments.ExportElectronicBillsFile(i, out_path)) {
      return false;
    }
  }
  return true;
}
java
import com.foxit.sdk.ofd.Attachments;
import com.foxit.sdk.ofd.OFDDoc;

public class OFDElectronicBillsExample {
    public static boolean exportElectronicBills(OFDDoc doc, String outputDir)
            throws Exception {
        if (doc.isEmpty()) {
            return false;
        }

        Attachments attachments = doc.getAttachments();
        if (attachments.isEmpty()) {
            return false;
        }

        int billsCount = attachments.getElectronicBillsCount();
        for (int i = 0; i < billsCount; i++) {
            // 先识别类型再处理
            int type = attachments.getElectronicBillsType(i);

            String outPath = outputDir + "bill_" + i + ".ofd";
            if (!attachments.exportElectronicBillsFile(i, outPath)) {
                return false;
            }
        }
        return true;
    }
}
csharp
using foxit.ofd;

bool ExportElectronicBills(OFDDoc doc, string outputDir)
{
    if (doc.IsEmpty())
    {
        return false;
    }

    Attachments attachments = doc.GetAttachments();
    if (attachments.IsEmpty())
    {
        return false;
    }

    int billsCount = attachments.GetElectronicBillsCount();
    for (int i = 0; i < billsCount; ++i)
    {
        Attachments.ElectronicBillsType type = attachments.GetElectronicBillsType(i);
        string outPath = outputDir + "bill_" + i + ".ofd";
        if (!attachments.ExportElectronicBillsFile(i, outPath))
        {
            return false;
        }
    }
    return true;
}
python
from FoxitPDFSDKPython3 import *

def export_electronic_bills(doc, output_dir):
    if doc.IsEmpty():
        return False

    attachments = doc.GetAttachments()
    if attachments.IsEmpty():
        return False

    for i in range(attachments.GetElectronicBillsCount()):
        bill_type = attachments.GetElectronicBillsType(i)
        out_path = "{}bill_{}.ofd".format(output_dir, i)
        if not attachments.ExportElectronicBillsFile(i, out_path):
            return False
    return True
go
package main

import (
	"fmt"
	"foxit.com/fsdk"
)

func ExportElectronicBills(doc fsdk.OFDDoc, outputDir string) bool {
	if doc.IsEmpty() {
		return false
	}

	attachments := doc.GetAttachments()
	if attachments.IsEmpty() {
		return false
	}

	billsCount := attachments.GetElectronicBillsCount()
	for i := 0; i < billsCount; i++ {
		_ = attachments.GetElectronicBillsType(i)
		outPath := fmt.Sprintf("%sbill_%d.ofd", outputDir, i)
		if !attachments.ExportElectronicBillsFile(i, outPath) {
			return false
		}
	}
	return true
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");

function exportElectronicBills(doc, outputDir) {
  if (doc.IsEmpty()) {
    return false;
  }

  const attachments = doc.GetAttachments();
  if (attachments.IsEmpty()) {
    return false;
  }

  const billsCount = attachments.GetElectronicBillsCount();
  for (let i = 0; i < billsCount; i++) {
    const type = attachments.GetElectronicBillsType(i);
    const outPath = outputDir + "bill_" + i + ".ofd";
    if (!attachments.ExportElectronicBillsFile(i, outPath)) {
      return false;
    }
  }
  return true;
}

附件目录 ​

对附件较多的文档,可通过 CreateAttachmentDirectory 创建根目录,用 CreateSubDirectory 建立子目录,再将已添加附件的 GetID() 通过 AddAttachment(attach_id) 挂入目录。目录中的附件须先在 Attachments 集合中创建。

c
#include "fs_ofddoc_c.h"
#include "fs_ofdattachment_c.h"
#include "fs_ofdattachmentdirectory_c.h"

FS_BOOL BuildAttachmentDirectory(FS_OFDDOC_HANDLE doc, const wchar_t* file_path) {
  FS_OFDATTACHMENTS_HANDLE attachments = NULL;
  FS_OFDATTACHMENT_HANDLE att = NULL;
  FS_OFDATTACHMENTDIRECTORY_HANDLE root = NULL;
  FS_OFDATTACHMENTDIRECTORY_HANDLE sub = NULL;
  FS_BOOL empty = FALSE;
  FS_BOOL result = FALSE;
  FS_UINT32 attach_id = 0;

  FSDK_OFDDoc_OFDIsEmpty(doc, &empty);
  if (empty) {
    return FALSE;
  }

  FSDK_OFDDoc_OFDGetAttachments(doc, &attachments);
  FSDK_OFDAttachments_IsEmpty(attachments, &empty);
  if (empty) {
    FSDK_OFDDoc_OFDCreateAttachments(doc, &attachments);
  }
  FSDK_OFDAttachments_IsEmpty(attachments, &empty);
  if (empty) {
    return FALSE;
  }

  FSDK_OFDAttachments_AddAttachment(attachments, &att);
  FSDK_OFDAttachment_SetFile0(att, file_path, NULL, &result);
  if (!result) {
    return FALSE;
  }
  FSDK_OFDAttachment_GetID(att, &attach_id);

  FSDK_OFDAttachments_CreateAttachmentDirectory(attachments, &root);
  FSDK_OFDAttachmentDirectory_SetName(root, "root");
  FSDK_OFDAttachmentDirectory_CreateSubDirectory(root, "凭证", &sub);
  FSDK_OFDAttachmentDirectory_AddAttachment(sub, attach_id, &result);
  return result;
}
c++
#include "ofd/fs_ofdattachment.h"
#include "ofd/fs_ofdattachmentdirectory.h"

using namespace foxit;
using namespace foxit::ofd;

bool BuildAttachmentDirectory(OFDDoc& doc, const char* file_path) {
  if (doc.IsEmpty()) {
    return false;
  }

  Attachments attachments = doc.GetAttachments();
  if (attachments.IsEmpty()) {
    attachments = doc.CreateAttachments();
  }
  // IsEmpty 表示集合对象无效(创建失败),不是“附件个数为 0”
  if (attachments.IsEmpty()) {
    return false;
  }

  Attachment att = attachments.AddAttachment();
  if (!att.SetFile(file_path)) {
    return false;
  }
  uint32 attach_id = att.GetID();

  // 创建目录树并挂载附件
  AttachmentDirectory root = attachments.CreateAttachmentDirectory();
  root.SetName("root");
  AttachmentDirectory sub = root.CreateSubDirectory("凭证");
  return sub.AddAttachment(attach_id);
}
java
import com.foxit.sdk.ofd.Attachment;
import com.foxit.sdk.ofd.AttachmentDirectory;
import com.foxit.sdk.ofd.Attachments;
import com.foxit.sdk.ofd.OFDDoc;

public class OFDAttachmentDirectoryExample {
    public static boolean buildAttachmentDirectory(OFDDoc doc, String filePath)
            throws Exception {
        if (doc.isEmpty()) {
            return false;
        }

        Attachments attachments = doc.getAttachments();
        if (attachments.isEmpty()) {
            attachments = doc.createAttachments();
        }
        // isEmpty 表示集合对象无效(创建失败),不是“附件个数为 0”
        if (attachments.isEmpty()) {
            return false;
        }

        Attachment att = attachments.addAttachment();
        if (!att.setFile(filePath)) {
            return false;
        }
        int attachId = att.getID();

        // 创建目录树并挂载附件
        AttachmentDirectory root = attachments.createAttachmentDirectory();
        root.setName("root");
        AttachmentDirectory sub = root.createSubDirectory("凭证");
        return sub.addAttachment(attachId);
    }
}
csharp
using foxit.ofd;

bool BuildAttachmentDirectory(OFDDoc doc, string filePath)
{
    if (doc.IsEmpty())
    {
        return false;
    }

    Attachments attachments = doc.GetAttachments();
    if (attachments.IsEmpty())
    {
        attachments = doc.CreateAttachments();
    }
    if (attachments.IsEmpty())
    {
        return false;
    }

    using (Attachment att = attachments.AddAttachment())
    {
        if (!att.SetFile(filePath))
        {
            return false;
        }
        uint attachId = att.GetID();

        using (AttachmentDirectory root = attachments.CreateAttachmentDirectory())
        {
            root.SetName("root");
            using (AttachmentDirectory sub = root.CreateSubDirectory("凭证"))
            {
                return sub.AddAttachment(attachId);
            }
        }
    }
}
python
from FoxitPDFSDKPython3 import *

def build_attachment_directory(doc, file_path):
    if doc.IsEmpty():
        return False

    attachments = doc.GetAttachments()
    if attachments.IsEmpty():
        attachments = doc.CreateAttachments()
    if attachments.IsEmpty():
        return False

    att = attachments.AddAttachment()
    if not att.SetFile(file_path):
        return False
    attach_id = att.GetID()

    root = attachments.CreateAttachmentDirectory()
    root.SetName("root")
    sub = root.CreateSubDirectory("凭证")
    return sub.AddAttachment(attach_id)
go
package main

import "foxit.com/fsdk"

func BuildAttachmentDirectory(doc fsdk.OFDDoc, filePath string) bool {
	if doc.IsEmpty() {
		return false
	}

	attachments := doc.GetAttachments()
	if attachments.IsEmpty() {
		attachments = doc.CreateAttachments()
	}
	if attachments.IsEmpty() {
		return false
	}

	att := attachments.AddAttachment()
	if !att.SetFile(filePath) {
		return false
	}
	attachID := att.GetID()

	root := attachments.CreateAttachmentDirectory()
	root.SetName("root")
	sub := root.CreateSubDirectory("凭证")
	return sub.AddAttachment(attachID)
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");

function buildAttachmentDirectory(doc, filePath) {
  if (doc.IsEmpty()) {
    return false;
  }

  let attachments = doc.GetAttachments();
  if (attachments.IsEmpty()) {
    attachments = doc.CreateAttachments();
  }
  if (attachments.IsEmpty()) {
    return false;
  }

  const att = attachments.AddAttachment();
  if (!att.SetFile(filePath)) {
    return false;
  }
  const attachId = att.GetID();

  const root = attachments.CreateAttachmentDirectory();
  root.SetName("root");
  const sub = root.CreateSubDirectory("凭证");
  return sub.AddAttachment(attachId);
}

保存 ​

附件增删改或目录结构调整后,须调用包层保存写回文件。

c
#include "fs_ofdpackage_c.h"

FS_BOOL SaveDocumentWithAttachments(FS_OFDPACKAGE_HANDLE package, const wchar_t* output_path) {
  FS_BOOL empty = FALSE;
  FS_BOOL save_result = FALSE;

  FSDK_OFDPackage_OFDIsEmpty(package, &empty);
  if (empty) {
    return FALSE;
  }

  FSDK_OFDPackage_OFDSaveAs0(package, output_path, &save_result);
  return save_result;
}
c++
#include "ofd/fs_ofdpackage.h"

using namespace foxit::ofd;

bool SaveDocumentWithAttachments(OFDPackage& package, const wchar_t* output_path) {
  if (package.IsEmpty()) {
    return false;
  }

  return package.SaveAs(WString(output_path));
}
java
import com.foxit.sdk.ofd.OFDPackage;

public class OFDSaveAttachmentExample {
    public static boolean saveDocumentWithAttachments(OFDPackage pkg,
            String outputPath) throws Exception {
        if (pkg.isEmpty()) {
            return false;
        }

        return pkg.saveAs(outputPath);
    }
}
csharp
using foxit.ofd;

bool SaveDocumentWithAttachments(OFDPackage package, string outputPath)
{
    if (package.IsEmpty())
    {
        return false;
    }

    return package.SaveAs(outputPath);
}
python
from FoxitPDFSDKPython3 import *

def save_document_with_attachments(package, output_path):
    if package.IsEmpty():
        return False
    return package.SaveAs(output_path)
go
package main

import "foxit.com/fsdk"

func SaveDocumentWithAttachments(pkg fsdk.OFDPackage, outputPath string) bool {
	if pkg.IsEmpty() {
		return false
	}
	return pkg.SaveAs(outputPath)
}
js
const FSDK = require("@fuxinsoft/fuxin-pdf-ofd-sdk-node");

function saveDocumentWithAttachments(pkg, outputPath) {
  if (pkg.IsEmpty()) {
    return false;
  }
  return pkg.SaveAs(outputPath);
}

注意事项 ​

  • 集合初始化:文档无附件集合时 GetAttachments() 的 IsEmpty() 为 true(对象无效,非“个数为 0”),须 CreateAttachments();创建后仍 IsEmpty() 表示创建失败。附件个数请用 GetAttachmentCount()。
  • 大文件读写:SetFile / GetFile 支持 ReaderCallback / WriterCallback,处理大附件时建议使用回调,避免整文件载入内存。
  • 权限语义:EnableVisible、EnableDeletable 等控制阅读器侧行为;SDK 侧开发者仍可读写附件,保存后权限标记会写入文档。
  • 元数据:添加附件时建议填写 format、usage,便于阅读器分类显示;AttachmentInfo 含创建/修改时间。
  • 电子凭证:导出前先用 GetElectronicBillsType 判断类型;无凭证时 GetElectronicBillsCount 为 0,ExportElectronicBillsFile 将失败。
  • 附件目录:目录节点通过 attach_id 引用已存在于 Attachments 集合中的附件,而非直接嵌入新文件。
  • 文档级快捷接口:OFDDoc::AddAttachment(file_path) 可一步添加文件;复杂元数据与权限仍推荐 Attachments + Attachment 流程。
  • 持久化:变更后须 OFDPackage::Save / SaveAs;与 OFD 文件包 保存流程一致。