Excel to PDF is not preserving the links completely

I have an excel with links as content in the cells. When I am trying to convert this excel to pdf, the links are only half printed. Ideally I would expect links to be printed in other page if the current page content is overflowing. But this does not happen.
And also when I hover on the links in pdf, instead of the complete link, some extra junk is also being displayed .
I am using .net and aspose version 24.7.0
I am using orientation as ‘portrait’ and pagesize ‘PaperA4’
Attaching below the documents for reference.
Reference files.zip (46.5 KB)

1 Like

@shreyap ,

The pdf generated by Aspose.Cells for .NET 24.7 is OK: There are two pages and the links are OK in pdf.
Aspose.24.7.pdf (10.6 KB)

Also, the file “AsposePdf.pdf” you shared is generated by Aspose.PDF instead of Aspose.Cells, and the content is not consistent with the source file “Aspose.xlsx”.

If you still have issue, please share us a runnable project to reproduce the issue.

@peyton.xu In the zip which I have shared, Aspose.pdf is generated by aspose and MSExcelpdf is generated by Microsoft excel. As you can see that the pdf generated by aspose is NOT
OK. The links are broken and the remaining part of the link is not printed in the next sheet as well

I would like to have a pdf generated by aspose that aligns with the ms excel i.e. links to be completely displayed. I am Ok with the links getting spread across sheets and when I hover on the links, the content should not have any junk data in it.

@shreyap ,

Please check the pdf generated by Aspose.Cells v24.7. It aligns with Excel.

@peyton.xu Can it be done using Aspose.pdf ?

@shreyap
Would you like to share your sample code? Did you encounter an issue after using the Aspose.PDF product to manipulate files? If yes, we will transfer the post to the Aspose.PDF forum. Aspose.PDF staff member will assist you accordingly soon.

@shreyap ,

Aspose.Cells can convert your source xlsx file to pdf correctly. What is your purpose of using Aspose.Pdf component?

@peyton.xu I have some additional styles being applied. therefore I am using aspose.pdf

@peyton.xu I am wrapping the text before converting it to pdf. But the text isnt getting wrapped. Am I doing something wrong? Below is my code.

Aspose.Cells.Range range = worksheet.Cells.MaxDisplayRange;
Style style = workbook.CreateStyle();
style.IsTextWrapped = true;
StyleFlag flag = new StyleFlag();
flag.WrapText = true;
if (range != null)
{
    range.ApplyStyle(style, flag);
}
worksheet.AutoFitColumns();
worksheet.AutoFitRows();

@shreyap
If you automatically adjust the column width, the column width will be recalculated. Please try the following example code:

