如何解决每个文件夹无法合并超过 300 个 pdf 文件使用的库是 gdpicture
我正在使用gdpicture库删除空白tiff文件,将tif转换为pdf,旋转pdf文件,最后合并pdf文件。 我拥有使用最新版本的最终许可证。 当我使用 270 个文件时,我能够成功执行上述功能。 当文件大小增加到每个文件夹 300 个文件时。 只有 1 个 pdf 文件成功合并为多页文件,其余的则没有。 只有当每个文件夹的 pdf 文件超过 270 时,我才会收到错误合并文档状态访问被拒绝,我用 270 和 298 测试它在 298 时失败。 我是否遗漏了要由 回答的这个问题的任何关键信息?
using GdPicture14;
using Microsoft.VisualBasic.FileIO;
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using MessageBox = System.Windows.Forms.MessageBox;
using Path = System.IO.Path;
using SearchOption = System.IO.SearchOption;
namespace TanOCR
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
//private GdPicturePDF oPDF = null;
public MainWindow()
{
try
{
InitializeComponent();
cbPDFConform.Items.Clear();
for (int i = 0; i < Enum.GetNames(typeof(PdfConformance)).Length - 1; i++)
{
ComboBoxItem cbi = new ComboBoxItem();
cbi.Content = Enum.GetName(typeof(PdfConformance),(PdfConformance)i);
PdfConformance test = (PdfConformance)i;
cbi.Tag = (PdfConformance)i;
cbPDFConform.Items.Add(cbi);
}
cbPDFConform.SelectedIndex = 0;
cbProcessorCount.Items.Clear();
for (int i = 1; i <= Environment.ProcessorCount; i++)
{
cbProcessorCount.Items.Add(i.ToString());
if (Environment.ProcessorCount / 2 == i) { cbProcessorCount.SelectedIndex = i - 1; }
}
string configpath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,@"path.txt");
string[] configfile = File.ReadAllLines(configpath);
if (configfile.Length > 1)
{
ip.Text = configfile[0];
op.Text = configfile[1];
ex_tb.Text = configfile[2];
Protb.Text = configfile[3];
}
}
catch (Exception e1)
{
MessageBox.Show("e1" + e1.Message);
}
}
public static string browseFolder()
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = fbd.ShowDialog();
string path = string.Empty;
// Get the selected file name and display in a TextBox
if (result == (System.Windows.Forms.DialogResult)MessageBoxResult.OK)
{
// Open document
path = fbd.SelectedPath;
if (path[path.Length - 1] != '\\')
{
path = path + "\\";
}
}
return path;
}
private string ConvertTIFFTOPDFyes2()//required
{
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((Action)(() =>
{
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
PdfConformance optPDFConform = PdfConformance.Unknown;
var all_dir = Directory.EnumerateFiles(op.Text,"*.*",SearchOption.AllDirectories)
.Where(s => s.EndsWith(".tiff") || s.EndsWith(".tif")).ToArray();
//string[] all_dir = Directory.GetFiles(Protb.Text,SearchOption.AllDirectories);
for (int ad = 0; ad < all_dir.Length; ad++)
{
string fd = Path.GetDirectoryName(all_dir[ad]);
string op_path = fd.Replace(op.Text,Protb.Text);
string getextension = Path.GetExtension(all_dir[ad]);
string dict = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,"Redist","OCR");
string dirName = Path.GetFileName(Path.GetDirectoryName(all_dir[ad]));
string outputPath = (new FileInfo(all_dir[ad]).Directory.FullName).Replace(op.Text,Protb.Text);
fd = Path.GetDirectoryName(all_dir[ad]);
string fn = Path.GetFileNameWithoutExtension(all_dir[ad]);
//op_path = fd.Replace(op.Text,Protb.Text);
string filen = Path.Combine(outputPath,fn + ".pdf");
string savefile = Path.Combine(op_path,filen);
//string dest = Path.Combine(outputPath,dirName);
using (GdPictureDocumentConverter oConverter = new GdPictureDocumentConverter())
{
GdPictureStatus status = oConverter.LoadFromFile(all_dir[ad],GdPicture14.DocumentFormat.DocumentFormatTIFF);
if (status == GdPictureStatus.OK)
{
if (!Directory.Exists(op_path))
{
Directory.CreateDirectory(op_path);
}
optPDFConform = (PdfConformance)((ComboBoxItem)cbPDFConform.SelectedItem).Tag;
status = oConverter.SaveAsPDF(savefile,optPDFConform);
if (status == GdPictureStatus.OK)
{
}
else
{
MessageBox.Show("The file has failed to save. Status: " + status.ToString(),"TIFF to PDF Example",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("The file has failed to load. Status: " + status.ToString(),MessageBoxIcon.Error);
}
}
}
watch.Stop();
logtb.AppendText(Environment.NewLine + "tiff to pdf " + watch.ElapsedMilliseconds.ToString());
var watchDel = new System.Diagnostics.Stopwatch();
watchDel.Start();
Directory.Delete(ip.Text,true);
Directory.CreateDirectory(ip.Text);
FileSystem.CopyDirectory(op.Text,ip.Text,UIOption.AllDialogs);
Directory.Delete(op.Text,true);
Directory.CreateDirectory(op.Text);
watchDel.Stop();
logtb.AppendText(Environment.NewLine + "delete copy " + watchDel.ElapsedMilliseconds.ToString());
//clearFolder(op.Text);
OcrPDFPages_4();
//clearFolder(Protb.Text);testing withoOcrPDFPages_4ut merge
}));
return string.Empty;
}
private string deleteblanktiff1yes()//required
{
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((Action)(() =>
{
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
var all_dir = Directory.EnumerateFiles(ip.Text,SearchOption.AllDirectories)
.Where(s => s.EndsWith(".tiff") || s.EndsWith(".tif")).ToArray();
for (int ad = 0; ad < all_dir.Length; ad++)
{
string getextension = Path.GetExtension(all_dir[ad]);
string fd = Path.GetDirectoryName(all_dir[ad]);
string op_path = fd.Replace(ip.Text,op.Text);
string dict = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,"OCR");
string dirName = Path.GetFileName(Path.GetDirectoryName(all_dir[ad]));
string outputPath = (new FileInfo(all_dir[ad]).Directory.FullName).Replace(ip.Text,op.Text);
fd = Path.GetDirectoryName(all_dir[ad]);
string fn = Path.GetFileNameWithoutExtension(all_dir[ad]);
op_path = fd.Replace(ip.Text,op.Text);
string filen = Path.Combine(outputPath,fn + getextension);
string savefile = Path.Combine(op_path,filen);
using (GdPictureImaging gdpictureImaging = new GdPictureImaging())
{
if (!Directory.Exists(op_path))
{
Directory.CreateDirectory(op_path);
}
int imageID = gdpictureImaging.TiffCreateMultiPageFromFile_2(all_dir[ad],true);
int pageCount = gdpictureImaging.TiffGetPageCount(imageID);
int pageNo = 1;
while (pageNo <= pageCount)
{
gdpictureImaging.TiffSelectPage(imageID,pageNo);
if (gdpictureImaging.IsBlank(imageID))
{
gdpictureImaging.TiffDeletePage(imageID,pageNo);
pageCount--;
}
else
{
pageNo++;
}
}
gdpictureImaging.SaveAsTIFF(imageID,savefile,TiffCompression.TiffCompressionAUTO);
gdpictureImaging.ReleaseGdPictureImage(imageID);
}
}
watch.Stop();
logtb.AppendText("deleting blanks pages " + watch.ElapsedMilliseconds.ToString());
ConvertTIFFTOPDFyes2();
}));
return op.Text;
}
private string OcrPDFPages_4()
{
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((Action)(() =>
{
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
var all_dir = Directory.EnumerateFiles(Protb.Text,SearchOption.AllDirectories)
.Where(s => s.EndsWith(".pdf") || s.EndsWith(".PDF")).ToArray();
for (int ad = 0; ad < all_dir.Length; ad++)
{
string fd = Path.GetDirectoryName(all_dir[ad]);
string op_path = fd.Replace(Protb.Text,ex_tb.Text);
string dict = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,"OCR");
string dirName = Path.GetFileName(Path.GetDirectoryName(all_dir[ad]));
string getextension = Path.GetExtension(all_dir[ad]);
fd = Path.GetDirectoryName(all_dir[ad]);
string fn = Path.GetFileNameWithoutExtension(all_dir[ad]);
op_path = fd.Replace(Protb.Text,ex_tb.Text);
string filen = Path.Combine(op_path,filen);
using (GdPicturePDF oGdPicturePDF = new GdPicturePDF())
{
if (oGdPicturePDF.LoadFromFile(all_dir[ad],false) == GdPictureStatus.OK)
{
if (!Directory.Exists(op_path))
{
Directory.CreateDirectory(op_path);
}
if (oGdPicturePDF.OcrPages_4("*",int.Parse(cbProcessorCount.SelectedItem.ToString()),"eng",dict,"",250,OCRMode.FavorSpeed,1000,true) == GdPictureStatus.OK)
{
if (oGdPicturePDF.SaveToFile(savefile) == GdPictureStatus.OK)
{
}
else
MessageBox.Show("The resulting document can't be saved. Status: " + oGdPicturePDF.GetStat().ToString());
}
else
MessageBox.Show("The OCR process has failed. Status: " + oGdPicturePDF.GetStat().ToString());
oGdPicturePDF.CloseDocument();
}
else
MessageBox.Show("The file can't be loaded. Status: " + oGdPicturePDF.GetStat().ToString());
}
}
watch.Stop();
TimeSpan ts = watch.Elapsed;
ts.ToString("mm\\:ss\\.ff");
logtb.AppendText(Environment.NewLine + "OCR pages " + ts);
Directory.Delete(Protb.Text,true);
Directory.CreateDirectory(Protb.Text);
pageorientation();
}));
return string.Empty;
}
private string pageorientation()
{
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((Action)(() =>
{
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
var all_dir = Directory.EnumerateFiles(ex_tb.Text,SearchOption.AllDirectories)
.Where(s => s.EndsWith(".pdf") || s.EndsWith(".PDF")).ToArray();
//string[] all_dir = Directory.GetFiles(op.Text,SearchOption.AllDirectories);
string op_path = string.Empty;
for (int ad = 0; ad < all_dir.Length; ad++)
{
string outputPath = (new FileInfo(all_dir[ad]).Directory.FullName).Replace(ex_tb.Text,Protb.Text);
string fn = Path.GetFileNameWithoutExtension(all_dir[ad]);
string getextension = Path.GetExtension(all_dir[ad]);
string fd = Path.GetDirectoryName(all_dir[ad]);
op_path = fd.Replace(ex_tb.Text,fn + getextension);
string savefile = Path.Combine(op_path,filen);
string destpath = savefile;
string filepath = all_dir[ad];
string dict = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,"OCR");
string DICT_PATH = dict; //Specify your path.
OCRLanguage LANG = OCRLanguage.English; //Specify your language.
string caption = "Example: GuessPageTextRotation";
GdPicturePDF oGdPicturePDF = new GdPicturePDF();
GdPictureStatus status = oGdPicturePDF.LoadFromFile(all_dir[ad],false);
if (status == GdPictureStatus.OK)
{
if (!Directory.Exists(op_path))
{
Directory.CreateDirectory(op_path);
}
int pageCount = oGdPicturePDF.GetPageCount();
status = oGdPicturePDF.GetStat();
if (status == GdPictureStatus.OK)
{
string message = null;
int page_text_rotation = 0;
for (int i = 1; i <= pageCount; i++)
{
oGdPicturePDF.SelectPage(i);
status = oGdPicturePDF.NormalizePage();
if (status == GdPictureStatus.OK)
{
page_text_rotation = oGdPicturePDF.GuessPageTextRotation();
status = oGdPicturePDF.GetStat();
if (status == GdPictureStatus.OK)
{
{ }
//message = message + "text rotation: " + page_text_rotation.ToString();
if (page_text_rotation > 0)
{
status = oGdPicturePDF.RotatePage(page_text_rotation);
if (status == GdPictureStatus.OK)
{
//message = message + " :page has been rotated\n";
}
else
{
break;
}
}
else
{
}
}
}
else
{
break;
}
}
if (status == GdPictureStatus.OK)
{
status = oGdPicturePDF.SaveToFile(savefile);
if (status == GdPictureStatus.OK)
{ }
else
message = message + "\nThe pages have been rotated successfully but the file can't be saved.";
}
}
else
{
MessageBox.Show("The GetPageCount() method has failed with the status: " + status.ToString(),caption);
}
}
else
{
MessageBox.Show("The file can't be loaded.",caption);
}
oGdPicturePDF.Dispose();
}
watch.Stop();
logtb.AppendText(Environment.NewLine + "Rotating pages " + watch.ElapsedMilliseconds.ToString());
if (MergeChk.IsChecked == true)
{
merge();
}
else if (MergeChk.IsChecked == false)
{
FileSystem.CopyDirectory(
Protb.Text,op.Text,UIOption.AllDialogs);
Directory.Delete(Protb.Text,true);
Directory.CreateDirectory(Protb.Text);
FileSystem.CopyDirectory(
ip.Text,Protb.Text,UIOption.AllDialogs);
Directory.Delete(ip.Text,true);
Directory.CreateDirectory(ip.Text);
Directory.Delete(ex_tb.Text,true);
Directory.CreateDirectory(ex_tb.Text);
MessageBox.Show("The OCRed pdf files are created at " + op.Text);
}
}));
return string.Empty;
}
private void merge()
{
String saveToPath = String.Empty;
try
{
using (StreamWriter sw = new StreamWriter(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,@"path.txt"),false))
{
sw.WriteLine(ip.Text);
sw.WriteLine(op.Text);
sw.WriteLine(ex_tb.Text);
sw.WriteLine(Protb.Text);
sw.Close();
}
}
catch (Exception tm1)
{
MessageBox.Show("(tm1)" + tm1.Message);
}
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((Action)(() =>
{
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
var all_dir = Directory.EnumerateFiles(Protb.Text,SearchOption.AllDirectories)
.Where(s => s.EndsWith(".pdf") || s.EndsWith(".PDF")).ToArray();
for (int ad = 0; ad < all_dir.Length; ad++)
{
string name = Path.GetDirectoryName(all_dir[ad]);
var folder = Directory.EnumerateFiles(name,SearchOption.AllDirectories).ToArray();
try
{
string dict = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,"OCR");
string dirName = Path.GetFileName(Path.GetDirectoryName(folder[0]));
name = Path.GetDirectoryName(folder[0]);
string outputPath = (new FileInfo(folder[0]).Directory.FullName).Replace(ip.Text,op.Text);
string ocrfolder = (new FileInfo(folder[0]).Directory.FullName).Replace(Protb.Text,op.Text);
string filen = Path.Combine(ocrfolder,dirName + ".pdf");
if (!Directory.Exists(ocrfolder))
{
Directory.CreateDirectory(ocrfolder);
}
int no = folder.Length;
string[] arPDF = new string[no];
using (GdPicturePDF oGdPicturePDF = new GdPicturePDF())
{
GdPictureStatus status = oGdPicturePDF.MergeDocuments(ref folder,filen);
if (status == GdPictureStatus.OK)
{
}
else
{
MessageBox.Show("The MergeDocuments() method has failed with the status: " + status.ToString());
}
}
}
catch (Exception e5)
{
MessageBox.Show("e5" + e5.Message);
}
}
watch.Stop();
logtb.AppendText(Environment.NewLine + "merging pages " + watch.ElapsedMilliseconds.ToString());
Directory.Delete(ex_tb.Text,true);
Directory.CreateDirectory(ex_tb.Text);
Directory.Delete(Protb.Text,true);
Directory.CreateDirectory(Protb.Text);
FileSystem.CopyDirectory(ip.Text,Protb.Text);
Directory.Delete(ip.Text,true);
Directory.CreateDirectory(ip.Text);
MessageBox.Show("All documents have been successfully merged.");
}));
}
private void inbtn_Click(object sender,RoutedEventArgs e)
{
try
{
ip.Text = browseFolder();
}
catch (Exception e7)
{
MessageBox.Show("e7" + e7.Message);
}
}
private void obtn_Click(object sender,RoutedEventArgs e)
{
try
{
op.Text = browseFolder();
}
catch (Exception e8)
{
MessageBox.Show("e8" + e8.Message);
}
}
private void start_btn_Click(object sender,RoutedEventArgs e)
{
try
{
using (StreamWriter sw = new StreamWriter(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,false))
{
sw.WriteLine(ip.Text);
sw.WriteLine(op.Text);
sw.WriteLine(ex_tb.Text);
sw.WriteLine(Protb.Text);
sw.Close();
MessageBox.Show("path file is updated");
}
var watch = new System.Diagnostics.Stopwatch();
watch.Start();
deleteblanktiff1yes();
watch.Stop();
TimeSpan ts = watch.Elapsed;
ts.ToString("mm\\:ss\\.ff");
logtb.AppendText(Environment.NewLine + "full process " + ts);
}
catch (Exception e9)
{
MessageBox.Show("e9" + e9.Message);
}
}
private void pro_btn_Click(object sender,RoutedEventArgs e)
{
try
{
Protb.Text = browseFolder();
}
catch (Exception e10)
{
MessageBox.Show("e10" + e10.Message);
}
}
private void excep_Click(object sender,RoutedEventArgs e)
{
try
{
ex_tb.Text = browseFolder();
}
catch (Exception e11)
{
MessageBox.Show("e111" + e11.Message);
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。