How to Reduce Exported .MPP File Size Programmatically Using Aspose.Tasks for .NET

Hi Aspose Team,

We are using Aspose.Tasks for .NET to programmatically export Microsoft Project files (.mpp) in our web application.

Requirement:
We need to reduce the actual file size of the exported .mpp files as much as possible, while maintaining file integrity and compatibility with Microsoft Project. Compressing the file into a .zip or .gz is not an option, as the receiving system only accepts raw .mpp files.

What we’ve tried/considered:

  • We are already generating the .mpp file using Project.Save() method.
  • We are aware of standard compression (zip/gzip), but this is not acceptable for our workflow.
  • We have considered removing unused resources and tasks manually before saving, but are not sure if there are other best practices or settings in Aspose.Tasks to further minimize file size.

Questions:

  1. Are there any Aspose.Tasks API methods or save options that help minimize the size of the exported .mpp file?
  2. Are there recommended practices for cleaning up project data (e.g., removing unused objects, attachments, custom fields, notes, etc.) to further reduce file size programmatically using Aspose.Tasks?
  3. Are there any save settings (such as file format version) that typically result in smaller .mpp files?

Sample code currently used:

csharp

CopyEdit

// project is an Aspose.Tasks.Project object
project.Save("output.mpp", SaveFileFormat.MPP);

Any guidance, sample code, or recommended approaches to slim down the .mpp files would be greatly appreciated!

@civ2cob

Cause

To reduce the size of exported .mpp files using Aspose.Tasks for .NET, several factors must be considered, including the removal of unused resources, tasks, and other project data that may contribute to the file size.

Solution

  1. Remove Unused Resources and Tasks: Before saving the project, ensure that you remove any resources, tasks, or assignments that are not being used. This can significantly reduce the file size.

  2. Optimize Project Data: Clean up project data by removing unnecessary custom fields, notes, and attachments. This will help minimize the file size.

  3. Use Save Options: When saving the project, specify certain save options that might help in reducing the file size. For example, saving in a different format or adjusting the file format version can yield smaller file sizes.

Code

Here’s a sample code snippet demonstrating how to remove unused resources and save the project:

using Aspose.Tasks;

// Load the project
var project = new Project("input.mpp");

// Remove unused resources
foreach (var resource in project.Resources.ToList())
{
    if (resource.AssignmentCount == 0)
    {
        project.Resources.Remove(resource);
    }
}

// Optionally, remove tasks that are not needed
foreach (var task in project.RootTask.Children.ToList())
{
    if (task.GetActualDuration() == TimeSpan.Zero) // Example condition
    {
        project.RootTask.Children.Remove(task);
    }
}

// Save the optimized project
project.Save("output.mpp", SaveFileFormat.MPP);

Next Steps

  1. Implement the above code to clean up your project data before saving.
  2. Test the output file size to ensure it meets your requirements.
  3. If further size reduction is needed, explore additional optimizations based on your specific project data.

If you have any questions or need further assistance, feel free to ask!

I have updated code as below, Can we compress still?

public FileStream ProjectToStream(Project project)
{
    string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".mpp");
    try
    {
        // Remove unused resources
        foreach (var resource in project.Resources.ToList())
        {
            if (resource.Assignments.Count == 0)
            {
                project.Resources.Remove(resource);
            }
        }

        using (var fs = new FileStream(tempPath, FileMode.Create, FileAccess.ReadWrite))
        {
            project.Save(fs, SaveFileFormat.MPP);
        }

        return new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.DeleteOnClose);
    }
    catch (Exception ex)
    {
        throw new IOException("Error saving project to stream.", ex);
    }
}

@civ2cob ,

The answer depends on the answer to the following question:

Do you create resulting MPP from the scratch (from the empty project) or from user-uploaded mpp files ?

User-uploaded large enterprise MPP files may contain View data with custom RTF headers and footers with the total size of several MBytes.

Also Group and Filters data can be removed.

If VBA is present, it also can be cleared using MPPSaveOptions.ClearVba flag:

var p = new Project("EmptyProject.mpp");
p.Views.Clear();
p.TaskFilters.Clear();
p.ResourceFilters.Clear();
p.Tables.Clear();

p.Save(
    "output.mpp",
    new MPPSaveOptions()
    {
        WriteGroups = true,
        WriteFilters = true,
        WriteViewData = true,
        ClearVba = true
    });

The code above shrinks empty project MPP file from 250K to 127K.

Further minimization is quite problematical

The removal of a task yields ~ 600 bytes.
The removal of task extended attribute (custom field) yields 10-20 bytes * count of tasks in the project. Looks like it doesn’t worth it.

If the project’s entities (tasks, resource and assignments) contains notes with RTF formatting and/or embedded pictures, not just plain text, you can remove them also. But i think it’s a rare case.