Adobe Flex 3 ヘルプ

複数ページ出力のプリント

次の条件が満たされる場合、適切にフォーマットされた複数ページ出力をプリントすることができます。

  • 各コントロールが、単一のプリントページ以内のサイズに収まる場合。こうしたジョブは、固定長フィールドを持つフォームをプリントするときなどによく発生します。
  • プリント出力に、単一ページにはプリントできない長さの PrintDataGrid コントロールが含まれる場合。特に、コントロールの高さがデータによって異なる場合。この種の出力の良い例としては、顧客の注文書が挙げられます。注文書には、最初に顧客情報、次に行数が注文書によって異なる注文品目、最後に合計情報が記載されます。

長さが既知の複数ページ出力のプリント

複数ページに及ぶ文書の各コンポーネントの長さが分かっている場合は、次のように、プリントするページごとにプリントレイアウトコンポーネントを作成し、各レイアウトページを個別の addObject() メソッドで指定することができます。

printJob.addObject(introPrintView, "ShowAll");
printJob.addObject(finDetailPrintView, "ShowAll");
printJob.addObject(hrDetailPrintView, "ShowAll");
printJob.addObject(summaryPrintView, "ShowAll");

複数ページのグリッドに対する PrintDataGrid コントロールの使用

複数行の DataGrid コントロールがアプリケーション内で 1 つの画面に収まらない場合、通常は、すべてのデータを表示するために使用できるスクロールバーが表示されます。DataGrid コントロールをプリントするときは、出力が画面の表示と同一になります。つまり、DataGrid コントロールに、画面に表示されない行や列がある場合、それらの行や列はプリントされません。DataGrid コントロールを、高さが指定されていない(または高さが大きい)PrintDataGrid コントロールに置き換える場合、すべての行がプリントされますが、HTML 印刷結果で一般的に見られるように、一部の行が 1 ページの最下部に部分的に印刷され、他の部分が別のページの最上部に印刷される場合があります。

これらの問題は、PrintDataGrid コントロールの次の機能を使用して解決できます。これらの機能を使用すると、複数のページにまたがるデータを含むグリッドを、ページ間で行を分割せずに正しくプリントできます。

sizeToPage プロパティ 

プリントされたデータグリッドに、完全な行のみが含まれるようにします。



nextPage() メソッド 

プリント可能な次のデータページを取得します。



validNextPage プロパティ 

データをプリントするために追加のページが必要な場合は true です。



sizeToPage 属性を使用したページのフォーマット

PrintDataGrid ページは、コントロールの現在のビューに表示される行で構成されます。例えば、PrintDataGrid コントロールの高さが 130 ピクセルだとします。各行とヘッダーの高さの合計は 30 ピクセルで、コントロールのデータプロバイダは 10 行のデータを提供します。この場合、プリントされる PrintDataGrid ページには 3 つの完全な行とヘッダーが含まれることになります。sizeToPage プロパティでは、4 番目の行を部分的に含めるかどうかを指定します。

sizeToPage プロパティのデフォルトは true です。この設定では、部分的に見えている行や空白の行が PrintDataGrid コントロールにより削除され、現在のビュー内の完全な行のみが含まれるようにコントロールのサイズが変更されます。前の段落で説明したデータグリッドで、このプロパティが true の場合、DataGrid は、3 つの完全なデータ行を表示するように縮小されます。不完全な行は表示されません。属性が false の場合、このグリッドの最下部には行の一部が表示されます。

次のプロパティは、sizeToPage プロパティの影響を受けるページサイズについての情報を提供します。

プロパティ

内容

currentPageHeight

sizeToPage プロパティが true の場合のグリッドの高さを、ピクセル単位で指定します。sizeToPage プロパティが true の場合、currentPageHeight プロパティは height プロパティと等しくなります。

originalHeight

sizeToPage プロパティが false の場合のグリッドの高さを、ピクセル単位で指定します。sizeToPage プロパティが false の場合、originalHeight プロパティは height プロパティと等しくなります。

通常のアプリケーションでは、sizeToPage 属性をデフォルト(true)のまま使用し、height プロパティを使用してグリッドの高さを決定します。

