如何在 C++ 中將 PDF 轉換為 PDFA
便攜式文件格式 (PDF) 一直是跨各種平台共用文件的首選格式。 然而,隨著數位存檔的重要性日益凸顯,越來越需要以標準化的格式存檔文檔,以確保長期保存和存取。 PDF/A (PDFA) 格式是專門為此目的而設計的,它提供了一種穩定且獨立的格式,適合存檔。
了解 PDF/A
PDF/A 是 ISO 標準的 PDF 變體,旨在長期保存文檔,保持內容的可存取性和完整性。 與普通的 PDF 文件相比,PDF/A 文件具有特定的限制,以確保其穩健性和獨立性。
本文將指導您使用 C++ 中的 QPDF 庫將 PDF 文件轉換為 PDF/A 格式。 QPDF 是一個功能強大的 C++ 庫,它使開發人員能夠以程式設計方式管理和轉換 PDF 文檔,包括 PDF 到 PDF/A 的轉換,只需幾行程式碼即可完成。
QPDF C++函式庫
QPDF是一個 C++ 函式庫和命令列工具,旨在支援處理、轉換和解析 PDF 檔案。 它允許開發人員以程式設計方式存取和操作 PDF 文件的內容,並得到廣泛支援。 QPDF 是一個開源項目,提供一系列功能,包括加密、解密、線性化、最佳化和 PDF/A 一致性。
先決條件
開始之前,請確保您已完成以下設定:
- Code::Blocks IDE:從官方網站下載並安裝Code::Blocks ( Code::Blocks 官方網站)。
- QPDF 庫:從 QPDF 網站( QPDF 官方網站)下載適用於您作業系統的最新版本的 QPDF 庫。
建立 PDF 到 PDF/A 項目
1.開啟 Code::Blocks:在您的電腦上啟動 Code::Blocks IDE。 2.建立新項目:點選頂部選單中的"檔案" ,然後選擇"新建" ,再選擇"項目" 。 3.選擇項目類型:在"從範本新建"視窗中,選擇"控制台應用程式" ,然後按一下"開始" 。 選擇C/C++語言,然後按一下"下一步" 。 4.輸入項目詳情:在"項目標題"欄位中提供項目名稱(例如,"PDFtoPDFA")。 選擇專案檔案的儲存位置,然後按一下"下一步" 。 5.選擇編譯器:選擇一個編譯器,如果需要,可以從清單中手動選擇一個。按一下"完成" 。
將 QPDF 新增到項目中
若要在 Code::Blocks 中包含 QPDF 頭文件,請依照下列步驟操作:
- 點選選單中的"項目" 。
- 從上下文選單中選擇"建置選項" 。
- 在"建置選項"對話方塊中,前往"搜尋目錄"標籤。
- 在"編譯器"標籤中,按一下"新增"按鈕,然後瀏覽至包含 QPDF 頭檔的目錄(通常位於include資料夾中)。
- 然後,在"連結"標籤中,按一下"新增"按鈕,並包含lib和bin目錄。
- 按一下"確定"關閉"建置選項"對話方塊。
此外,在連結階段,您還需要與 QPDF 庫建立連線。 請遵循以下步驟:
- 在"建置選項"對話方塊中,前往"連結器設定"標籤。
- 在"連結庫"下,按一下"新增" ,然後瀏覽至包含 QPDF 庫檔案的目錄(在 Windows 上通常具有.a或.lib檔案副檔名)。
- 再次按一下"新增" ,然後輸入 QPDF 庫的名稱(例如libqpdf.a或qpdf.lib )。
- 按一下"確定"關閉"產生選項"對話方塊。
將 PDF 檔案轉換為 PDF/A 檔案的步驟
包含必要的頭檔
#include <iostream>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFWriter.hh>
// The above code includes necessary header files for standard input/output operations and QPDF functionalities.#include <iostream>
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFWriter.hh>
// The above code includes necessary header files for standard input/output operations and QPDF functionalities.設定輸入和輸出檔案的路徑
在main函數(C++程式的起始點)中,設定輸入和輸出檔案的路徑:
std::string input_pdf_path = "input.pdf";
std::string output_pdf_a_path = "output.pdfa";
// These lines declare and initialize strings to store the paths of the input PDF and the output PDF/A file.std::string input_pdf_path = "input.pdf";
std::string output_pdf_a_path = "output.pdfa";
// These lines declare and initialize strings to store the paths of the input PDF and the output PDF/A file.建立用於 PDF 檔案的 QPDF 對象
QPDF input_pdf;
input_pdf.processFile(input_pdf_path.c_str());
// Create a QPDF object for the input PDF and process the file to prepare it for conversion.QPDF input_pdf;
input_pdf.processFile(input_pdf_path.c_str());
// Create a QPDF object for the input PDF and process the file to prepare it for conversion.建立用於寫入 PDF/A 檔案的QPDFWriter對象
QPDFWriter writer(input_pdf, output_pdf_a_path.c_str());
// This creates a QPDFWriter object to handle writing the converted PDF/A file.QPDFWriter writer(input_pdf, output_pdf_a_path.c_str());
// This creates a QPDFWriter object to handle writing the converted PDF/A file.設定 PDF/A 一致性並轉換 PDF 文檔
writer.setQDFMode(true);
writer.write();
// Set the QPDFWriter to operate in PDF/A compliant mode and write the output file.writer.setQDFMode(true);
writer.write();
// Set the QPDFWriter to operate in PDF/A compliant mode and write the output file.輸出
輸出檔不可編輯,且符合PDF/A標準:
在 C# 中將 PDF 轉換為 PDF/A
IronPDF Library是一個 .NET PDF 函式庫,它提供了處理 PDF 文件的全面功能。 它允許開發人員使用 C# 或 VB.NET 來建立、修改和轉換 PDF 文件。 使用者可以使用 IronPDF 從 HTML、ASPX、Word 文件 (.doc) 或圖像動態生成 PDF 文件,並添加圖表、表格和圖像資料(如 JPG、PNG 格式)等豐富元素。 它還支援合併、拆分和編輯現有 PDF 文件的頁面,以及提取文字和操作 PDF 資料的內容。
IronPDF 只需幾行程式碼即可將PDF 文件轉換為 PDF/A-3b 格式標準。 以下程式碼有助於實現此任務:
using IronPdf;
// Create a PdfDocument object or open any PDF file
PdfDocument pdf = PdfDocument.FromFile("wikipedia.pdf");
// Use the SaveAsPdfA method to save to file
pdf.SaveAsPdfA("pdf-a3-wikipedia.pdf", PdfAVersions.PdfA3);
// The above C# code demonstrates the use of IronPDF to convert a PDF document to a PDF/A-3b compliant file.using IronPdf;
// Create a PdfDocument object or open any PDF file
PdfDocument pdf = PdfDocument.FromFile("wikipedia.pdf");
// Use the SaveAsPdfA method to save to file
pdf.SaveAsPdfA("pdf-a3-wikipedia.pdf", PdfAVersions.PdfA3);
// The above C# code demonstrates the use of IronPDF to convert a PDF document to a PDF/A-3b compliant file.結論
本文將指導您使用 QPDF 函式庫,透過 C++ 將標準 PDF 文件轉換為 PDF/A 格式。 PDF/A 合規性可確保內容的保存和訪問,使其成為存檔的理想選擇。
IronPDF 也支援多種格式轉換,例如將 HTML、圖片和 Word 文件轉換為 PDF 文件。 如需了解更多信息,請訪問IronPDF 文件頁面。
IronPDF 提供免費試用版,供用戶測試其商業生產功能。 直接下載 IronPDF。






