将文档作为附件添加到 PDF 为您带来了很多便利。例如,您可以将多个文档作为单个文档传输;您可以在 PDF 文档中打开另一个文件,而无需从其他位置查找该文档;您可以降低丢失 PDF 中引用文档的可能性。
Spire.PDF for C++ 允许您以两种方式附加文件:
- 文档级附件(或常规附件):文档级附件是指添加到“附件”选项卡中的附件,在特定页面上找不到该附件。
 - 注释附件:注释附件是指添加到页面特定位置的附件。注释附件在页面上显示为回形针图标;审阅者可以双击图标打开文件。
 
本文将向您展示如何使用 Spire.PDF for C++ 在 C++ 中添加或删除 PDF 文档中的常规附件和注释附件。
安装 Spire.PDF for C++
有两种方法可以将 Spire.PDF for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire. PDF for C++ 集成到 C++ 程序中
向 PDF 添加常规附件
要添加常规附件,可以使用 PdfDocument->GetAttachments()->Add() 方法。以下是详细步骤。
- 创建一个 PdfDocument 对象。
 - 使用 PdfDocument->LoadFromFile() 方法加载 PDF 文档。
 - 基于外部文件创建 PdfAttachment 对象。
 - 使用 PdfDocument->GetAttachments()->Add() 方法将附件添加到 PDF。
 - 使用 PdfDocument.SaveToFile() 方法将文档保存到另一个 PDF 文件。
 
- C++
 
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main() {
    //指定输入文件路径
    wstring inputPdfPath = L"C:\\Users\\Administrator\\Desktop\\AI创意赛.pdf";
    wstring inputFilePath = L"C:\\Users\\Administrator\\Desktop\\参赛人员名单.xlsx";
    //指定输出文件路径
    wstring outputFilePath = L"Output\\添加附件.pdf";
    //创建PdfDocument对象
    PdfDocument* doc = new PdfDocument();
    //加载示例PDF文件
    doc->LoadFromFile(inputPdfPath.c_str());
    //基于外部文件创建PdfAttachment对象
    PdfAttachment* attachment = new PdfAttachment(inputFilePath.c_str());
    //将附件添加到PDF
    doc->GetAttachments()->Add(attachment);
    //保存到文件
    doc->SaveToFile(outputFilePath.c_str());
    delete doc;
}
向 PDF 添加注释附件
注释附件由 PdfAttachmentAnnotation 类表示。您需要基于外部文件创建该类的实例,然后使用 PdfPageBase->GetAnnotationsWidget()->Add() 方法将其添加到特定页面。以下是详细步骤。
- 创建一个 PdfDocument 对象。
 - 使用 PdfDocument->LoadFromFile() 方法加载 PDF 文档。
 - 使用 PdfDocument->GetPages()->GetItem() 方法获取特定页面以添加注释。
 - 基于外部文件创建 PdfAttachmentAnnotation 对象。
 - 使用 PdfPageBase->GetAnnotationsWidget->Add() 方法将注释附件添加到页面。
 - 使用 PdfDocument->SaveToFile() 方法保存文档。
 
