附件 (Attachment)
在 福昕 PDF SDK 中,attachments 指的是文档附件而不是文件附件注释。它允许将整个文件封装在文档中,就像电子邮件附件一样。福昕 PDF SDK 提供应用程序 APIs 来访问附件,例如加载附件,获取附件,插入/删除附件,以及访问附件的属性。
从 PDF 文档中导出嵌入的附件文件,并将其另存为单个文件
c++
#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfattachments.h"
using namespace foxit;
using namespace common;
using namespace pdf;
using namespace foxit::common;
// Assuming PDFDoc doc has been loaded.
// Get information of attachments.
Attachments attachments(doc);
int count = attachments.GetCount();
for (int i = 0; i < count; i++) {
WString key = attachments.GetKey(i);
FileSpec file_spec = attachments.GetEmbeddedFile(key);
if (!file_spec.IsEmpty()) {
WString name = file_spec.GetFileName();
if (file_spec.IsEmbedded()) {
WString exFilePath = "output_directory";
file_spec.ExportToFile(exFilePath);
}
}
}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfattachments_c.h"
// Assuming FS_PDFDOC_HANDLE doc has been loaded.
// Get information of attachments.
FS_ATTACHMENTS_HANDLE attachments;
FS_PDFNAMETREE_HANDLE pdfnametree;
FSDK_PDFNameTree_Create0(&pdfnametree);
FSDK_Attachments_Create(doc, pdfnametree, &attachments);
int count;
FSDK_Attachments_GetCount(attachments, &count);
for (int i = 0; i < count; i++) {
FS_WSTR key;
FSDK_WStr_Init(key);
FSDK_Attachments_GetKey(attachments, i, &key);
FS_FILESPEC_HANDLE file_spec;
FSDK_Attachments_GetEmbeddedFile(attachments, (const wchar_t*)key.str, &file_spec);
FSDK_WStr_Clear(key);
FS_BOOL return_value;
FSDK_FileSpec_IsEmpty(file_spec, &return_value);
if (!return_value) {
FS_WSTR name;
FSDK_WStr_Init(name);
FS_BOOL isEmbedded;
FSDK_FileSpec_GetFileName(file_spec, &name);
FSDK_FileSpec_IsEmbedded(file_spec, &isEmbedded);
if (isEmbedded) {
const wchar_t *exFilePath = "output_directory";
FS_BOOL bExportStatus;
FSDK_FileSpec_ExportToFile(file_spec, exFilePath, &bExportStatus);
}
FSDK_WStr_Clear(name);
}
}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
java
import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.pdf.Attachments;
import com.foxit.sdk.pdf.FileSpec;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.objects.PDFNameTree;
...
PDFNameTree empty_nametree = new PDFNameTree();
{
// Get information of attachments.
Attachments attachments = new Attachments(doc, empty_nametree);
int count = attachments.getCount();
for (int i = 0; i < count; i++) {
String key = attachments.getKey(i);
FileSpec file_spec = attachments.getEmbeddedFile(key);
if (!file_spec.isEmpty()) {
String name = file_spec.getFileName();
if (file_spec.isEmbedded()) {
String export_file_path = output_path + name;
file_spec.exportToFile(export_file_path);
}
}
}
}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
py
import sys
import site
if sys.version_info.major == 2:
_PYTHON2_ = True
else:
_PYTHON2_ = False
if _PYTHON2_:
#replace with the python2 lib path
site.addsitedir('../../../')
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
# Assuming PDFDoc doc has been loaded.
# Get information of attachments.
attachments = Attachments(doc)
count = attachments.GetCount()
for i in range(0, count):
key = attachments.GetKey(i)
file_spec = attachments.GetEmbeddedFile(key)
if not file_spec.IsEmpty():
name = file_spec.GetFileName()
if file_spec.IsEmbedded():
exFilePath = "output_directory"
file_spec.ExportToFile(exFilePath)
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
objc
#include "FSPDFObjC.h"
...
FSAttachments *attachments = [[FSAttachments alloc] initWithDoc:doc nametree:[[FSPDFNameTree alloc] init]];
int count = [attachments getCount];
for (int i = 0; i < count; i++) {
NSString *key = [attachments getKey:i];
FSFileSpec *file_spec = [attachments getEmbeddedFile:key];
if (![file_spec isEmpty]) {
NSString *name = [file_spec getFileName];
if ([file_spec isEmbedded]) {
NSString *exFilePath = [[NSString alloc] initWithFormat:@"%@%@", output_directory, name];
bool bExportStatus = [file_spec exportToFile:exFilePath];
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
// Assuming PDFDoc doc has been loaded.
// Get information of attachments.
let attachments = new FSDK.Attachments(doc);
let count = attachments.GetCount();
for (var i = 0; i < count; i++) {
let key = attachments.GetKey(i);
var file_spec = attachments.GetEmbeddedFile(key);
if (!file_spec.IsEmpty()) {
let name = file_spec.GetFileName();
}
if (file_spec.IsEmbedded()) {
let exFilePath = "output_directory"
file_spec.ExportToFile(exFilePath);
}
}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
csharp
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
using foxit.pdf.objects;
// Assuming PDFDoc doc has been loaded.
string text_path = "The input path of the attached file you need to insert";
using (Attachments attachments = new Attachments(doc, new PDFNameTree()))
{
int count = attachments.GetCount();
for (int i = 0; i < count; i++)
{
string key = attachments.GetKey(i);
using (FileSpec file_spec = attachments.GetEmbeddedFile(key))
{
if (!file_spec.IsEmpty())
{
string name = file_spec.GetFileName();
if (file_spec.IsEmbedded())
{
String exFilePath = "output_directory";
file_spec.ExportToFile(exFilePath);
}
}
}
}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
删除 PDF 文档中所有的附件文件
c++
#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfattachments.h"
...
using namespace foxit;
using namespace common;
using namespace pdf;
using namespace foxit::common;
// Assuming PDFDoc doc has been loaded.
// Get information of attachments.
Attachments attachments(doc);
int count = attachments.GetCount();
for (int i = 0; i < count; i++) {
WString key = attachments.GetKey(i);
attachment.RemoveEmbeddedFile(key);
}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfattachments_c.h"
...
// Assuming FS_PDFDOC_HANDLE doc has been loaded.
// Get information of attachments.
FS_ATTACHMENTS_HANDLE attachments;
FS_PDFNAMETREE_HANDLE pdfnametree;
FSDK_PDFNameTree_Create0(&pdfnametree);
FSDK_Attachments_Create(doc, pdfnametree, &attachments);
int count;
FSDK_Attachments_GetCount(attachments, &count);
for (int i = 0; i < count; i++) {
FS_WSTR key;
FSDK_WStr_Init(key);
FSDK_Attachments_GetKey(attachments, i, &key);
FS_BOOL isRemove;
FSDK_Attachments_RemoveEmbeddedFile( attachments, (const wchar_t*)key.str, &isRemove);
FSDK_WStr_Clear(key);
}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
java
import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.pdf.Attachments;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.objects.PDFNameTree;
...
// Assuming PDFDoc doc has been loaded.
PDFNameTree empty_nametree = new PDFNameTree();
{
// Get information of attachments.
Attachments attachments = new Attachments(doc, empty_nametree);
attachments.removeAllEmbeddedFiles();
}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
py
import sys
import site
if sys.version_info.major == 2:
_PYTHON2_ = True
else:
_PYTHON2_ = False
if _PYTHON2_:
#replace with the python2 lib path
site.addsitedir(‘../../../’)
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
# Assuming PDFDoc doc has been loaded.
# Get information of attachments.
attachments = Attachments(doc)
count = attachments.GetCount()
for i in range(0, count):
key = attachments.GetKey(i)
attachments.RemoveEmbeddedFile(key)
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
objc
#include "FSPDFObjC.h"
...
// Assuming FSPDFDoc doc has been loaded.
FSAttachments *attachments = [[FSAttachments alloc] initWithDoc:doc nametree:[[FSPDFNameTree alloc] init]];
[attachments removeAllEmbeddedFiles];
...
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
// Assuming PDFDoc doc has been loaded.
// Get information of attachments.
let attachments = new FSDK.Attachments(doc);
let count = attachments.GetCount();
for (var i = 0; i < count; i++) {
let key = attachments.GetKey(i);
attachments.RemoveEmbeddedFile(key)
...
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
csharp
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
using foxit.pdf.objects;
// Assuming PDFDoc doc has been loaded.
...
using (Attachments attachments = new Attachments(doc, new PDFNameTree()))
{
int count = attachments.GetCount();
for (int i = 0; i < count; i++)
{
string key = attachments.GetKey(i);
attachment.RemoveEmbeddedFile(key);
}
}
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18