Product name & version
Aspose.Words(23.2.0) for .NET 8.0
Detailed description of the conversion issue :
When the system attempts to generate Word, it uses 251KB.
The same file we are converting into PDF, which has a size of 17.8MB.
Same 17.8MB PDF file we are converting into bytes for storage in our database.
Due to this, system dumping a large volume of data into our database.
Which is having a significant influence on our business. Users are having trouble in using our app.
Currently, we are clearing some historical data as a quick win.
Need immediate assistance to resolve this issue.
- Code snippet
public async Task SaveInvoiceFileInEDMAsync(string invoiceCode, string templateCode = null)
{
var invoiceCodes = new List<string>() { invoiceCode };
var result = await GenerateInvoiceByPlugin(invoiceCodes, templateCode) ?? await _invoiceService.DownloadInvoiceFileAsync(new InvoiceTemplate() { InvoiceCodes = invoiceCodes });
_ = result ?? throw new Exception($"Cannot generate invoice file for {invoiceCode}");
result = _pdfConverter.ConvertToPDF(result);
// Log pdf file size in KB after aspose conversion
FileInfo fileInfo = new FileInfo(result.FilePath);
_logger.LogInformation($"Invoice {invoiceCode} PDF size after Aspose conversion: {fileInfo.Length / 1024} KB.");
_logger.LogInformation($"Invoice {invoiceCode} PDF file stream size after Aspose conversion: {result.FileStream.Length / 1024} KB.");
// Convert Stream → byte[] reliably
var bytes = await result.FileStream.ToByteArrayAsync();
// log byte size in KB after stream to byte array conversion
_logger.LogInformation($"Invoice {invoiceCode} PDF size after stream to byte array conversion: {bytes.Length / 1024} KB.");
await _invoiceService.SaveInvoiceFileInEDMAsync(invoiceCode, result.FileName, bytes);
}
FileModel ConvertToPDF(FileModel fileModel)
{
var pdffilePath = "";
var filePath = fileModel.FilePath;
bool isEncryption = false;
string extension = Path.GetExtension(filePath);
if (extension == ".docx")
{
pdffilePath = filePath.Replace(".docx", ".pdf");
ConvertWordToPDF(filePath, pdffilePath, isEncryption);
fileModel.FileName = fileModel.FileName.Replace(".docx", ".pdf");
}
using var fileStream = new FileStream(pdffilePath, FileMode.Open, FileAccess.Read);
if (fileStream.CanSeek)
fileStream.Position = 0;
var memoryStream = new MemoryStream();
fileStream.CopyTo(memoryStream);
memoryStream.Position = 0;
fileModel.FileStream = memoryStream; // assign clean stream
fileModel.FilePath = pdffilePath;
return fileModel;
}
public string ConvertWordToPDF(string wordFileName, string pdfFileName = null, bool isEncryption = false)
{
if (string.IsNullOrEmpty(pdfFileName))
{
pdfFileName = wordFileName.Replace(".docx", ".pdf", StringComparison.OrdinalIgnoreCase);
}
// Set PDF save options
PdfSaveOptions option = new PdfSaveOptions
{
EmbedFullFonts = false, // Do not embed full fonts
ImageCompression = PdfImageCompression.Jpeg,
JpegQuality = 70, // Adjust quality as needed (0-100)
SaveFormat = SaveFormat.Pdf,
OptimizeOutput = true, // Try to optimize output (if available)
};
var doc = new Document(wordFileName);
// to avoid pdf size issue commnted out HarfBuzz text shaper
//doc.LayoutOptions.TextShaperFactory = Aspose.Words.Shaping.HarfBuzz.HarfBuzzTextShaperFactory.Instance;
doc.Save(pdfFileName, option);
return pdfFileName;
}
- Exception/error message
- Sample input/output files
No Error only size difference screen shot i have already attached



