Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Mar 26, 2025 5:28 pm

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?

The font is already installed so that rules the font does not exist.

Codebase:

Code: Select all
/*
  * 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



See attached for pictures.

maiza989
 
Posts: 5
Joined: Thu Mar 07, 2024 1:34 pm

Thu Mar 27, 2025 1:29 am

Hello,

Thanks for your inquiry.
To facilitate our further investigation, please provide a Word file that can reproduce your issue. You can upload it to the attachment or send it to this email: [email protected]. Thanks in advance.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 657
Joined: Mon Dec 27, 2021 2:23 am

Tue Apr 01, 2025 3:51 pm

Hey,
William.

I just sent you an email with the word document I was testing with.

maiza989
 
Posts: 5
Joined: Thu Mar 07, 2024 1:34 pm

Tue Apr 01, 2025 7:08 pm

Please see attached Word document sample I used.

maiza989
 
Posts: 5
Joined: Thu Mar 07, 2024 1:34 pm

Wed Apr 02, 2025 1:33 am

Hello,

Thansk for your reply.
I have checked your Word file and did not find any barcodes in this file. I speculate that you will merge fields and replace them with barcodes in the future. Therefore, we suggest that you provide us with the merged barcode file to help us reproduce and investigate the issue more quickly. If there is any misunderstanding, please remember to tell us more details about your issue. Thank you in advance.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 657
Joined: Mon Dec 27, 2021 2:23 am

Fri Apr 04, 2025 12:42 pm

William.Zhang wrote:Hello,

Thansk for your reply.
I have checked your Word file and did not find any barcodes in this file. I speculate that you will merge fields and replace them with barcodes in the future. Therefore, we suggest that you provide us with the merged barcode file to help us reproduce and investigate the issue more quickly. If there is any misunderstanding, please remember to tell us more details about your issue. Thank you in advance.

Sincerely,
William
E-iceblue support team


Yes, This is a template with merge fields set up to pull correct information based on the provided data. From my testing, if there is a hardcoded barcode the program does not change font and leave it as IDAutomationHC font. But when a merge field is set up with barcode font it will edit it.

In the same provided Word file it has the merge field already set up. While in the document you can press Alt + F9 to view the set up of the merge fields. Providing an example of a file already merged file defeat the purpose of my program. As I am trying to edit thousands of template with it before a merge is completed.

maiza989
 
Posts: 5
Joined: Thu Mar 07, 2024 1:34 pm

Mon Apr 07, 2025 6:08 am

Hello,

Thanks for your reply.
After further testing, I have reproduced the issue you mentioned. I have logged this issue to our tracking system with ticket SPIREDOC-11195. Our development team will further investigate and fix it. Once there is any progress, I will inform you in time.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 657
Joined: Mon Dec 27, 2021 2:23 am

Thu May 22, 2025 1:06 pm

Is there any updates regrading this issue?
William.Zhang wrote:Hello,

Thanks for your reply.
After further testing, I have reproduced the issue you mentioned. I have logged this issue to our tracking system with ticket SPIREDOC-11195. Our development team will further investigate and fix it. Once there is any progress, I will inform you in time.

Sincerely,
William
E-iceblue support team

maiza989
 
Posts: 5
Joined: Thu Mar 07, 2024 1:34 pm

Fri May 23, 2025 6:50 am

Hello,

Thanks for your following up.
Apologies, but we haven't made significant progress on issue SPIREDOC-11195 yet. Our development team is still working on it. If there are any updates, I'll notify you as soon as possible.

Sincerely,
William
E-iceblue support team
User avatar

William.Zhang
 
Posts: 657
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.Doc