Setting the Border on specific cells within Table

Hello, I’m using Aspose.Pdf.Generator version 8.3.1.0 and I’m trying to change the border on certain cells within my Table such that when a specific value is encountered, I want to set the top border to a blue line. However, this is not working properly as only the top border of the table is changing whereas I want the border on the individual cells to be changed . Here is my code:


using Aspose.Pdf.Generator;
dataTable.ImportDataTable(ConstructDummyDataTable(), false, 0,0);

foreach (Row row in dataTable.Rows)
{
string value = row.Cells[Constants.ICDDESCRIPTION_IDX].GetText();
if (value.Equals(“Total”))
{
GraphInfo line = new GraphInfo();
line.DashLengthInBlack = 2.5F;
line.LineWidth = 2.0F;
line.IsAddedArrowAtEnd = true;
line.Color = new Color(“Blue”);

BorderInfo bdrTotalCell = new Aspose.Pdf.Generator.BorderInfo((int)Aspose.Pdf.Generator.BorderSide.Top, line);

Aspose.Pdf.Generator.TextInfo txtBoldInfo = new Aspose.Pdf.Generator.TextInfo();
txtBoldInfo.IsTrueTypeFontBold = true;
txtBoldInfo.FontSize = 10F;
txtBoldInfo.FontName = “Calibri”;
row.DefaultCellTextInfo = txtBoldInfo;

row.Cells[Constants.LOCALINPUT_IDX].Border = bdrTotalCell;
row.Cells[Constants.USADJ_IDX].Border = bdrTotalCell;
row.Cells[Constants.USGAAP_IDX].Border = bdrTotalCell;
}
}

Please note that for some reason the borders on the individual cells are not appearning. Please help. I’ve been working over the thanksgiving holiday to get this to work properly and I’ve tried everything.

Thanks.

Hi Henry,

Thanks for contacting support and sorry for the inconvenience which you have been facing.

Please note that the Aspose.Pdf.Generator namespace has some issues when creating table, row, cells objects. Therefore in order to produce correct output, please try using the new Document Object Model approach of Aspose.Pdf for .NET. I have tried setting the top border of one row as Blue and its properly appearing. For your reference, I have also attached the resultant PDF generated with Aspose.Pdf for .NET 8.6.0.

C#

// Instantiate the Document object by calling its empty constructor
Document doc = new Document();

// Instantiate a table object
Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();

// Add the table in paragraphs collection of the desired section
page.Paragraphs.Add(tab1);

// Set with column widths of the table
tab1.ColumnWidths = "50 50 200";

// Set default cell border using BorderInfo object
tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F);

// Set table border using another customized BorderInfo object
tab1.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);

// Create MarginInfo object and set its left, bottom, right and top margins
Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo();
margin.Top = 5f;
margin.Left = 5f;
margin.Right = 5f;
margin.Bottom = 5f;

// Set the default cell padding to the MarginInfo object
tab1.DefaultCellPadding = margin;

// Create rows in the table and then cells in the rows
Aspose.Pdf.Row row1 = tab1.Rows.Add();
row1.Cells.Add("col1");
row1.Cells.Add("col2");
row1.Cells.Add();
TextFragment mytext = new TextFragment("col3 with large text string");
row1.Cells[2].Paragraphs.Add(mytext);
row1.Cells[2].IsWordWrapped = false;
row1.Cells[2].Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.Top, Aspose.Pdf.Color.Blue);

Aspose.Pdf.Row row2 = tab1.Rows.Add();
row2.Cells.Add("item1");
row2.Cells.Add("item2");
row2.Cells.Add("item3");

// Save the Pdf
doc.Save("c:/pdftest/TableCell_Border.pdf");

Hi there, do you have any code samples using the Aspose.PDF namespace and not the Aspose.PDF.Generator namespace. It looks like I’m going to have to redo my entire solution using the Aspose.PDF namespace and I need some samples. The samples provided on your website are in the Aspose.Pdf.Generator namespace. I would like code samples of how to create a header/footer on each page, how to set the page to a landscape on the margins, and how to import data from a datatable. I’m not very satisfied with the product now considering that everything that I have written will now have to be rewritten.

Thanks.

Hi Henry,

Due to technical difficulties and limitations in legacy Aspose.Pdf.Generator namespace, we had to rework and introduce the new Document Object Model based on Aspose.Pdf namespace. With every new release, we are trying our level best to introduce same set of features/functionalities in Aspose.Pdf namespace as present in Aspose.Pdf.Generator. Please visit the following links for further details:

I am afraid currently Aspose.Pdf namespace does not support the feature to convert Portrait page to Landscape orientation. However for the sake of implementation, I have logged this issue as PDFNEWNET-36115 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

The issues you have found earlier (filed as PDFNEWNET-36115) have been fixed in Aspose.Pdf for .NET 9.6.0.

Hi Henry,

Thanks for your patience. As stated above your reported issue is resolved in Aspose.Pdf for .NET 9.6.0. Note that the Rotation property changes the orientation of the entire page, including its contents. To change the page size, you can set the MediaBox of the page as follows.

// Load the existing PDF document
Document doc = new Document("PdfWithText.pdf");

// Iterate through each page in the document
foreach (Page page in doc.Pages)
{
    // Get the original page dimensions (MediaBox)
    Aspose.Pdf.Rectangle r = page.MediaBox;

    // Swap the height and width for landscape orientation
    double newHeight = r.Width;
    double newWidth = r.Height;

    // Preserve the lower-left X coordinate (LLX)
    double newLLX = r.LLX;

    // Move the lower-left Y coordinate (LLY) upwards to adjust for the new height
    double newLLY = r.LLY + (r.Height - newHeight);

    // Set the new MediaBox dimensions for the page
    page.MediaBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);

    // If a CropBox was set, adjust it as well
    page.CropBox = new Aspose.Pdf.Rectangle(newLLX, newLLY, newLLX + newWidth, newLLY + newHeight);
}

// Save the modified document
doc.Save("36115.pdf");

Please note that in that case, you can cut some contents of the document (because we decrease height).
To avoid this, you can increase the width proportionally and leave the height intact as follows:

Aspose.Pdf.Rectangle r = page.MediaBox;

// Keep the same height
double newHeight = r.Height;

// Expand the width proportionally to make the orientation landscape (assuming the previous orientation was portrait)
double newWidth = r.Height * r.Height / r.Width;

// Alternative option using PdfPageEditor facade to apply zoom to page contents
Document doc = new Document("PdfWithText.pdf");

Aspose.Pdf.Rectangle rect = doc.Pages[1].Rect;

PdfPageEditor ppe = new PdfPageEditor();
ppe.BindPdf("PdfWithText.pdf");
ppe.Zoom = (float)(rect.Width / rect.Height);
ppe.PageSize = new Aspose.Pdf.PageSize((float)rect.Height, (float)rect.Width);

ppe.Save("36115-1.pdf");

Please feel free to contact us for any further assistance.

Best Regards,