Repeating Cell background from same image causes null reference exception

Guys, When you try to repeat a background image in multiple table cells, the generator spits out a nasty null reference exception. One table cell works, but if you use the same image for more than one cell it blows up. Please help! I saw a post from from May that indicated this same problem. I need this functionality to be working and this limitation is unacceptable from a suite of this caliber.


Thank you,

Jason

Hi Jason,


Thanks for contacting support.

Can you please share the code snippet so that we can test the scenario at our end. We apologize for this inconvenience.
Here is the code, although some elements you won't need, replace the generated paths with hard coded ones.

Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
Aspose.Pdf.Generator.Table tab1 = new Aspose.Pdf.Generator.Table();
sec1.Paragraphs.Add(tab1);
tab1.ColumnWidths = "50% 50%";
tab1.DefaultCellBorder = new Aspose.Pdf.Generator.BorderInfo((int)Aspose.Pdf.Generator.BorderSide.All, 0.1F);
List usrs = PlatformRequest.CurrentEvent.User.ToList(); ;
List.Enumerator nxUsr = usrs.GetEnumerator();

string bgImage = BaseViewModel.PhysicalFilePath(BaseViewModel.EventWebFilePath("Namebadge_Background.jpg"));
bool done = false;
while (!done)
{
for (int rw = 0; rw < 3 && !done; rw++)
{
done = !nxUsr.MoveNext();
if (!done)
{
Aspose.Pdf.Generator.Row row1 = tab1.Rows.Add();
row1.VerticalAlignment = Aspose.Pdf.Generator.VerticalAlignmentType.Center;
row1.FixedRowHeight = 300;
for (int cl = 0; cl < 2 && !done; cl++)
{
row1.Cells.Add(nxUsr.Current.full_name);
done = !nxUsr.MoveNext();
}

//Iterate through all cells in the row and set their background color to blue
foreach (Aspose.Pdf.Generator.Cell curCell in row1.Cells)
{
curCell.BackgroundImageFile = bgImage;
curCell.BackgroundImageType = Aspose.Pdf.Generator.ImageFileType.Jpeg;
}
}
}
}

//Save the Pdf
string outputPath = BaseViewModel.PhysicalFilePath(BaseViewModel.EventWebFilePath("Namebadge.pdf"));
using (FileStream f = System.IO.File.Create(outputPath))
{
pdf1.Save(f);
}

Hi Jason,


Thanks for sharing additional information. Please use following code snippet of new generator(Aspose.Pdf.Document). Hopefully it will help you to achieve your requirements.

Moreover, I would like to update you that we are making improvements/enhancements in new DOM approach and not working over Aspose.Pdf.Generator, as its obsolete now.

String imagePath = myDir + “aspose.imaging-logo.jpg”;
Document pdf = new Document();
Page page1 = pdf.Pages.Add();
page1.PageInfo.IsLandscape = true;

Aspose.Pdf.Table t = new Aspose.Pdf.Table();
t.ColumnWidths = “200 200”;
t.RepeatingRowsCount = 1;
Aspose.Pdf.Row row1 = t.Rows.Add();
row1.FixedRowHeight = 10;
row1.BackgroundColor = Aspose.Pdf.Color.LightGray;
row1.Cells.Add(“Header 1”);
row1.Cells.Add(“Header 2”);
for (int i = 0; i < 3; i++)
{

row1 = t.Rows.Add();
row1.FixedRowHeight = 200;
Aspose.Pdf.Cell cell1 = row1.Cells.Add(“Data 1-” + i);
cell1.BackgroundImageFile = imagePath;
Aspose.Pdf.Cell cell2 = row1.Cells.Add(“Data 2-” + i);
cell2.BackgroundImageFile = imagePath;
}

page1.Paragraphs.Add(t);

pdf.Save(myDir+“TableDOM.PDF”);

Please feel free to contact us for any further assistance.

Best Regards,

Thank you Tilal, this is working much better.


Please have the development team clearly mark the old object model as “Deprecated” in all code description blocks. There was no indicator that a new object model was present and I was using sample code from your team. Putting deprecated at the beginning of every description will alert your customers that something new is available with better capabilities.

Have a great day,

Jason

Hi Jason,


Aspose.Pdf.Generator namespace is legacy Aspose.Pdf for .NET (before v6.0.0) which provides the capability to create PDF files from scratch and Aspose.Pdf namespace was introduced in Aspose.Pdf for .NET 6.0.0, which provides the capability to create as well as manipulate existing PDF files. The reason we are keeping Aspose.Pdf.Generator namespace (legacy Aspose.Pdf for .NET) is that many of our old customers are still using this namespace. Nevertheless, we are encouraging our customers to migrate their code to new DOM API.

I understand, thank you. However, marking the code as deprecated in documentation does nothing to prevent current users from continuing to use the old code. It only serves as a warning to new users to start using more current forward looking code. If you don’t mark the code as deprecated, then new users like me will start development projects using the old code and that will slow down the forward migration to new code and create many new support requests for old code that you want to move away from. Clearly marking the code as deprecated is more of a favor to you than to us because the sooner people stop using the old code, the less support volume you will have to manage.


Hope this helps, and thank you very much for introducing me to the new Pdf namespace.

Jason

Hi Jason,


Thanks for sharing the details. We will consider marking the old/legacy approach as deprecated so that users can migrate to new Document Object Model approach. Please do let me know if I can be of any further assistance.

Hi Jason,


We have further looked into the request of marking old/legacy approach of creating tables with Aspose.Pdf.Generator approach and I am afraid currently we cannot mark it as deprecated, until the new DOM approach/structure supports all the features/functionalities currently being offered by Aspose.Pdf.Generator namespace.

Nevertheless, we keep encouraging our customers to migrate to new DOM approach because most of the enhancements and bug fixes are now being made in new approach.

In case I can be of any further assistance, please feel free to contact.