Word docx file not opening after inserting footer using Aspose Words for Java lib

Hi
I am processing word file to insert footer using Aspose Words API for Java 23.12. In standalone code it works perfectly fine.

But when i download the file to a location in bytes using the following code snippet:

try
{
    for (int length = 0; (length = content.read(bytes)) > 0;)
    {
        try
        {
            fos.write(bytes, 0, length);
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    fos.close();

Then do the processing of inserting footer using Aspose Words in this location using below code(which works well in standalone mode), is failing for docx and dotx file. For doc and dot file it worked fine.

String FooterText = "Once printed, this document is reference only. This document is valid for /on the date printed.  Please reference Agile for the latest revision";
footerText = FooterText.substring(0, 74);
footerText1 = FooterText.substring(74);
String fname = file.getName();
obj.logMonitor("Fname " + fname);
com.aspose.words.Document doc = new Document(fileDir + File.separator + fname);
com.aspose.words.DocumentBuilder builder = new com.aspose.words.DocumentBuilder(doc);

for (Section s : doc.getSections())
{
    s.getPageSetup().setDifferentFirstPageHeaderFooter(false);
    s.getPageSetup().setOddAndEvenPagesHeaderFooter(false);
    // Move to the section.
    builder.moveToSection(doc.getSections().indexOf(s));
    // Move to footer and put some text
    builder.moveToHeaderFooter(com.aspose.words.HeaderFooterType.FOOTER_PRIMARY);
    //   builder.moveToHeaderFooter(com.aspose.words.HeaderFooterType.FOOTER_EVEN);
    //   builder.moveToHeaderFooter(com.aspose.words.HeaderFooterType.FOOTER_FIRST);
    builder.getFont().setName("Arial");
    builder.getFont().setBold(false);
    builder.getFont().setSize(10);
    builder.write(footerText);
    //builder.write("Date ");
    //commenting below lines on 05/16/2024 and trying another approach using LocalDateTime
    //	builder.insertField(" DATE", "");
    //trying to print time also 05/16/2024
    //	builder.write(" ");
    //	builder.insertField(" TIME", "");

    LocalDateTime today = LocalDateTime.now();
    builder.write(" " + today.getMonthValue() + "/" + today.getDayOfMonth() + "/" + today.getYear() + " " + today.getHour() + ":" + today.getMinute());
    builder.write(footerText1);
}

doc.save(fileDir + File.separator + fname);

When tried to open the docx file it says

When tried to open the dotx file it says

The files having the issue is

issue file.zip (1.9 MB)

The sample file where the footer is inserted is

sample file.zip (1.9 MB)

Can you let us know what may be the issue?

Thanks
Rama

@Rama_Menon The problematic document is damaged. As you may know DOCX and DOTX files are ZIP archives with a bunch of XML files inside. ZIP files in issue file are damaged. Most likely they were written improperly. Please try saving the document to file and check whether the file is valid. If so the problem is in the code where file bytes are written to the file.

@alexey.noskov Thanks for the reply.Issue is resolved.

1 Like