简体   繁体   中英

Set PageHight auto in xps document in C#

Can we set height auto in xps document in C#? If I set height and the contains is more then that then it gives me blank page.

My code for that template is below

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:xrd="clr-namespace:CodeReason.Reports.Document;assembly=CodeReason.Reports" 
          PageWidth="8cm" PageHeight="40cm" >
<xrd:ReportProperties>
    <xrd:ReportProperties.ReportName>ProductsReport</xrd:ReportProperties.ReportName>
    <xrd:ReportProperties.ReportTitle>NORTHGATE MARKET</xrd:ReportProperties.ReportTitle>
</xrd:ReportProperties>
<Section Padding="5,5,5,5" FontSize="12" FontFamily="Courier New">
    <Paragraph FontSize="15" FontFamily="Arial Rounded MT" TextAlignment="Center" FontWeight="Bold">
        <xrd:InlineContextValue PropertyName="ReportTitle" />

    </Paragraph>
    <Paragraph>
        <TextBlock  TextWrapping="Wrap" Width="7cm" TextAlignment="Center" HorizontalAlignment="Center" Text="BIENVENIDOS" FontSize="12"></TextBlock>
    </Paragraph>
    <Paragraph>
        <TextBlock FontFamily="Calibri" FontSize="12" Width="7cm"  TextAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap">3828 Peck Rd.<LineBreak/>El Monte, CA 91732 <LineBreak/> Ph. (626) 442-8200</TextBlock>
    </Paragraph>
    <xrd:SectionDataGroup DataGroupName="ItemList">
        <Table CellSpacing="0" >
            <Table.Resources>
                <Style x:Key="headerFooterRowStyle" TargetType="{x:Type TableRowGroup}">
                    <Setter Property="FontWeight" Value="DemiBold"/>
                    <Setter Property="FontSize" Value="12"/>
                </Style>

                <Style x:Key="dataRowStyle" TargetType="{x:Type TableRowGroup}">
                    <Setter Property="FontSize" Value="9"/>
                </Style>

                <Style TargetType="{x:Type TableCell}">
                    <Setter Property="Padding" Value="0.1cm"/>

                </Style>
            </Table.Resources>

            <Table.Columns>
                <TableColumn Width="3*" />
                <TableColumn Width="1*" />
                <TableColumn Width="*" />
            </Table.Columns>
            <TableRowGroup Style="{StaticResource headerFooterRowStyle}">
                <TableRow>

                    <TableCell>
                        <Paragraph TextAlignment="Left">
                            <Bold>Code</Bold>
                        </Paragraph>
                    </TableCell>
                    <TableCell>
                        <Paragraph TextAlignment="Right">
                            <Bold>Qty.</Bold>
                        </Paragraph>
                    </TableCell>
                    <TableCell>
                        <Paragraph TextAlignment="Right">
                            <Bold>Price</Bold>
                        </Paragraph>
                    </TableCell>
                </TableRow>
            </TableRowGroup>
            <TableRowGroup Style="{StaticResource dataRowStyle}">
                <xrd:TableRowForDataTable TableName="Products">
                    <TableCell>
                        <Paragraph TextAlignment="Left">
                            <xrd:InlineTableCellValue PropertyName="Code" />
                        </Paragraph>
                    </TableCell>
                    <TableCell>
                        <Paragraph TextAlignment="Right">
                            <xrd:InlineTableCellValue PropertyName="QTY" />
                        </Paragraph>
                    </TableCell>
                    <TableCell>
                        <Paragraph TextAlignment="Right">
                            <xrd:InlineTableCellValue PropertyName="SellPrice"/>
                        </Paragraph>
                    </TableCell>
                </xrd:TableRowForDataTable>
            </TableRowGroup>
        </Table>
    </xrd:SectionDataGroup>

    <Paragraph >
        <Grid Width="250px">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
        </Grid>
        <Separator Grid.Row="0" Width="250" Height="1px"></Separator>
        <TextBlock  HorizontalAlignment="Left" Grid.Row="0" FontSize="12" >
               Recipe Receipt Id # $RECIPE_ID
        </TextBlock>
        <TextBlock FontSize="14" HorizontalAlignment="Left" Margin="0,7px" Grid.Row="1" TextWrapping="Wrap" >
                <Bold>
                    $RECIPE_NAME
                </Bold>
        </TextBlock>
        <Separator Grid.Row="2" Width="250" Height="1px"></Separator>
        <TextBlock Grid.Row="3" TextAlignment="Justify" Margin="0,7px" TextWrapping="Wrap"> $RECIPE_DESCRIPTION</TextBlock>
        <TextBlock Grid.Row="4" MaxWidth="250" Margin="10px,0px,0px,0px" FontWeight="SemiBold" TextWrapping="Wrap">$INGREDIENTS</TextBlock>
        <TextBlock Grid.Row="5" TextAlignment="Justify" Margin="0,7px" MaxWidth="250" TextWrapping="Wrap">$DIRECTIONS</TextBlock>
        <TextBlock Grid.Row="6" TextAlignment="Justify" TextWrapping="Wrap">
          <Bold>Nutritions</Bold> - $NUTRITION
        </TextBlock>
        <TextBlock Grid.Row="7" FontSize="12" Margin="0,7px" HorizontalAlignment="Left" TextWrapping="Wrap" >
            <Bold>Preparation Time</Bold> - $PREPARATION_TIME Minute
        </TextBlock>
        <TextBlock Grid.Row="8" FontSize="12"  HorizontalAlignment="Left" TextWrapping="Wrap"  >
            <Bold>Cooking Time</Bold> - $COOKING_TIME Minute
        </TextBlock>

        <TextBlock Grid.Row="9" HorizontalAlignment="Stretch" Width="250" TextAlignment="Center" Margin="0,7px" TextWrapping="Wrap" Padding="0px,5px" VerticalAlignment="Top">
            <Bold>Scan me for $RECIPE_NAME</Bold>
        </TextBlock>
        <Image Grid.Row="10" Height="100px" Width="100px" HorizontalAlignment="Left" Margin="75px,0px"  Source="$IMAGE_SOURCE"></Image>
        <TextBlock Grid.Row="11" HorizontalAlignment="Stretch" Width="250" TextAlignment="Center" Padding="0px,7px,0px,0px" VerticalAlignment="Top">
            <Bold>Interested in knowing more?</Bold>
        </TextBlock>
        <TextBlock Grid.Row="12" HorizontalAlignment="Stretch" Width="250" TextAlignment="Center" VerticalAlignment="Top">
            <Bold>www.recipereceipt.com</Bold>
        </TextBlock>
    </Paragraph>

</Section>

and my code is like below.

 ReportDocument reportDocument = new ReportDocument();
 StreamReader reader = new StreamReader(new FileStream(@"Template\SalesReport.xaml", FileMode.Open, FileAccess.Read));
 string readerString = reader.ReadToEnd();
 reportDocument.XamlData = readerString;
 reportDocument.XamlImagePath = System.IO.Path.Combine(Environment.CurrentDirectory,                                      @"Templates\");

 reader.Close();
 ReportData reportData = new ReportData();
 DataTable table = new DataTable("Products");
 table.Columns.Add("Code", typeof (string));
 table.Columns.Add("QTY", typeof (int));
 table.Columns.Add("SellPrice", typeof (double));
 List<string> uniqueProductCode = new List<string>();
 for (int i = 1; i <= 5; i++)
 {
table.Rows.Add(new object[]
                   {
                       "Test " + i.ToString(),
                       i,
                       i*52
                   });
uniqueProductCode.Add("gdsfgdsgs");
 }
 reportData.DataTables.Add(table);
 XpsDocument xps =  reportDocument.CreateXpsDocument(reportData);
 DocumentViewer documentViewer = new DocumentViewer();
 documentViewer.Name = "documnetViewer";
 documentViewer.Document = xps.GetFixedDocumentSequence();
 documentViewer.Print();

Sachin, this is my code. I dynamically load stack panels with content from file, refresh their binding values and calculate their size. After that i make a decision about adding element on that page or go to other page. My code samples here:

    public bool ApplyVisualBlock(FrameworkElement visulaBlock)
    {
        //update binding values
        UpdateAllBindingTargets(visulaBlock);

        _contentContainer.Children.Add(visulaBlock);

        //calculate size
        visulaBlock.Measure(new Size(ContentWidth, ContentHeight));

        double visualBlockHeight = Math.Ceiling(visulaBlock.DesiredSize.Height);
        if ((visualBlockHeight + _currentContentHeight) > ContentHeight)
        {
            //if current cocntent height more than page height go to other page 
            _contentContainer.Children.Remove(visulaBlock);
            return false;
        }
        _currentContentHeight = _currentContentHeight + visualBlockHeight;
        return true;

    }



 public  void UpdateAllBindingTargets( DependencyObject obj)
    {
            FrameworkElement visualBlock = obj as FrameworkElement;
        if (visualBlock==null)
            return;
        if (visualBlock.DataContext==null)
            return;
        Object objDataContext = visualBlock.DataContext;

        IEnumerable<KeyValuePair<DependencyProperty, BindingExpression>> allElementBinding = GetAllBindings(obj);
        foreach (KeyValuePair<DependencyProperty, BindingExpression> bindingInfo in allElementBinding)
        {
            BindingOperations.ClearBinding(obj, bindingInfo.Key);

            Binding myBinding = new Binding(bindingInfo.Value.ParentBinding.Path.Path);
            myBinding.Source = objDataContext;
            visualBlock.SetBinding(bindingInfo.Key, myBinding);
            BindingOperations.GetBindingExpression(visualBlock, bindingInfo.Key).UpdateTarget();
        }
    }

    public  IEnumerable<KeyValuePair<DependencyProperty, BindingExpression>> GetAllBindings( DependencyObject obj)
    {
        var stack = new Stack<DependencyObject>();

        stack.Push(obj);

        while (stack.Count > 0)
        {
            var cur = stack.Pop();
            var lve = cur.GetLocalValueEnumerator();

            while (lve.MoveNext())
                if (BindingOperations.IsDataBound(cur, lve.Current.Property))
                {
                    KeyValuePair<DependencyProperty,BindingExpression> result=new KeyValuePair<DependencyProperty, BindingExpression>(lve.Current.Property,lve.Current.Value as BindingExpression);
                    yield return result;
                }

            int count = VisualTreeHelper.GetChildrenCount(cur);
            for (int i = 0; i < count; ++i)
            {
                var child = VisualTreeHelper.GetChild(cur, i);
                if (child is FrameworkElement)
                    stack.Push(child);
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM