我给你提供一个最主要的入口点方法,这个是所有转换操作都会进入的一个函数,不是不想开源,只是开源要把代码什么的整理清晰,这个很累。
/// <summary>
/// 转换文件通用方法
/// </summary>
/// <param name="ListViewControl">转换文件所在的listview控件名称</param>
/// <param name="StatusLabelControl">用于表示进度状态的标签控件</param>
/// <param name="ConverType">转换的目标类型</param>
/// <param name="ExtensionString">保存文件的后缀名称</param>
public static void ConvertStart(
ListView ListViewControl,
Label StatusLabelControl,
ConvertFileType ConverType,
string ExtensionString,
bool UseSourcePath = false,
Button CallButton = null)
{
if (ListViewControl.Items.Count > 0)
{
string TempSaveDocxPathString = string.Empty;
string SelectSavePathstring = "A";
if (!UseSourcePath)
{
IEnumerable<string> ISelectSavePathstring = Util.ShowSelectDialog(
false,
true,
string.Empty,
string.Empty);//调用文件选择窗口,获取文件夹名称
SelectSavePathstring = ISelectSavePathstring.FirstOrDefault();
}
if (string.Compare(SelectSavePathstring, default, StringComparison.Ordinal) != 0 &&
SelectSavePathstring != string.Empty &&
SelectSavePathstring != null)
{
CallButton.Enabled = false;
for (int TempIndex = 0; TempIndex < ListViewControl.Items.Count; TempIndex++)
{
string ListViewFirstColumnText = ListViewControl.Items[TempIndex].Text;
string FileNameWithOutExtension = Path.GetFileNameWithoutExtension(ListViewFirstColumnText);//不带后缀的文件名
string FileNameSourcePath = Path.GetDirectoryName(ListViewFirstColumnText);//所选文件列表中文件的源目录
if (UseSourcePath)
{
SelectSavePathstring = FileNameSourcePath;
}
string ConvertedFileName = Path.Combine(
SelectSavePathstring,
$"{FileNameWithOutExtension}{ExtensionString}");
StatusLabelControl.Text = $"正在处理第{TempIndex + 1}个文件";
//根据不同类型调用不同的函数
switch (ConverType)
{
//word转pdf
case ConvertFileType.Word2Pdf:
TempSaveDocxPathString = Path.Combine(SelectSavePathstring, "TempDocx");
Directory.CreateDirectory(TempSaveDocxPathString);
object SelectedPdfCompliance = Enum.Parse(
typeof(Aspose.Words.Saving.PdfCompliance),
BodyForm.MainForm.PDFComplianceBox.SelectedItem.ToString());
Util.ConvertWord2Pdf(
ListViewFirstColumnText,
ConvertedFileName,
TempSaveDocxPathString,
(Aspose.Words.Saving.PdfCompliance)SelectedPdfCompliance,
(Aspose.Words.Settings.MsWordVersion)Enum.Parse(
typeof(Aspose.Words.Settings.MsWordVersion),
BodyForm.MainForm.Word2PDFOptimizCombox.SelectedItem.ToString()),
BodyForm.MainForm.word2pdf_need_update_fields.Checked,
(Aspose.Words.Saving.ColorMode)Enum.Parse(
typeof(Aspose.Words.Saving.ColorMode),
BodyForm.MainForm.word2pdf_colorMode.SelectedValue.ToString()),
BodyForm.MainForm.EmbedFonts.Checked,
BodyForm.MainForm.WORD2PDF_LoadPassword.Text,
BodyForm.MainForm.UseLocalOffice.Checked);
break;
//pdf转word
case ConvertFileType.Pdf2Word:
Util.ConvertPdf2Word(
ListViewFirstColumnText,
ConvertedFileName,
BodyForm.MainForm.PDF2WORD_UseBullet.Checked,
BodyForm.MainForm.PDF2WORD_UseSUbOcr.Checked,
(Aspose.Pdf.DocSaveOptions.RecognitionMode)Enum.Parse(
typeof(Aspose.Pdf.DocSaveOptions.RecognitionMode),
BodyForm.MainForm.PDF2WORD_Mode.SelectedValue.ToString()),
BodyForm.MainForm.PDF2WORD_LoadPassword.Text);
break;
//excel转pdf
case ConvertFileType.Excel2Pdf:
Util.ConvertExcel2Pdf(
ListViewFirstColumnText,
ConvertedFileName,
BodyForm.MainForm.ExportEachSheet.Checked);
break;
//word转图片
case ConvertFileType.Word2Image:
Util.ConvertWord2Image(
ListViewFirstColumnText,
ConvertedFileName,
ExtensionString,
BodyForm.MainForm.SetGrayCheckBox.Checked,
BodyForm.MainForm.PageExportCheck.Checked,
Convert.ToInt32(BodyForm.MainForm.DpiItemList.SelectedValue));
break;
//pdf转excel
case ConvertFileType.Pdf2Excel:
Util.ConvertPdf2Excel(
ListViewFirstColumnText,
ConvertedFileName,
BodyForm.MainForm.ExcelSheelMode.Checked);
break;
//批量替换word内容
case ConvertFileType.WordReplace:
if (BodyForm.MainForm.WordReplaceFindTextBox.Text != string.Empty)
{
Util.ReplaceDocumentContent(
ListViewFirstColumnText,
ConvertedFileName,
"WORD",
BodyForm.MainForm.WordReplaceFindTextBox.Text,
BodyForm.MainForm.WordReplaceTextBox.Text,
BodyForm.MainForm.IgnoreFootContent.Checked,
BodyForm.MainForm.IgnoreCase.Checked,
false,
-1,
-1,
false,
ReplaceWordFontSetting.FontName,
ReplaceWordFontSetting.FontSize,
BodyForm.MainForm.UseNewStyle.Checked);
}
break;
//批量替换Excel内容
case ConvertFileType.ExcelReplace:
Util.ReplaceDocumentContent(
ListViewFirstColumnText,
ConvertedFileName,
"EXCEL",
BodyForm.MainForm.ExcelFindTextBox.Text,
BodyForm.MainForm.ExcelReplaceBox.Text,
false,
BodyForm.MainForm.ExcelNoIgnoreCase.Checked,
BodyForm.MainForm.RemoveRows.Checked,
Convert.ToInt32(BodyForm.MainForm.ExcelStartRow.Text),
Convert.ToInt32(BodyForm.MainForm.ExcelEndRow.Text),
BodyForm.MainForm.UseRegex.Checked);
break;
// 批量图片转为ICO
case ConvertFileType.Image2Ico:
Util.ConvertImage2Ico(
ListViewFirstColumnText,
ConvertedFileName,
Convert.ToInt32(BodyForm.MainForm.IcoSizeDrop.SelectedValue));
break;
// Word批量转换为Excel
case ConvertFileType.Word2Excel:
Util.ConvertWord2Excel(ListViewFirstColumnText, ConvertedFileName);
break;
// Excel批量转换为Word
case ConvertFileType.Excel2Word:
Util.ConvertExcel2Word(ListViewFirstColumnText, ConvertedFileName);
break;
// pdf拆分
case ConvertFileType.SplitPdf:
Util.SplitPdf(
ListViewFirstColumnText,
SelectSavePathstring,
BodyForm.MainForm.SplitPdf_Range.Checked,
BodyForm.MainForm.SplitPdfRange_Start.Text.Trim(),
BodyForm.MainForm.SplitPdfRange_End.Text.Trim(),
int.Parse(BodyForm.MainForm.SplitPdf_PerPageCount.Value.ToString()),
BodyForm.MainForm.SplitPdf_ByBookMark.Checked,
BodyForm.MainForm.SplitPdf_Single.Checked);
break;
// pdf添加水印
case ConvertFileType.AddWaterMarkPdf:
Util.AddWaterMark2Pdf(
ListViewFirstColumnText,
ConvertedFileName,
BodyForm.MainForm.PdfWaterMarkText.Text,
float.Parse(BodyForm.MainForm.PdfWaterMarkOpcityNumber.Text.ToString()),
BodyForm.MainForm.PdfWaterMarkRotateDegNumber.Text,
BodyForm.MainForm.PdfWaterMarkHorSetting.SelectedIndex,
BodyForm.MainForm.PDFWaterMarkVerSetting.SelectedIndex,
PrivateFontSetting.FontColor,
PrivateFontSetting.FontName,
PrivateFontSetting.FontSize,
BodyForm.MainForm.AddWaterMarkBackGroundImageFileName,
BodyForm.MainForm.WaterMarkImageHeight.Text,
BodyForm.MainForm.WaterMarkImageWidth.Text);
break;
// pdf移除水印
case ConvertFileType.RemovePdfWaterMark:
Util.RemovePdfWaterMark(ListViewFirstColumnText, ConvertedFileName);
break;
// ppt转pdf
case ConvertFileType.PPT2PDF:
object SelectCommentPostion = Enum.Parse(
typeof(Aspose.Slides.Export.NotesPositions),
BodyForm.MainForm.ExportNotePageCombox.SelectedValue.ToString());
Util.ConvertPPT2PDF(
ListViewFirstColumnText,
ConvertedFileName,
BodyForm.MainForm.DrawBlackLine.Checked,
BodyForm.MainForm.ConvertPPT2PDFPassword.Text.Trim(),
BodyForm.MainForm.ExportHiddenPage.Checked,
(Aspose.Slides.Export.NotesPositions)SelectCommentPostion);
break;
// pdf转ppt
case ConvertFileType.PDF2PPT:
Util.ConvertPDF2PPT(
ListViewFirstColumnText,
ConvertedFileName,
BodyForm.MainForm.ExtraOcrBox.Checked,
BodyForm.MainForm.SplitShapeBox.Checked,
BodyForm.MainForm.RenderImageBox.Checked);
break;
// pdf版本转换
case ConvertFileType.ConvertPdfCompliance:
object SelectPdfCompliance = Enum.Parse(
typeof(Aspose.Pdf.PdfFormat),
BodyForm.MainForm.ConvertPdfComplianceVersion.SelectedItem.ToString());
Util.ConvertPdfCompliance(
ListViewFirstColumnText,
ConvertedFileName,
(Aspose.Pdf.PdfFormat)SelectPdfCompliance);
break;
// 压缩PDF
case ConvertFileType.CompressPDF:
Util.CompressPDF(
ListViewFirstColumnText,
ConvertedFileName,
BodyForm.MainForm.CompressPDF_WEB.Checked,
BodyForm.MainForm.CompressPDF_CompressImage.Checked,
int.Parse(BodyForm.MainForm.CompressPDF_ImageQuailty.SelectedItem.ToString()),
int.Parse(BodyForm.MainForm.CompressPDF_ImageResoution.SelectedItem.ToString()),
(Aspose.Pdf.Optimization.ImageCompressionVersion)Enum.Parse(
typeof(Aspose.Pdf.Optimization.ImageCompressionVersion),
BodyForm.MainForm.CompressPDF_ImageCompressVersion.SelectedValue.ToString()),
BodyForm.MainForm.CompressPDF_CompressImageFlate.Checked,
BodyForm.MainForm.CompressPDF_RemoveUnuseStream.Checked,
BodyForm.MainForm.CompressPDF_LinkSameStream.Checked,
BodyForm.MainForm.CompressPDF_CancelEmbedFont.Checked,
BodyForm.MainForm.CompressPDF_RemoveForms.Checked,
BodyForm.MainForm.CompressPDF_RemoveUnuseObject.Checked);
break;
// 压缩图片
case ConvertFileType.CompressImage:
bool UseSelfExt = BodyForm.MainForm.CompressImage_NotUseSelfExt.Checked;//是否不使用源文件后缀名
string SavedFileName = string.Empty;
if (UseSelfExt)
{
SavedFileName = ConvertedFileName;
}
else
{
string ImageFileExtenString = Path.GetExtension(ListViewFirstColumnText);
SavedFileName = Path.Combine(
SelectSavePathstring,
$"{FileNameWithOutExtension}{ImageFileExtenString}");
}
Util.CompressImage(
ListViewFirstColumnText,
SavedFileName,
(Aspose.Imaging.FileFormats.Jpeg.JpegCompressionMode)Enum.Parse(
typeof(Aspose.Imaging.FileFormats.Jpeg.JpegCompressionMode),
BodyForm.MainForm.CompressJpegMode_Combox.SelectedItem.ToString()),
(Aspose.Imaging.FileFormats.Jpeg.JpegCompressionColorMode)Enum.Parse(
typeof(Aspose.Imaging.FileFormats.Jpeg.JpegCompressionColorMode),
BodyForm.MainForm.CompressJpegColorMode_Combox.SelectedItem.ToString()),
int.Parse(BodyForm.MainForm.CompressJpegQuailty_Combox.SelectedItem.ToString()),
int.Parse(BodyForm.MainForm.CompressPngLevel_Combox.SelectedItem.ToString()),
(Aspose.Imaging.FileFormats.Png.PngColorType)Enum.Parse(
typeof(Aspose.Imaging.FileFormats.Png.PngColorType),
BodyForm.MainForm.CompressPngColorMode_Combox.SelectedItem.ToString()),
(Aspose.Imaging.FileFormats.Bmp.BitmapCompression)Enum.Parse(
typeof(Aspose.Imaging.FileFormats.Bmp.BitmapCompression),
BodyForm.MainForm.CompressBmpMode_Combox.SelectedItem.ToString()),
int.Parse(BodyForm.MainForm.CompressBmpPixeBits_Combox.SelectedItem.ToString()));
break;
case ConvertFileType.Html2PDF:
Util.ConvertHtml2PDFAsync(
ListViewFirstColumnText,
ConvertedFileName,
BodyForm.MainForm.html2pdf_textbox_chromepath.Text,
BodyForm.MainForm.html2pdf_combox_pagesize.SelectedIndex,
bool.Parse(BodyForm.MainForm.html2pdf_combox_pagedirect.SelectedValue.ToString()),
BodyForm.MainForm.html2pdf_checkbox_usecss.Checked,
BodyForm.MainForm.html2pdf_textbox_margin_top.Text,
BodyForm.MainForm.html2pdf_textbox_margin_left.Text,
BodyForm.MainForm.html2pdf_textbox_margin_bottom.Text,
BodyForm.MainForm.html2pdf_textbox_margin_right.Text,
BodyForm.MainForm.html2pdf_timeout.Value,
BodyForm.MainForm.html2pdf_wait_timeout.Value,
BodyForm.MainForm.html2pdf_wait_all_resource.Checked);
//Util.ConvertHtml2Pdf(ListViewFirstColumnText, ConvertedFileName);
break;
case ConvertFileType.Word2EPUB:
int TempImageDpiValue = 100;
if (BodyForm.MainForm.CoverImageDpi.Text != string.Empty)
{
TempImageDpiValue = int.Parse(BodyForm.MainForm.CoverImageDpi.Text);
}
Util.ConvertWord2EPUB(
ListViewFirstColumnText,
ConvertedFileName,
BodyForm.MainForm.Word2EpubAuthorName.Text,
BodyForm.MainForm.Word2EpubConverImage.Text,
TempImageDpiValue);
break;
case ConvertFileType.EPUB2Word:
Util.ConvertEPUB2Word(ListViewFirstColumnText, ConvertedFileName);
break;
case ConvertFileType.EPUB2Pdf:
Util.ConvertEPUB2Pdf(ListViewFirstColumnText, ConvertedFileName);
break;
case ConvertFileType.SplitWord:
Util.SplitWord(
ListViewFirstColumnText,
SelectSavePathstring,
BodyForm.MainForm.SplitWord_Single.Checked,
BodyForm.MainForm.SplitWord_Range_Start.Text.Trim(),
BodyForm.MainForm.SplitWord_Range_End.Text.Trim(),
int.Parse(BodyForm.MainForm.SplitWord_PageCount.Value.ToString()),
BodyForm.MainForm.SplitWordReUpdatePageNumber.Checked);
break;
case ConvertFileType.WeBp2Image:
Util.ConvertWeBp2Image(ListViewFirstColumnText, ConvertedFileName);
break;
case ConvertFileType.SplitPPT:
Util.SplitPPT(
ListViewFirstColumnText,
SelectSavePathstring,
BodyForm.MainForm.SplitPPT_SignleExport.Checked,
BodyForm.MainForm.SplitPPT_RangeExport_Start.Text,
BodyForm.MainForm.SplitPPT_RangeExport_End.Text,
(Aspose.Slides.SlideSizeType)Enum.Parse(
typeof(Aspose.Slides.SlideSizeType),
BodyForm.MainForm.SplitPdfSizeCombox.SelectedItem.ToString()),
(Aspose.Slides.SlideSizeScaleType)Enum.Parse(
typeof(Aspose.Slides.SlideSizeScaleType),
BodyForm.MainForm.SplitPdfScaleCombox.SelectedValue.ToString()));
break;
default:
break;
}
ListViewControl.Items[TempIndex].SubItems.Add(ConvertedFileName);
ListViewControl.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
if (File.Exists(ConvertedFileName))
{
FileInfo ConverdFileInfo = new FileInfo(ConvertedFileName);
//获取转换后的文件大小
ListViewControl.Items[TempIndex].SubItems
.Add(Util.GetFileSizeDisplayString(ConverdFileInfo.Length));
ListViewControl.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
ListViewControl.BeginInvoke(
new Action(
() =>
{
ListViewControl.Items[TempIndex - 1].Selected = true;
ListViewControl.EnsureVisible(TempIndex - 1);
ListViewControl.Focus();
}));
}
StatusLabelControl.Text = "处理完毕";
CallButton.Enabled = true;
//处理完毕后需要做的事情
switch (ConverType)
{
case ConvertFileType.Word2Pdf:
if (BodyForm.MainForm.OpenPdfOptions.Checked)
{
Task<List<string>> TempPdfFileNames = GetListViewLastItemContentAsync(
BodyForm.MainForm.WordFileListView);
TempPdfFileNames.Result
.ForEach(
(pdfFileName) =>
{
Process.Start(pdfFileName);
});
}
if (BodyForm.MainForm.RemoveTempDocx.Checked)
{
if (TempSaveDocxPathString != string.Empty)
{
Directory.Delete(TempSaveDocxPathString, true);
}
}
break;
default:
break;
}
BodyForm.MainForm
.BeginInvoke(
new Action(
() =>
{
if (string.Compare(
BodyForm.MainForm.ShowOpenTargetDialog,
"on",
StringComparison.Ordinal) ==
0)
{
DialogResult MessageBoxResult = MessageBox.Show(
"处理完毕,是否需要打开转换后目录?",
"提示",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (MessageBoxResult == DialogResult.Yes)
{
Process.Start(SelectSavePathstring);
}
}
}));
}
}
}