PDFのメタデータを追加、削除、更新できます。
標準メタデータフィールドの作成者、タイトル、件名などのを管理できます。
さらに、カスタムメタデータフィールドを管理し、XMLコンテンツでXMPメタデータを直接変更することもできます。
Toolbox Add-onのAPIリファレンスはこちらです。(すべて英文)
C#のサンプルプロジェクトではPdftools SDK(Toolbox Add-on)ライブラリ(DLL)をNuGetから自動でダウンロードします。
CのサンプルプロジェクトにはPdftools SDK(Toolbox Add-on)ライブラリ(DLL)が含まれています。
ライセンスキー無し(無償)で試用できます。ただし、結果に「透かし」が入ります。
「透かし」の削除をご希望の場合は問い合わせページまたはメールでお問い合わせください。
License Agreement(利用許諾契約書)が含まれていますので必ず確認してください。
サンプルプロジェクトではメタデータばかりではなく、PDFの属性値も一覧表示します。
PDFの属性(PF準拠性や暗号化情報)とメタデータ(作成者、タイトル、作成日など)を一覧表示します。
// Open input document pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL(pInStream, _T("Failed to open input file \"%s\".\n"), szInPath); PtxSysCreateFILEStreamDescriptor(&descriptor, pInStream, 0); pInDoc = PtxPdf_Document_Open(&descriptor, szPassword); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInDoc, _T("Input file \"%s\" cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInPath, szErrorBuff, Ptx_GetLastError()); // Conformance TPtxPdf_Conformance conformance = PtxPdf_Document_GetConformance(pInDoc); if (conformance == 0) { GOTO_CLEANUP(szErrorBuff, Ptx_GetLastError()); } _tprintf(_T("Conformance: ")); switch (conformance) { case ePtxPdf_Conformance_Pdf10: _tprintf(_T("PDF 1.0\n")); break; case ePtxPdf_Conformance_Pdf11: _tprintf(_T("PDF 1.1\n")); break; case ePtxPdf_Conformance_Pdf12: _tprintf(_T("PDF 1.2\n")); break; case ePtxPdf_Conformance_Pdf13: _tprintf(_T("PDF 1.3\n")); break; case ePtxPdf_Conformance_Pdf14: _tprintf(_T("PDF 1.4\n")); break; case ePtxPdf_Conformance_Pdf15: _tprintf(_T("PDF 1.5\n")); break; case ePtxPdf_Conformance_Pdf16: _tprintf(_T("PDF 1.6\n")); break; case ePtxPdf_Conformance_Pdf17: _tprintf(_T("PDF 1.7\n")); break; case ePtxPdf_Conformance_Pdf20: _tprintf(_T("PDF 2.0\n")); break; case ePtxPdf_Conformance_PdfA1B: _tprintf(_T("PDF/A1-b\n")); break; case ePtxPdf_Conformance_PdfA1A: _tprintf(_T("PDF/A1-a\n")); break; case ePtxPdf_Conformance_PdfA2B: _tprintf(_T("PDF/A2-b\n")); break; case ePtxPdf_Conformance_PdfA2U: _tprintf(_T("PDF/A2-u\n")); break; case ePtxPdf_Conformance_PdfA2A: _tprintf(_T("PDF/A2-a\n")); break; case ePtxPdf_Conformance_PdfA3B: _tprintf(_T("PDF/A3-b\n")); break; case ePtxPdf_Conformance_PdfA3U: _tprintf(_T("PDF/A3-u\n")); break; case ePtxPdf_Conformance_PdfA3A: _tprintf(_T("PDF/A3-a\n")); break; } // Encryption information TPtxPdf_Permission permissions; BOOL iRet = PtxPdf_Document_GetPermissions(pInDoc, &permissions); if (iRet == FALSE) { if (Ptx_GetLastError() != ePtx_Error_Success) GOTO_CLEANUP(szErrorBuff, Ptx_GetLastError()); _tprintf(_T("Not encrypted\n")); } else { _tprintf(_T("Encryption:\n")); _tprintf(_T(" - Permissions: ")); if (permissions & ePtxPdf_Permission_Print) _tprintf(_T("Print, ")); if (permissions & ePtxPdf_Permission_Modify) _tprintf(_T("Modify, ")); if (permissions & ePtxPdf_Permission_Copy) _tprintf(_T("Copy, ")); if (permissions & ePtxPdf_Permission_Annotate) _tprintf(_T("Annotate, ")); if (permissions & ePtxPdf_Permission_FillForms) _tprintf(_T("FillForms, ")); if (permissions & ePtxPdf_Permission_SupportDisabilities) _tprintf(_T("SupportDisabilities, ")); if (permissions & ePtxPdf_Permission_Assemble) _tprintf(_T("Assemble, ")); if (permissions & ePtxPdf_Permission_DigitalPrint) _tprintf(_T("DigitalPrint, ")); _tprintf(_T("\n")); } // Get metadata of input PDF pMetadata = PtxPdf_Document_GetMetadata(pInDoc); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pMetadata, _T("Failed to get metadata. %s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); _tprintf(_T("Document information:\n")); // Get title size_t nTitle = PtxPdf_Metadata_GetTitle(pMetadata, NULL, 0); if (nTitle != 0) { TCHAR* szTitle = (TCHAR*)malloc(nTitle * sizeof(TCHAR)); if (szTitle != NULL) { PtxPdf_Metadata_GetTitle(pMetadata, szTitle, nTitle); _tprintf(_T(" - Title: %s\n"), szTitle); free(szTitle); } } // Get author size_t nAuthor = PtxPdf_Metadata_GetAuthor(pMetadata, NULL, 0); if (nAuthor != 0) { TCHAR* szAuthor = (TCHAR*)malloc(nAuthor * sizeof(TCHAR)); if (szAuthor != NULL) { PtxPdf_Metadata_GetAuthor(pMetadata, szAuthor, nAuthor); _tprintf(_T(" - Author: %s\n"), szAuthor); free(szAuthor); } } // Get creator size_t nCreator = PtxPdf_Metadata_GetCreator(pMetadata, NULL, 0); if (nCreator != 0) { TCHAR* szCreator = (TCHAR*)malloc(nCreator * sizeof(TCHAR)); if (szCreator != NULL) { PtxPdf_Metadata_GetCreator(pMetadata, szCreator, nCreator); _tprintf(_T(" - Creator: %s\n"), szCreator); free(szCreator); } } // Get producer size_t nProducer = PtxPdf_Metadata_GetProducer(pMetadata, NULL, 0); if (nProducer != 0) { TCHAR* szProducer = (TCHAR*)malloc(nProducer * sizeof(TCHAR)); if (szProducer != NULL) { PtxPdf_Metadata_GetProducer(pMetadata, szProducer, nProducer); _tprintf(_T(" - Producer: %s\n"), szProducer); free(szProducer); } } // Get subject size_t nSubject = PtxPdf_Metadata_GetSubject(pMetadata, NULL, 0); if (nSubject != 0) { TCHAR* szSubject = (TCHAR*)malloc(nSubject * sizeof(TCHAR)); if (szSubject != NULL) { PtxPdf_Metadata_GetSubject(pMetadata, szSubject, nSubject); _tprintf(_T(" - Subject: %s\n"), szSubject); free(szSubject); } } // Get keywords size_t nKeywords = PtxPdf_Metadata_GetKeywords(pMetadata, NULL, 0); if (nKeywords != 0) { TCHAR* szKeywords = (TCHAR*)malloc(nKeywords * sizeof(TCHAR)); if (szKeywords != NULL) { PtxPdf_Metadata_GetKeywords(pMetadata, szKeywords, nKeywords); _tprintf(_T(" - Keywords: %s\n"), szKeywords); free(szKeywords); } } // Get creation date if (PtxPdf_Metadata_GetCreationDate(pMetadata, &date) == TRUE) { _tprintf(_T(" - Creation Date: %02d-%02d-%d %02d:%02d:%02d%c%02d:%02d\n"), date.iYear, date.iMonth, date.iDay, date.iHour, date.iMinute, date.iSecond, date.iTZSign >= 0 ? '+' : '-', date.iTZHour, date.iTZMinute); } // Get modification date if (PtxPdf_Metadata_GetModificationDate(pMetadata, &date) == TRUE) { _tprintf(_T(" - Modification Date: %02d-%02d-%d %02d:%02d:%02d%c%02d:%02d\n"), date.iYear, date.iMonth, date.iDay, date.iHour, date.iMinute, date.iSecond, date.iTZSign >= 0 ? '+' : '-', date.iTZHour, date.iTZMinute); } // Get custom entries _tprintf(_T("Custom entries:\n")); TPtx_StringMap* pCustomEntries = PtxPdf_Metadata_GetCustomEntries(pMetadata); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pCustomEntries, _T("Failed to get custom entries. %s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); for (int i = Ptx_StringMap_GetBegin(pCustomEntries), iEnd = Ptx_StringMap_GetEnd(pCustomEntries); i != iEnd; i = Ptx_StringMap_GetNext(pCustomEntries, i)) { size_t nKeySize = Ptx_StringMap_GetKey(pCustomEntries, i, NULL, 0); TCHAR* szKey = (TCHAR*)malloc(nKeySize * sizeof(TCHAR)); nKeySize = Ptx_StringMap_GetKey(pCustomEntries, i, szKey, nKeySize); size_t nValueSize = Ptx_StringMap_GetValue(pCustomEntries, i, NULL, 0); TCHAR* szValue = (TCHAR*)malloc(nValueSize * sizeof(TCHAR)); nValueSize = Ptx_StringMap_GetValue(pCustomEntries, i, szValue, nValueSize); if (szKey && nKeySize && szValue && nValueSize) _tprintf(_T(" - %s: %s\n"), szKey, szValue); free(szKey); free(szValue); }
// Open input document using (Stream inStream = new FileStream(inPath, FileMode.Open, FileAccess.Read)) using (Document inDoc = Document.Open(inStream, password)) { // Conformance Console.WriteLine("Conformance: {0}", inDoc.Conformance.ToString()); // Encryption information Permission? permissions = inDoc.Permissions; if (!permissions.HasValue) { Console.WriteLine("Not encrypted"); } else { Console.WriteLine("Encryption:"); Console.Write(" - Permissions: "); foreach (Enum flag in Enum.GetValues(typeof(Permission))) if (permissions.Value.HasFlag(flag)) Console.Write("{0}, ", flag.ToString()); Console.WriteLine(); } // Get metadata Metadata metadata = inDoc.Metadata; Console.WriteLine("Document information:"); // Get title string title = metadata.Title; if (title != null) Console.WriteLine(" - Title: {0}", title); // Get author string author = metadata.Author; if (author != null) Console.WriteLine(" - Author: {0}", author); // Get subject string subject = metadata.Subject; if (subject != null) Console.WriteLine(" - Subject: {0}", subject); // Get keywords string keywords = metadata.Keywords; if (keywords != null) Console.WriteLine(" - Keywords: {0}", keywords); // Get creation date DateTimeOffset? creationDate = metadata.CreationDate; if (creationDate != null) Console.WriteLine(" - Creation Date: {0}", creationDate); // Get modification date DateTimeOffset? modificationDate = metadata.ModificationDate; if (modificationDate != null) Console.WriteLine(" - Modification Date: {0}", modificationDate); // Get creator string creator = metadata.Creator; if (creator != null) Console.WriteLine(" - Creator: {0}", creator); // Get producer string producer = metadata.Producer; if (producer != null) Console.WriteLine(" - Producer: {0}", producer); // Custom entries Console.WriteLine("Custom entries:"); foreach (var entry in metadata.CustomEntries) Console.WriteLine(" - {0}: {1}", entry.Key, entry.Value); }
サンプルプロジェクトではPDFドキュメントの作成者、タイトル、作成者などのメタデータを設定します。
他に、別のPDFドキュメントまたはXMPメタデータファイルのコンテンツからメタデータをコピーすることもできます。
PDFの作成者、タイトル、作成者などのメタデータを設定します。
他の手順として、別のPDFドキュメントのメタデータやXMPファイルのコンテンツを使用することもできます。
// Open input document using (Stream inStream = new FileStream(inPath, FileMode.Open, FileAccess.Read)) using (Document inDoc = Document.Open(inStream, null)) // Create output document using (Stream outStream = new FileStream(outPath, FileMode.Create, FileAccess.ReadWrite)) using (Document outDoc = Document.Create(outStream, inDoc.Conformance, null)) { // Copy document-wide data CopyDocumentData(inDoc, outDoc); // Set Metadata if (args.Length == 3) { Metadata mdata; // Add metadata from a input file using FileStream metaStream = File.OpenRead(mdatafile); if (mdatafile.EndsWith(".pdf")) { // Use the metadata of another PDF file using Document metaDoc = Document.Open(metaStream, ""); mdata = Metadata.Copy(outDoc, metaDoc.Metadata); } else { // Use the content of an XMP metadata file mdata = Metadata.Create(outDoc, metaStream); } outDoc.Metadata = mdata; } else { // Set some metadata properties Metadata metadata = outDoc.Metadata; metadata.Author = "Your Author"; metadata.Title = "Your Title"; metadata.Subject = "Your Subject"; metadata.Creator = "Your Creator"; } // Define page copy options PageCopyOptions copyOptions = new PageCopyOptions(); // Copy all pages and append to output document PageList copiedPages = PageList.Copy(outDoc, inDoc.Pages, copyOptions); outDoc.Pages.AddRange(copiedPages); }
private static void CopyDocumentData(Document inDoc, Document outDoc) { // Copy document-wide data (except metadata) // Output intent if (inDoc.OutputIntent != null) outDoc.OutputIntent = IccBasedColorSpace.Copy(outDoc, inDoc.OutputIntent); // Viewer settings outDoc.ViewerSettings = ViewerSettings.Copy(outDoc, inDoc.ViewerSettings); // Associated files (for PDF/A-3 and PDF 2.0 only) FileReferenceList outAssociatedFiles = outDoc.AssociatedFiles; foreach (FileReference inFileRef in inDoc.AssociatedFiles) outAssociatedFiles.Add(FileReference.Copy(outDoc, inFileRef)); // Plain embedded files FileReferenceList outEmbeddedFiles = outDoc.PlainEmbeddedFiles; foreach (FileReference inFileRef in inDoc.PlainEmbeddedFiles) outEmbeddedFiles.Add(FileReference.Copy(outDoc, inFileRef)); }
// Open input document pInStream = _tfopen(szInPath, _T("rb")); GOTO_CLEANUP_IF_NULL(pInStream, _T("Failed to open input file \"%s\".\n"), szInPath); PtxSysCreateFILEStreamDescriptor(&descriptor, pInStream, 0); pInDoc = PtxPdf_Document_Open(&descriptor, _T("")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInDoc, _T("Input file \"%s\" cannot be opened. %s (ErrorCode: 0x%08x).\n"), szInPath, szErrorBuff, Ptx_GetLastError()); // Create output document pOutStream = _tfopen(szOutPath, _T("wb+")); GOTO_CLEANUP_IF_NULL(pOutStream, _T("Failed to open output file \"%s\".\n"), szOutPath); PtxSysCreateFILEStreamDescriptor(&outDescriptor, pOutStream, 0); iConformance = PtxPdf_Document_GetConformance(pInDoc); pOutDoc = PtxPdf_Document_Create(&outDescriptor, &iConformance, NULL); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutDoc, _T("Output file \"%s\" cannot be created. %s (ErrorCode: 0x%08x).\n"), szOutPath, szErrorBuff, Ptx_GetLastError()); // Copy document-wide data GOTO_CLEANUP_IF_FALSE_PRINT_ERROR(copyDocumentData(pInDoc, pOutDoc), _T("Failed to copy document-wide data. %s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); // Configure copy options pCopyOptions = PtxPdf_PageCopyOptions_New(); // Copy all pages pInPageList = PtxPdf_Document_GetPages(pInDoc); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pInPageList, _T("Failed to get pages of the input document. %s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); pCopiedPages = PtxPdf_PageList_Copy(pOutDoc, pInPageList, pCopyOptions); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pCopiedPages, _T("Failed to copy pages. %s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); // Add copied pages to output pOutPageList = PtxPdf_Document_GetPages(pOutDoc); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pOutPageList, _T("Failed to get pages of the output document. %s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); GOTO_CLEANUP_IF_FALSE_PRINT_ERROR(PtxPdf_PageList_AddRange(pOutPageList, pCopiedPages), _T("Failed to add copied pages to output. %s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); if (argc == 4) { // Add metadata from a input file pMdataStream = _tfopen(szMdatafile, _T("rb")); GOTO_CLEANUP_IF_NULL(pMdataStream, _T("Failed to open metadata file \"%s\".\n"), szMdatafile); PtxSysCreateFILEStreamDescriptor(&mdataDescriptor, pMdataStream, 0); // Get file extension TCHAR* szExt = _tcsrchr(szMdatafile, '.'); _tcscpy(szExtension, szExt); if (_tcscmp(szExtension, _T(".pdf")) == 0) { // Use the metadata of another PDF file TPtxPdf_Document* pMetaDoc = PtxPdf_Document_Open(&mdataDescriptor, _T("")); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pMetaDoc, _T("Failed to open metadata file. %s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); TPtxPdf_Metadata* pMetadata = PtxPdf_Metadata_Copy(pOutDoc, PtxPdf_Document_GetMetadata(pMetaDoc)); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pMetadata, _T("Failed to copy metadata. %s (ErrorCode: 0x%08x)."), szErrorBuff, Ptx_GetLastError()); GOTO_CLEANUP_IF_FALSE_PRINT_ERROR(PtxPdf_Document_SetMetadata(pOutDoc, pMetadata), _T("Failed to set metadata. %s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); } else { // Use the content of an XMP metadata file GOTO_CLEANUP_IF_FALSE_PRINT_ERROR( PtxPdf_Document_SetMetadata(pOutDoc, PtxPdf_Metadata_Create(pOutDoc, &mdataDescriptor)), _T("Failed to set metadata. %s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); } } else { // Set some metadata properties TPtxPdf_Metadata* pMetadata = PtxPdf_Document_GetMetadata(pOutDoc); GOTO_CLEANUP_IF_NULL_PRINT_ERROR(pMetadata, _T("Failed to get metadata. %s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); GOTO_CLEANUP_IF_FALSE_PRINT_ERROR(PtxPdf_Metadata_SetAuthor(pMetadata, _T("Your Author")), _T("%s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); GOTO_CLEANUP_IF_FALSE_PRINT_ERROR(PtxPdf_Metadata_SetTitle(pMetadata, _T("Your Title")), _T("%s(ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); GOTO_CLEANUP_IF_FALSE_PRINT_ERROR(PtxPdf_Metadata_SetSubject(pMetadata, _T("Your Subject")), _T("%s(ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); GOTO_CLEANUP_IF_FALSE_PRINT_ERROR(PtxPdf_Metadata_SetCreator(pMetadata, _T("Your Creator")), _T("%s (ErrorCode: 0x%08x).\n"), szErrorBuff, Ptx_GetLastError()); }
int copyDocumentData(TPtxPdf_Document* pInDoc, TPtxPdf_Document* pOutDoc) { // Copy document-wide data (excluding metadata) TPtxPdf_FileReferenceList* pInFileRefList; TPtxPdf_FileReferenceList* pOutFileRefList; // Output intent if (PtxPdf_Document_GetOutputIntent(pInDoc) != NULL) if (PtxPdf_Document_SetOutputIntent(pOutDoc, PtxPdfContent_IccBasedColorSpace_Copy( pOutDoc, PtxPdf_Document_GetOutputIntent(pInDoc))) == FALSE) return FALSE; // Viewer settings if (PtxPdf_Document_SetViewerSettings( pOutDoc, PtxPdfNav_ViewerSettings_Copy(pOutDoc, PtxPdf_Document_GetViewerSettings(pInDoc))) == FALSE) return FALSE; // Associated files (for PDF/A-3 and PDF 2.0 only) pInFileRefList = PtxPdf_Document_GetAssociatedFiles(pInDoc); pOutFileRefList = PtxPdf_Document_GetAssociatedFiles(pOutDoc); if (pInFileRefList == NULL || pOutFileRefList == NULL) return FALSE; for (int iFileRef = 0; iFileRef < PtxPdf_FileReferenceList_GetCount(pInFileRefList); iFileRef++) if (PtxPdf_FileReferenceList_Add( pOutFileRefList, PtxPdf_FileReference_Copy(pOutDoc, PtxPdf_FileReferenceList_Get(pInFileRefList, iFileRef))) == FALSE) return FALSE; // Plain embedded files pInFileRefList = PtxPdf_Document_GetPlainEmbeddedFiles(pInDoc); pOutFileRefList = PtxPdf_Document_GetPlainEmbeddedFiles(pOutDoc); if (pInFileRefList == NULL || pOutFileRefList == NULL) return FALSE; for (int iFileRef = 0; iFileRef < PtxPdf_FileReferenceList_GetCount(pInFileRefList); iFileRef++) if (PtxPdf_FileReferenceList_Add( pOutFileRefList, PtxPdf_FileReference_Copy(pOutDoc, PtxPdf_FileReferenceList_Get(pInFileRefList, iFileRef))) == FALSE) return FALSE; return TRUE; }
質問のページからお送りいただくようお願いします。
または、メールでsupport@trustss.co.jpあてにお送りください。
ご購入前の技術的質問も無償で対応します。サポート受付ページからお願いします。