---
title: "PDFコンテンツ"
slug: "pdf-content"
abstract: "PDFコンテンツから属性値を取得するためのシンプルおよび複雑なサンプルJavaScript文書エクストラクタの例。"
---
# PDFコンテンツ

[PDFコンテンツから属性値を抽出](https://doc.sitecore.com/search/ja/users/search-user-guide/index-items/configure-indexing/configure-for-pdfs.html "Configure for PDFs")するためのサンプルのJavaScript文書エクストラクターをいくつかご用意しています。各例には[PDFのHTML構造](https://doc.sitecore.com/search/ja/users/search-user-guide/index-items/configure-indexing/configure-for-pdfs/extract-html-from-a-pdf.html "Extract HTML from a PDF")の抽出と対応するドキュメントエクストラクターが含まれています。

## サンプル1:PDFコンテンツから属性値を取得するためのシンプルなJavaScriptエクストラクター

この例は、条件付き関数を使って属性値を取得するシンプルなJavaScriptエクストラクタの作成方法を示しています。

PDFは*[https://archive.doc.sitecore.com/xp/en/legacy-docs/web-forms-for-marketers-8.0.pdf](https://archive.doc.sitecore.com/xp/en/legacy-docs/web-forms-for-marketers-8.0.pdf)*で入手可能です。簡潔にまとめたHTML構造は以下の通りです。

<head>
 <meta name="pdf:PDFVersion" content="1.6">
 <meta name="xmp:CreatorTool" content="Adobe Acrobat Standard DC 18.11.20058">
 <meta name="dc:description" content="All the official Sitecore documentation.">
 <meta name="dc:creator" content="John Doe">
 <meta name="dcterms:created" content="2018-09-13T09:53:31Z">
 <meta name="dcterms:modified" content="2018-09-13T09:53:31Z">
 <meta name="dc:format" content="application/pdf; version=1.6">
 ...
 <title>�</title>
</head>

<body>
 <div class="page">
 <p> </p>
 <p> </p>
 <p>Web Forms for Marketers 8.0 Rev: September 13, 2018 </p>
 <p> </p>
 <p> </p>
 <p>Web Forms for Marketers 8.0 </p>
 <p>All the official Sitecore documentation. </p>
 </div>
 <div class="page">
 <p> </p>
 <p>Add an ASCX control to the page In the Web Forms for Marketers module, you can convert and export a form to an
 .ascx file and then add it to your website as an ASCX control. For developers, this can make it easier to develop
 their custom form control. </p>
 <p>To add an ASCX control to the page: </p>
 <p>1. Using a text editor, in the \\layouts folder of your Sitecore installation, create a new default.aspx page and
 insert the following code: </p>
 <p>&lt;%@ Page Language="C#" AutoEventWireup="true" %&gt; </p>
 <p>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" </p>
 <p>"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; </p>
 <p>&lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; </p>
 <p>&lt;head runat="server"&gt; </p>
 <p> &lt;title&gt;Untitled Page&lt;/title&gt; </p>
 <p>&lt;/head&gt; </p>
 <p>&lt;body&gt; </p>
 <p> &lt;form id="form1" runat="server"&gt; </p>
 <p> &lt;/form&gt; </p>
 <p>&lt;/body&gt; </p>
 <p>&lt;/html&gt; </p>
 ....
 <p> </p>
 </div>
 </body>

以下は、このJavaScript PDF*から名前*、*タイプ*、*ウェブサイト*、*説明*、要 *旨*、*著者* 、*last\_modified*属性を抽出するためのドキュメントエクストラクターのサンプルです。

function extract(request, response) {
 $ = response.body;

 return {
 'name': $('div.page:eq(0)').text().trim().substring(0, 40) || 'No Name',
 'type': 'pdf',
 'website':'Sitecore Documentation',
 'description' : $('div.page:eq(0)').text().trim().substring(0, 100) || 'No Description',
	'author': $('metaname="dc:creator"').attr('content') || 'No Author',
	'last\_modified': $('metaname="dcterms:created"').attr('content') || 'No Last Modified Date'
 };
}

この関数は以下のロジックを使って属性値を取得します:

-   name\- 最初のdivタグのテキストをpageのclassでトリミングします。その後、最初の40文字のみを使用します。テキストがなければ、値を*No Name*に設定します。
-   type - 固定値( *pdf*)を使用する。
-   website - 固定値、 *Sitecore Documentation*を用いる。
-   description\- クラスpageの最初の<div>タグのテキストを、最初の100文字のみ使用します。テキストがなければ、値を*No Description*に設定します。
-   author\- 最初のmetaタグの内容値をdc:creatorのnameで使用します。テキストがなければ、値を*No Author*に設定します。
-   last\_modified\- 最初のmetaタグのテキストをdcterms:createdのnameで使用します。テキストがなければ、値を*No Last Modified Date*に設定します。

## サンプル2:PDFコンテンツから属性値を取得するための複雑なJavaScriptエクストラクター

この例は、多くのネスト関数を持つ複雑なJavaScriptエクストラクタを作成し、望む属性値を取得する方法を示しています。また、PDFがいる親ページを追跡するためのparent\_url属性の抽出方法も定義しています。

PDFは*[https://www.sitecore.com/customers/associations/us-masters-swimming](https://www.sitecore.com/customers/associations/us-masters-swimming)*で「 **ケーススタディをダウンロード**」をクリックしてご覧いただけます。簡潔にまとめたHTML構造は以下の通りです:

<head>
 <meta name="pdf:PDFVersion" content="1.4">
 <meta name="xmp:CreatorTool" content="Adobe InDesign 15.0 (Macintosh)">
 <meta name="dcterms:created" content="2020-01-28T22:16:01Z">
 <meta name="dcterms:modified" content="2020-01-28T22:16:02Z">
 ...
 <title>�</title>
</head>

<body>
 <div class="page">
 <p> </p>
 <p> </p>
 <p> </p>
 <p>Industry: Associations • Founded: 1970 • Employees: 17 </p>
 <p>Headquarters: Boca Raton, Florida, USA • usms.org </p>
 <p>....
 <p> </p>
 <div class="annotation"> <a href="https://usms.org">https://usms.org</a> </div>
 </div>
 <div class="page">
 <p> </p>
 <p> </p>
 <p>Sitecore is the global leader in experience management software that combines content management, commerce,
 and customer insights. The Sitecore® Experience Cloud™ empowers marketers </p>
 ...
 <div class="annotation"> <a href="https://sitecore.com">https://sitecore.com</a> </div>
 <div class="annotation"> <a href="https://buildabonfire.com ">https://buildabonfire.com </a> </div>
 </div>
</body>

以下は、このPDFから*id*、*type*、*last\_modified*、*名前*、*description*、*parent\_url*属性を抽出するためのドキュメントエクストラクタJavaScriptサンプルです:

function extract(request, response) {
 const translate\_re = /&(nbsp|amp|quot|lt|gt);/g;

 function decodeEntities(encodedString) {
 return encodedString.replace(translate\_re, function(match, entity) {
 return translateentity;
 }).replace(/&#(\\d+);/gi, function(match, numStr) {
 const num = parseInt(numStr, 10);
 return String.fromCharCode(num);
 });
 }

 function sanitize(text) {
 return text ? decodeEntities(String(text).trim()) : text;
 }

 $ = response.body;
 url = request.url;
 id = url.replace(/.:/&?=%/g, '\_');
 name = sanitize($('name').text());
 description = $('body').text().substring(0, 7000);

 $p = request.context.parent.response.body;
 if (name.length <= 4 && $p) {
 name = $p('name').text();
 }

 parentUrl = request.context.parent.request.url;
 last\_modified = request.context.parent.documents0.data.last\_modified;

 return {
 'id': id,
 'type': "pdf",
 'parent\_url': parentUrl
 'last\_modified': last\_modified,
 'name': name, //
 'description': description,

 };
}

この関数は以下のロジックを使って属性値を取得します:

-   id - URLの特殊文字をアンダースコア(\_)に置き換える。
-   type - 固定値( *pdf*)を使用する。
-   parent\_url - 親コンテキスト内で(request.context.parent) request オブジェクトにアクセスする。次に、 url パラメータにアクセスする。
-   last\_modified\- 親コンテキスト(request.context.parent)内で最初のドキュメント配列(documents0)にアクセスする。次に、URLの*last\_modified*属性*のデータオブジェクトにアクセスする*。
-   name - name HTML要素または親文書の name HTML要素のいずれかを使用する。以下のように:
    -   まず、 <name> HTML要素のテキストを消毒してください。
    -   次に、サニティ化された名前が短すぎるかどうかを確認するために、長さが4文字以下かを確認します。
    -   名前が短すぎて、かつ親文書に定義された本文($p)がある場合は、親文書の name タグを使いましょう。
-   description - <body> HTML要素のテキストをサニテリングし、最初の7000文字に制限する。