liuaoran 2 years ago
parent
commit
1f0582a03e

+ 9 - 0
PDF Office/ViewModels/EditTools/Background/BackgroundCreateFileContentViewModel.cs

@@ -9,6 +9,7 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows;
 
 namespace PDF_Office.ViewModels.EditTools.Background
 {
@@ -90,6 +91,14 @@ namespace PDF_Office.ViewModels.EditTools.Background
             set => isFirstEnter = value;
         }
 
+        private Visibility _createModFileVisible =  Visibility.Collapsed;
+        public Visibility CreateModFileVisible
+        {
+            get => _createModFileVisible;
+            set=>_createModFileVisible= value;
+        }
+        
+
         public DelegateCommand OpenFileCommand { get; set; }
 
 

+ 83 - 2
PDF Office/ViewModels/EditTools/Background/BackgroundDocumentContentViewModel.cs

@@ -1,5 +1,8 @@
-using ComPDFKitViewer.PdfViewer;
+using ComPDFKit.PDFDocument;
+using ComPDFKit.PDFPage;
+using ComPDFKitViewer.PdfViewer;
 using PDF_Office.EventAggregators;
+using PDF_Office.Helper;
 using PDF_Office.Model;
 using PDF_Office.Model.EditTools.Background;
 using Prism.Commands;
@@ -8,8 +11,12 @@ using Prism.Mvvm;
 using Prism.Regions;
 using System;
 using System.Collections.Generic;
+using System.Drawing;
 using System.Linq;
+using System.Threading.Tasks;
 using System.Windows;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
 
 namespace PDF_Office.ViewModels.EditTools.Background
 {
@@ -17,15 +24,88 @@ namespace PDF_Office.ViewModels.EditTools.Background
     {
         public IEventAggregator eventAggregator;
         public CPDFViewer PDFViewer;
+        public CPDFDocument document;
+        public CPDFBackground background;
+
+
+        private ImageSource _imageSource = null;
+        public ImageSource ImageSource
+        {
+            get { return _imageSource; }
+            set
+            {
+                SetProperty(ref _imageSource, value);
+            }
+        }
+
+
         public BackgroundDocumentContentViewModel(IEventAggregator  eventAggregator)
         {
             this.eventAggregator = eventAggregator;
             eventAggregator.GetEvent<SetBackgroundEvent>().Subscribe(SetBackground);
         }
 
+        public async void AwaitRenderBitmap(CPDFDocument doc)
+        {
+            await RenderBitmap(doc);
+        }
+
         public void SetBackground( BackgroundInfo backgroundInfo)
         {
+            CreateBackground();
+            document.PdfToImage("0-1", "D:\\");
+            document.WriteToFilePath("D:\\sb22222.pdf");
+            AwaitRenderBitmap(document);
+        }
+
+        public void CreateBackground()
+        {
+            byte[] color = {0,255, 255};
+            background = document.GetBackground();
+            background.SetBackgroundType(C_Background_Type.BG_TYPE_COLOR);
+            background.SetColor(color);
+            background.SetHorizalign(C_Background_Horizalign.BG_HORIZALIGN_CENTER);
+            background.SetVertalign(C_Background_Vertalign.BG_VERTALIGN_CENTER);
+            background.SetOpacity(50);
+            background.SetPages("0");
+            background.SetRotation(0);
+            background.SetScale(1);
+            background.SetXOffset(0);
+            background.SetYOffset(0);
+
+            background.Update();
+            background.Release();
+        }
 
+        public static BitmapSource ToBitmapSource(System.Drawing.Bitmap image)
+        {
+            IntPtr ptr = image.GetHbitmap();//obtain the Hbitmap
+            BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap
+            (
+                ptr,
+                IntPtr.Zero,
+                Int32Rect.Empty,
+                System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
+            );
+            return bs;
+        }
+
+
+        public async Task RenderBitmap(CPDFDocument doc)
+        {
+
+            CPDFPage page = doc.PageAtIndex(0, true);
+
+            byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
+            Bitmap bitmap = await ToolMethod.RenderPageBitmap(document, (int)(page.PageSize.Width * 1.4), (int)(page.PageSize.Height * 1.4), 0, true, true);
+            //await Task.Run(delegate
+            //{
+            //    page.RenderPageBitmap(0, 0, (int)page.PageSize.Width, (int)page.PageSize.Height, 0xffffffff, bmp_data, 1);
+            //});
+            //PixelFormat fmt = PixelFormats.Bgra32;
+            //BitmapSource bps = BitmapSource.Create((int)page.PageSize.Width, (int)page.PageSize.Height, 96.0, 96.0, fmt, null, bmp_data, ((int)page.PageSize.Width * fmt.BitsPerPixel + 7) / 8);
+            ImageSource = ToBitmapSource(bitmap);
+            doc.ReleasePages();
         }
 
         public bool IsNavigationTarget(NavigationContext navigationContext)
@@ -40,7 +120,8 @@ namespace PDF_Office.ViewModels.EditTools.Background
         public void OnNavigatedTo(NavigationContext navigationContext)
         {
             navigationContext.Parameters.TryGetValue<CPDFViewer>(ParameterNames.PDFViewer, out PDFViewer);
-
+            document = PDFViewer.Document;
+            AwaitRenderBitmap(PDFViewer.Document);
         }
     }
 }

