Skip to content

Commit

Permalink
Merge pull request #52 from lindexi/t/lindexi/WordPageToImages
Browse files Browse the repository at this point in the history
转换Word页面为图片
  • Loading branch information
lindexi authored Nov 25, 2021
2 parents b94e2cf + 5889443 commit 56e6ea3
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,4 @@ ModelManifest.xml
/uwp/src/Imageshack/cloundes/cloundes/Model/Appid.cs
/uwp/src/VarietyHiggstGushed/VarietyHiggstGushed/_pkginfo.txt
/uwp/control/BitStamp/BitStamp/AppxPackages/
/wpf/dotnetCampus.WPF/Lib
9 changes: 9 additions & 0 deletions wpf/WordPageToImages/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="WordPageToImages.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WordPageToImages"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions wpf/WordPageToImages/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WordPageToImages
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
10 changes: 10 additions & 0 deletions wpf/WordPageToImages/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly:ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
Binary file added wpf/WordPageToImages/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions wpf/WordPageToImages/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Window x:Class="WordPageToImages.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WordPageToImages"
mc:Ignorable="d"
Title="将Word文件每个页面单独转换为图片文件" Height="450" Width="800" Icon="Icon.png">
<Grid AllowDrop="True" Drop="Grid_OnDrop">
<Rectangle Margin="10,10,10,10" Stroke="Black" Fill="Gray"
StrokeThickness="5"
StrokeDashArray="2,2"
RadiusX="5"
RadiusY="5">
</Rectangle>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="30" Text="拖入docx文件"></TextBlock>
</Grid>
</Window>
81 changes: 81 additions & 0 deletions wpf/WordPageToImages/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using Microsoft.Office.Interop.Word;
using Path = System.IO.Path;
using Window = System.Windows.Window;

namespace WordPageToImages
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void ConvertWordPageToImages(FileInfo wordFile, DirectoryInfo outputFolder)
{
var applicationClass = new ApplicationClass();
//applicationClass.Visible = false; 默认值就是 false 值
var folder = outputFolder.FullName;

// 截图使用只读方式打开,这里传入的要求是绝对路径
Document document = applicationClass.Documents.Open(wordFile.FullName, ReadOnly: true);

var count = 0;

foreach (Microsoft.Office.Interop.Word.Window documentWindow in document.Windows)
{
var documentWindowPanes = documentWindow.Panes;
for (var index = 0; index < documentWindowPanes.Count; index++)
{
Pane documentWindowPane = documentWindowPanes[index + 1];
var pagesCount = documentWindowPane.Pages.Count;
for (int i = 0; i < pagesCount; i++)
{
var page = documentWindowPane.Pages[i + 1];
Console.WriteLine($"{page.Width};{page.Height}");
count++;
var file = Path.Combine(folder, $"{count}.png");

var bits = page.EnhMetaFileBits;
using (var ms = new MemoryStream((byte[]) (bits)))
{
var image = System.Drawing.Image.FromStream(ms);
image.Save(file);
}
//page.SaveAsPNG(file); // System.Runtime.InteropServices.COMException: '该方法无法用于对那个对象。'
}
}
}

document.Close();
applicationClass.Quit();
}

private void Grid_OnDrop(object sender, DragEventArgs e)
{
try
{
var data = (string[]?) e.Data.GetData(DataFormats.FileDrop);
if (data is not null)
{
var wordFile = data[0];
var folder = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
ConvertWordPageToImages(new FileInfo(wordFile), Directory.CreateDirectory(folder));

Process.Start("explorer", $" \"{folder}\" ");
}
}
catch (Exception exception)
{
Debug.WriteLine(exception);
}
}
}
}
19 changes: 19 additions & 0 deletions wpf/WordPageToImages/WordPageToImages.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
<Resource Include="Icon.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Interop.Microsoft.Office.Core" Version="2.8.0" />
<PackageReference Include="Microsoft.Office.Interop.Word" Version="15.0.4797.1003" />
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions wpf/WordPageToImages/WordPageToImages.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WordPageToImages", "WordPageToImages.csproj", "{4505645A-A27E-49FB-AEE2-0EABED734A60}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4505645A-A27E-49FB-AEE2-0EABED734A60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4505645A-A27E-49FB-AEE2-0EABED734A60}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4505645A-A27E-49FB-AEE2-0EABED734A60}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4505645A-A27E-49FB-AEE2-0EABED734A60}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 56e6ea3

Please sign in to comment.