1 つの PrintDataGrid コントロールページがプリントページより長い場合、 sizeToPage プロパティはページの分割には影響ありません。行を分割せずに複数のページのデータグリッドをプリントするには、nextPage() メソッドを使用してグリッドの項目を複数のビューに分割する必要があります。nextPage() メソッドと validNextPage プロパティを使用した複数ページのプリントを参照してください。

nextPage() メソッドと validNextPage プロパティを使用した複数ページのプリント

validNextPage プロパティは、現在のプリントページに収まる行を超えるデータが PrintDataGrid コントロールにある場合、true になります。このプロパティは、追加のページをフォーマットしてプリントする必要があるかどうかを判別するために使用します。

nextPage() メソッドを使用すると、PrintDataGrid コントロールの最初の行を、直前の PrintDataGrid ページの最後の行に続くデータプロバイダ行として設定することにより、データプロバイダコンテンツのページ間を移動できます。つまり、nextPage() メソッドを使用すると、グリッドの verticalScrollPosition プロパティが、グリッドの rowCount プロパティの値の分だけ増加します。

次のコードは、行が複数ページにまたがることなく、複数のページにグリッドをプリントするループを示しています。

// Queue the first page.
printJob.addObject(thePrintView);
// While there are more pages, print them.
while (thePrintView.myDataGrid.validNextPage) {
    //Put the next page of data in the view.
    thePrintView.myDataGrid.nextPage();
    //Queue the additional page.
    printJob.addObject(thePrintView);
}

例:複数ページの PrintDataGrid コントロールを使用したプリントのセクションでは、nextPage() メソッドを使用して、複数ページのデータグリッドを持つレポートをプリントする方法を説明しています。

PrintDataGrid レイアウトの更新

PrintDataGrid コントロールを使用して複数ページにまたがる単一のデータグリッドをプリントするときは、このグリッドの各ページを個別にキューに入れます。ただ単に nextPage() メソッドを使用して PrintDataGrid のページを移動するだけでなく、各ページのカスタマイズも行う場合は、各ページをプリントする前に、validateNow() メソッドを呼び出してページレイアウトを更新する必要があります。出力コンポーネントのプリントを参照してください。

例:複数ページの PrintDataGrid コントロールを使用したプリント

次の例では、データプロバイダ内で項目数を指定できるデータグリッドをプリントします。これにより、DataGrid コントロールのコンテンツを 1 ページ、2 ページ、または複数のページにプリントするように設定し、印刷結果における、サイズが異なるデータセットの影響を見ることができます。

この例は、納品書や領収書のように、グリッドの前にヘッダー情報を配置し、グリッドの後にフッター情報を配置する方法も示しています。また、プリントするページに応じて、ヘッダーとフッターを表示するか非表示にするかを選択する機能を使用しています。コードをできるだけ短くするため、この例では簡単なプレースホルダー情報のみを使用しています。

アプリケーションは、次のファイルで構成されています。

  • ユーザーにフォームを表示するアプリケーションファイル。フォームには、行数を設定する TextArea コントロールおよび Button コントロールや、「印刷」ボタンが含まれます。このファイルには、ビューを初期化し、データを取得し、ユーザーのプリント要求を処理するためのコードが含まれています。プリント出力用テンプレートとして、FormPrintView MXML コンポーネントを使用します。
  • プリント出力をフォーマットするための FormPrintView.mxml ファイル。これには 2 つの主な要素があります。
    • プリント出力テンプレートに PrintDataGrid コントロールを含め、2 つの MXML コンポーネントを使用してヘッダーとフッターのコンテンツをフォーマットします。
    • showPage() 関数は、ページのタイプに応じて、出力の特定のページに含めるテンプレートの部分を決定します。ページのタイプには、先頭、中間、最終および単一があります。複数ページ出力の先頭ページでは、showPage() 関数はフッターを非表示にし、中間ページと最終ページではヘッダーを非表示にします。単一ページの場合、ヘッダーとフッターの両方が表示されます。
  • 出力の最初と最後のコンテンツを指定する FormPrintHeader.mxml ファイルおよび formPrintFooter.mxml ファイル。アプリケーションを簡潔に保つため、ヘッダーには単一の静的 Label コントロールがあります。ヘッダーには、Quantity 列の数値の合計が表示されます。完全なアプリケーションでは、ヘッダーページに発送先住所などの情報を表示し、フッターページに発送明細を表示することもできます。

これらのファイルには、コードの目的を説明した詳しいコメントが含まれます。

複数ページプリントのアプリケーションファイル

次のコードは、複数ページプリントのアプリケーションファイルを示しています。

<?xml version="1.0"?>
<!-- printing\MultiPagePrint.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    initialize="initData();">

    <mx:Script>
    <![CDATA[
        import mx.printing.*;
        import mx.collections.ArrayCollection;
        // Import the MXML custom print view control.
        import myComponents.FormPrintView;

        // Declare variables and initialize simple variables.
        // The dgProvider ArrayCollection is the DataGrid data provider.
        // It must be bindable because you change its contents dynamically.
        [Bindable]
        public var dgProvider:ArrayCollection;
        public var footerHeight:Number = 20;
        public var prodIndex:Number;
        public var prodTotal:Number = 0;
        
        // Data initialization, called when the application initializes.
        public function initData():void {
            // Create the data provider for the DataGrid control.
            dgProvider = new ArrayCollection; 
        }
        
        // Fill the dgProvider ArrayCollection with the specified items.
        public function setdgProvider(items:int):void { 
            // First initialize the index and clear any existing data.
            prodIndex=1;
            dgProvider.removeAll();
            
            // Fill the ArrayCollection, and calculate a product total.
            // For simplicity, it increases the Index field value by
            // 1, and the Qty field by 7 for each item.
            for (var z:int=0; z<items; z++) 
            {
                var prod1:Object = {};
                prod1.Qty = prodIndex * 7;
                prod1.Index = prodIndex++;
                prodTotal += prod1.Qty; 
                dgProvider.addItem(prod1);
            }
        }

        // The function to print the output.
        public function doPrint():void {
            // Create a FlexPrintJob instance.
            var printJob:FlexPrintJob = new FlexPrintJob();
            
            // Start the print job.
            if (printJob.start()) {
                // Create a FormPrintView control 
                // as a child of the application.
                var thePrintView:FormPrintView = new FormPrintView();
                addChild(thePrintView);
                
                // Set the print view properties.
                thePrintView.width=printJob.pageWidth;
                thePrintView.height=printJob.pageHeight;
                thePrintView.prodTotal = prodTotal;
                
                // Set the data provider of the FormPrintView 
                // component's DataGrid to be the data provider of 
                // the displayed DataGrid.
                thePrintView.myDataGrid.dataProvider = 
                    myDataGrid.dataProvider;
                
                // Create a single-page image.
                thePrintView.showPage("single");
                
                // If the print image's DataGrid can hold all the  
                // data provider's rows, add the page to the print job. 
                if(!thePrintView.myDataGrid.validNextPage)
                {
                    printJob.addObject(thePrintView);
                }
                // Otherwise, the job requires multiple pages.
                else
                {
                    // Create the first page and add it to the print job.
                    thePrintView.showPage("first");
                    printJob.addObject(thePrintView);
                    thePrintView.pageNumber++;
                    
                    // Loop through the following code 
                    // until all pages are queued.
                    while(true)
                    {
                        // Move the next page of data to the top of 
                        // the PrintDataGrid.
                        thePrintView.myDataGrid.nextPage();

                        // Try creating a last page.
                        thePrintView.showPage("last");  

                        // If the page holds the remaining data, or if 
                        // the last page was completely filled by the last  
                        // grid data, queue it for printing.
                        // Test if there is data for another 
                        // PrintDataGrid page.
                        if(!thePrintView.myDataGrid.validNextPage) 
                        {
                            // This is the last page; 
                            // queue it and exit the print loop.
                            printJob.addObject(thePrintView);
                            break;
                        }
                        else
                        // This is not the last page. Queue a middle page. 
                        {
                            thePrintView.showPage("middle");
                            printJob.addObject(thePrintView);
                            thePrintView.pageNumber++;
                        }
                    }
                }
                // All pages are queued; remove the FormPrintView 
                // control to free memory.
                removeChild(thePrintView);
            }
            // Send the job to the printer.
            printJob.send();
        }
    ]]>
    </mx:Script>

    <!-- The form that appears on the user's system.-->
    <mx:Form id="myForm" width="80%">
        <mx:FormHeading label="Product Information"/>
                <mx:DataGrid id="myDataGrid" dataProvider="{dgProvider}">
                <mx:columns>
                    <mx:DataGridColumn dataField="Index"/>
                    <mx:DataGridColumn dataField="Qty"/>
                </mx:columns>
            </mx:DataGrid>
        <mx:Text width="100%" 
            text="Specify the number of lines and click Fill Grid first. 
            Then you can click Print."/>
        <mx:TextInput id="dataItems" text="35"/>
        <mx:HBox>
            <mx:Button id="setDP" 
                label="Fill Grid"
                click="setdgProvider(int(dataItems.text));"/>
            <mx:Button id="printDG" 
                label="Print" 
                click="doPrint();"/>
        </mx:HBox>
    </mx:Form>