+ 11 - 0
PDF Office/ViewModels/EditTools/Watermark/WatermarkDocumentContentViewModel.cs

@@ -50,8 +50,13 @@ namespace PDF_Office.ViewModels.EditTools.Watermark
         public void SetWatermark(WatermarkInfo watermarkInfo)
         {
             CreateWatermark(watermarkInfo);
+<<<<<<< Updated upstream
             document.PdfToImage("1", "C:\\Users\\kdanmobile\\Desktop");
             document.WriteToFilePath("C:\\Users\\kdanmobile\\Desktop\\sb22222.pdf");
+=======
+            document.PdfToImage("0-1", "D:\\");
+            document.WriteToFilePath("D:\\sb22222.pdf");
+>>>>>>> Stashed changes
             AwaitRenderBitmap(document);
         }
 
@@ -100,6 +105,8 @@ namespace PDF_Office.ViewModels.EditTools.Watermark
                 Int32Rect.Empty,
                 System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()
             );
+            ;
+
             return bs;
         }
 
@@ -110,8 +117,12 @@ namespace PDF_Office.ViewModels.EditTools.Watermark
             byte[] bmp_data = new byte[(int)page.PageSize.Width * (int)page.PageSize.Height * 4];
             Bitmap bitmap = await ToolMethod.RenderPageBitmap(document, (int)(page.PageSize.Width * 1 * 1.4), (int)(page.PageSize.Height * 1 * 1.4), 0, true, true);
             ImageSource = ToBitmapSource(bitmap);
+<<<<<<< Updated upstream
             doc.ReleasePages();
 
+=======
+            MessageBox.Show(ImageSource.ToString());
+>>>>>>> Stashed changes
 
         }
         public async void AwaitRenderBitmap(CPDFDocument doc)

+ 4 - 3
PDF Office/Views/EditTools/Background/BackgroundCreateColorContent.xaml

@@ -4,7 +4,7 @@
              xmlns:prism="http://prismlibrary.com/"             
              prism:ViewModelLocator.AutoWireViewModel="True"
                xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:cus="clr-namespace:PDF_Office.CustomControl" xmlns:background="clr-namespace:PDF_Office.ViewModels.EditTools.Background" d:DataContext="{d:DesignInstance Type=background:BackgroundCreateColorContentViewModel}"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:cus="clr-namespace:PDF_Office.CustomControl" xmlns:background="clr-namespace:PDF_Office.ViewModels.EditTools.Background" xmlns:CompositeControl="clr-namespace:PDF_Office.CustomControl.CompositeControl" d:DataContext="{d:DesignInstance Type=background:BackgroundCreateColorContentViewModel}"
              mc:Ignorable="d"
              d:DesignHeight="632"
              d:DesignWidth="260">
@@ -26,7 +26,7 @@
         <Grid Grid.Row="0" >
             <StackPanel Orientation="Vertical">
                 <TextBlock Text="背景颜色" Height="20" Width="48" HorizontalAlignment="Left" Margin="0,16,16,8" Foreground="#666666"> </TextBlock>
-                <StackPanel Orientation="Horizontal">
+                <!--<StackPanel Orientation="Horizontal">
                     <Button Name="RedColorBtn" Height="32" Width="32" Margin="0,0,8,0" Command="{Binding ChangeColorCommand}" CommandParameter="{Binding ElementName=RedColorBtn}">
                         <Rectangle Height="22" Width="22" Fill="Red" RadiusX="20" RadiusY="20"></Rectangle>
                     </Button>
@@ -47,7 +47,8 @@
                             </Grid>
                         </Grid>
                     </Button>