- C++
 
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main() {
    //指定输入文件路径
    wstring inputPdfPath = L"AI创意赛.pdf";
    wstring inputFilePath = L"赛制规则及流程安排.docx";
    //指定输出文件路径
    wstring outputFilePath = L"Output\\添加注释附件.pdf";
    //创建PdfDocument对象
    PdfDocument* doc = new PdfDocument();
    //加载示例PDF文件
    doc->LoadFromFile(inputPdfPath.c_str());
    //获取特定页面
    PdfPageBase* page = doc->GetPages()->GetItem(0);
    //在PDF上绘制标签
    wstring label = L"现场相关事宜安排见附件:";
    PdfTrueTypeFont* font = new PdfTrueTypeFont(L"宋体", 13.0f, PdfFontStyle::Bold, true);
    float x = 35;
    float y = doc->GetPages()->GetItem(0)->GetActualSize()->GetHeight() - 220;
    page->GetCanvas()->DrawString(label.c_str(), font, PdfBrushes::GetRed(), x, y);
    //转换要附加到流的文件
    ifstream is1(inputFilePath.c_str(), ifstream::in | ios::binary);
    is1.seekg(0, is1.end);
    int length1 = is1.tellg();
    is1.seekg(0, is1.beg);
    char* buffer1 = new  char[length1];
    is1.read(buffer1, length1);
    Stream* stream = new Spire::Common::Stream((unsigned char*)buffer1, length1);
    SizeF* size = font->MeasureString(label.c_str());
    RectangleF* bounds = new RectangleF((float)(x + size->GetWidth() + 5), (float)y, 10, 15);
    //基于文件创建PdfAttachmentAnnotation对象
    PdfAttachmentAnnotation* annotation = new PdfAttachmentAnnotation(bounds, L"赛制规则及流程安排.docx", stream);
    annotation->SetColor(new PdfRGBColor(Spire::Common::Color::GetDarkOrange()));
    annotation->SetFlags(PdfAnnotationFlags::ReadOnly);
    annotation->SetIcon(PdfAttachmentIcon::Graph);
    annotation->SetText(L"单击此处打开文件");
    //将附件注释添加到PDF
    page->GetAnnotationsWidget()->Add(annotation);
    //保存文件
    doc->SaveToFile(outputFilePath.c_str());
    delete doc;
}
删除 PDF 中的常规附件
Spire.PDF for C++ 提供 PdfDocument->GetAttachments() 方法用于返回 PDF 文档的常规附件集合。然后您可以使用 PdfAttachmentCollection->RemoveAt() 方法或 PdfAttachmentCollection->Clear() 方法删除特定附件或所有附件。详细步骤如下。
- 创建一个 PdfDocument 对象。
 - 使用 PdfDocument->LoadFromFile() 方法加载 PDF 文档。
 - 使用 PdfDocument->GetAttachments() 方法从文档中获取附件集合。
 - 使用 PdfAttachmentCollection->RemoveAt() 方法删除特定附件。 要一次删除所有附件,请使用 PdfAttachmentCollection->Clear() 方法。
 - 使用 PdfDocument->SaveToFile() 方法保存文档。
 
- C++
 
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main() {
	//指定输入文件路径
	wstring inputPdfPath = L"C:\\Users\\Administrator\\Desktop\\示例文档.pdf";
	//指定输出文件路径
	wstring outputFilePath = L"Output\\删除附件.pdf";
	//创建PdfDocument对象
	PdfDocument* doc = new PdfDocument();
	//加载PDF文件
	doc->LoadFromFile(inputPdfPath.c_str());
	//获取所有附件
	PdfAttachmentCollection* attachments = doc->GetAttachments();
	//删除所有附件
	attachments->Clear();
	//删除指定附件
	//attachments->RemoveAt(0);
	//保存文件
	doc->SaveToFile(outputFilePath.c_str());
	doc->Close();
	delete doc;
}删除 PDF 中的注释附件
注释是基于页面的元素。 您可以使用 PdfPageBase->GetAnnotationsWidget() 方法从特定页面获取注释,并确定某个注释是否为注释附件。之后,使用 PdfAnnotationCollection->Remove() 方法从注释集合中删除注释附件。 以下是详细步骤。
- 创建一个 PdfDocument 对象。
 - 使用 PdfDocument->LoadFromFile() 方法加载 PDF 文档。
 - 使用 PdfPageBase->GetAnnotationsWidget() 方法从特定页面获取注释集合。
 - 确定注释是否为 PdfAttachmentAnnotationWidget 的实例。如果是,请使用 PdfAnnotationCollection->Remove() 方法删除注释附件。
 - 使用 PdfDocument->SaveToFile() 方法保存文档。
 
- C++
 
#include "Spire.Pdf.o.h";
using namespace Spire::Pdf;
using namespace std;
int main() {
	//指定输入文件路径
	wstring inputPdfPath = L"C:\\Users\\Administrator\\Desktop\\示例文档.pdf";
	//指定输出文件路径
	wstring outputFilePath = L"Output\\删除附件.pdf";
	//创建PdfDocument对象
	PdfDocument* doc = new PdfDocument();
	//加载PDF文件
	doc->LoadFromFile(inputPdfPath.c_str());
	//获取所有附件
	PdfAttachmentCollection* attachments = doc->GetAttachments();
	//删除所有附件
	attachments->Clear();
	//删除指定附件
	//attachments->RemoveAt(0);
	//保存文件
	doc->SaveToFile(outputFilePath.c_str());
	doc->Close();
	delete doc;
}申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
    


					



