-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from lindexi/t/lindexi/WordPageToImages
转换Word页面为图片
- Loading branch information
Showing
9 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
)] |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |