We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to prevent whitespace in a generated PDF document?

Hi,

We are using Aspose.PDF version 21.11.0 to convert excel files to PDF. The settings for the excel worksheet are shown below in screenshot1. When this excel worksheet gets converted to PDF, the graph seems to be left-aligned and adds additional whitespace onto the right which looks like screenshot 2 below.

printsettings.PNG (113.2 KB)

image.png (42.3 KB)

Here’s the code used to convert an excel document to PDF -

if (fileExtension.ToLower().Contains(".xls"))
{
//Convert Excel doc to PDF
var workbook = new Aspose.Cells.Workbook(mStream);
foreach (Aspose.Cells.Worksheet sheet in workbook.Worksheets.Where(worksheet => !worksheet.Name.StartsWith(“sheet_”)))
{
sheet.IsVisible = false;
}
if (workbook.Worksheets.Any())
{
var pdfStream = new MemoryStream();
workbook.Save(pdfStream, Aspose.Cells.SaveFormat.Pdf);
BlobContainerClient container = new BlobContainerClient(_settings.CloudStorageConnection, “containername”);
BlobClient blob = container.GetBlobClient($"/converted/{newFilePath}");
pdfStream.Position = 0;
blob.Upload(pdfStream, true);
pdfStream.Close();
}
}

We are not specifying any formatting explicitly using Aspose.

I want to make sure that the graph looks exactly the same in Excel’s print preview format without any whitespace. How can I accomplish this? Any help is greatly appreciated. Thank you in advance.

Regards
SLS

@s2205,

Thanks for the screenshots.

Please try our latest version/fix: Aspose.Cells for .NET 22.2. If you still find the issue with latest version/fix, kindly do zip and attach your template Excel file containing the underlying chart, we will it soon.

Hi @Amjad_Sahi. Thank you for your response. Please find the attached zip folder containing the excel sheet. excel sheet.zip (158 Bytes)

@s2205,

I downloaded your attachment (zipped archive). I extracted the zipped archive to some folder. I found it has a folder “excel sheet” which is empty. Could you please re-attach the zipped archive containing correct Excel file. We will check your issue soon.

Hi @Amjad_Sahi, I’m so sorry about that. I attached the excel sheet again. Please let me know if you are able to see it.

Appreciate your time and help.

Regards
SLS

excel sheet.zip (13.6 KB)

@s2205

It works OK with Aspose.Cells v21.11 and the latest release Aspose.Cells v22.2.
output_v22.2.pdf (30.0 KB)
output_v21.11.pdf (30.0 KB)

Code:

Workbook wb = new Workbook("testgraph1.xlsx");
wb.Save("output.pdf");

Please share the output pdf on your side if you still get different result.

Hi @Peyton.Xu. Thank you for your response. Please see the attached document on how it looks like on my end. After running our application, I verified in the blob storage container which has the same format as the attachment ( makes me think that the conversion didn’t happen properly).

Graph.pdf (90.9 KB)

We convert multiple excel sheets to PDF and merge them into one single PDF. This graph is one of the worksheets.

We are saving the file to PDF as shown below -

var pdfStream = new MemoryStream();
workbook.Save(pdfStream, Aspose.Cells.SaveFormat.Pdf);

I am not sure what I am missing here. Appreciate your help.

Regards
SLS

@s2205

Please share the output pdf directly saved by Aspose.Cells.

I doubt that it is the issue of the merging tool. Please check it on your side.

@Peyton.Xu Here’s the code snippet for merging the PDF’s together -

                CloudStorageAccount account = null;
				var downloadMemStream = new MemoryStream();
				var uploadMemStream = new MemoryStream();

				//Get converted Documents from Azure
				if (CloudStorageAccount.TryParse(_settings.CloudStorageConnection, out account))
				{
					List<Aspose.Pdf.Document> documents = new List<Aspose.Pdf.Document>();
					BlobContinuationToken blobContinuationToken = null;
					CloudBlobClient client = account.CreateCloudBlobClient();						
					CloudBlobContainer cloudBlobContainer = client.GetContainerReference("containername");
					CloudBlobDirectory cloundBlobDirectory = cloudBlobContainer.GetDirectoryReference("converted");						
					var mergedDocument = new Aspose.Pdf.Document();

					try 
					{ 
						do
						{
							var blobResultSegment = await cloundBlobDirectory.ListBlobsSegmentedAsync(blobContinuationToken);
							var blobs = blobResultSegment.Results.Where(segment => segment.Uri.PathAndQuery.Contains(newGuid.ToString()));
							blobContinuationToken = blobResultSegment.ContinuationToken;

							foreach (CloudBlockBlob cloudBlockBlob in blobs)
							{
								await cloudBlockBlob.DownloadToStreamAsync(downloadMemStream);
								var document = new Aspose.Pdf.Document(downloadMemStream);
								documents.Add(document);

								fileInfos.FirstOrDefault(fileInfo => Path.GetFileNameWithoutExtension(cloudBlockBlob.Name).Contains(fileInfo.Name)).PageCount = document.Pages.Count;
							}
						} while (blobContinuationToken != null);

						//Merge the PDFs into one Document
						mergedDocument = new Aspose.Pdf.Document();
						documents.ForEach(document =>
						{
							mergedDocument.Pages.Add(document.Pages);
						});
					}

Sharing the screenshot of the PDF document that is downloaded from the azure blob storage container.

image.png (62.5 KB)

Please advise.

PS: I am not able to share the complete output PDF document because it contains actual data. Also, the converted document itself isn’t formatted properly. So I wonder if merging is the issue.

Regards
SLS

@s2205

You use Aspose.Cells to convert excel files to separated pdf files, and then use Aspose.Pdf to merge all the separated pdf files into one final pdf file. Aspose.Cells and Aspose.Pdf are two separated products.

To locate the possible issue in Aspose.Cells, you need to compare Aspose.Cells generated pdf files(before being merged into one final pdf file) with the source excel files. If there are some issues, please share the Aspose.Cells generated pdf file and the corresponding source excel file.