PDFデータをPDF/Aに変換します。
暗号化されたPDFを読み込んだり、不正なデータを修正したりします。
PDFの内容や添付されたファイルなどを削除できます。
APIリファレンスはこちらです。
| 入力ファイルの規格 | 出力ファイルの規格 | PDF 1.x, PDF 2.0 | PDF/A-1b, PDF/A-1a, PDF/A-2b, PDF/A-2a, PDF/A-2u, PDF/A-3b, PDF/A-3a, PDF/A-3u |
|---|
入力PDFへの処理
カラー・プロファイル及びフォント
変換処理
C#のサンプルプロジェクトではPdftools SDKライブラリ(DLL)をNuGetから自動でダウンロードします。
CのサンプルプロジェクトにはPdftools SDKライブラリ(DLL)が含まれています。
ライセンスキー無し(無償)で試用できます。ただし、結果に「透かし」が入ります。
「透かし」の削除をご希望の場合は問い合わせページまたはメールでお問い合わせください。
License Agreement(利用許諾契約書)が含まれていますので必ず確認してください。
入力された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オブジェクトを生成 PdfTools.PdfA.Validation.Validator validator = new Validator(); PdfTools.PdfA.Validation.AnalysisOptions alysisOptions = new AnalysisOptions (){ Conformance = conformance }; // 分析を実行し、結果を確認 // PDF文書が準拠していない場合にのみ続行 PdfTools.PdfA.Validation.AnalysisResult analysisResult = validator.Analyze(inDoc, analysisOptions); if (analysisResult.IsConforming) { Console.WriteLine($"Document conforms to {inDoc.Conformance} already."); return; } // Converterオブジェクトを生成 PdfTools.PdfA.Conversion.Converter converter = new Converter(); // 変換イベントのハンドラーを追加 PdfTools.PdfA.Conversion.EventSeverity 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)
他の機能サンプルを参照してください。
質問のページからお送りいただくようお願いします。
または、メールでsupport@trustss.co.jpあてにお送りください。
ご購入前の技術的質問も無償で対応します。サポート受付ページからお願いします。