Date localization and formatting for PDF exporting

Hi


I’m currently working on generating PDF files from projects in a web application, and must take the current user’s culture setting into account. I’ve successfully localized and formatted the four custom columns as you can see in the attached image, but I can’t find how to format and localize the dates and texts above the “graphical” part of the image, the “Oct '13”, “Nov 13’”, …, and the “B”-“M”-“E” parts.

How do you localize these?

Hi Lars,

Thank you for writing to Aspose Support team.

At present, we could not find any such method to achieve this. We have further requested our product team to share their feedback about this requirement. We’ll inform you here as soon as the information is available about this.

Hi Lars,

You can format the date as shown in the following code sample, but it can’t be localized to the local culture at present. An enhancement ticket with id: TASKS-34120 has been logged in our issue tracking system for consideration by our Product team towards provision of this feature. We’ll notify you here once there is some update available in this regard.


Sample Code:



Project project = new Project(“NewProductDev.mpp”); // test project (attached)


// default view of this test project is Gantt Chart view

GanttChartView view = project.DefaultView as GanttChartView;

// customize labels

// September 23, … for middle tier

view.MiddleTimescaleTier.Label = DateLabel.Week_mmmm_dd;

// M, T, W… for bottom tier

view.BottomTimescaleTier.Label = DateLabel.Day_di;

project.Save(“Day_di.pdf”, SaveFileFormat.PDF);


// Sep 23, … for middle tier

view.MiddleTimescaleTier.Label = DateLabel.Week_mmm_dd;

// Mon, Tue, Wed… for bottom tier

view.BottomTimescaleTier.Label = DateLabel.Day_ddd;

project.Save(“Day_ddd.pdf”, SaveFileFormat.PDF);


// Sep 23, '14 … for middle tier

view.MiddleTimescaleTier.Label = DateLabel.Week_mmm_dd_yyy;

// Monday, Tuesday, Wednesday… for bottom tier

view.BottomTimescaleTier.Label = DateLabel.Day_dddd;

project.Save(“Day_dddd.pdf”, SaveFileFormat.PDF);

Hi again


When creating our Aspose projects, we don’t create them from .mpp files. Instead we are generating the tasks and links programmantically. Therefore, our projects don’t have a default project view, or any views at all actually. How can I access the GanttChartView used when saving to PDF, when there are no views in the Views collection on the project?

Best regards,
Lars

Hi Lars,

Please try creating new GanttChartView as shown in the following sample code:

Sample Code:

GanttChartView view = new GanttChartView();
project.Views.Add(view);

Yes, that was the first thing I tried. But I get a null reference exception as a result.


System.NullReferenceException: Object reference not set to an instance of an object.

at Aspose.Tasks.Project.(String )
at Aspose.Tasks.Project.()
at ​ .()
at ​ …ctor(Project , ​ , SaveOptions )
at .(SaveOptions )
at .(Stream , SaveOptions )

It works fine if I remove the GanttChartView part.

Example code:

private void SaveAsPdf(Project project, MemoryStream stream, CultureInfo culture)
{
Aspose.Tasks.Project asposeProject = projectConverter.ConvertToAsposeProject(project);

GanttChartView view = new GanttChartView();
asposeProject.Views.Add(view);

view.MiddleTimescaleTier.Label = DateLabel.Quarter_Qq;
view.MiddleTimescaleTier.Label = DateLabel.Month_mm;

var saveOptions = new PdfSaveOptions()
{
PresentationFormat = PresentationFormat.GanttChart,
FitContent = true,
PageSize = Aspose.Tasks.Visualization.PageSize.A3,
LegendOnEachPage = false,
Timescale = Timescale.Months,
View = GetProjectViewForPdfExport(culture)
};

asposeProject.Save(stream, saveOptions);
}

private ProjectView GetProjectViewForPdfExport(CultureInfo culture)
{
var columns = new List()
{
new GanttChartColumn(this.keyLocalizer.Translate(“Task_Name”, culture), 150, FormatTaskName),
new GanttChartColumn(this.keyLocalizer.Translate(“Task_StartDate”, culture), 80, x => x.Get(Tsk.Start).ToShortDateString()),
new GanttChartColumn(this.keyLocalizer.Translate(“Task_EndDate”, culture), 80, x => x.Get(Tsk.Finish).ToShortDateString()),
new GanttChartColumn(this.keyLocalizer.Translate(“Task_Duration”, culture), 60, x => FormatDuration(x, culture)),
};

return new ProjectView(columns);
}

private string FormatTaskName(Task task)
{
int wbsLevel = int.Parse(task.Get(Tsk.WBSLevel));
string taskName = task.Get(Tsk.Name);

return taskName.PadLeft(taskName.Length + wbsLevel * 3); // Indent 3 white spaces per wbs level
}

