当前位置:网站首页>PDF转Word有那么难吗?做一个文件转换器,都解决了
PDF转Word有那么难吗?做一个文件转换器,都解决了
2022-08-01 22:54:00 【InfoQ】
前言:

一、对KML文件的经纬度操作(想要看PDF转换直接跳到PDF转换位置)
1.1读取KML文件的经纬度

1.1提取过程,不导出TXT文档

1.2提取过程,导出TXT文档


1.2 读取TXT文件生成KML文件
1.1读取过程使用文件

1.2读取过程使用输入框


1.3注意事项
二、对PDF文件的格式转换
1.1PDF转Word文档


1.2PDF转EXCEl和HTML

1.3PDF转图片

1.4注意事项
三、小工具界面设计


四、代码逻辑
using PdfiumViewer;
using Spire.Pdf;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FileConversion
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string KmlPath="";
public string txtPath = "";
public string PDFPath = "";
public string PDFPhotoPath = "";
public bool flag = false;
private void button6_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "KML文件(*.kml)|*.kml|所有文件|*.*";
if (openFile.ShowDialog() != DialogResult.OK)//打开文件是否点击了取消
return ;
KmlPath = openFile.FileName;
textBox1.Text = KmlPath;
}
private void button7_Click(object sender, EventArgs e)
{
DataExtract data = new DataExtract();
if(KmlPath.Equals("")==true)
{
MessageBox.Show("选择文件之后才能导出");
}
else
{
if (checkBox1.Checked == true && checkBox2.Checked == false|| checkBox1.Checked == true && checkBox2.Checked == true)
{
List<string> list = data.MapConfigs(KmlPath);
string localFilePath = "";//文件路径
SaveFileDialog save = new SaveFileDialog();
save.Filter = "Txt(*.txt)|*.txt"; //设置文件类型
save.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录
if (save.ShowDialog() == DialogResult.OK)//点了保存按钮进入
{
localFilePath = save.FileName.ToString(); //获得文件路径
string fileName = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名,不带路径
//FileStream file = new FileStream(localFilePath, FileMode.Create);
foreach (string kml in list)
{
textBox2.Text += kml + "\r\n";
File.AppendAllText(localFilePath, kml+"\r\n");
}
}
}
else if (checkBox1.Checked == false && checkBox2.Checked == true)
{
List<string> list = data.MapConfigs(KmlPath);
foreach (string kml in list)
{
textBox2.Text += kml + "\r\n";
}
}
else
{
MessageBox.Show("选择你需要的项");
}
}
}
private void button1_Click(object sender, EventArgs e)
{
panel3.Visible = true;
panel4.Visible = false;
panel5.Visible = false;
}
private void button9_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "Txt(*.txt)|*.txt";
if (openFile.ShowDialog() != DialogResult.OK)//打开文件是否点击了取消
return;
txtPath = openFile.FileName;
textBox4.Text = txtPath;
}
private void button8_Click(object sender, EventArgs e)
{
AddKmlFile addKml = new AddKmlFile();
List<string> list = new List<string>();
if (txtPath.Equals("") == true && richTextBox1.Text.Equals("") == true)
{
MessageBox.Show("不能生成空KMl,导入txt文件或者在文本框粘贴");
}
else if(txtPath.Equals("")==false&& richTextBox1.Text.Equals("") == true)
{
list.Clear();
string[] stringlines = File.ReadAllLines(txtPath, Encoding.Default);
for (int i = 0; i < stringlines.Length; i++)
{
list.Add(stringlines[i]);
}
addKml.addKmlFile(list);
}
else
{
string str = "";
list.Clear();
TextReader read = new System.IO.StringReader(richTextBox1.Text);
str= read.ReadLine();
while(str!=null)
{
list.Add(str);
str = read.ReadLine();
}
addKml.addKmlFile(list);
}
}
private void Form1_Load(object sender, EventArgs e)
{
panel3.Visible = true;
panel4.Visible = false;
panel5.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
panel3.Visible = false;
panel4.Visible = true;
panel5.Visible = false;
}
private void button10_Click(object sender, EventArgs e)
{
Spire.Pdf.PdfDocument pdf = new Spire.Pdf.PdfDocument();
if(textBox3.Text.Equals("")!=true&&(radioButton1.Checked!=false|| radioButton2.Checked != false || radioButton3.Checked != false) )
{
if (radioButton1.Checked)
{
try
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "DOC(*.doc)|*.doc"; //设置文件类型
save.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录
save.FileName = "请输入你要保存的文件名";
if (save.ShowDialog() == DialogResult.OK)//点了保存按钮进入
{
label6.Text = "转换中请耐心等待.....";
pdf.LoadFromFile(PDFPath);
pdf.SaveToFile(save.FileName.ToString(), FileFormat.DOC);//生成word的物理路径
label6.Text = "转换成功";
}
}
catch
{
label6.Text = "转换失败";
}
}
else if(radioButton2.Checked)
{
try
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "XlS(*.xlsx)|*.xlsx"; //设置文件类型
save.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录
if (save.ShowDialog() == DialogResult.OK)//点了保存按钮进入
{
label6.Text = "转换中请耐心等待.....";
pdf.LoadFromFile(PDFPath);
pdf.SaveToFile(save.FileName.ToString(), FileFormat.XLSX);//生成word的物理路径
label6.Text = "转换成功";
}
}
catch
{
label6.Text = "转换失败";
}
}
else if(radioButton3.Checked)
{
try
{
SaveFileDialog save = new SaveFileDialog();
save.Filter = "HTML(*.html)|*.html"; //设置文件类型
save.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录
if (save.ShowDialog() == DialogResult.OK)//点了保存按钮进入
{
label6.Text = "转换中请耐心等待.....";
pdf.LoadFromFile(PDFPath);
pdf.SaveToFile(save.FileName.ToString(), FileFormat.HTML);//生成word的物理路径
label6.Text = "转换成功";
}
}
catch
{
label6.Text = "转换失败";
}
}
}
else
{
MessageBox.Show("选择路径和转换方式之后才能生成文件");
}
}
private void button11_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "PDF(*.pdf)|*.pdf";
if (openFile.ShowDialog() != DialogResult.OK)//打开文件是否点击了取消
return;
PDFPath = openFile.FileName;
textBox3.Text = PDFPath;
}
public static void PDFConvertTo(string inFilePath, string outFilePath, ImageFormat format)
{
outFilePath = $"{outFilePath}\\{DateTime.Now.ToString("yyyyMMdd-HHmmss")}";
using (var document = PdfiumViewer.PdfDocument.Load(inFilePath))
{
var pageCount = document.PageCount;
for (int i = 0; i < pageCount; i++)
{
string outFile = $"{outFilePath}({i + 1}).{format}";
var dpi = 300;
using (var image = document.Render(i, dpi, dpi, PdfRenderFlags.CorrectFromDpi))
{
var encoder = ImageCodecInfo.GetImageEncoders().First(c => c.FormatID == format.Guid);
var encParams = new EncoderParameters(1);
encParams.Param[0] = new EncoderParameter(
System.Drawing.Imaging.Encoder.Quality, 10L);
image.Save(outFile, encoder, encParams);
}
}
}
}
private void button5_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "PDF(*.pdf)|*.pdf";
if (openFile.ShowDialog() != DialogResult.OK)//打开文件是否点击了取消
return;
PDFPhotoPath = openFile.FileName;
textBox5.Text = PDFPhotoPath;
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox5.Text.Equals("") != true &&( radioButton5.Checked != false || radioButton6.Checked != false || radioButton7.Checked != false || radioButton8.Checked != false || radioButton9.Checked != false || radioButton10.Checked != false || radioButton11.Checked != false || radioButton12.Checked != false))
{
try
{
SaveFileDialog save = new SaveFileDialog();
save.RestoreDirectory = true; //保存对话框是否记忆上次打开的目录
save.FileName = "选择你需要保存的位置";
if (save.ShowDialog() == DialogResult.OK)//点了保存按钮进入
{
label7.Text = "转换中请耐心等待.....";
if(radioButton5.Checked==true)
{
PDFConvertTo(PDFPhotoPath, save.FileName.Substring(0, save.FileName.LastIndexOf("\\")), ImageFormat.Gif);
}
else if(radioButton6.Checked==true)
{
PDFConvertTo(PDFPhotoPath, save.FileName.Substring(0, save.FileName.LastIndexOf("\\")), ImageFormat.Bmp);
}
else if (radioButton7.Checked == true)
{
PDFConvertTo(PDFPhotoPath, save.FileName.Substring(0, save.FileName.LastIndexOf("\\")), ImageFormat.Jpeg);
}
else if (radioButton8.Checked == true)
{
PDFConvertTo(PDFPhotoPath, save.FileName.Substring(0, save.FileName.LastIndexOf("\\")), ImageFormat.Png);
}
else if (radioButton9.Checked == true)
{
PDFConvertTo(PDFPhotoPath, save.FileName.Substring(0, save.FileName.LastIndexOf("\\")), ImageFormat.Exif);
}
else if (radioButton10.Checked == true)
{
PDFConvertTo(PDFPhotoPath, save.FileName.Substring(0, save.FileName.LastIndexOf("\\")), ImageFormat.Wmf);
}
else if (radioButton11.Checked == true)
{
PDFConvertTo(PDFPhotoPath, save.FileName.Substring(0, save.FileName.LastIndexOf("\\")), ImageFormat.Icon);
}
else if (radioButton12.Checked == true)
{
PDFConvertTo(PDFPhotoPath, save.FileName.Substring(0, save.FileName.LastIndexOf("\\")), ImageFormat.Tiff);
}
label7.Text = "转换成功";
}
}
catch
{
label6.Text = "转换失败";
}
}
else
{
MessageBox.Show("选择路径和转换格式之后才能生成文件");
}
}
private void button3_Click_1(object sender, EventArgs e)
{
panel3.Visible = false;
panel4.Visible = false;
panel5.Visible = true;
}
}
}
五、软件小工具下载安装

总结

边栏推荐
猜你喜欢
随机推荐
excel clear format
杭电多校3 1012. Two Permutations dp*
Deep learning Course2 first week Practical aspects of Deep Learning exercises
联邦学习入门
解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”
JS prototype hasOwnProperty in Add method Prototype end point Inherit Override parent class method
excel split text into different rows
自建 Prometheus 采集腾讯云容器服务监控数据最佳实践
number of solutions to solve a multivariate multi-degree equation
Ten years after graduation, financial freedom: those things that are more important than hard work, no one will ever teach you
关于ETL的两种架构(ETL架构和ELT架构)
数据分析04
隔离和降级
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
Advanced Algebra_Proof_The algebraic multiplicity of any eigenvalue of a matrix is greater than or equal to its geometric multiplicity
小程序中的多表联合查询
Graph Theory - Strongly Connected Component Condensation + Topological Sort
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
46.全排列
AQS