C#. NET экспорт StackPanel в формате .pdf с использованием itextsharp - PullRequest
0 голосов
/ 28 января 2020

я пытаюсь экспортировать объект стека панели как .pdf. у этого объекта есть только стек изображений, который я загрузил с помощью image.source. Изображение отображается в приложении WPF, но я получаю некоторые ошибки. Мне удалось исправить несколько ошибок, но это не позволило мне использовать стековую панель в качестве Document.Paginator.

Мой XAML-код

<StackPanel Orientation="Horizontal" Background="LightBlue" Height="40" VerticalAlignment="Top">
    <Label Margin="10,0,0,0" Height="23" Name="Label1">
        Current File:
    </Label>
    <Label Margin="5,0,0,0" Height="25" Name="FileNameLabel" Width="300" />
    <Button Margin="5,0,0,0" Height="23" Name="BrowseButton" Width="75" Click="BrowseButton_Click">
        Browse
    </Button>
</StackPanel>
    <StackPanel>
    <Image Name="ImageViewer1" Height="auto" Width="auto" />
    </StackPanel>

Мой C# код:

        InitializeComponent();
        // create an instance of your XAML object (Window or UserControl)
        var window   = new Window   ();

        // create a FixedDocument and add a page of your XAML object
        var fixedDocument = new FixedDocument();
        fixedDocument.DocumentPaginator.PageSize = new Size(96 * 8.5, 96 * 11);

        PageContent pageContent = new PageContent();
        FixedPage fixedPage = new FixedPage();
        fixedPage.Children.Add(stakas);
        fixedDocument.Pages.Add(pageContent);
        ((IAddChild)pageContent).AddChild(fixedPage);


        Document doc = new Document(PageSize.LETTER, 0f, 0f, 0f, 0f);
        PdfWriter.GetInstance(doc, new System.IO.FileStream("C:\\myFile.pdf", System.IO.FileMode.Create));
        doc.Open();

       // // cycle through each page of the WPF FixedDocument
        DocumentPaginator paginator = window.DocumentPaginator;
        for (int i = 0; i < paginator.PageCount; i++)
      {
           // render the fixed document to a WPF Visual object
            Visual visual = paginator.GetPage(i).Visual;

            // create a temporary file for the bitmap image
           string targetFile = Path.GetTempFileName();

            // convert XPS file to an image
            using (System.IO.FileStream outStream = new System.IO.FileStream(targetFile, System.IO.FileMode.Create))
            {
                PngBitmapEncoder enc = new PngBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(CreateBitmapFromVisual(visual, 300, 300)));
                enc.Save(outStream);
            }

            // add the image to the iTextSharp PDF document
            using (System.IO.FileStream fs = new System.IO.FileStream(targetFile, System.IO.FileMode.Open))
            {
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(fs), System.Drawing.Imaging.ImageFormat.Png);
                png.ScalePercent(24f);
                doc.Add(png);
            }
        }
        doc.Close();
    }



    private void BrowseButton_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.InitialDirectory = "C:\\";
        dlg.Filter = "Image files (*.jpg)|*.jpg|All Files (*.*)|*.*";
        dlg.RestoreDirectory = true;

        if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string selectedFileName = dlg.FileName;
            FileNameLabel.Content = selectedFileName;
            BitmapImage bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.UriSource = new Uri(selectedFileName);
            bitmap.EndInit();
            ImageViewer1.Source = bitmap;


        }
    }

Может быть, кто-то может помочь?

...