add magazine support

This commit is contained in:
tomse 2024-03-04 20:18:00 +01:00
parent df98f39674
commit a314d4d1f8
3 changed files with 214 additions and 59 deletions

View File

@ -119,6 +119,7 @@ namespace PDFWorkflowManager
this.btnDisposePics = new System.Windows.Forms.Button();
this.btnDeleteTemp = new System.Windows.Forms.Button();
this.btnDeleteCacheWork = new System.Windows.Forms.Button();
this.chkMagazines = new System.Windows.Forms.CheckBox();
this.menuStrip1.SuspendLayout();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
@ -767,6 +768,7 @@ namespace PDFWorkflowManager
// groupExport
//
this.groupExport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.groupExport.Controls.Add(this.chkMagazines);
this.groupExport.Controls.Add(this.panelBanner);
this.groupExport.Controls.Add(this.panelSort);
this.groupExport.Enabled = false;
@ -1028,6 +1030,16 @@ namespace PDFWorkflowManager
this.btnDeleteCacheWork.UseVisualStyleBackColor = true;
this.btnDeleteCacheWork.Click += new System.EventHandler(this.btnDeleteCacheWork_Click);
//
// chkMagazines
//
this.chkMagazines.AutoSize = true;
this.chkMagazines.Location = new System.Drawing.Point(7, 22);
this.chkMagazines.Name = "chkMagazines";
this.chkMagazines.Size = new System.Drawing.Size(72, 17);
this.chkMagazines.TabIndex = 56;
this.chkMagazines.Text = "Magazine";
this.chkMagazines.UseVisualStyleBackColor = true;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
@ -1069,6 +1081,7 @@ namespace PDFWorkflowManager
this.tabPage2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.groupExport.ResumeLayout(false);
this.groupExport.PerformLayout();
this.panelBanner.ResumeLayout(false);
this.panelSort.ResumeLayout(false);
this.panelSort.PerformLayout();
@ -1173,6 +1186,7 @@ namespace PDFWorkflowManager
private System.Windows.Forms.Button btnDisposePics;
private System.Windows.Forms.Button btnDeleteTemp;
private System.Windows.Forms.Button btnDeleteCacheWork;
private System.Windows.Forms.CheckBox chkMagazines;
}
}

View File

