So, I have an app that go through a folder with doc and docx files and replaced images with a new one. The app works fine, but I have some barcodes in the document with specific font. After the program run through the document it change the font only for the bar code. does anyone have any ideas to tackle this?
base code:
/*
* A method that make sure folder exist and files in that certain folder exists.
*/
public void Run()
{
Console.ForegroundColor = ConsoleColor.Gray;
try
{
if (Directory.Exists(folderPath)) // Check if Folder exist
{
string[] files = Directory.GetFiles(folderPath, "*.doc*"); // Get all .doc or .docx files in folder
if(files.Length > 0)
{
foreach (string filePath in files)
{
ProcessDocument(filePath); // Process each files in the folder
}// end of foreach
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"No files found in folder: {Path.GetFileName(folderPath)}");
Console.ForegroundColor = ConsoleColor.Gray;
}
}// end of if statement
else
{
Console.WriteLine("Folder not found.");
}// end of if-else
}// end of Try
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Error finding folder: " + ex.Message);
Console.ForegroundColor = ConsoleColor.Gray;
}// end of Catch
}// end of Run
/*
* A method that start the file processing of each section of the word documents.
*/
private void ProcessDocument(string filePath)
{
fileCount ++;
Console.WriteLine($"\nProcessing File \"{fileCount}\": {Path.GetFileName(filePath)}");
try
{
if( doc != null && doc.Sections != null )
{
doc.LoadFromFile(filePath); // Load file into Document Object
//CreateCopyOfFile(folderPath, backupPath);
foreach (Section section in doc.Sections)
{
ProcessSection(section, filePath); // Go through each section in side the Word document
}// end of outter-outter foreach
if(imageCount == 0 && !isMoved)
{
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine($"\tNo Picture Found in doc {Path.GetFileName(filePath)}");
Console.ForegroundColor = ConsoleColor.Gray;
}// end of if-statement
if(!isMoved) // If isMoved is true dont save file. This is a check for if file was moved an converted to .docx
{
FileFormat fileFormat = filePath.EndsWith(".docx", StringComparison.OrdinalIgnoreCase) ? FileFormat.Docx : FileFormat.Doc; // Determine the file format based on the file extension
doc.SaveToFile(filePath, fileFormat);
}// end of if-statement isMoved
Console.WriteLine($"\t\t\tClosed File: {Path.GetFileName(filePath)}");
imageCount = 0;
isMoved = false;
}// end of If-Statement
}// end of Try
catch (NullReferenceException ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"!!!Error processing file {Path.GetFileName(filePath)}: {ex.Message}!!!");
Console.ForegroundColor = ConsoleColor.Gray;
MoveFileToTargetFolder(filePath);
}// end of catch
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"!!!Error processing file {Path.GetFileName(filePath)}: {ex.Message}!!!");
Console.ForegroundColor = ConsoleColor.Gray;
}// end of catch
}// end of ProcessDocument
/*
* A method that process each paragraph of the word documents. This method will detect images or textboxes and actions will be taken based on that.
*/
private void ProcessSection(Section section, string filePath)
{
try
{
foreach (Paragraph paragraph in section.Paragraphs)
{
foreach (DocumentObject docObj in paragraph.ChildObjects)
{
if (docObj.DocumentObjectType == DocumentObjectType.Picture)
{
DocPicture newPicture = docObj as DocPicture; // Create a new DocPicture with the desired image
if(newPicture.Width < imageMaxWidth && newPicture.Width > imageMinWidth
&& newPicture.Height < imageMaxHeight && newPicture.Height > imageMinHeight) // Only change the image the width is bigger than 90px and smaller than 250px and height smaller than 100px and larger than 50px
{
isImageChanged = true;
imageCount++; // Increment image count if a picture is found
newPicture.LoadImage(Image.FromFile(imagePath)); // Replace image found in document with new image
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine($"\tChanged Image \"{imageCount}\" in file: {Path.GetFileName(filePath)}");
Console.ForegroundColor = ConsoleColor.Gray;
}// end of inner if-statement
}// end of if-statement
else if(docObj.DocumentObjectType == DocumentObjectType.TextBox)
{
Console.WriteLine($"\t\tTextbox detected in file: {Path.GetFileName(filePath)}.");
ConvertToDocx(filePath); // Convert the file to .docx
isMoved = true;
return;
}
}// end of inner for each
// Log if a paragraph contains the barcode font and is skipped
}// end of outter foreach
if(isImageChanged == true && imageCount > 0)
{
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine($"\t\tTotal images found in {Path.GetFileName(filePath)}: {imageCount}");
Console.ForegroundColor = ConsoleColor.Gray;
isImageChanged = false;
}
}// end of Try
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"\t\tError looking for a picture in file {Path.GetFileName(filePath)}: {ex.Message}\n\tMoving to \"Error\" folder");
Console.ForegroundColor = ConsoleColor.Gray;
}// end of catch
}// end of ProcessSection
This is what I am talking about