6 ドキュメントの取得

この章の内容は、次のとおりです。

6.1 ドキュメントの取得について

Imaging APIには、DocumentContentServiceでドキュメントを取得するための操作がいくつか用意されています。すべての取得方法は一意のドキュメントIDを入力として受け付け、ドキュメントのコンテンツをjavax.activation.DataHandlerとして戻します。

DataHandlerの結果を処理するとき、コール側コードは、DataHandlerをサーバーへのオープン・ストリームの周りにあるラッパーであると仮定し、DataHandler.writeToメソッドを使用して、このコンテンツをファイルなどの出力ストリームに維持するか、またはDataHandler.getInputStreamメソッドを使用して、入力ストリームを直接読み込むことにより、即座に結果を処理します。入力ストリームに直接アクセスする場合、ストリームの読込みが完了したら、忘れずにコール側でストリームをクローズします。

ネイティブ・フォーマットのオリジナル・ドキュメントでレンダリング操作を実行するときの取得方法の中には、レンダリング・プロセスをコントロールするための追加パラメータを受け付けるものもあります。この章の最後にある例6-1に、ここで説明するトピックのサンプル・コードを示します。

6.2 オリジナル・ドキュメントの取得

元のドキュメントはDocumentContentService.retrieve操作により取得されます。この操作は単純な取得メソッドで、引数としてドキュメントIDのみをとります。DataHandlerとして、ドキュメントのバイナリ・コンテンツが返されます。この場合、Imagingレンダリング・エンジンはドキュメントに対して何の処理も行いません。ただし、関連付けられているセキュアな注釈がドキュメントにあり、コール側ユーザーがこうした注釈を削除する権限を持っていない場合は、この操作により例外が返されます。

6.3 注釈付きドキュメントのレンディションの取得

ドキュメントは、関連付けられた注釈とともにDocumentContentService.retrieveRendition操作によってレンダリングできます。この操作はdocumentIdや、注釈を含めるかどうかを示すフラグなど、多くの引数を受け入れます。また、レンダリング対象とするページ・セットを指定するパラメータも含みます。このパラメータに複数のページを指定すると、それらのページのみを含む単一のマルチページ・ドキュメントになります。このパラメータにNULLまたはページ0を渡すと、ドキュメント全体がレンダリングされます。このメソッドに対するパラメータの詳細は、retrieveRenditionのJavadocを参照してください。

retrieveRenditionメソッドは、レンディション・クラス・インタフェースを戻します。ドキュメントのコンテンツを含むDataHandlerには、Rendition.getContent()メソッドからアクセスできます。

6.4 ドキュメントからページを個別に取得

retrieveRenditionメソッドは、ドキュメントの複数ページにわたるレンディションを1つ戻すために使用されますが、個々のページを取得するにはDocumentContentService.retrievePageを使用します。このメソッドでは、RenderOptionsクラス・インスタンスを受け入れて、どのようにページをレンダリングするかをコントロールできます。RenderOptionsには、ページの回転、ページの拡大縮小、fitモード、出力形式などを指定するためのオプションが用意されています。

retrievePageメソッドは、RenderPageのリストを含むRenderResultクラス・インスタンスを戻します。ページのDataHandlerコンテンツには、RenderPage.getPageData()からアクセスできます。

6.5 ドキュメント取得の例

例6-1は、このセクションで説明した基本概念を示しています。

例6-1 ドキュメント取得の例

package devguidesamples;
 
import java.io.FileOutputStream;
 
import java.io.IOException;
import java.io.InputStream;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
 
import javax.activation.DataHandler;
 
import oracle.imaging.BasicUserToken;
import oracle.imaging.DocumentContentService;
import oracle.imaging.DocumentService;
import oracle.imaging.ImagingException;
import oracle.imaging.NameId;
import oracle.imaging.RenderOptions;
import oracle.imaging.RenderPage;
import oracle.imaging.RenderResult;
import oracle.imaging.Rendition;
import oracle.imaging.Search;
import oracle.imaging.SearchArgument;
import oracle.imaging.SearchService;
import oracle.imaging.SearchValue;
import oracle.imaging.ServicesFactory;
import oracle.imaging.UserToken;
 
public class RetrieveDocumentSample {
   public static void main(String[] args)
      throws IOException {
      try { // try-catch
         UserToken credentials = new BasicUserToken("ipmuser", "ipmuserpwd");
         ServicesFactory servicesFactory =
            ServicesFactory.login(credentials, Locale.US, "http://ipmhost:16000/imaging/ws");
 
         try { // try-finally to ensure logout
            SearchService searchService = servicesFactory.getSearchService();
            DocumentContentService docContentService = 
                             servicesFactory.getDocumentContentService();
 
            // The find the document with invoice number 1234 using the Invoices search
            List<SearchArgument> searchArguments = new ArrayList<SearchArgument>();
            SearchValue searchValue = new SearchValue(SearchValue.Type.NUMBER, 1234);
            SearchArgument searchArgument = new SearchArgument("Invoice Number", searchValue);
            searchArgument.setOperatorValue(Search.Operator.EQUAL);
            searchArguments.add(searchArgument);
            Search.ResultSet resultSet =
               searchService.executeSavedSearch(new NameId("Invoices"), searchArguments);
            if (resultSet.getTotalResults() == 0) {
               System.out.println("Document not found");
            }
            String documentId = resultSet.getResults().get(0).getDocumentId();
            String documentName = resultSet.getResults().get(0).getDocument().getName();
 
            DataHandler fileData = null;
            FileOutputStream outputStream = null;
            
            // retrieve original native document content.
            fileData = docContentService.retrieve(documentId);
            outputStream = new FileOutputStream(documentName);
            fileData.writeTo(outputStream);
            outputStream.close();
            
            // Retrieve a document rendition with annotations
            Rendition rendition = docContentService.retrieveRendition(documentId, 
                                                     true, 
                                                     true, 
                                                     RenderOptions.RenditionOutput.ORIGINALORTIFF, 
                                                     null);
            
            fileData = rendition.getContent();
            outputStream = new FileOutputStream(documentName);
            fileData.writeTo(outputStream);
            outputStream.close();  
            
            //Render a specific page to JPEG format.
            RenderOptions renderOptions = new RenderOptions();
            renderOptions.setPageNumber(2);
            renderOptions.setFormat(RenderOptions.OutputFormat.JPEG);
            RenderResult result = docContentService.retrievePage(documentId, renderOptions); 
            RenderPage page = result.getPages()[0];
            
            fileData = page.getPageData();
            outputStream = new FileOutputStream(documentName);
            fileData.writeTo(outputStream);
            outputStream.close();  
         }
         finally {
            if (servicesFactory != null) {
               servicesFactory.logout();
            }
         }
      }
      catch (ImagingException e) {
         System.out.println(e.getMessage());
      }
   }
}