@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
@ -145,13 +146,13 @@ namespace PDFWorkflowManager
private string strExeFilePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string bannerPage = "";
bool sortNormal = true;
private string workDir = Properties.Settings.Default.WorkDir;
private string workOutDir = Properties.Settings.Default.WorkOutDir;
private string origsDir = Properties.Settings.Default.OrigsDir;
private string tempDir = Properties.Settings.Default.TempDir;
private string strPostProcessor = Properties.Settings.Default.PostProcessor;
private string strExtension = "tif";
public MainForm()
{
@ -549,6 +550,183 @@ namespace PDFWorkflowManager
Application.Exit();
}
private string prepCopyToTempOutdir(int sourceFileCount, string[] strFiles, string sourceDir)
{
string tempSortDir = Path.Combine(tempDir, "sort");
Directory.CreateDirectory(tempSortDir);
int downCount = sourceFileCount;
int upCount = 1;
// Make this to a function
if (radioSortMagazine.Checked == true)
{
for (int i = 0; i < sourceFileCount;)
{
File.Copy(strFiles[i], Path.Combine(tempSortDir, downCount.ToString().PadLeft(4, '0') + ".tif"), true);
i++;
downCount--;
File.Copy(strFiles[i], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".tif"), true);
i++;
upCount++;
File.Copy(strFiles[i], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".tif"), true);
i++;
upCount++;
File.Copy(strFiles[i], Path.Combine(tempSortDir, downCount.ToString().PadLeft(4, '0') + ".tif"), true);
i++;
downCount--;
}
// fixme why sleep?
Thread.Sleep(1000);
}
else if (radioSortFlatBed.Checked == true)
{
for (int i = 1; i < sourceFileCount;)
{
File.Copy(strFiles[i], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".tif"), true);
i++;
upCount++;
}
File.Copy(strFiles[0], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".tif"), true);
// fixme why sleep?
Thread.Sleep(1000);
}
else if (radioSortNormal.Checked == true)
{
for (int i = 0; i < sourceFileCount;)
{
File.Copy(strFiles[i], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".tif"), true);
i++;
upCount++;
}
}
return tempSortDir;
}
static void ConvertToJpeg(string sourceFileName, string destinationFileName, int compressionLevel, int dpi = 300)
{
try
{
// Load the source image
using (Image sourceImage = Image.FromFile(sourceFileName))
{
// Set encoding parameters for JPEG
EncoderParameters encoderParameters = new EncoderParameters(1);
encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, compressionLevel);
// Get the JPEG codec
ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");
// Save the image in JPEG format with specified compression level
sourceImage.Save(destinationFileName, jpegCodec, encoderParameters);
sourceImage.Dispose();
}
}
catch (Exception ex)
{
}
}
static void ConvertTiffTo150DpiJpg(string inputTiffPath, string outputJpgPath)
{
using (var tiffImage = Image.FromFile(inputTiffPath))
{
var newWidth = (int)(tiffImage.Width * 150 / tiffImage.HorizontalResolution);
var newHeight = (int)(tiffImage.Height * 150 / tiffImage.VerticalResolution);
using (var newImage = new Bitmap(newWidth, newHeight))
{
newImage.SetResolution(150, 150);
using (var graphics = Graphics.FromImage(newImage))
{
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.DrawImage(tiffImage, 0, 0, newWidth, newHeight);
}
// Set JPEG compression level
var encoderParameters = new EncoderParameters(1);
var encoderParameter = new EncoderParameter(Encoder.Quality, 85L); // Set compression level here (0-100)
encoderParameters.Param[0] = encoderParameter;
// Save as JPEG with 150 DPI and specified compression level
var jpegCodecInfo = GetEncoderInfo(ImageFormat.Jpeg);
newImage.Save(outputJpgPath, jpegCodecInfo, encoderParameters);
}
}
}
static ImageCodecInfo GetEncoderInfo(string mimeType)
{
// Get image codecs for all image formats
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
// Find the correct image codec
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == mimeType)
{
return codec;
}
}
// If no appropriate codec found, return null
return null;
}
private string prepConvertToTempOutdir(int sourceFileCount, string[] strFiles, string sourceDir)
{
string tempSortDir = Path.Combine(tempDir, "sort");
Directory.CreateDirectory(tempSortDir);
int downCount = sourceFileCount;
int upCount = 1;
if (radioSortMagazine.Checked == true)
{
for (int i = 0; i < sourceFileCount;)
{
ConvertToJpeg(strFiles[i], Path.Combine(tempSortDir, downCount.ToString().PadLeft(4, '0') + ".jpg"), 85);
i++;
downCount--;
ConvertToJpeg(strFiles[i], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".jpg"), 85);
i++;
upCount++;
ConvertToJpeg(strFiles[i], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".jpg"), 85);
i++;
upCount++;
ConvertToJpeg(strFiles[i], Path.Combine(tempSortDir, downCount.ToString().PadLeft(4, '0') + ".jpg"), 85);
i++;
downCount--;
}
// fixme why sleep?
Thread.Sleep(1000);
sourceDir = tempSortDir;
}
else if (radioSortFlatBed.Checked == true)
{
for (int i = 1; i < sourceFileCount;)
{
ConvertToJpeg(strFiles[i], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".jpg"), 85);
i++;
upCount++;
}
ConvertToJpeg(strFiles[0], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".jpg"), 85);
// fixme why sleep?
Thread.Sleep(1000);
}
else if (radioSortNormal.Checked == true)
{
for (int i = 0; i < sourceFileCount;)
{
ConvertToJpeg(strFiles[i], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".jpg"), 85);
i++;
upCount++;
}
}
return tempSortDir;
}
private async void btnConvertToPDF_Click(object sender, EventArgs e)
{
try
@ -556,7 +734,6 @@ namespace PDFWorkflowManager
string[] strFiles = Directory.GetFiles(workOutDir, "*.tif");
int sourceFileCount = strFiles.Length;
string outputDir = Path.Combine(tempDir, "output");
//string inputDir = txtProjectDir.Text + @"\work\out\";
Directory.CreateDirectory(outputDir);
string sourceDir = workOutDir;
@ -577,51 +754,23 @@ namespace PDFWorkflowManager
);
}
if (radioSortMagazine.Checked == true)
// TODO - check for magazine - finish this function
if (chkMagazines.Checked == true)
{
string tempSortDir = Path.Combine(tempDir, "sort"); ;
Directory.CreateDirectory(tempSortDir);
int downCount = sourceFileCount;
int upCount = 1;
sourceDir = prepConvertToTempOutdir(sourceFileCount, strFiles, sourceDir);
// convert to jpg function
strExtension = "jpg";
}
else
{
sourceDir = prepCopyToTempOutdir(sourceFileCount, strFiles, sourceDir);
strExtension = "tif";
}
strFiles = Directory.GetFiles(sourceDir, "*." + strExtension);
for (int i = 0; i < sourceFileCount;)
{
File.Copy(strFiles[i], Path.Combine(tempSortDir, downCount.ToString().PadLeft(4, '0') + ".tif"), true);
i++;
downCount--;
File.Copy(strFiles[i], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".tif"), true);
i++;
upCount++;
File.Copy(strFiles[i], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".tif"), true);
i++;
upCount++;
File.Copy(strFiles[i], Path.Combine(tempSortDir, downCount.ToString().PadLeft(4, '0') + ".tif"), true);
i++;
downCount--;
}
strFiles = Directory.GetFiles(tempSortDir, "*.tif");
Thread.Sleep(1000);
sourceDir = tempSortDir;
}
else if (radioSortFlatBed.Checked == true)
{
string tempSortDir = Path.Combine(tempDir, "sort"); ;
Directory.CreateDirectory(tempSortDir);
int downCount = sourceFileCount;
int upCount = 1;
for (int i = 1; i < sourceFileCount;)
{
File.Copy(strFiles[i], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".tif"), true);
i++;
upCount++;
}
File.Copy(strFiles[0], Path.Combine(tempSortDir, upCount.ToString().PadLeft(4, '0') + ".tif"), true);
strFiles = Directory.GetFiles(tempSortDir, "*.tif");
Thread.Sleep(1000);
sourceDir = tempSortDir;
}
ProcessStartInfo startInfo = new ProcessStartInfo();
@ -650,24 +799,24 @@ namespace PDFWorkflowManager
{
toolStripProgressBar1.Control.Invoke((MethodInvoker)delegate
{
toolStripProgressBar1.Value = toolStripProgressBar1.Value + 1;
toolStripProgressBar1.Value++;
});
}
else
{
toolStripProgressBar1.Value = toolStripProgressBar1.Value + 1;
toolStripProgressBar1.Value++;
}
});
});
Thread.Sleep(1000);
string[] inFiles = Directory.GetFiles(sourceDir, "*.tif");
string[] inFiles = Directory.GetFiles(sourceDir, "*." + strExtension);
string[] outFiles = Directory.GetFiles(outputDir, "*.pdf");
txtPageCount.Text = inFiles.Length.ToString();
btnMakePDF.Enabled = true;
// Parallel doesn't always return full result, we pick up the missing and process them again single threaded
if (Directory.GetFiles(sourceDir, "*.tif").Length != outFiles.Length)
if (Directory.GetFiles(sourceDir, "*." + strExtension).Length != outFiles.Length)
{
string[] arrayInFiles = new string[inFiles.Length];
string[] arrayOutFiles = new string[outFiles.Length];
@ -686,14 +835,14 @@ namespace PDFWorkflowManager
foreach (string item in difference)
{
startInfo.Arguments = "\"" + Path.Combine(sourceDir, item + ".tif") + "\"" + " " + "\"" + Path.Combine(outputDir, item) + "\"" + " -l " + selectedLanguage + " pdf txt";
startInfo.Arguments = "\"" + Path.Combine(sourceDir, item + "." + strExtension) + "\"" + " " + "\"" + Path.Combine(outputDir, item) + "\"" + " -l " + selectedLanguage + " pdf txt";
using (var process = Process.Start(startInfo))
{
process.WaitForExit();
}
}
if (Directory.GetFiles(sourceDir, "*.tif").Length != Directory.GetFiles(outputDir, "*.pdf").Length)
if (Directory.GetFiles(sourceDir, "*." + strExtension).Length != Directory.GetFiles(outputDir, "*.pdf").Length)
{
MessageBox.Show("Not all files were converted to PDF");
}
@ -713,7 +862,7 @@ namespace PDFWorkflowManager
{
try
{
string[] outFiles = Directory.GetFiles(workOutDir, "*.tif");
string[] outFiles = Directory.GetFiles(workOutDir, "*." + strExtension);
txtPageCount.Text = outFiles.Length.ToString();
}
catch
@ -770,15 +919,7 @@ namespace PDFWorkflowManager
}
// Create Thumbnail
string[] outFiles;
if (sortNormal)
{
outFiles = Directory.GetFiles(workOutDir, "*.tif");
}
else
{
outFiles = Directory.GetFiles(Path.Combine(tempDir, "sort"), "*.tif");
}
string[] outFiles = Directory.GetFiles(Path.Combine(tempDir, "sort"), "*." + strExtension);
createThumbNail(outFiles[0]);
textToOcrFile(outputDir);