บทนำ
Aspose.PDF FOSS for C++ เป็นไลบรารีโอเพนซอร์สที่ใช้ C++20 สำหรับทำงานกับเอกสาร PDF. พื้นผิว API ครอบคลุมการจัดการเอกสารและหน้า, การสกัดข้อความ, การเรนเดอร์แบบแรสเตอร์, การเข้ารหัส, คำอธิบายประกอบ, และฟิลด์ AcroForm เชิงโต้ตอบ — การดำเนินการที่มักจำเป็นที่สุดเมื่อแอปพลิเคชัน C++ ต้องสร้าง, ตรวจสอบ, หรือแปลงไฟล์ PDF โดยไม่ต้องพึ่งพาเอนจิ้น PDF ภายนอกหรือ SDK เชิงพาณิชย์.
ไลบรารีนี้ลิงก์กับเพียงแค่ไลบรารีมาตรฐานของ C++ เท่านั้น. ส่วนประกอบภายในเช่นตัวเข้ารหัสแรสเตอร์ BMP, JPEG, และ TIFF และตัวเรนเดอร์หน้า ถูกพัฒนาใหม่ตั้งแต่ต้นภายในโปรเจกต์, ดังนั้นจึงไม่มีการพึ่งพาเรนเดอร์ PDF ของระบบ, ส่วนประกอบ PDF เชิงพาณิชย์, หรือโค้ดแคร์ภาพจากบุคคลที่สาม. สิ่งนี้ทำให้รอยเท้าการพึ่งพาเป็นศูนย์นอกจากคอมไพเลอร์ที่สอดคล้องกับ C++20 และไลบรารีมาตรฐาน.
Aspose.PDF FOSS for C++ ถูกปล่อยภายใต้สัญญาอนุญาต MIT. บทความนี้จะพาไปดูพื้นที่คุณสมบัติหลักที่เปิดเผยโดยคลาส Document และประเภทที่สนับสนุนของมัน, ต่อด้วยตัวอย่าง เริ่มต้นอย่างรวดเร็ว และการสนับสนุนรูปแบบที่ยืนยันในปัจจุบัน.
คุณสมบัติหลัก
การจัดการเอกสารและหน้า
คลาส Document เป็นจุดเริ่มต้นสำหรับการเปิด, ตรวจสอบ, และบันทึกไฟล์ PDF. หน้าเข้าถึงได้ผ่าน Document.Pages(), ซึ่งจะคืนค่า PageCollection ที่สนับสนุน Count(), Add(), Insert(pageNumber), และ Delete(pageNumber). เมทาดาต้าระดับเอกสารจะถูกอ่านและอัปเดตผ่าน Document.Info() (อินสแตนซ์ของ DocumentInfo) หรือกำหนดโดยตรงด้วย Document.SetTitle().
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/page_collection.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
std::cout << "Page count: " << doc.Pages().Count() << "\n";
// Insert a blank page after page 1, then drop the last page
doc.Pages().Insert(2);
doc.Pages().Delete(doc.Pages().Count());
doc.SetTitle("Generated Report");
doc.Save("output.pdf");
}
Document.Optimize() และ Document.OptimizeResources() ลดขนาดไฟล์ผลลัพธ์โดยการรวมและกำจัดทรัพยากรที่ไม่ได้ใช้ก่อนบันทึก.
การสกัดข้อความ
Aspose::Pdf::Text::TextAbsorber ดึงเนื้อหาข้อความธรรมดาของเอกสารทั้งหมดหรือหน้าเดียว. Visit() เดินทางผ่านเป้าหมายและ Text() คืนสตริงที่สะสมไว้. TextFragmentAbsorber ทำงานในระดับละเอียดกว่าโดยเปิดเผยชุดข้อความแต่ละชุดผ่าน TextFragmentCollection.
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <aspose/pdf/text_fragment_absorber.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
// Extract all text from the document
Aspose::Pdf::Text::TextAbsorber absorber;
absorber.Visit(doc);
std::cout << absorber.Text() << "\n";
// Extract text fragments from page 1
Aspose::Pdf::Text::TextFragmentAbsorber fragmentAbsorber;
fragmentAbsorber.Visit(doc.Pages()[1]);
auto fragments = fragmentAbsorber.TextFragments();
std::cout << "Fragments on page 1: " << fragments.Count() << "\n";
}
การเรนเดอร์เป็นภาพเรสเตอร์
หน้าเรนเดอร์เป็นรูปแบบเรสเตอร์ผ่านคลาสอุปกรณ์ตามรูปแบบ — PngDevice, JpegDevice, BmpDevice และ TiffDevice — แต่ละคลาสทำงานตาม Process(page, output). ความละเอียดของผลลัพธ์ถูกควบคุมด้วยคลาส Resolution.
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/png_device.hpp>
#include <aspose/pdf/bmp_device.hpp>
#include <aspose/pdf/resolution.hpp>
#include <fstream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
auto page = doc.Pages()[1];
Aspose::Pdf::Devices::PngDevice png(Aspose::Pdf::Devices::Resolution(150));
std::ofstream pngOut("page1.png", std::ios::binary);
png.Process(page, pngOut);
Aspose::Pdf::Devices::BmpDevice bmp(Aspose::Pdf::Devices::Resolution(150));
std::ofstream bmpOut("page1.bmp", std::ios::binary);
bmp.Process(page, bmpOut);
}
JpegDevice ปฏิบัติตามรูปแบบ Process(page, output) เดียวกันสำหรับการส่งออก JPEG. TiffDevice ยังสนับสนุนการส่งออกหลายหน้าในคำเรียกเดียวผ่าน Process(document, fromPage, toPage, output) และเปิดเผย RenderingOptions() เพื่อควบคุมวิธีการเรนเดอร์ฟิลด์ฟอร์มลงในผลลัพธ์เรสเตอร์.
การเข้ารหัสและสิทธิ์การเข้าถึง
Document.Encrypt() ปกป้องเอกสารด้วยรหัสผ่านผู้ใช้และเจ้าของ โดยเลือกอัลกอริทึมการเข้ารหัสผ่าน enum CryptoAlgorithm: RC4x40, RC4x128, AESx128 หรือ AESx256. สิทธิ์ที่ได้รับจะอธิบายด้วยประเภท Permissions ซึ่งค่าธงประกอบด้วย PrintDocument, ModifyContent, ExtractContent, ModifyTextAnnotations, FillForm, ExtractContentWithDisabilities, AssembleDocument และ PrintingQuality.
#include <aspose/pdf/document.hpp>
int main() {
Aspose::Pdf::Document doc("input.pdf");
doc.Encrypt("user-password", "owner-password",
Aspose::Pdf::Permissions(),
Aspose::Pdf::CryptoAlgorithm::AESx256);
doc.Save("encrypted.pdf");
}
เอกสารที่เข้ารหัสที่เปิดอยู่แล้วจะถูกถอดรหัสในที่เดียวด้วย Document.Decrypt(). Document.IsEncrypted() รายงานว่าเอกสารที่โหลดอยู่ในขณะนี้มีพจนานุกรมการเข้ารหัสหรือไม่.
คำอธิบาย
คำอธิบายระดับหน้า — หมายเหตุข้อความ, รูปร่าง, และการทำเครื่องหมาย — สามารถเข้าถึงได้ผ่าน Page.Annotations() ซึ่งคืนค่า AnnotationCollection. รายการแต่ละรายการเปิดเผยคุณสมบัติที่ใช้ร่วมกันเช่น Contents(), Rect(), Color(), Border(), และ AnnotationType(), และสามารถลบออกจากหน้า หรือทำให้แบนเป็นเนื้อหาคงที่ได้.
#include <aspose/pdf/document.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
auto& annotations = doc.Pages()[1].Annotations();
std::cout << "Annotation count: " << annotations.Count() << "\n";
for (int i = 0; i < static_cast<int>(annotations.Count()); ++i) {
std::cout << "Contents: " << annotations[i].Contents() << "\n";
}
// Remove the first annotation on the page
if (annotations.Count() > 0) {
annotations.Delete(annotations[0]);
}
doc.Save("output.pdf");
}
Annotation ใด ๆ ที่เป็นเอกเทศก็สามารถแปลงเป็นเนื้อหาหน้าคงที่โดยตรงด้วยเมธอด Flatten() ของมันเอง โดยไม่ขึ้นกับกระบวนการทำแฟลตของฟอร์มที่อธิบายด้านล่าง.
ฟอร์มโต้ตอบ
ฟิลด์ AcroForm บนเอกสารถูกจัดการผ่าน Document.Form() ซึ่งคืนค่าอินสแตนซ์ Form ที่รองรับ Count(), HasField(fieldName), Delete(fieldName), และ Flatten(). ฟิลด์ใหม่จะถูกแนบด้วย Form.Add(field, pageNumber), โดยที่ field เป็นชนิดย่อย Field ที่เป็นคอนกรีต เช่น TextBoxField, CheckboxField, ChoiceField, หรือ ButtonField.
#include <aspose/pdf/document.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
auto& form = doc.Form();
std::cout << "Form fields: " << form.Count() << "\n";
if (form.HasField("Signature1")) {
form.Delete("Signature1");
}
// Convert every remaining field into static page content
form.Flatten();
doc.Save("flattened.pdf");
}
เริ่มต้นอย่างรวดเร็ว
เพิ่มไลบรารีเป็นไดเรกทอรีย่อยของ CMake และลิงก์กับเป้าหมาย aspose_pdf_foss:
add_subdirectory(aspose.pdf-foss-for-cpp)
target_link_libraries(your_app PRIVATE aspose_pdf_foss)
เปิดเอกสาร, ดึงข้อความออก, และเรนเดอร์หน้าที่หนึ่งเป็น PNG:
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <aspose/pdf/png_device.hpp>
#include <aspose/pdf/resolution.hpp>
#include <fstream>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
std::cout << "Pages: " << doc.Pages().Count() << "\n";
Aspose::Pdf::Text::TextAbsorber absorber;
absorber.Visit(doc);
std::cout << absorber.Text() << "\n";
Aspose::Pdf::Devices::PngDevice png(Aspose::Pdf::Devices::Resolution(150));
std::ofstream out("page1.png", std::ios::binary);
png.Process(doc.Pages()[1], out);
}
รูปแบบที่รองรับ
| รูปแบบ | ส่วนขยาย | อ่าน | เขียน |
|---|---|---|---|
| BMP | bmp | — | ✓ |
| JPEG | jpeg | — | ✓ |
| TIFF | tiff | — | ✓ |
| Text | txt | — | ✓ |
| SVG | svg | ✓ | — |
โอเพ่นซอร์สและการให้สิทธิ์ใช้งาน
Aspose.PDF FOSS สำหรับ C++ ถูกเผยแพร่ภายใต้สัญญาอนุญาต MIT ซึ่งอนุญาตให้ใช้เชิงพาณิชย์, แก้ไข, และกระจายต่อได้โดยไม่มีค่าลิขสิทธิ์. โค้ดต้นฉบับมีให้ที่ github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Cpp.
การใช้เชิงพาณิชย์ได้รับอนุญาตตามเงื่อนไขของสัญญาอนุญาต MIT. หากโครงการของคุณต้องการการสนับสนุนระดับองค์กร, ครอบคลุมรูปแบบที่ขยายเพิ่ม, หรือการบูรณาการคลาวด์, Aspose.PDF — Enterprise Product Family เป็นข้อเสนอเชิงพาณิชย์แยกต่างหาก.