-                </StackPanel>
+                </StackPanel>-->
+                <CompositeControl:ColorContent Grid.Row="1" Margin="0,-2,0,0"  x:Name="cusColor"/>
             </StackPanel>
         </Grid>
         <Grid Grid.Row="1">

+ 13 - 11
PDF Office/Views/EditTools/Background/BackgroundCreateFileContent.xaml

@@ -18,9 +18,7 @@
     <Grid Margin="16,0,0,0">
         <Grid.RowDefinitions>
             <RowDefinition Height="48"></RowDefinition>
-            <RowDefinition Height="157"></RowDefinition>
-            <RowDefinition Height="163"></RowDefinition>
-            <RowDefinition Height="103"></RowDefinition>
+            <RowDefinition Height="423"></RowDefinition>
             <RowDefinition></RowDefinition>
         </Grid.RowDefinitions>
         <Grid Grid.Row="0">
@@ -29,7 +27,14 @@
                 <Button Height="32" Width="32" Command="{Binding OpenFileCommand}"></Button>
             </StackPanel>
         </Grid>
-        <Grid Grid.Row="1">
+        <Grid Grid.Row="1" Visibility="{Binding CreateModFileVisible}">
+            <Grid.RowDefinitions>
+                <RowDefinition Height="157"></RowDefinition>
+                <RowDefinition Height="163"></RowDefinition>
+                <RowDefinition Height="103"></RowDefinition>
+                <RowDefinition></RowDefinition>
+            </Grid.RowDefinitions>
+            <Grid Grid.Row="0">
             <StackPanel Orientation="Vertical">
                 <TextBlock Text="外观" Margin="0,16,0,8" Foreground="#666666"></TextBlock>
                 <StackPanel Orientation="Horizontal">
@@ -45,7 +50,7 @@
                 <cus:CommonWritableComboBox HorizontalAlignment="Left" Width="66" Height="32" Margin="0,11,28,0" TypeSouce="{Binding RotationList, Mode=TwoWay}"  Text="100 %"></cus:CommonWritableComboBox>
             </StackPanel>
         </Grid>
-        <Grid  Grid.Row="2" Margin="0,16,0,0" VerticalAlignment="Bottom">
+        <Grid  Grid.Row="1" Margin="0,16,0,0" VerticalAlignment="Bottom">
             <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="3*"/>
                 <ColumnDefinition Width="241*"/>
@@ -84,18 +89,15 @@
                             <cus:NumericUpDown Width="66" Height="32" Margin="9,0,0,0"  Text="{Binding HorizOffsetValue, Mode=TwoWay}"></cus:NumericUpDown>
                         </StackPanel>
                     </StackPanel>
-
-                </StackPanel>
-
-
+                </StackPanel> 
             </StackPanel>
         </Grid>
-
-        <Grid Grid.Row="3" Margin="0,16,0,0" VerticalAlignment="Bottom">
+        <Grid Grid.Row="2" Margin="0,16,0,0" VerticalAlignment="Bottom">
             <StackPanel Orientation="Vertical">
                 <TextBlock Text="页面范围" FontSize="12" Foreground="#666666" Height="20" Width="48" HorizontalAlignment="Left"></TextBlock>
                 <cus:WritableComboBox  Height="32" Width="228" Margin="0,8,0,0" HorizontalAlignment="Left"></cus:WritableComboBox>
             </StackPanel>
         </Grid>
     </Grid>
+    </Grid>
 </UserControl>

+ 7 - 2
PDF Office/Views/EditTools/Background/BackgroundDocumentContent.xaml

@@ -4,11 +4,16 @@
              xmlns:prism="http://prismlibrary.com/"             
              prism:ViewModelLocator.AutoWireViewModel="True"
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
-              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
-              mc:Ignorable="d"
+              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:background="clr-namespace:PDF_Office.ViewModels.EditTools.Background" d:DataContext="{d:DesignInstance Type=background:BackgroundDocumentContentViewModel}"
+             mc:Ignorable="d"
              d:DesignHeight="720"
              d:DesignWidth="1020" >
     <Grid Height="720" Width="1020" Background="SkyBlue">
+        <Border  Margin="0,16,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" Background="#FFFFFF"  Height="679" Width="514">
+            <Viewbox>
+                <Image Name="Image"  Source="{Binding ImageSource}"></Image>
+            </Viewbox>
+        </Border>
         <Border  Height="64" Width="293" HorizontalAlignment="Center"  VerticalAlignment="Bottom"  Panel.ZIndex="1" Margin="9" Background="#323232" CornerRadius="4">
             <Grid>
                 <StackPanel Orientation="Horizontal"  VerticalAlignment="Center">