</mx:Application>

前の例で実行する SWF ファイルは以下のとおりです。

出力コンポーネントのプリント

次のコードでは、FormPrintView.mxml カスタムコンポーネントファイルを表示します。

<?xml version="1.0"?>
<!-- printing\myComponents\FormPrintView.mxml -->
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:MyComp="myComponents.*" 
    backgroundColor="#FFFFFF"
    paddingTop="50" paddingBottom="50" paddingLeft="50">

    <mx:Script>
        <![CDATA[
            import mx.core.*
            
            // Declare and initialize the variables used in the component.
            // The application sets the actual prodTotal value.
            [Bindable]
            public var pageNumber:Number = 1;
            [Bindable]
            public var prodTotal:Number = 0;

            // Control the page contents by selectively hiding the header and
            // footer based on the page type.
            public function showPage(pageType:String):void {
                if(pageType == "first" || pageType == "middle") {
                    // Hide the footer.
                    footer.includeInLayout=false;
                    footer.visible = false;
                }
                if(pageType == "middle" || pageType == "last") {
                    // The header won't be used again; hide it.
                    header.includeInLayout=false;
                    header.visible = false;
                }
                if(pageType == "last") {
                    // Show the footer.
                    footer.includeInLayout=true;
                    footer.visible = true;
                }
                //Update the DataGrid layout to reflect the results.
                validateNow();
            }
        ]]>
    </mx:Script>

    <!-- The template for the printed page, 
        with the contents for all pages. -->
    <mx:VBox width="80%" horizontalAlign="left">
        <mx:Label text="Page {pageNumber}"/>
    </mx:VBox>
    <MyComp:FormPrintHeader id="header"/>
    
    <!-- The sizeToPage property is true by default, so the last
        page has only as many grid rows as are needed for the data. -->
    <mx:PrintDataGrid id="myDataGrid" width="60%" height="100%">
    <!-- Specify the columns to ensure that their order is correct. -->
        <mx:columns>
            <mx:DataGridColumn dataField="Index" />
            <mx:DataGridColumn dataField="Qty" />
        </mx:columns>
    </mx:PrintDataGrid>
    
    <!-- Create a FormPrintFooter control 
        and set its prodTotal variable. -->
    <MyComp:FormPrintFooter id="footer" pTotal="{prodTotal}"/>
</mx:VBox>

ヘッダーとフッターのファイル

次のコードは、FormPrintHeader.mxml ファイルを表示します。

<?xml version="1.0"?>
<!-- printing\myComponents\FormPrintHeader.mxml -->
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" 
    width="60%"
    horizontalAlign="right" >

    <mx:Label text="This is a placeholder for first page contents"/>
</mx:VBox>

次のコードは、FormPrintFooter.mxml ファイルを表示します。

