我已经创建了一个函数,它将单个精细分割成多个文件。例如,我有一个包含 100 页的文件,现在我想为每 15 页创建新文件,这意味着它将创建 7 个文件,每个文件有 15 页。(100/15 = 7)
现在我的问题是我已经实现了使用Thread、ISynchronizeInvoke和委托来分割文件的逻辑,以实现流畅的流程和用户体验。它正在工作,但我想并行或同时执行每个拆分,而不是一个接一个的过程。
此代码已编写在 Splitter.cs 文件中,我编写了以下代码:
#region Private Variables
private NotifyProgress _notifyDelegate;
private Thread _thread;
private ISynchronizeInvoke _synchronizingObject;
//this is the definition of the progress delegate - it defines the "signature" of the routine...
public delegate void NotifyProgress(int TotalFiles, int ProcessFileIndex, int TotalPages, int PageIndex, string Size);
#endregion
private void splitFiles()
{
//Intialize a new PdfReader instance with the contents of the source Pdf file:
PdfReader Reader = new PdfReader(PDFFile);
Reader.RemoveUnusedObjects();
Reader.ConsolidateNamedDestinations();
for (int f = 0; f < Files.Count(); f++)
{
List<int> Pages = Files[f].Pages;
string FileName = (f + 1).ToString();
string folderPath = Path.Combine(OutputFolderPath, GeneratedFilesFolder);
string gereatedPath = Path.Combine(folderPath, string.Format(FileNameFormat, FileName));
//This method will create folder if the path doesn't exist
CreateFolder(folderPath);
PdfImportedPage importedPage = null;
Document currentDocument = new Document();
PdfSmartCopy pdfWriter = null;
bool bIsFirst = true;
long _size = 0;
for (int p = 0; p < Pages.Count; p++)
{
NotifyUI(Files.Count(), f, Pages.Count, p, _size);
if (bIsFirst)
{
bIsFirst = false;
currentDocument = new Document(Reader.GetPageSizeWithRotation(1));
pdfWriter = new PdfSmartCopy(currentDocument, new FileStream(gereatedPath, FileMode.Create));
pdfWriter.SetFullCompression();
//pdfWriter.CompressionLevel = PdfStream..BEST_COMPRESSION;
pdfWriter.PdfVersion = Reader.PdfVersion;
currentDocument.Open();
}
_size += pdfWriter.CurrentDocumentSize;
importedPage = pdfWriter.GetImportedPage(Reader, Pages[p]);
pdfWriter.AddPage(importedPage);
}
currentDocument.Close();
pdfWriter.Close();
FileInfo _f = new FileInfo(gereatedPath);
NotifyUI(Files.Count(), f, Pages.Count, Pages.Count - 1, _f.Length);
}
}
private void NotifyUI(int TotalFiles, int ProcessFileIndex, int TotalPages, int PageIndex, long Size)
{
//this method will fail because we're not telling the delegate which thread to run in...
object[] args = { TotalFiles, ProcessFileIndex + 1, TotalPages, PageIndex + 1, CalculateFileSize(Size) };
//call the delegate, specifying the context in which to run...
_synchronizingObject.Invoke(_notifyDelegate, args);
}
此代码已写入 Form.cs 文件
private void DelegateProgress(int TotalFiles, int ProcessFileIndex, int TotalPages, int PageIndex, string Size)
{
if (splitter != null && PDFSplitter.TotalPages > 0)
{
this.Invoke((MethodInvoker)delegate
{
int IndividualProgress = PageIndex * 100 / TotalPages;
lstFiles.Items[ProcessFileIndex - 1].SubItems[1].Text = Size;
TextProgressBar pb = (TextProgressBar)lstFiles.GetEmbeddedControl(3, ProcessFileIndex - 1);
pb.Text = string.Format("{0:00} %", IndividualProgress);
pb.Value = IndividualProgress;
int OverallProgress = ProcessFileIndex * 100 / TotalFiles;
ProgressStripItem statusProgrss = (ProgressStripItem)tsStatus.Items[3];
statusProgrss.TextProgressBar.Value = OverallProgress;
statusProgrss.TextProgressBar.Text = string.Format("{0:00}%", OverallProgress);
if (OverallProgress >= 100 && IndividualProgress >= 100)
{
tslblMessage.Text = "File has been split successfully.";
DialogResult dr = MessageBox.Show(tslblMessage.Text + "\nDo you want to open split files folder?", "Split Completed", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (dr == System.Windows.Forms.DialogResult.Yes)
{
OpenSplitFilePath();
}
for (int i = 0; i < lstFiles.Items.Count; i++)
{
lstFiles.RemoveEmbeddedControl(lstFiles.GetEmbeddedControl(3, i));
}
lstFiles.Items.Clear();
splitter = null;
}
});
}
}