private string FormatDuration(Task task, CultureInfo culture)
{
return string.Format("{0} {1}", (int)MathUtils.Ceiling(task.Get(Tsk.Duration).TimeSpan.TotalDays, 0), this.keyLocalizer.Translate(“Days”, culture));
}

Hi Lars,


The PresentationFormat.GanttChart should have worked this way. We have tried to execute your sample code but are getting a number of missing classes, raising compilation errors. Can you please share a sample console application for our reference to reproduce the problem at our end? We shall analyze the problem at our end and assist you further.
Hi!

I've attached a sample project that shows the null reference exception issue

/Lars

Hi Lars,


I have tested the project, and observed the issue. It is logged in our issue tracking system under id: TASKS-34126 for further investigation by the product team. I shall write here as soon as some feedback is received in this regard.

The issues you have found earlier (filed as TASKS-34126) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

The issues you have found earlier (filed as TASKS-34120) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.

Hi again!


Nice to see that you added support for localization (TASKS-34120).
How do I use this? Can you provide an example where you localize the column headers and use, for example, swedish localization for the M T W T F S S parts?

Regards,
Lars

Hi Lars,

Thank you for writing to Aspose support again.

I have checked the documentation and found that date labels will be rendered with CultureInfo.CurrentCulture now. You may give a try to the following sample code and let us know the feedback.

Project project = new Project("Sample.mpp");
// default view of this test project is Gantt Chart view
GanttChartView view = project.DefaultView as GanttChartView;
// customize labels
// September 23, ... for middle tier
view.MiddleTimescaleTier.Label = DateLabel.WeekMmmmDd;
// M, T, W.. for bottom tier
view.BottomTimescaleTier.Label = DateLabel.DayDi;
project.Save(path + "Day_di.pdf", SaveFileFormat.PDF);
// Sep 23, ... for middle tier
view.MiddleTimescaleTier.Label = DateLabel.WeekMmmDd;
// Mon, Tue, Wed.. for bottom tier
view.BottomTimescaleTier.Label = DateLabel.DayDdd;
project.Save(path + "Day_ddd.pdf", SaveFileFormat.PDF);
// Sep 23, '14 ... for middle tier
view.MiddleTimescaleTier.Label = DateLabel.WeekMmmDdYyy;
// Monday, Tuesday, Wednesday.. for bottom tier
view.BottomTimescaleTier.Label = DateLabel.DayDddd;
project.Save(path + "Day_dddd.pdf", SaveFileFormat.PDF);

Hi!


It does not work for me. I’ve attached a sample project where I set the current thread’s culture to swedish and then save a project as PDF. I still get english weekday names.

Regards,

Hi Lars,


We have reviewed the problem and observed that Weekday labels are fixed and do not consider the current CultureInfo. I am afraid that this requirement may not be fulfilled by Aspose.Tasks. Please feel free to share your thoughts in this regard.

Ok, too bad.

Then I have to do the best I can with the date labels that exist. Though it seems that the date label settings when using the month time scale have no effect. I have attached a sample project where I use two different date labels settings for the middle and bottom tiers. The result is the same. Why is it so?

Hi Lars,

Thank you for providing feedback.

Regarding the date localization according to the selected CultureInfo, I have logged an investigation ticket to implement this enhancement under Id: TASKS-34380 in our issue tracking system. I shall write here as soon as some feedback is received about this enhancement.

For the second issue where using two different label settings are not effective, please update your following function in the code and test the scenario again.

    view.MiddleTimescaleTier.Label = middleLabel;
    //THIS LINE IS ADDED
    view.MiddleTimescaleTier.Unit = TimescaleUnit.Quarters;
    view.BottomTimescaleTier.Label = bottomLabel;
    //THIS LINE IS ADDED
    view.BottomTimescaleTier.Unit = TimescaleUnit.Months;
    var saveOptions = new PdfSaveOptions()
    {
        PresentationFormat = PresentationFormat.GanttChart,
        FitContent = true,
        PageSize = PageSize.A4,
        LegendOnEachPage = false,
        View = GetProjectViewForPdfExport()
    };
    asposeProject.Save(stream, saveOptions);
}

Adding the units to the tiers makes no difference for me. The two resulting PDF documents are still identical after I updated my test project. I’ve attached this test project.


If you run it, do you get another result?

Regards,
Lars

Hi Lars,


I have updated your previous project sent in this post and replaced the function ExportAsPdf () as given above. I get two different PDF as attached here for your reference. I am attaching the modified project again for your testing. Please give it a try and let us know the feedback.

P.S. I tested your code in the current post and got same PDF files. Seems there is some difference in both the projects.

I updated the dll to the 9.1.0 version and now it works as expected. Thank you!