// Instantiating a Workbook object
Workbook workbook = new Workbook(filePath + "Aspose.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
Aspose.Cells.Range range = worksheet.Cells.MaxDisplayRange;

Style style = workbook.CreateStyle();
style.IsTextWrapped = true;
StyleFlag flag = new StyleFlag();
flag.WrapText = true;
if (range != null)
{
    range.ApplyStyle(style, flag);
}
//worksheet.AutoFitColumns();
worksheet.AutoFitRows();

// Save the workbook.
workbook.Save(filePath + "out_net.pdf");

@John.He , Thank You for the help. I am able to successfully wrap.
But after wrapping I am identifying the text and trying to replace it with some other text and at that time I am getting the below error #=zsL9eOjbwhCzMh7nSVVL4mXz19pcC: 'Font doesn't include tables to decode text' probably because of after wrapping some new line characters are being added to the text and textFragment.Text.Replace("[", "").Replace("]", "") is throwing the error.

How to handle this?

@shreyap,

Please note, if you wrap text to all the cells (apart from hyperlinked cells) in the worksheet, it will affect, mess up, or disorganize data in other cells. Also, when you retrieve the wrapped text from a cell for other APIs, it may include line breaks or new line characters. For your scenario/case, you should set wrap to linked cells only. Alternatively, you may change/rename long (display) text of the hyperlinks (in the cells) to smaller text.
e.g.,
Sample code:

// Instantiating a Workbook object
Workbook workbook = new Workbook("e:\\test2\\Aspose.xlsx");
Worksheet worksheet = workbook.Worksheets[0];

int i=0;
foreach (Aspose.Cells.Hyperlink link in worksheet.Hyperlinks)
{
   //Change the long display text.
   link.TextToDisplay = "Link" + ++i;

}

// Save the workbook.
workbook.Save("e:\\test2\\out_net.pdf");

Hope, this helps a bit.

@amjad.sahi ,
Wrapping is an issue we have been facing since very long time. Not just for hyperlinks, we have encountered scenarios where normal cell in excel also had long data that needs to be wrapped when converted to pdf. That is the reason I applied wrap to entire workbook.

And also It is not just links which I want to replace text with. It could be any data.

@shreyap,

The text wrapping feature in Aspose.Cells is similar to that of MS Excel. You can manually wrap the text in MS Excel, save the file, and then retrieve the wrapped text/data from the cells, which will be the same as when you set the wrapping text using Aspose.Cells.

If you still believe this is an issue with Aspose.Cells, please provide a standalone console application (complete source code without compilation errors) and resource files to reproduce the issue, and we will investigate it.

@shreyap ,

It seems that you’re using Aspose.Pdf to modify the pdf generated by Aspose.Cells. The exception is thrown from Aspose.Pdf. You can reproduce the issue in Aspose.Pdf forum.

Anyway, for replacing text, you can replace text using Aspose.Cells before saving to pdf.

@peyton.xu , I have data in each cell and would like to identify the data which is in particular format (lets say enclosed within braces) and replace it and also apply some styles to it like font size etc. How can I do it in Aspose.cells and then save it as a pdf?

@shreyap,

You can use Find/Search options provided by Aspose.Cells to find/detect which cells has your specific string/chars. You may try using some loops to traverse the worksheet cells to retrieve each cell accordingly. Then, you may replace data in the cell with your desired data. Also, you may change/change the formatting/style of the cell for your needs. See the documents with exmaples for your reference.
https://docs.aspose.com/cells/net/find-or-search-data/
https://docs.aspose.com/cells/net/find-cells-with-specific-style/
https://docs.aspose.com/cells/net/search-data-using-original-values/

In case, you still have any issue/confusion, let us know with details, sample code and sample file(s). We will evaluate and assist you to accomplish your task.

@shreyap,

Moreover, see the following sample code to accomplish your task for your requirements.
e.g.,
Sample code:

// Load the workbook
Workbook workbook = new Workbook("e:\\test2\\Bk_data1.xlsx");

// Get the first worksheet
Worksheet worksheet = workbook.Worksheets[0];

// Define a regex pattern to match data enclosed within braces
string pattern =  @"\[[^\]]+\]";
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(pattern);

// Traverse through all cells
foreach (Aspose.Cells.Cell cell in worksheet.Cells)
{
   string cellValue = cell.StringValue;

   if (regex.IsMatch(cellValue))
   {
           // Replace the data - for example, replace with "MyData"
           string newValue = regex.Replace(cellValue, "MyData");

           // Set the new value in the cell
           cell.PutValue(newValue);

           // Apply styles - example: set font size to 14
           Style style = cell.GetStyle();
           style.Font.Size = 14;
           style.Font.IsBold = true;
           cell.SetStyle(style);
   }
}

// Save the workbook as a PDF
workbook.Save("e:\\test2\\outasdfasdf.pdf", SaveFormat.Pdf);

Please find attached the input and output PDF for your reference.
files1.zip (30.9 KB)

@amjad.sahi, Can you please confirm why does this exception occur when I try to modify textFragment.text when I have special characters.

@shreyap
Would you like to share your sample code and sample files? We will check it soon.