<?xml version="1.0"?>
<!-- printing\myComponents\FormPrintFooter.mxml -->
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" 
    width="60%"
    horizontalAlign="right">
    
    <!-- Declare and initialize the product total variable. -->
    <mx:Script>
        <![CDATA[
            [Bindable]
            public var pTotal:Number = 0;
        ]]>
        </mx:Script>

    <mx:Label text="Product Total: {pTotal}"/>
</mx:VBox>

PrintAdvancedDataGrid コントロールの使用

PrintAdvancedDataGrid コントロールおよび PrintOLAPDataGrid コントロールは、それぞれ AdvancedDataGrid コントロールおよび OLAPDataGrid コントロールに対して、PrintDataGrid コントロールが DataGrid コントロールに対して提供するのと同じ機能を提供します。.詳細については、複数ページのグリッドに対する PrintDataGrid コントロールの使用を参照してください。

次の例では、PrintAdvancedDataGrid コントロールを使用して、AdvancedDataGrid コントロールのインスタンスをプリントします。

<?xml version="1.0"?>
<!-- printing\ADGPrint.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[
            import mx.printing.*;
            import mx.collections.ArrayCollection;
            import mx.printing.PrintAdvancedDataGrid;
                    
            include "SimpleHierarchicalData.as";

            // Create a PrintJob instance.
            private function doPrint():void {
                // Create an instance of the FlexPrintJob class.
                var printJob:FlexPrintJob = new FlexPrintJob();
                
                // Initialize the PrintAdvancedDataGrid control.
                var printADG:PrintAdvancedDataGrid = 
                    new PrintAdvancedDataGrid();
                // Exclude the PrintAdvancedDataGrid control from layout.
                printADG.includeInLayout = false;
                printADG.source = adg;

                // Add the print-specific control to the application.                
                addChild(printADG);
                
                // Start the print job.
                if (printJob.start() == false) {                
                    // User cancelled print job.
                    // Remove the print-specific control to free memory.                
                    removeChild(printADG);
                    return;
                }

                // Add the object to print. Do not scale it.
                printJob.addObject(printADG, FlexPrintJobScaleType.NONE);

                // Send the job to the printer.
                printJob.send();

                // Remove the print-specific control to free memory.                
                removeChild(printADG);
            }
        ]]>
    </mx:Script>

    <mx:VBox id="myVBox"
        width="100%" height="100%">
        <mx:AdvancedDataGrid id="adg"
            width="100%" height="100%">
            <mx:dataProvider>
                <mx:HierarchicalData source="{dpHierarchy}"/>
            </mx:dataProvider>
            <mx:columns>
                <mx:AdvancedDataGridColumn dataField="Region"/>
                <mx:AdvancedDataGridColumn dataField="Territory_Rep"
                    headerText="Territory Rep"/>
                <mx:AdvancedDataGridColumn dataField="Actual"/>
                <mx:AdvancedDataGridColumn dataField="Estimate"/>
            </mx:columns>
        </mx:AdvancedDataGrid>    

        <mx:Button id="myButton" 
            label="Print" 
            click="doPrint();"/>
    </mx:VBox>    
</mx:Application>

前の例で実行する SWF ファイルは以下のとおりです。

この例では、PrintAdvancedDataGrid.source プロパティを使用して、AdvancedDataGrid コントロールから PrintAdvancedDataGrid コントロールを初期化します。

PrintAdvancedDataGrid コントロールでは、AdvancedDataGrid コントロールをサポートするために、PrintDataGrid コントロールでは利用できない次のプロパティを追加しています。

プロパティ

内容

allowInteractions

true の場合、コントロールによる列のサイズ変更、列の並べ替え、ノードの展開または折り畳みなどを許可します。デフォルト値は false です。

displayIcons

true の場合、ナビゲーションツリー内にフォルダアイコンとリーフアイコンが表示されます。デフォルト値は true です。

source

PrintAdvancedDataGrid コントロールおよびそのすべてのプロパティを、指定した AdvancedDataGrid コントロールから初期化します。

validPreviousPage

データプロバイダには PrintAdvancedDataGrid コントロールで現在表示されている行の前にデータ行があることを示します。

 

このページに新しいコメントが追加された場合に、電子メールでの通知を希望する。 | コメントレポート