Làm việc với các Tệp vCard (VCF) trong C++
vCard là định dạng trao đổi dữ liệu liên hệ được hiểu bởi điện thoại, khách hàng email và dịch vụ thư mục. Aspose.Email cho C++ xử lý nó qua VCardContact lớp trong Aspose::Email::PersonalInfo::VCard không gian tên - một mô hình đối tượng vCard thuần túy không cần Outlook hay MAPI. Nếu danh bạ của bạn nằm trong tệp MSG hoặc PST, hãy sử dụng MapiContact thay thế; nó cũng có thể nhập và xuất sang VCF, như được hiển thị trong Tạo, Lưu và Tải Danh Bạ Outlook.
Các mẫu dưới đây sử dụng các tiêu đề và không gian tên sau:
#include <PersonalInfo/vCard/VCardContact.h>
#include <PersonalInfo/vCard/VCardIdentificationInfo.h>
#include <PersonalInfo/vCard/VCardFullName.h>
#include <PersonalInfo/vCard/VCardOrganization.h>
#include <PersonalInfo/vCard/VCardEmail.h>
#include <PersonalInfo/vCard/VCardEmailCollection.h>
#include <PersonalInfo/vCard/VCardEmailType.h>
#include <PersonalInfo/vCard/VCardDeliveryAddress.h>
#include <PersonalInfo/vCard/VCardDeliveryAddressCollection.h>
#include <PersonalInfo/vCard/VCardDeliveryAddressType.h>
#include <PersonalInfo/vCard/VCardTelephoneNumber.h>
#include <PersonalInfo/vCard/VCardTelephoneNumberCollection.h>
#include <PersonalInfo/vCard/VCardTelephoneType.h>
#include <PersonalInfo/vCard/VCardGeo.h>
#include <PersonalInfo/vCard/VCardPhoto.h>
#include <PersonalInfo/vCard/VCardPhotoType.h>
#include <PersonalInfo/vCard/VCardValueLocation.h>
#include <PersonalInfo/vCard/VCardSecurity.h>
#include <PersonalInfo/vCard/VCardLoadOptions.h>
#include <PersonalInfo/vCard/VCardSaveOptions.h>
#include <PersonalInfo/vCard/VCardVersion.h>
#include <system/io/file.h>
#include <system/text/encoding.h>
#include <system/object_ext.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::PersonalInfo::VCard;
Chọn Giữa VCardContact và MapiContact
| | VCardContact | MapiContact | | — | — | — | | Định dạng gốc | vCard 2.1 / 3.0 / 4.0 | Outlook MSG, mục PST | | Nguồn điển hình | .vcf tệp, xuất danh bạ, xuất LDAP | Outlook, Exchange, lưu trữ PST | | Mô hình đối tượng | Phản chiếu các thuộc tính vCard (N, FN, ADR, TEL, GEO, KEY …) | Phản chiếu biểu mẫu liên hệ Outlook và các thuộc tính MAPI | | Tệp đa liên hệ | Có, thông qua LoadAsMultiple | Một liên hệ mỗi tệp; một VCF đa liên hệ tải như một MapiDistributionList |
Phần còn lại của bài viết này nói về VCardContact.
Tạo một Liên hệ và Lưu dưới dạng VCF
Một VCardContact bắt đầu rỗng; dữ liệu được thêm thông qua các đối tượng thuộc tính được nhóm. VCardIdentificationInfo giữ các trường liên quan đến tên (của nó Tên đầy đủ thành viên lưu trữ cấu trúc N thuộc tính), VCardOrganization công ty và chức vụ, và Emails tập hợp các địa chỉ email. Lưu ghi tệp dưới dạng vCard 2.1 theo mặc định.
SharedPtr<VCardContact> contact = MakeObject<VCardContact>();
// name block
SharedPtr<VCardIdentificationInfo> identification = MakeObject<VCardIdentificationInfo>();
identification->set_DisplayName(u"Bertha Buell");
identification->set_Nickname(u"Bertie");
identification->set_Birthday(DateTime(1980, 5, 20));
identification->get_FullName()->set_GivenName(u"Bertha");
identification->get_FullName()->set_MiddleName(u"A.");
identification->get_FullName()->set_FamilyName(u"Buell");
contact->set_IdentificationInfo(identification);
// company block
SharedPtr<VCardOrganization> organization = MakeObject<VCardOrganization>();
organization->set_Organization(u"Awthentikz");
organization->set_Title(u"Social work assistant");
contact->set_Organization(organization);
// e-mail block
SharedPtr<VCardEmail> email = MakeObject<VCardEmail>();
email->set_EmailAddress(u"BerthaABuell@example.com");
email->set_EmailType(VCardEmailType::INTERNET);
contact->get_Emails()->Add(email);
// write the vCard (VCF) file
contact->Save(u"bertha.vcf");
Điền phần còn lại của mô hình
Các đoạn mã dưới đây tải liên hệ đã tạo ở trên, thêm một nhóm thuộc tính và lưu lại lần nữa. Các enum đại diện cho các tham số loại vCard (kiểu địa chỉ, kiểu điện thoại, kiểu email) là các enum cờ, vì vậy có thể kết hợp nhiều giá trị với |.
Địa Chỉ Bưu Điện
Một VCardDeliveryAddress ánh xạ tới một ADR dòng. Nó AddressType nhận VCardDeliveryAddressType cờ: HOME, WORK, POSTAL, PARCEL, DOM, INTL, PREF.
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
SharedPtr<VCardDeliveryAddress> address = MakeObject<VCardDeliveryAddress>();
address->set_AddressType(VCardDeliveryAddressType::WORK | VCardDeliveryAddressType::PREF);
address->set_Street(u"4 Darwinia Loop");
address->set_Locality(u"Eighty Mile Beach");
address->set_Region(u"WA");
address->set_PostalCode(u"6725");
address->set_CountryName(u"Australia");
contact->get_DeliveryAddresses()->Add(address);
contact->Save(u"bertha.vcf");
Số Điện Thoại
Mỗi VCardTelephoneNumber trở thành một TEL dòng; VCardTelephoneType cung cấp VOICE, WORK, HOME, CELL, FAX, PAGER, PREF và hơn thế nữa.
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
SharedPtr<VCardTelephoneNumber> work = MakeObject<VCardTelephoneNumber>();
work->set_TelephoneNumber(u"(08) 9080 1183");
work->set_TelephoneType(VCardTelephoneType::WORK | VCardTelephoneType::VOICE);
contact->get_TelephoneNumbers()->Add(work);
SharedPtr<VCardTelephoneNumber> mobile = MakeObject<VCardTelephoneNumber>();
mobile->set_TelephoneNumber(u"(925) 599 3355");
mobile->set_TelephoneType(VCardTelephoneType::CELL);
contact->get_TelephoneNumbers()->Add(mobile);
contact->Save(u"bertha.vcf");
Vị Trí Địa Lý
VCardGeo lưu GEO thuộc tính dưới dạng cặp vĩ độ/kinh độ của float các giá trị.
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
SharedPtr<VCardGeo> geo = MakeObject<VCardGeo>();
geo->set_Latitude(48.858844f);
geo->set_Longitude(2.294351f);
contact->set_Geo(geo);
contact->Save(u"bertha.vcf");
Ảnh
Hình ảnh được gán qua Ảnh thuộc tính của khối nhận dạng như một VCardPhoto. ValueLocation chọn giữa nhúng các byte (INLINE) và trỏ tới một tệp bên ngoài (URL); PhotoType đặt tên cho định dạng hình ảnh.
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
// embed the image bytes inline
SharedPtr<VCardPhoto> photo = MakeObject<VCardPhoto>();
photo->set_Data(IO::File::ReadAllBytes(u"portrait.jpg"));
photo->set_PhotoType(VCardPhotoType::JPEG);
photo->set_ValueLocation(VCardValueLocation::INLINE);
contact->get_IdentificationInfo()->set_Photo(photo);
contact->Save(u"contact_with_photo.vcf");
// or reference an external image by URL
SharedPtr<VCardPhoto> linkedPhoto = MakeObject<VCardPhoto>();
linkedPhoto->set_Uri(u"https://example.com/portrait.jpg");
linkedPhoto->set_PhotoType(VCardPhotoType::JPEG);
linkedPhoto->set_ValueLocation(VCardValueLocation::URL);
contact->get_IdentificationInfo()->set_Photo(linkedPhoto);
contact->Save(u"contact_with_photo_url.vcf");
Đọc lại Mô Hình
Sau khi tải, các nhóm thuộc tính giống nhau sẽ hiển thị các giá trị đã phân tích. ObjectExt::ToString hiển thị một liệt kê enum cờ dưới dạng danh sách có thể đọc được như PREF, WORK.
SharedPtr<VCardContact> loaded = VCardContact::Load(u"bertha.vcf");
if (loaded->get_IdentificationInfo() != nullptr)
{
Console::WriteLine(loaded->get_IdentificationInfo()->get_DisplayName());
}
for (SharedPtr<VCardDeliveryAddress> address : loaded->get_DeliveryAddresses())
{
Console::WriteLine(ObjectExt::ToString(address->get_AddressType()) + u": " + address->get_Street() + u", " + address->get_Locality());
}
for (SharedPtr<VCardTelephoneNumber> phone : loaded->get_TelephoneNumbers())
{
Console::WriteLine(ObjectExt::ToString(phone->get_TelephoneType()) + u": " + phone->get_TelephoneNumber());
}
if (loaded->get_Geo() != nullptr && !loaded->get_Geo()->get_IsEmpty())
{
Console::WriteLine(u"Geo: " + Convert::ToString(loaded->get_Geo()->get_Latitude()) + u", " + Convert::ToString(loaded->get_Geo()->get_Longitude()));
}
Khóa Công cộng hoặc Chứng chỉ
Cái KEY thuộc tính của một vCard - một khóa công cộng hoặc một chứng chỉ X.509 - được hiển thị qua VCardSecurity. Khóa giữ giá trị đã mã hoá, Loại kiểu đã khai báo của nó, và SaveToPEM ghi ra dưới dạng tệp PEM với các BEGIN CERTIFICATE / END CERTIFICATE hàng rào.
SharedPtr<VCardContact> contact = VCardContact::Load(u"signed.vcf");
if (contact->get_Security() != nullptr && !String::IsNullOrEmpty(contact->get_Security()->get_Key()))
{
Console::WriteLine(u"Key type: " + contact->get_Security()->get_Type());
// export the embedded public key / certificate to a PEM file
contact->get_Security()->SaveToPEM(u"public-key.pem");
}
Tải một vCard
Phương thức tĩnh Load phương pháp đọc một danh bạ duy nhất từ đường dẫn hoặc từ stream. Khi tệp được ghi bằng một trang mã cũ, hãy truyền vào một VCardLoadOptions đối tượng mà PreferredEncoding tên mà mã hoá.
// load from a file
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
Console::WriteLine(contact->get_IdentificationInfo()->get_DisplayName());
// load from a stream
SharedPtr<IO::FileStream> stream = IO::File::OpenRead(u"bertha.vcf");
SharedPtr<VCardContact> fromStream = VCardContact::Load(stream);
stream->Close();
// control the text encoding used while reading
SharedPtr<VCardLoadOptions> loadOptions = MakeObject<VCardLoadOptions>();
loadOptions->set_PreferredEncoding(System::Text::Encoding::get_UTF8());
SharedPtr<VCardContact> decoded = VCardContact::Load(u"bertha.vcf", loadOptions);
Console::WriteLine(decoded->get_IdentificationInfo()->get_DisplayName());
Đọc Nhiều Danh Bạ Từ Một Tệp
Các xuất khẩu danh bạ thường đặt nhiều BEGIN:VCARD ... END:VCARD các block trong một tệp. IsMultiContacts phát hiện trường hợp đó, và LoadAsMultiple trả về tất cả danh bạ dưới dạng danh sách. Cả hai đều có các phiên bản overload cho tệp, stream và tùy chọn tải. Khi các overload stream được kết hợp, hãy quay lại đầu stream giữa việc kiểm tra và tải.
if (VCardContact::IsMultiContacts(u"contacts.vcf"))
{
// read every contact stored in the file
auto contacts = VCardContact::LoadAsMultiple(u"contacts.vcf");
for (SharedPtr<VCardContact> contact : contacts)
{
Console::WriteLine(contact->get_IdentificationInfo()->get_DisplayName());
}
}
// the same check and load work on a stream
SharedPtr<IO::FileStream> stream = IO::File::OpenRead(u"contacts.vcf");
if (VCardContact::IsMultiContacts(stream))
{
stream->Seek(0, IO::SeekOrigin::Begin);
Console::WriteLine(u"Contacts in stream: " + Convert::ToString(VCardContact::LoadAsMultiple(stream)->get_Count()));
}
stream->Close();
Tùy chọn Lưu
VCardSaveOptions kiểm soát đầu ra:
- Version - VCardVersion
V21(mặc định),V30hoặcV40; - PreferredTextEncoding - mã hoá của văn bản được ghi;
- UseExtensions - whether
X-các thuộc tính mở rộng được ghi (truemặc định); - ProductId - the
PRODIDgiá trị.
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
SharedPtr<VCardSaveOptions> saveOptions = MakeObject<VCardSaveOptions>();
saveOptions->set_Version(VCardVersion::V30); // write vCard 3.0 (default is V21)
saveOptions->set_PreferredTextEncoding(System::Text::Encoding::get_UTF8()); // encoding used when writing
saveOptions->set_UseExtensions(true); // allow extended (X-) properties
saveOptions->set_ProductId(u"My Company"); // PRODID value
contact->Save(u"contact_v3.vcf", saveOptions);
Xem Thêm
- Tạo, Lưu và Tải Danh Bạ Outlook -
MapiContact, bao gồm nhập và xuất VCF. - Làm việc với Liên hệ trong Tệp PST - danh bạ được lưu trong một PST.