複数の PDF文書を一つの出力ドキュメントに結合します。
// 出力ストリームを生成 pOutStream = _tfopen(szOutPath, _T("wb+")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to create output file \"%s\" for writing.\n"), szOutPath); TPdfToolsSys_StreamDescriptor outDesc; PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0); pAssembler = PdfToolsDocumentAssembly_DocumentAssembler_New(&outDesc, NULL, NULL); for (int i = 1; i < argc - 1; i++) { szInPath = argv[i]; // 入力PDFファイルを開く pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath); TPdfToolsSys_StreamDescriptor inDesc; PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0); pInDoc = PdfToolsPdf_Document_Open(&inDesc, _T("")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR( pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath, szErrorBuff, PdfTools_GetLastError()); // 入力PDF文書の内容を出力文書に追加 GOTO_CLEANUP_IF_FALSE_PRINT_ERROR( PdfToolsDocumentAssembly_DocumentAssembler_Append(pAssembler, pInDoc, NULL, NULL, NULL, NULL), _T("Failed to append a page. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError()); PdfToolsPdf_Document_Close(pInDoc); fclose(pInStream); pInDoc = NULL; pInStream = NULL; } // 入力文書を出力文書に結合 pOutDoc = PdfToolsDocumentAssembly_DocumentAssembler_Assemble(pAssembler); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError());
private static void Merge(IEnumerableinPaths, string outPath) { // 出力ストリームを生成 using var outStream = File.Create(outPath); using var docAssembler = new PdfTools.DocumentAssembly.DocumentAssembler(outStream); foreach (var inPath in inPaths) { using var inStream = File.OpenRead(inPath); using var inDoc = PdfTools.Pdf.Document.Open(inStream); // 入力PDF文書の内容を出力文書に追加 docAssembler.Append(inDoc); } // 入力文書を出力文書に結合 docAssembler.Assemble(); }
def merge(input_paths: str, output_path: str): # 出力ストリームを生成 with io.FileIO(output_path, 'wb+') as output_stream: with DocumentAssembler(output_stream, None, None) as assembler: for input_path in input_paths: with open(input_path, 'rb') as input_stream: with Document.open(input_stream) as input_document: # 入力PDF文書の内容を出力文書に追加 assembler.append(input_document) # 入力文書を出力文書に結合 assembler.assemble()
merge(input_paths, output_path)
一つのPDF文書を複数のPDF文書に分割します。
// 入力PDFファイルを開く pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath); TPdfToolsSys_StreamDescriptor inDesc; PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0); pInDoc = PdfToolsPdf_Document_Open(&inDesc, _T("")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR( pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath, szErrorBuff, PdfTools_GetLastError()); // 入力PDF文書をページごとに分割し、それぞれを一つの出力文書PDFとして生成 int nPageCount = PdfToolsPdf_Document_GetPageCount(pInDoc); TCHAR szPageFileName[256]; for (int i = 1; i <= nPageCount; i++) { CreateOutputFileName(szPageFileName, szOutPath, i); // 入力PDF文書の各ページを出力するストリームを作成 pOutStream = _tfopen(szPageFileName, _T("wb+")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to open the input file \"%s\" for reading.\n"), szPageFileName); TPdfToolsSys_StreamDescriptor outDesc; PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0); // PDFをページに分割 pAssembler = PdfToolsDocumentAssembly_DocumentAssembler_New(&outDesc, NULL, NULL); GOTO_CLEANUP_IF_FALSE_PRINT_ERROR( PdfToolsDocumentAssembly_DocumentAssembler_Append(pAssembler, pInDoc, &i, &i, NULL, NULL), _T("Failed to append a page. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError()); pOutDoc = PdfToolsDocumentAssembly_DocumentAssembler_Assemble(pAssembler); PdfToolsDocumentAssembly_DocumentAssembler_Close(pAssembler); if (pOutDoc) PdfToolsPdf_Document_Close(pOutDoc); if (pOutStream) fclose(pOutStream); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError()); }
private static void Split(string inPath, string outPathPrefix) { // 入力PDFファイルを開く using var inStream = File.OpenRead(inPath); using var inDoc = PdfTools.Pdf.Document.Open(inStream); // 入力文書をページごとに分割 for (int i = 1; i <= inDoc.PageCount; ++i) { using var outStream = File.Create(outPathPrefix + "_page_" + i + ".pdf"); using var docAssembler = new PdfTools.DocumentAssembly.DocumentAssembler(outStream); docAssembler.Append(inDoc, i, i); docAssembler.Assemble(); } }
def split_pdf(input_file_path: str, output_file_path: str): # 入力PDFファイルを開く with open(input_file_path, 'rb') as input_stream: with Document.open(input_stream) as input_document: # 入力文書をページごとに分割 for i in range(1, input_document.page_count + 1): current_out_file = construct_file_name(output_file_path, i) with open(current_out_file, 'wb+') as output_stream: with DocumentAssembler(output_stream, None, None) as assembler: assembler.append(input_document, i, i) assembler.assemble()
# 入力パスと入力ドキュメントのページ番号からファイル名を構築 def construct_file_name(input_file: str, page_number: int): # 入力パスからディレクトリとファイル名を分割 directory, basename = os.path.split(input_file) # ファイルのベース名と拡張子を分割 base, extension = os.path.splitext(basename) return os.path.join(directory, f"{base}_page_{page_number}{extension}")
split_pdf(input_path, output_path)
入力されたPDF文書を分析します。PDF/A-2bに準拠していない場合は、PDF/A-2bに変換します。
void EventListener(void* pContext, const char* szDataPart, const char* szMessage, TPdfToolsPdfAConversion_EventSeverity iSeverity, TPdfToolsPdfAConversion_EventCategory iCategory, TPdfToolsPdfAConversion_EventCode iCode, const char* szContext, int iPageNo) { // iSeverityはイベントの推奨される重要度です // オプション: 提案された重大度は、変換プロセスの要件、たとえばイベントのカテゴリ (カテゴリなど) // に応じて変更できます。 if (iSeverity > iEventsSeverity) iEventsSeverity = iSeverity; // 変換状態をレポート TCHAR cSeverity = iSeverity == ePdfToolsPdfAConversion_EventSeverity_Information ? 'I' : ePdfToolsPdfAConversion_EventSeverity_Warning ? 'W' : 'E'; if (iPageNo > 0) _tprintf(_T("- %c %d: %s (%s on page %d)\n"), cSeverity, iCategory, szMessage, szContext, iPageNo); else _tprintf(_T("- %c %d: %s (%s)\n"), cSeverity, iCategory, szMessage, szContext); }
void ConvertIfNotConforming(const TCHAR* szInPath, const TCHAR* szOutPath, TPdfToolsPdf_Conformance iConf) { TPdfToolsPdfAValidation_AnalysisOptions* pAOpt = NULL; TPdfToolsPdfAValidation_Validator* pValidator = NULL; TPdfToolsPdfAValidation_AnalysisResult* pARes = NULL; TPdfToolsPdfAConversion_ConversionOptions* pConvOpt = NULL; TPdfToolsPdfAConversion_Converter* pConv = NULL; TPdfToolsPdf_Document* pOutDoc = NULL; TPdfToolsPdf_Document* pInDoc = NULL; FILE* pInStream = NULL; FILE* pOutStream = NULL; // 入力PDFファイルを開く pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath); TPdfToolsSys_StreamDescriptor inDesc; PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0); pInDoc = PdfToolsPdf_Document_Open(&inDesc, _T("")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInDoc, _T("Failed to open document \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath, szErrBuf, PdfTools_GetLastError()); // 入力文書のPDF/A標準準拠を分析する検証ツールを生成 pAOpt = PdfToolsPdfAValidation_AnalysisOptions_New(); PdfToolsPdfAValidation_AnalysisOptions_SetConformance(pAOpt, iConf); pValidator = PdfToolsPdfAValidation_Validator_New(); pARes = PdfToolsPdfAValidation_Validator_Analyze(pValidator, pInDoc, pAOpt); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pARes, _T("Failed to analyze document. %s (ErrorCode: 0x%08x).\n"), szErrBuf, PdfTools_GetLastError()); // PDF/Aへの変換が必要かを確認する if (PdfToolsPdfAValidation_AnalysisResult_IsConforming(pARes)) { printf("Document conforms to %s already.\n", PdfToolsPdf_Conformance_ToStringA(iConf)); goto cleanup; } // 書き込み用の出力ストリームを作成 pOutStream = _tfopen(szOutPath, _T("wb+")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to create the output file \"%s\".\n"), szOutPath); TPdfToolsSys_StreamDescriptor outDesc; PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0); // コンバーターオブジェクトを使用して入力ドキュメントをPDF/Aに変換 // および、その変換イベントハンドラ生成 pConvOpt = PdfToolsPdfAConversion_ConversionOptions_New(); pConv = PdfToolsPdfAConversion_Converter_New(); PdfToolsPdfAConversion_Converter_AddConversionEventHandlerA( pConv, NULL, (TPdfToolsPdfAConversion_Converter_ConversionEventA)EventListener); pOutDoc = PdfToolsPdfAConversion_Converter_Convert(pConv, pARes, pInDoc, &outDesc, pConvOpt, NULL); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("Failed to convert document. %s (ErrorCode: 0x%08x).\n"), szErrBuf, PdfTools_GetLastError()); // 重要なコンバージョンイベントが発生したかどうかを確認 switch (iEventsSeverity) { case ePdfToolsPdfAConversion_EventSeverity_Information: { TPdfToolsPdf_Conformance iOutConf; PdfToolsPdf_Document_GetConformance(pOutDoc, &iOutConf); printf("Successfully converted document to %s.\n", PdfToolsPdf_Conformance_ToStringA(iOutConf)); break; } case ePdfToolsPdfAConversion_EventSeverity_Warning: { TPdfToolsPdf_Conformance iOutConf; PdfToolsPdf_Document_GetConformance(pOutDoc, &iOutConf); printf("Warnings occurred during the conversion of document to %s.\n", PdfToolsPdf_Conformance_ToStringA(iOutConf)); printf("Check the output file to decide if the result is acceptable.\n"); break; } case ePdfToolsPdfAConversion_EventSeverity_Error: { printf("Unable to convert document to %s because of critical conversion events.\n", PdfToolsPdf_Conformance_ToStringA(iConf)); break; } cleanup: PdfToolsPdf_Document_Close(pOutDoc); PdfTools_Release(pConv); PdfTools_Release(pConvOpt); PdfTools_Release(pARes); PdfTools_Release(pValidator); PdfTools_Release(pAOpt); PdfToolsPdf_Document_Close(pInDoc); if (pInStream) fclose(pInStream); if (pOutStream) fclose(pOutStream); }
static void ConvertIfNotConforming(string inPath, string outPath, Conformance conformance) { // 入力ファイルを開く using var inStr = File.OpenRead(inPath); using var inDoc = Document.Open(inStr); // Validatorオブジェクトを生成し、Conformanceオブジェクトを使用して、 // Validatorの動作を制御するAnalysisOptionsオブジェクトを生成 var validator = new Validator(); var analysisOptions = new AnalysisOptions() { Conformance = conformance }; // 分析を実行し、結果を確認 // PDF文書が準拠していない場合にのみ続行 var analysisResult = validator.Analyze(inDoc, analysisOptions); if (analysisResult.IsConforming) { Console.WriteLine($"Document conforms to {inDoc.Conformance} already."); return; } // Converterオブジェクトを生成 var converter = new Converter(); // 変換イベントのハンドラーを追加 var eventsSeverity = EventSeverity.Information; converter.ConversionEvent += (s, e) => { // イベントの推奨される重要度を取得 var severity = e.Severity; // オプション: 提案された重要度は変換プロセスの要件(イベントのカテゴリなど)に応じて変更できます。 if (severity > eventsSeverity) eventsSeverity = severity; // 変換イベントを報告 Console.WriteLine("- {0} {1}: {2} ({3}{4})", severity.ToString()[0], e.Category, e.Message, e.Context, e.PageNo > 0 ? " page " + e.PageNo : "" ); }; // 出力ファイルのストリームを生成 using var outStr = File.Create(outPath); // 変換オブジェクトとその変換イベントハンドラーを使用して入力ドキュメントをPDF/Aに変換 using var outDoc = converter.Convert(analysisResult, inDoc, outStr); // 重要な変換イベントが発生したかを確認 switch (eventsSeverity) { case EventSeverity.Information: Console.WriteLine($"Successfully converted document to {outDoc.Conformance}."); break; case EventSeverity.Warning: Console.WriteLine($"Warnings occurred during the conversion of document to {outDoc.Conformance}."); Console.WriteLine($"Check the output file to decide if the result is acceptable."); break; case EventSeverity.Error: throw new Exception($"Unable to convert document to {conformance} because of critical conversion events."); } }
def convert_if_not_conforming(input_file_path: str, output_file_path: str, conformance: Conformance): with io.FileIO(input_file_path, 'rb') as in_stream: with Document.open(in_stream) as input_document: # Validatorオブジェクトを生成し、Conformanceオブジェクトを使用して、 # Validatorの動作を制御するAnalysisOptionsオブジェクトを生成 validator = Validator() analysis_options = AnalysisOptions() analysis_options.conformance = conformance # 分析を実行し、結果を確認 # PDF文書が準拠していない場合にのみ続行 analysis_result = validator.analyze(input_document, analysis_options) if analysis_result.is_conforming: print(f"Document conforms to {input_document.conformance.name} already.") return # Converterオブジェクトを生成 converter = Converter() # 変換イベントのハンドラーを追加 converter.add_conversion_event_handler(event_handler) with io.FileIO(output_file_path, 'wb+') as output_stream: # コンバーターオブジェクトとその変換イベントハンドラーを使用して、入力ドキュメントをPDF/Aに変換します。 output_document = converter.convert(analysis_result, input_document, output_stream, None) # 重要な変換イベントが発生したかを確認 match events_severity: case EventSeverity.INFORMATION: print(f"Successfully converted document to {output_document.conformance.name}.") case EventSeverity.WARNING: print(f"Warnings occurred during the conversion of document to {output_document.conformance.name}.") print("Check the output file to decide if the result is acceptable.") case EventSeverity.ERROR: raise Exception(f"Unable to convert document to {conformance.name} because of critical conversion events.")
def event_handler(data_part: str, message: str, severity: EventSeverity, category: EventCategory, code: EventCode, context_info: str, page_no: int): # イベントの推奨重大度を取得 suggested_severity = severity # オプションで: 提案された重大度は、変換プロセスの要件や、たとえばイベントのカテゴリに応じて変更できます。 global events_severity if suggested_severity > events_severity: events_severity = suggested_severity # コンバージョンイベントを報告 if suggested_severity == EventSeverity.INFORMATION: severity_char = 'I' elif suggested_severity == EventSeverity.WARNING: severity_char = 'W' else: severity_char = 'E' if page_no > 0: print(f"- {severity_char} {category.name}: {message} ({context_info} on page {page_no})") else: print(f"- {severity_char} {category.name}: {message} ({context_info})")
# グローバルな events_severity を定義 events_severity = EventSeverity.INFORMATION convert_if_not_conforming(input_file_path, output_file_path, conformance)
PDF文書の指定されたページをラスタライズされたイメージに変換します。
この例の変換プロファイルは、PDF文書をアーカイブ用の TIFF イメージに変換します。
// 入力PDFファイルを開く pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath); TPdfToolsSys_StreamDescriptor inDesc; PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0); pInDoc = PdfToolsPdf_Document_Open(&inDesc, _T("")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR( pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath, szErrorBuff, PdfTools_GetLastError()); // 書き込み用の出力ストリームを生成 pOutStream = _tfopen(szOutPath, _T("wb+")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to open the output file \"%s\" for writing.\n"), szOutPath); TPdfToolsSys_StreamDescriptor outDesc; PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0); // 変換パラメータを定義するプロファイルを生成 // このArchiveプロファイルはPDF文書をアーカイブ用のTIFF画像に変換します。 pProfile = (TPdfToolsPdf2ImageProfiles_Profile*)PdfToolsPdf2ImageProfiles_Archive_New(); // オプション:変換プロセスの要件に応じてプロファイルのパラメータを変更できます。 // PDF文書を画像データに変換 pConverter = PdfToolsPdf2Image_Converter_New(); pOutDoc = (TPdfToolsImage_Document*)PdfToolsPdf2Image_Converter_ConvertDocument(pConverter, pInDoc, &outDesc, pProfile); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError());
private static void Pdf2Image(string inPath, string outPath) { // 入力PDFファイルを開く using var inStr = File.OpenRead(inPath); using var inDoc = Document.Open(inStr); // 変換パラメータを定義するプロファイルを生成 // このArchiveプロファイルはPDF文書をアーカイブ用のTIFF画像に変換します。 var profile = new Profiles.Archive(); // オプション:変換プロセスの要件に応じてプロファイルのパラメータを変更できます。 // 出力ストリームを生成 using var outStr = File.Create(outPath); // PDF文書を画像データに変換 using var outDoc = new Converter().ConvertDocument(inDoc, outStr, profile); }
def pdf_to_image(input_pdf_path: str, output_image_path: str): # 入力PDFファイルを開く with io.FileIO(input_pdf_path, 'rb') as input_pdf_stream: with Document.open(input_pdf_stream) as input_pdf_document: # 変換パラメータを定義するプロファイルを生成 # このArchiveプロファイルはPDF文書をアーカイブ用のTIFF画像に変換します。 profile = Archive() # オプション:変換プロセスの要件に応じてプロファイルのパラメータを変更できます。 # 出力ストリームを生成 with io.FileIO(output_image_path, 'wb+') as output_stream: # PDF文書を画像データに変換 converter = Converter() converter.convert_document(input_pdf_document, output_stream, profile)
pdf_to_image(input_pdf_path, output_image_path)
画像をPDF文書に変換します。
ここでは変換プロファイルのデフォルト設定が使用されています。
変換プロファイルは画像の各ページをA4サイズ縦向きページに配置し各辺の余白2cmにします。
// 入力画像ファイルを開く pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath); TPdfToolsSys_StreamDescriptor inDesc; PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0); pInDoc = PdfToolsImage_Document_Open(&inDesc); GOTO_CLEANUP_IF_NULL_PRINT_ERROR( pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath, szErrorBuff, PdfTools_GetLastError()); // 書き込み用の出力ストリームを生成 pOutStream = _tfopen(szOutPath, _T("wb+")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to open the output file \"%s\" for writing.\n"), szOutPath); TPdfToolsSys_StreamDescriptor outDesc; PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0); // 変換パラメータを定義するプロファイルを生成 // 既定のプロファイルは画像をPDF文書に変換します pProfile = (TPdfToolsImage2PdfProfiles_Profile*)PdfToolsImage2PdfProfiles_Default_New(); // 画像をPDF文書に変換 pConverter = PdfToolsImage2Pdf_Converter_New(); pOutDoc = (TPdfToolsPdf_Document*)PdfToolsImage2Pdf_Converter_Convert(pConverter, pInDoc, &outDesc, pProfile, NULL); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError());
private static void Image2Pdf(string inPath, string outPath) { // 入力画像ファイルを開く using var inStr = File.OpenRead(inPath); using var inDoc = Document.Open(inStr); // 変換パラメータを定義するプロファイルを生成 // 既定のプロファイルは画像をPDF文書に変換します var profile = new Profiles.Default(); // オプションで:変換プロセスの要件に応じてプロファイルのパラメータを変更できます // 出力ストリームを生成 using var outStr = File.Create(outPath); // 画像をPDF文書に変換 using var outDoc = new Converter().Convert(inDoc, outStr, profile); }
def convert_image_to_pdf(input_path: str, output_path: str): # 入力画像ファイルを開く with io.FileIO(input_path, 'rb') as in_stream: with Document.open(in_stream) as input_document: # 変換パラメータを定義するプロファイルを生成 (既定のプロファイル) # 既定のプロファイルは画像をPDF文書に変換します profile = Default() # オプションで:変換プロセスの要件に応じてプロファイルのパラメータを変更できます # 出力ストリームを生成 with io.FileIO(output_path, 'wb+') as output_stream: # 画像をPDF文書に変換 converter = Converter() converter.convert(input_document, output_stream, profile)
# オプションで: 以下のようにプロキシを指定できます # Sdk.set_proxy("http://myproxy:8080") convert_image_to_pdf(input_path, output_path)
画像をアクセシブルなPDF/A-2a文書に変換します。
PDF/AレベルAの要件に従って画像に代替テキストを追加します。
このことで、支援技術を使用する障害のある方にもアクセシビリティを確保するサンプルです。
private static void Image2Pdf(string inPath, string alternateText, string outPath) { // 画像ファイルをオープン using var inStr = File.OpenRead(inPath); using var inDoc = Document.Open(inStr); // 変換パラメータを定義するプロファイルを作成 // Archiveプロファイルは、アーカイブ用に画像を PDF/A ドキュメントに変換します。 var profile = new Profiles.Archive(); // 出力ドキュメントの準拠をPDF/A-2aに設定 profile.Conformance = new Conformance(2, Conformance.PdfALevel.A); // PDF/AレベルAの場合は画像の各ページに代替テキストが必要 // これは他のPDF/A(PDF/A-2bなど)レベルではオプションです profile.Language = "en"; profile.AlternateText.Add(alternateText); // オプションで:変換プロセスの要件に応じて他のプロファイル パラメータを変更することもできます。 // 出力ストリームを生成 using var outStr = File.Create(outPath); // 画像をタグ付きPDF/Aドキュメントに変換 using var outDoc = new Converter().Convert(inDoc, outStr, profile); }
def image_to_pdf(input_path: str, alternate_text: str, output_path: str): # 画像ファイルをオープン with io.FileIO(input_path, 'rb') as image_stream: with Document.open(image_stream) as image_document: # 変換パラメータを定義するプロファイルを作成 # Archiveプロファイルは、アーカイブ用に画像を PDF/A ドキュメントに変換します。 profile = Archive() # 出力ドキュメントの準拠をPDF/A-2aに設定 profile.conformance = Conformance.PDF_A2_A # PDF/AレベルAの場合は画像の各ページに代替テキストが必要 # これは他のPDF/A(PDF/A-2bなど)レベルではオプションです profile.language = "en" profile.alternate_text.append(alternate_text) # オプションで、変換プロセスの要件に応じて他のプロファイル パラメータを変更することもできます。 # 出力ストリームを生成 with io.FileIO(output_path, 'wb+') as output_stream: # 画像をタグ付きPDF/Aドキュメントに変換 converter = Converter() converter.convert(image_document, output_stream, profile)
image_to_pdf(input_path, alternate_text, output_path)
複数の画像をPDF文書に変換します。
画像リストを単一のPDFに変換します。
サポートされている画像形式は、TIFF、JPEG、BMP、GIF、PNG、JBIG2、JPEG2000です。
private static void Images2Pdf(IEnumerableinPaths, string outPath) { var streams = new List (); var images = new DocumentList(); try { // 入力画像を開いてリストに保存 foreach (var inPath in inPaths) { var stream = File.OpenRead(inPath); streams.Add(stream); images.Add(Document.Open(stream)); } // 変換パラメータを定義するプロファイルを生成 var profile = new Profiles.Default(); // オプションで:変換プロセスの要件に応じてプロファイルのパラメータを変更できます。 // 出力ストリームを生成 using var outStream = File.Create(outPath); using var outPdf = new Converter().ConvertMultiple(images, outStream, profile); } finally { foreach (var image in images) image.Dispose(); foreach (var stream in streams) stream.Dispose(); } }
def images_to_pdf(input_image_paths: list[str], output_file_path: str): try: stream_list = [] images = ImageDocumentList() # 入力画像を開いてリストに保存 for input_image_path in input_image_paths: image_stream = io.FileIO(input_image_path, 'rb') stream_list.append(image_stream) images.append(ImageDocument.open(image_stream)) # 変換パラメータを定義するプロファイルを生成 profile = Default() # オプションで:変換プロセスの要件に応じてプロファイルのパラメータを変更できます。 # 出力ストリームを生成 with io.FileIO(output_file_path, 'wb+') as output_stream: converter = Converter() converter.convert_multiple(images, output_stream, profile) finally: if 'images' in locals(): for image in images: image.__exit__(None, None, None) if 'stream_list' in locals(): for stream in stream_list: stream.__exit__(None, None, None)
images_to_pdf(input_image_paths, output_file_path)
PDF文書をユーザー名とパスワードで暗号化します。
文書を開くにはユーザー名とパスワードが必要となります。
// 入力PDFファイルを開く pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath); TPdfToolsSys_StreamDescriptor inDesc; PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0); pInDoc = PdfToolsPdf_Document_Open(&inDesc, _T("")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR( pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath, szErrorBuff, PdfTools_GetLastError()); // 書き込み用の出力ストリームを生成 pOutStream = _tfopen(szOutPath, _T("wb+")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to open the output file \"%s\" for writing.\n"), szOutPath); TPdfToolsSys_StreamDescriptor outDesc; PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0); // 暗号化オプションを設定 pOptions = PdfToolsSign_OutputOptions_New(); // ドキュメントを開くときに必要なユーザー パスワードを設定 // これにより入力ファイルのPDF/A準拠が削除されることに注意してください // (警告のePdfToolsSign_WarningCategory_PdfARemoved参照) pEncryption = PdfToolsPdf_Encryption_New(szPassword, NULL, ePdfToolsPdf_Permission_All); PdfToolsPdf_OutputOptions_SetEncryption((TPdfToolsPdf_OutputOptions*)pOptions, pEncryption); // 署名の削除を許可します。それ以外の場合は署名された入力文書の暗号化プロパティは無視されます // (警告のePdfToolsSign_WarningCategory_SignedDocEncryptionUnchanged参照) PdfToolsSign_OutputOptions_SetRemoveSignatures(pOptions, ePdfToolsSign_SignatureRemoval_Signed); // PDF文書を暗号化 pSigner = PdfToolsSign_Signer_New(); pOutDoc = PdfToolsSign_Signer_Process(pSigner, pInDoc, &outDesc, pOptions, NULL); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError());
static void Encrypt(string password, string inPath, string outPath) { // 入力PDFファイルを開く using var inStr = File.OpenRead(inPath); using var inDoc = Document.Open(inStr); // 出力ファイルのストリームを生成 using var outStr = File.Create(outPath); // 暗号化オプションを設定 var outputOptions = new Sign.OutputOptions() { // ドキュメントを開くときに必要なユーザー パスワードを設定 // これにより、入力ファイルの PDF/A 準拠が削除されることに注意してください // (警告のSign.WarningCategory.PdfARemovedを参照) Encryption = new Encryption(password, null, Permission.All), // 署名の削除を許可します。それ以外の場合は署名された入力文書の暗号化プロパティは無視されます // (警告のSign.WarningCategory.SignedDocEncryptionUnchangedを参照) RemoveSignatures = Sign.SignatureRemoval.Signed, }; // PDF文書を暗号化 using var outDoc = new Sign.Signer().Process(inDoc, outStr, outputOptions); }
def encrypt(password: str, input_path: str, output_path: str): # 入力PDFファイルを開く with io.FileIO(input_path, 'rb') as in_stream: with Document.open(in_stream) as input_document: # 出力ファイルのストリームを生成 with io.FileIO(output_path, 'wb+') as output_stream: # 暗号化オプションを設定 output_options = SignOutputOptions() # ドキュメントを開くときに必要なユーザー パスワードを設定 # これにより、入力ファイルの PDF/A 準拠が削除されることに注意してください # (警告のSign.WarningCategory.PdfARemovedを参照) output_options.encryption = Encryption(password, None, Permission.ALL) # 署名の削除を許可します。それ以外の場合は署名された入力文書の暗号化プロパティは無視されます # (警告のSign.WarningCategory.SignedDocEncryptionUnchangedを参照) output_options.remove_signatures = SignatureRemoval.SIGNED # PDF文書を暗号化 signer = Signer() signer.process(input_document, output_stream, output_options)
# PDF文書を暗号化 encrypt(password, input_path, output_path)
PDF文書から暗号化情報を削除し、通常通り開けるPDF文書にします。
// Use password to open encrypted input document pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath); TPdfToolsSys_StreamDescriptor inDesc; PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0); pInDoc = PdfToolsPdf_Document_Open(&inDesc, szPassword); GOTO_CLEANUP_IF_NULL_PRINT_ERROR( pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath, szErrorBuff, PdfTools_GetLastError()); // Create output stream for writing pOutStream = _tfopen(szOutPath, _T("wb+")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to open the output file \"%s\" for writing.\n"), szOutPath); TPdfToolsSys_StreamDescriptor outDesc; PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0); // Check if input file is encrypted BOOL bIsGetPermissionsSuccessful = PdfToolsPdf_Document_GetPermissions(pInDoc, &iPermissions); if (!bIsGetPermissionsSuccessful) { if (PdfTools_GetLastError() == 0) { _tprintf(_T("Validation failed, input file \"%s\" is not encrypted.\n"), szInPath); iRet = 1; goto cleanup; } else { nBufSize = PdfTools_GetLastErrorMessage(NULL, 0); PdfTools_GetLastErrorMessage(szErrorBuff, MIN(ARRAY_SIZE(szErrorBuff), nBufSize)); _tprintf(_T("Failed to get permissions for input file \"%s\", error %s \n"), szInPath, szErrorBuff); iRet = 1; goto cleanup; } } // Set encryption options pOptions = PdfToolsSign_OutputOptions_New(); // Set encryption parameters to no encryption PdfToolsPdf_OutputOptions_SetEncryption((TPdfToolsPdf_OutputOptions*)pOptions, NULL); // Allow removal of signatures. Otherwise the Encryption property is ignored for signed input documents // (see warning category Sign.WarningCategory.SignedDocEncryptionUnchanged). PdfToolsSign_OutputOptions_SetRemoveSignatures(pOptions, ePdfToolsSign_SignatureRemoval_Signed); // Decrypt the document pSigner = PdfToolsSign_Signer_New(); pOutDoc = PdfToolsSign_Signer_Process(pSigner, pInDoc, &outDesc, pOptions, NULL); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError());
static void Decrypt(string password, string inPath, string outPath) { // 暗号化された入力文書を開くにはパスワードを使用する using var inStr = File.OpenRead(inPath); using var inDoc = Document.Open(inStr, password); if (inDoc.Permissions == null) throw new Exception("Input file is not encrypted."); // 出力ファイルのストリームを作成 using var outStr = File.Create(outPath); // 暗号化オプションを設定 var outputOptions = new Sign.OutputOptions() { // 暗号化パラメータを暗号化なしに設定 Encryption = null, // 署名の削除を許可します。それ以外の場合は署名された入力ドキュメントの暗号化プロパティを無視します // (警告のSign.WarningCategory.SignedDocEncryptionUnchangedを参照) RemoveSignatures = Sign.SignatureRemoval.Signed, }; // 文書を復号 using var outDoc = new Sign.Signer().Process(inDoc, outStr, outputOptions); }
def decrypt(password, input_path, output_path): # 暗号化された入力文書を開くにはパスワードを使用する with io.FileIO(input_path, 'rb') as in_stream: with Document.open(in_stream, password) as input_document: if input_document.permissions == None: print(f"Input file is not encrypted.") return # 出力ファイルのストリームを作成 with io.FileIO(output_path, 'wb+') as output_stream: # 暗号化オプションを設定 output_options = SignOutputOptions() # 暗号化パラメータを暗号化なしに設定 output_options.encryption = None # 署名の削除を許可します。それ以外の場合は署名された入力ドキュメントの暗号化プロパティを無視します # (警告のSign.WarningCategory.SignedDocEncryptionUnchangedを参照) output_options.remove_signatures = SignatureRemoval.SIGNED # 文書を復号 signer = Signer() signer.process(input_document, output_stream, output_options)
# PDF文書を復号 decrypt(password, input_path, output_path)
"Web"最適化プロファイルを使用してPDF文書を最適化します。
// 入力PDFファイルを開く pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath); TPdfToolsSys_StreamDescriptor inDesc; PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0); pInDoc = PdfToolsPdf_Document_Open(&inDesc, _T("")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR( pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath, szErrorBuff, PdfTools_GetLastError()); // 書き込み用の出力ストリームを生成 pOutStream = _tfopen(szOutPath, _T("wb+")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to open the output file \"%s\" for writing.\n"), szOutPath); TPdfToolsSys_StreamDescriptor outDesc; PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0); // 最適化パラメータを定義するプロファイルを生成 // Webプロファイルは、電子文書交換用に文書を最適化するのに適しています。 pProfile = (TPdfToolsOptimizationProfiles_Profile*)PdfToolsOptimizationProfiles_Web_New(); // オプション:最適化プロセスの要件に応じてプロファイルのパラメータを変更できます。 // PDF文書を最適化 pOptimizer = PdfToolsOptimization_Optimizer_New(); pOutDoc = PdfToolsOptimization_Optimizer_OptimizeDocument(pOptimizer, pInDoc, &outDesc, pProfile, NULL); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError());
private static void Optimize(string inPath, string outPath) { // 入力PDFファイルを開く using var inStr = File.OpenRead(inPath); using var inDoc = Document.Open(inStr); // 最適化パラメータを定義するプロファイルを生成 // Webプロファイルは、電子文書交換用に文書を最適化するのに適しています。 var profile = new Profiles.Web(); // オプション:最適化プロセスの要件に応じてプロファイルのパラメータを変更できます。 // 出力ストリームを生成 using var outStr = File.Create(outPath); // PDF文書を最適化 using var outDoc = new Optimizer().OptimizeDocument(inDoc, outStr, profile); }
def optimize_pdf(input_path: str, output_path: str): # 入力PDFファイルを開く with io.FileIO(input_path, 'rb') as input_stream: with Document.open(input_stream) as input_document: # 最適化パラメータを定義するプロファイルを生成 # Webプロファイルは、電子文書交換用に文書を最適化するのに適しています。 profile = Web() # オプション:最適化プロセスの要件に応じてプロファイルのパラメータを変更できます。 # 出力ストリームを生成 with io.FileIO(output_path, 'wb+') as output_stream: # PDF文書を最適化 optimizer = Optimizer() optimizer.optimize_document(input_document, output_stream, profile)
optimize_pdf(input_path, output_path)
PDF文書の注釈(アノテーション)をフラット化することで最適化します。
この最適化によって注釈は静的コンテンツに変換されます。
// 入力PDFファイルを開く pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath); TPdfToolsSys_StreamDescriptor inDesc; PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0); pInDoc = PdfToolsPdf_Document_Open(&inDesc, _T("")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR( pInDoc, _T("Failed to create a document from the input file \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath, szErrorBuff, PdfTools_GetLastError()); // 書き込み用の出力ストリームを生成 pOutStream = _tfopen(szOutPath, _T("wb+")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutStream, _T("Failed to open the output file \"%s\" for writing.\n"), szOutPath); TPdfToolsSys_StreamDescriptor outDesc; PdfToolsSysCreateFILEStreamDescriptor(&outDesc, pOutStream, 0); // 最適化パラメータを定義するプロファイルを生成 // MinimalFileSizeプロファイルは、最小限のファイルサイズを生成するために使用されます。 pProfile = (TPdfToolsOptimizationProfiles_Profile*)PdfToolsOptimizationProfiles_MinimalFileSize_New(); // オプション:最適化プロセスの要件に応じてプロファイルのパラメータを変更できます。 // プロファイルを設定して注釈をフラット化します(フォームとリンクを含めます) TPdfToolsOptimization_RemovalOptions* pRemovalOptions = PdfToolsOptimizationProfiles_Profile_GetRemovalOptions(pProfile); PdfToolsOptimization_RemovalOptions_SetAnnotations(pRemovalOptions, ePdfToolsOptimization_ConversionStrategy_Flatten); PdfToolsOptimization_RemovalOptions_SetFormFields(pRemovalOptions, ePdfToolsOptimization_ConversionStrategy_Flatten); PdfToolsOptimization_RemovalOptions_SetLinks(pRemovalOptions, ePdfToolsOptimization_ConversionStrategy_Flatten); // ドキュメントを最適化 pOptimizer = PdfToolsOptimization_Optimizer_New(); pOutDoc = PdfToolsOptimization_Optimizer_OptimizeDocument(pOptimizer, pInDoc, &outDesc, pProfile, NULL); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("The processing has failed. (ErrorCode: 0x%08x).\n"), PdfTools_GetLastError());
private static void FlattenAnnotations(string inPath, string outPath) { // 入力PDFファイルを開く using var inStr = File.OpenRead(inPath); using var inDoc = Document.Open(inStr); // 最適化パラメータを定義するプロファイルを生成 // MinimalFileSizeプロファイルは最小限のファイルサイズを生成するために使用されます。 var profile = new Profiles.MinimalFileSize(); // オプション:最適化プロセスの要件に応じてプロファイルのパラメータを変更できます。 profile.RemovalOptions.Annotations = ConversionStrategy.Flatten; profile.RemovalOptions.FormFields = ConversionStrategy.Flatten; profile.RemovalOptions.Links = ConversionStrategy.Flatten; // 出力ストリームを生成 using var outStr = File.Create(outPath); // ドキュメントを最適化 using var outDoc = new Optimizer().OptimizeDocument(inDoc, outStr, profile); }
def flatten_annotations(input_path: str, output_path: str): # 入力PDFファイルを開く with io.FileIO(input_path, 'rb') as in_stream: with Document.open(in_stream) as input_document: # ファイルサイズを最小化するための最適化プロファイルを生成 profile = MinimalFileSize() # 注釈、フォームフィールド、リンクをフラット化する profile.removal_options.annotations = ConversionStrategy.FLATTEN profile.removal_options.form_fields = ConversionStrategy.FLATTEN profile.removal_options.links = ConversionStrategy.FLATTEN # 出力ストリームを生成 with io.FileIO(output_path, 'wb+') as output_stream: # ドキュメントを最適化 optimizer = Optimizer() optimizer.optimize_document(input_document, output_stream, profile)
flatten_annotations(input_path, output_path)
視覚的な外観を持つドキュメント署名を追加します。
視覚的な外観はXML(またはjson)ファイルで指定します。外観にはテキスト、画像、またはPDFを追加できます。
この署名は可視部分と不可視部分で構成され、不可視部分のみで文書の署名部分を検証し、署名者のIDを認証できます。
署名用の証明書はパスワードで保護されたPKCS#12ファイル(.pfxまたは.p12)で指定します。
static void Sign(string certificateFile, string password, string appConfigFile, string inPath, string outPath) { // 組み込みの暗号化プロバイダへのセッションを生成 using var session = new BuiltIn.Provider(); // 証明書ファイルを開く using var pfxStr = File.OpenRead(certificateFile); // .pfx(または.p12)ファイルから署名設定を作成 BuiltIn.SignatureConfiguration signature = session.CreateSignatureFromCertificate(pfxStr, password); // XMLまたはjsonファイルから外観を作成 using var appStream = File.OpenRead(appConfigFile); if (Path.GetExtension(appConfigFile) == ".xml") signature.Appearance = Appearance.CreateFromXml(appStream); else signature.Appearance = Appearance.CreateFromJson(appStream); signature.Appearance.PageNumber = 1; signature.Appearance.CustomTextVariables.Add("company", "Daily Planet"); // 入力のPDFファイルを開く using var inStr = File.OpenRead(inPath); using var inDoc = Document.Open(inStr); // 出力ファイルのストリームを生成 using var outStr = File.Create(outPath); // 入力PDF文書に電子署名 using var outDoc = new Signer().Sign(inDoc, signature, outStr); }
def sign(certificate_file: str, password: str, appearance_config_file: str, input_path: str, output_path: str): # 組み込みの暗号化プロバイダへのセッションを生成 with Provider() as session: # 証明書ファイルを開く with io.FileIO(certificate_file, 'rb') as pfx_stream: # .pfx(または.p12)ファイルから署名設定を作成 signature = session.create_signature_from_certificate(pfx_stream, password) # XMLまたはjsonファイルから外観を作成 with io.FileIO(appearance_config_file, 'rb') as appearance_stream: if appearance_config_file.endswith(".xml"): signature.appearance = Appearance.create_from_xml(appearance_stream) else: signature.appearance = Appearance.create_from_json(appearance_stream) signature.appearance.page_number = 1 signature.appearance.custom_text_variables["company"] = "Daily Planet" # 入力のPDFファイルを開く with io.FileIO(input_path, 'rb') as input_stream: with Document.open(input_stream) as input_document: # 出力ファイルのストリームを生成 with io.FileIO(output_path, 'wb+') as output_stream: # 入力PDF文書に電子署名 signer = Signer() signer.sign(input_document, signature, output_stream)
# オプションで: プロキシを指定できます # Sdk.set_proxy("http://myproxy:8080") # PDF文書に電子署名 sign(certificate_file, password, appearance_config_file, input_path, output_path)
入力PDF文書に含まれるすべての電子署名の署名情報を抽出して検証し、結果をコンソールに出力します。
// ヘルパー関数によって署名検証の詳細を出力します。
static int Validate(string inputFile, string certDir) { // 既定の検証プロファイルを使用します。 var profile = new Default(); // オフライン操作の場合はファイルシステムからカスタム信頼リスト(CustomTrustList)を構築し、外部失効チェックを無効にします。 if (certDir != null && certDir.Length != 0) { Console.WriteLine("Using 'offline' validation mode with custom trust list."); Console.WriteLine(); // 証明書を保持するためのカスタム信頼リストを生成 var ctl = new CustomTrustList(); // 証明書ディレクトリ内のファイルを反復処理し、カスタム信頼リストに証明書を追加 if (Directory.Exists(certDir)) { var directoryListing = Directory.EnumerateFiles(certDir); foreach (string fileName in directoryListing) { try { using var certStr = File.OpenRead(fileName); if (fileName.EndsWith(".cer") || fileName.EndsWith(".pem")) { ctl.AddCertificates(certStr); } else if (fileName.EndsWith(".p12") || fileName.EndsWith(".pfx")) { // パスワードが必要な場合はaddArchive(certStr, password)を使用します。 ctl.AddArchive(certStr); } } catch (Exception e) { Console.WriteLine("Could not add certificate '" + fileName + "' to custom trust list: " + e.Message); } } } else { // dirがディレクトリではない場合の処理 Console.WriteLine("Directory " + certDir + " is missing. No certificates were added to the custom trust list."); } Console.WriteLine(); // 検証プロファイルへのカスタム信頼リスト割り当て profile.CustomTrustList = ctl; // ファイルに埋め込まれた情報とカスタム信頼リストからの検証を許可 var vo = profile.ValidationOptions; vo.TimeSource = TimeSource.ProofOfExistence | TimeSource.ExpiredTimeStamp | TimeSource.SignatureTime; vo.CertificateSources = DataSource.EmbedInSignature | DataSource.EmbedInDocument | DataSource.CustomTrustList; // 失効チェックを無効にします。 profile.SigningCertTrustConstraints.RevocationCheckPolicy = RevocationCheckPolicy.NoCheck; profile.TimeStampTrustConstraints.RevocationCheckPolicy = RevocationCheckPolicy.NoCheck; } // 文書内のすべての署名を検証 (最新のものだけではありません。) var signatureSelector = SignatureSelector.All; // 検証オブジェクト(Validator)とイベントリスナーを作成 var validator = new Validator(); validator.Constraint += (s, e) => { Console.WriteLine(" - " + e.Signature.Name + (e.DataPart.Length > 0 ? (": " + e.DataPart) : "") + ": " + ConstraintToString(e.Indication, e.SubIndication, e.Message)); }; try { using var inStr = File.OpenRead(inputFile); // 入力のPDFファイルを開く // パスワードが必要な場合は、Open(inStr, password)を使用します。 using var document = Document.Open(inStr); // 文書、プロファイル、セレクタを検証メソッドに渡して実行 Console.WriteLine("Validation Constraints"); var results = validator.Validate(document, profile, signatureSelector); Console.WriteLine(); Console.WriteLine("Signatures validated: " + results.Count); Console.WriteLine(); // 結果を出力 foreach (var result in results) { var field = result.SignatureField; Console.WriteLine(field.FieldName + " of " + field.Name); try { Console.WriteLine(" - Revision : " + (field.Revision.IsLatest ? "latest" : "intermediate")); } catch (Exception ex) { Console.WriteLine("Unable to validate document Revision: " + ex.Message); } PrintContent(result.SignatureContent); Console.WriteLine(); } return 0; } catch (Exception ex) { Console.WriteLine("Unable to validate file: " + ex.Message); return 5; } }
private static void PrintContent(SignatureContent content) { if(content != null) { Console.WriteLine(" - Validity : " + ConstraintToString(content.Validity)); switch (content) { case UnsupportedSignatureContent: break; case CmsSignatureContent signature: { Console.WriteLine(" - Validation: " + signature.ValidationTime + " from " + signature.ValidationTimeSource); Console.WriteLine(" - Hash : " + signature.HashAlgorithm); Console.WriteLine(" - Signing Cert"); PrintContent(signature.SigningCertificate); Console.WriteLine(" - Chain"); foreach (var cert in signature.CertificateChain) { Console.WriteLine(" - Issuer Cert " + (signature.CertificateChain.IndexOf(cert) + 1)); PrintContent(cert); } Console.WriteLine(" - Chain : " + (signature.CertificateChain.IsComplete ? "complete" : "incomplete") + " chain"); Console.WriteLine(" Time-Stamp"); PrintContent(signature.TimeStamp); break; } case TimeStampContent timeStamp: { Console.WriteLine(" - Validation: " + timeStamp.ValidationTime + " from " + timeStamp.ValidationTimeSource); Console.WriteLine(" - Hash : " + timeStamp.HashAlgorithm); Console.WriteLine(" - Time : " + timeStamp.Date); Console.WriteLine(" - Signing Cert"); PrintContent(timeStamp.SigningCertificate); Console.WriteLine(" - Chain"); foreach (var cert in timeStamp.CertificateChain) { Console.WriteLine(" - Issuer Cert " + (timeStamp.CertificateChain.IndexOf(cert) + 1)); PrintContent(cert); } Console.WriteLine(" - Chain : " + (timeStamp.CertificateChain.IsComplete ? "complete" : "incomplete") + " chain"); break; } default: Console.WriteLine("Unsupported signature content type " + content.GetType().Name); break; } } else { Console.WriteLine(" - null"); } }
private static void PrintContent(Certificate cert) { if(cert != null) { Console.WriteLine(" - Subject : " + cert.SubjectName); Console.WriteLine(" - Issuer : " + cert.IssuerName); Console.WriteLine(" - Validity : " + cert.NotBefore + " - " + cert.NotAfter); try { Console.WriteLine(" - Fingerprint: " + FormatSha1Digest(new BigInteger(SHA1.Create().ComputeHash(cert.RawData)).ToByteArray(), "-")); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine(" - Source : " + cert.Source); Console.WriteLine(" - Validity : " + ConstraintToString(cert.Validity)); } else { Console.WriteLine(" - null"); } }
private static String ConstraintToString(ConstraintResult constraint) { return ConstraintToString(constraint.Indication, constraint.SubIndication, constraint.Message); }
private static String ConstraintToString(Indication indication, SubIndication subIndication, String message) { return (indication == Indication.Valid ? "" : (indication == Indication.Indeterminate ? "?" : "!")) + "" + subIndication + " " + message; }
// SHA-1ダイジェスト文字列を生成するヘルパー関数 private static String FormatSha1Digest(byte[] bytes, String delimiter) { var result = new StringBuilder(); foreach (byte aByte in bytes) { int number = (int)aByte & 0xff; String hex = number.ToString("X2"); result.Append(hex.ToUpper() + delimiter); } return result.ToString().Substring(0, result.Length - delimiter.Length); }
def constraint_to_string(indication: Indication, sub_indication: str, message: str, is_full_revision_covered: bool = None): # is_full_revision_covered が指定されている場合はバイト範囲の検証を処理する if is_full_revision_covered is None or is_full_revision_covered: indication_str = ( "" if indication == Indication.VALID else "?" if indication == Indication.INDETERMINATE else "!" ) return f"{indication_str}{sub_indication} {message}" byte_range_invalid = "!Invalid signature byte range." if indication == Indication.VALID: return byte_range_invalid else: return f"{byte_range_invalid} {sub_indication} {message}"
def format_sha1_digest(fingerprint: str, delimiter: str): return delimiter.join(fingerprint[i:i+2] for i in range(0, len(fingerprint), 2))
def print_certificate(cert: Certificate): if cert is not None: print(f" - Subject : {cert.subject_name}") print(f" - Issuer : {cert.issuer_name}") print(f" - Validity : {cert.not_before} - {cert.not_after}") try: # 整数のリストをバイトに変換 raw_data_bytes = bytes(cert.raw_data) # hashlibを使ってFingerprint計算 fingerprint = hashlib.sha1(raw_data_bytes).hexdigest().upper() print(f" - Fingerprint: {format_sha1_digest(fingerprint, '-')}") except Exception as ex: print(str(ex)) # 個々のデータソース名を抽出して表示 sources = [source.name for source in DataSource if source in cert.source] print(f" - Source : {', '.join(sources)}") print(f" - Validity : {constraint_to_string(cert.validity.indication, cert.validity.sub_indication.name, cert.validity.message)}") else: print(" - null")
def print_signature_content(content: SignatureContent, is_full_revision_covered: bool = None): if content is not None: print(f" - Validity : {constraint_to_string(content.validity.indication, content.validity.sub_indication.name, content.validity.message, is_full_revision_covered)}") if isinstance(content, UnsupportedSignatureContent): pass # サポートされていないコンテンツに対する処理はしません elif isinstance(content, CmsSignatureContent): print(f" - Validation: {content.validation_time} from {content.validation_time_source.name}") print(f" - Hash : {content.hash_algorithm.name}") print(" - Signing Cert") print_certificate(content.signing_certificate) print(" - Chain") for index, cert in enumerate(content.certificate_chain, start=1): print(f" - Issuer Cert {index}") print_certificate(cert) print(f" - Chain : {'complete' if content.certificate_chain.is_complete else 'incomplete'} chain") print(" Time-Stamp") print_signature_content(content.time_stamp) elif isinstance(content, TimeStampContent): print(f" - Validation: {content.validation_time} from {content.validation_time_source.name}") print(f" - Hash : {content.hash_algorithm.name}") print(f" - Time : {content.date}") print(" - Signing Cert") print_certificate(content.signing_certificate) print(" - Chain") for index, cert in enumerate(content.certificate_chain, start=1): print(f" - Issuer Cert {index}") print_certificate(cert) print(f" - Chain : {'complete' if content.certificate_chain.is_complete else 'incomplete'} chain") else: print(f"Unsupported signature content type {str(type(content))}") else: print(" - null")
def on_constraint_event(message: str, indication: Indication, sub_indication: SubIndication, signature: DocumentSignature, data_part: str): print(f" - {signature.name}" + (f": {data_part}" if len(data_part) > 0 else "") + ": " + constraint_to_string(indication, sub_indication.name, message))
def validate(input_file: str, cert_dir: str): # デフォルトの検証プロファイルを使用します profile = Default() # オフライン操作の場合は、ファイルシステムからカスタム信頼リストを構築し、外部失効チェックを無効にします if cert_dir: print("Using 'offline' validation mode with custom trust list.") print() # 証明書を保持するためのCustomTrustList(カスタム信頼リスト)を作成 ctl = CustomTrustList() # 証明書ディレクトリ内のファイルを反復処理し、カスタム信頼リストに証明書を追加します if os.path.isdir(cert_dir): for file_name in os.listdir(cert_dir): try: with io.FileIO(os.path.join(cert_dir, file_name), 'rb') as cert_stream: if file_name.endswith(".cer") or file_name.endswith(".pem"): ctl.add_certificates(cert_stream) elif file_name.endswith(".p12") or file_name.endswith(".pfx"): # If a password is required, use add_archive(certStr, password). ctl.add_archive(cert_stream) except Exception as e: print(f"Could not add certificate '{file_name}' to custom trust list: {e}") else: print(f"Directory {cert_dir} is missing. No certificates were added to the custom trust list.") print() profile.custom_trust_list = ctl # 検証オプションを構成 validation_options = profile.validation_options validation_options.time_source = TimeSource.PROOF_OF_EXISTENCE | TimeSource.EXPIRED_TIME_STAMP | TimeSource.SIGNATURE_TIME validation_options.certificate_sources = DataSource.EMBED_IN_SIGNATURE | DataSource.EMBED_IN_DOCUMENT | DataSource.CUSTOM_TRUST_LIST # 失効チェックを無効にします profile.signing_cert_trust_constraints.revocation_check_policy = RevocationCheckPolicy.NO_CHECK profile.time_stamp_trust_constraints.revocation_check_policy = RevocationCheckPolicy.NO_CHECK # 文書内のすべての署名を検証する(最新のものだけでなく) signatureSelector = SignatureSelector.ALL # Validatorオブジェクトとイベントリスナーを作成 validator = Validator() validator.add_constraint_handler(on_constraint_event) try: with io.FileIO(input_file, 'rb') as in_stream: # 入力文書をオープン # パスワードが必要な場合は、Open(inStr, password) を使用します with Document.open(in_stream) as document: print("Validation Constraints") results = validator.validate(document, profile, signatureSelector) print() print(f"Signatures validated: {len(results)}") print() for result in results: field = result.signature_field print(f"{field.field_name} of {field.name}") try: print(f" - Revision : {'latest' if field.revision.is_latest else 'intermediate'}") except Exception as ex: print(f"Unable to validate document Revision: {str(ex)}") print_signature_content(result.signature_content, field.is_full_revision_covered) print() except Exception as e: print(f"Unable to validate file: {e}")
# オプションで: プロキシを指定できます # Sdk.set_proxy("http://myproxy:8080") validate(input_file, cert_dir)
PDF文書の規格への準拠性を検証します。
void ErrorListener(void* pContext, const TCHAR* szDataPart, const TCHAR* szMessage, TPdfToolsPdfAValidation_ErrorCategory iCategory, const TCHAR* szContext, int iPageNo, int iObjectNo) { if (iPageNo > 0) _tprintf(_T("- %d: %s (%s on page %d)\n"), iCategory, szMessage, szContext, iPageNo); else _tprintf(_T("- %d: %s (%s)\n"), iCategory, szMessage, szContext); }
void Validate(const TCHAR* szInPath) { TPdfToolsPdf_Document* pInDoc = NULL; TPdfToolsPdfAValidation_Validator* pValidator = NULL; TPdfToolsPdfAValidation_ValidationResult* pResult = NULL; // 入力PDFファイルを開く FILE* pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInStream, _T("Failed to open the input file \"%s\" for reading.\n"), szInPath); TPdfToolsSys_StreamDescriptor inDesc; PdfToolsSysCreateFILEStreamDescriptor(&inDesc, pInStream, 0); pInDoc = PdfToolsPdf_Document_Open(&inDesc, _T("")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInDoc, _T("Failed to open document \"%s\". %s (ErrorCode: 0x%08x).\n"), szInPath, szErrBuf, PdfTools_GetLastError()); // すべての検証エラーメッセージをコンソールに書き出す検証オブジェクトを生成 pValidator = PdfToolsPdfAValidation_Validator_New(); PdfToolsPdfAValidation_Validator_AddErrorHandler(pValidator, NULL, (TPdfToolsPdfAValidation_Validator_Error)ErrorListener); // PDF文書の規格準拠性を検証 pResult = PdfToolsPdfAValidation_Validator_Validate(pValidator, pInDoc, NULL); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pResult, _T("Failed to validate document. %s (ErrorCode: 0x%08x).\n"), szErrBuf, PdfTools_GetLastError()); // 検証結果をレポート TPdfToolsPdf_Conformance iClaimedConformance; PdfToolsPdf_Document_GetConformance(pInDoc, &iClaimedConformance); if (PdfToolsPdfAValidation_ValidationResult_IsConforming(pResult)) printf("Document conforms to %s.\n", PdfToolsPdf_Conformance_ToStringA(iClaimedConformance)); else printf("Document does not conform to %s.\n", PdfToolsPdf_Conformance_ToStringA(iClaimedConformance)); cleanup: PdfTools_Release(pResult); PdfTools_Release(pValidator); PdfToolsPdf_Document_Close(pInDoc); if (pInStream) fclose(pInStream); }
private static ValidationResult Validate(string inPath) { // 入力PDFファイルを開く using var inStr = File.OpenRead(inPath); using var inDoc = Document.Open(inStr); // すべての検証エラーメッセージをコンソールに書き出す検証オブジェクトを生成 var validator = new Validator(); validator.Error += (s, e) => Console.WriteLine("- {0}: {1} ({2}{3})", e.Category, e.Message, e.Context, e.PageNo > 0 ? " on page" + e.PageNo : ""); // PDF文書の規格準拠性を検証 return validator.Validate(inDoc); }
def error_listener(context, data_part: str, message: str, category: ErrorCategory, context_text: str, page_no: int, object_no: int): if page_no > 0: print(f"- {category.name}: {message.decode()} ({context_text.decode()} on page {page_no})") else: print(f"- {category.name}: {message.decode()} ({context_text.decode()})")
def validate(input_file_path: str): # 入力文書を開く with io.FileIO(input_file_path, 'rb') as in_stream: with Document.open(in_stream) as input_document: # すべての検証エラーメッセージをコンソールに書き出す検証オブジェクトを生成 validator = Validator() validator.add_error_handler(error_listener) # PDF文書の規格準拠性を検証 return validator.validate(input_document)
validation_result = validate(input_file_path) # 検証結果を表示 if validation_result.is_conforming: print(f"Document conforms to {Conformance(validation_result.conformance).name}.") else: print(f"Document does not conform to {Conformance(validation_result.conformance).name}.")
質問のページからお送りいただくようお願いします。
または、メールでsupport@trustss.co.jpあてにお送りください。
ご購入前の技術的質問も無償で対応します。サポート受付ページからお願いします。