Convert Project to PDF in C++

Aspose.Tasks for C++ API provides the capability to render project data to PDF format. This article gives a detailed overview of the variety of options available in Aspose.Tasks for exporting projects to PDF.

Saving a Project as a PDF

The Project class exposes the Save method which is used to save a project in various formats. The Save method allows you to render project data to PDF using the SaveFileFormat enumeration type.

To save a project to PDF:

  1. Load a Microsoft Project file.
  2. Save the project to PDF using SaveFileFormat.PDF.

The following lines of code demonstrate how to achieve this using C++.

1// Read the input Project file
2System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"CreateProject2.mpp");
4// Save the Project as PDF
5project->Save(dataDir + u"SaveProjectAsPDF_out.pdf", Aspose::Tasks::Saving::SaveFileFormat::PDF);

Fitting Contents to Cell Size

It is common that a task (or resource) name is so long that it is truncated when project views are rendered. Aspose.Tasks for C++ API provides the FitContent property in the SaveOptions class to avoid truncation of task and resource names. The code example given below renders a project to PDF format with the FitContent property set to true.

1System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"CreateProject2.mpp");
2System::SharedPtr<SaveOptions> saveOptions = System::MakeObject<PdfSaveOptions>();
4// Set option fit content to true
8project->Save(dataDir + u"FitContentsToCellSize_out.pdf", saveOptions);

Printing or Hiding Legends when Rendering

To let you print or hide the legends on each page, the SaveOptions class provides the LegendOnEachPage property. If this flag is set to true, legends are printed on each page in the output file.

1System::SharedPtr<SaveOptions> saveOptions = System::MakeObject<PdfSaveOptions>();
3// Set the LegendOnEachPage property to false to hide legends

Supported Graphical Column Indicators

Aspose.Tasks for C++ API draws graphical column indicators when rendering project data to output PDF. The following graphical indicators are supported by Aspose.Tasks:

Indicator TypeGraphical Indicator
Task Indicatorstypes of task indicators in Microsoft Project
Resource Indicatorstypes of resource indicators in Microsoft Project
Assignment Indicatorstypes of assignment indicators in Microsoft Project

Saving to Multiple PDF Files

To save project data to multiple PDF files, set the SaveToSeparateFiles flag to true.

1System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"Software Development Plan.mpp");
2System::SharedPtr<PdfSaveOptions> saveOptions = System::MakeObject<PdfSaveOptions>();
7project->Save(dataDir + u"SaveToMultiplePDFFiles_out.pdf", System::StaticCast<Aspose::Tasks::Saving::SaveOptions>(saveOptions));

Customizing TextStyle for Project Data

Aspose.Tasks for C++ API allows developers to customize the text style for overallocated resources. By default, the style used for overallocated resources is similar to Microsoft Project (MSP), that is, red and bold. TextItemType.OverallocatedResources makes it possible to customize the color and style for the overallocated resources.

 1System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"CreateProject2.mpp");
 2System::SharedPtr<SaveOptions> options = System::MakeObject<PdfSaveOptions>();
 5System::SharedPtr<TextStyle> style = System::MakeObject<TextStyle>();
 8System::setter_or_wrap(style.GetPointer(), &TextStyle::get_FontStyle, &TextStyle::set_FontStyle, System::Drawing::FontStyle::Italic);
13project->Save(dataDir + u"CustomizeTextStyle_out.pdf", options);

Customizing Date Formats

Aspose.Tasks for C++ API allows developers to customize the date format using the DateFormat enumerator when rendering project data.

1System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"CreateProject2.mpp");
3auto options = [&]{ auto tmp_0 = System::MakeObject<PdfSaveOptions>(); tmp_0->set_PresentationFormat(Aspose::Tasks::Visualization::PresentationFormat::GanttChart); tmp_0->set_FitContent(true); tmp_0->set_UseProjectDefaultFont(false); tmp_0->set_DefaultFontName(u"Segoe UI Black"); return tmp_0; }();
4project->Save(dataDir + u"CreateProject2_out.pdf", System::StaticCast<Aspose::Tasks::Saving::SaveOptions>(options));

Setting Default Font

Setting default font during rending of documents helps when a font is not found. In such a case, the default font replaces the missing font, and output is not affected. Aspose.Tasks for C++ API lets you specify the default font using the DefaultFontName property of the PdfSaveOptions as shown in the following code sample.

 1System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"CreateProject2.mpp");
 2project->Set(Prj::StartDate(), System::DateTime(2014, 9, 22));
 4// By default project.DateFormat == DateFormat.Date_ddd_mm_dd_yy (Mon 09/22/14) customize DateFormat (September 22, 2014)
 5project->Set<DateFormat>(Prj::DateFormat(), Aspose::Tasks::DateFormat::DateMmmmDdYyyy);
 6project->Save(dataDir + u"CustomizeDateFormats1_out.pdf", Aspose::Tasks::Saving::SaveFileFormat::PDF);
 8// Export to date format 19/07/2016
 9project->Set<DateFormat>(Prj::DateFormat(), Aspose::Tasks::DateFormat::DateDdMmYyyy);
10project->Save(dataDir + u"CustomizeDateFormats2_out.pdf", Aspose::Tasks::Saving::SaveFileFormat::PDF);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.