we will appreciate if you explain to us how fileStamp.Save(outStream); method is working since on vmware it takes 12 to 19 seconds, on physical server in miliseconds, it seems there is a configuration must be done on your library or the vmware to help the save internal code, Also I wana add here it is not slow with all pdfs , just some pdfs even these pdfs are small and has 1 page.
Specifications:
.NET framework :4.8.1
aspovdf version:25.2.0.0
VMware specification similar to hardware specification:
Windows server 2019 Standard
4 physical processor and 8 cores per each
The code is :
using Aspose.Pdf;
using Aspose.Pdf.Facades;
public static class LogHelper
{
private static readonly string logPath =
“D:/logs/watermark_log.txt”;
public static void Log(string msg)
{
try
{
File.AppendAllText(
logPath,
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") +
" " + msg + Environment.NewLine
);
}
catch
{
// ignore logging errors
}
}
public static Stopwatch StartTimer(string name)
{
Log("[START] " + name);
return Stopwatch.StartNew();
}
public static void EndTimer(string name, Stopwatch sw)
{
sw.Stop();
Log("[END] " + name + " - " + sw.ElapsedMilliseconds + " ms");
}
}
static public byte[] setWaterMark(Stream ms)
{
try
{
Stopwatch swTotal = LogHelper.StartTimer("SetWaterMark - TOTAL");
// ----------------------------------------
// APPLY LICENSE
// ----------------------------------------
LogHelper.Log("Loading license...");
Stopwatch sw = LogHelper.StartTimer("LoadLicense");
try
{
var PdfLicense = new Aspose.Pdf.License();
PdfLicense.SetLicense(HttpContext.Current.Server.MapPath("~/License/Aspose.Pdf.lic"));
}
catch (Exception ex)
{
throw new Exception("Aspose License not applied: " + ex.Message);
}
LogHelper.EndTimer("LoadLicense", sw);
// ----------------------------------------
// LOAD PDF
// ----------------------------------------
LogHelper.Log("Loading PDF...");
sw = LogHelper.StartTimer("LoadPdf");
Aspose.Pdf.Document pdf = new Aspose.Pdf.Document(ms);
LogHelper.EndTimer("LoadPdf", sw);
// ----------------------------------------
// CREATE WATERMARK IMAGE
// ----------------------------------------
string LogID = DateTime.Now.ToString("yyyyMMdd") + "MIF";
LogHelper.Log("Creating watermark...");
sw = LogHelper.StartTimer("CreateWaterMark");
MemoryStream wmStream = CreateWaterMark(LogID);
LogHelper.EndTimer("CreateWaterMark", sw);
// ----------------------------------------
// STAMP PAGES
// ----------------------------------------
LogHelper.Log("Stamping pages...");
sw = LogHelper.StartTimer("StampPages");
Aspose.Pdf.Facades.PdfFileStamp fileStamp = new Aspose.Pdf.Facades.PdfFileStamp(pdf);
Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();
stamp.BindImage(wmStream);
stamp.IsBackground = false;
stamp.Opacity = 0.1f;
// Add watermark positions
int[] xPositions = { 100, 50, 125, 350, 200, 325 };
int[] yPositions = { 175, 400, 550, 175, 400, 550 };
for (int i = 0; i < xPositions.Length; i++)
{
stamp.SetOrigin(xPositions[i], yPositions[i]);
fileStamp.AddStamp(stamp);
}
LogHelper.EndTimer("StampPages", sw);
// ----------------------------------------
// SAVE PDF TO MEMORY STREAM
// ----------------------------------------
LogHelper.Log("Saving PDF to memory...");
sw = LogHelper.StartTimer("SavePdf");
var optimizeOptions = new Aspose.Pdf.Optimization.OptimizationOptions();
// Disable image recompression (takes a LOT of CPU on VMware)
optimizeOptions.ImageCompressionOptions.CompressImages = false;
optimizeOptions.ImageCompressionOptions.ResizeImages = false;
// Disable font optimization
optimizeOptions.UnembedFonts = false;
// Disable remove-unused-object scanning
optimizeOptions.RemoveUnusedObjects = false;
optimizeOptions.RemoveUnusedStreams = false;
pdf.OptimizeResources(optimizeOptions);
using (MemoryStream outStream = new MemoryStream())
{
fileStamp.Save(outStream); // << Directly save to MemoryStream
fileStamp.Close();
wmStream.Dispose();
LogHelper.EndTimer("SavePdf", sw);
LogHelper.EndTimer("SetWaterMark - TOTAL", swTotal);
return outStream.ToArray(); // Return byte array
}
}
catch (Exception ex)
{
LogHelper.Log(ex.StackTrace + ex.Message);
return null;
}
}
// string tempFile = Path.Combine(@"D:\files", Guid.NewGuid() + ".pdf");
static public MemoryStream CreateWaterMark(string textID)
{
MemoryStream output = new MemoryStream();
byte[] baseImage = File.ReadAllBytes(
HttpContext.Current.Server.MapPath("~/img/emptywatermark.png")
);
using (MemoryStream stream = new MemoryStream(baseImage))
using (System.Drawing.Image img = System.Drawing.Image.FromStream(stream))
using (System.Drawing.Graphics g = Graphics.FromImage(img))
{
System.Drawing.Brush myBrush =
new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(25, System.Drawing.Color.LightSteelBlue));
System.Drawing.Font f = new System.Drawing.Font(
System.Drawing.FontFamily.GenericSerif, 18);
g.RotateTransform(-45f);
g.DrawString(textID, f, myBrush, new System.Drawing.Point(-20, 105));
g.RotateTransform(45f);
img.Save(output, ImageFormat.Png);
}
output.Position = 0;
return output;
}
The log on VMWARE for the pdf is :
2025-12-10 12:26:25.449 [START] SetWaterMark - TOTAL
2025-12-10 12:26:25.451 Loading license…
2025-12-10 12:26:25.452 [START] LoadLicense
2025-12-10 12:26:25.587 [END] LoadLicense - 134 ms
2025-12-10 12:26:25.588 Loading PDF…
2025-12-10 12:26:25.588 [START] LoadPdf
2025-12-10 12:26:25.745 [END] LoadPdf - 156 ms
2025-12-10 12:26:25.746 Creating watermark…
2025-12-10 12:26:25.747 [START] CreateWaterMark
2025-12-10 12:26:25.750 [END] CreateWaterMark - 2 ms
2025-12-10 12:26:25.751 Stamping pages…
2025-12-10 12:26:25.751 [START] StampPages
2025-12-10 12:26:26.192 [END] StampPages - 440 ms
2025-12-10 12:26:26.193 Saving PDF to memory…
2025-12-10 12:26:26.194 [START] SavePdf
2025-12-10 12:26:45.594 [END] SavePdf - 19400 ms
2025-12-10 12:26:45.596 [END] SetWaterMark - TOTAL - 20145 ms
2025-12-10 12:40:06.988 [START] SetWaterMark - TOTAL
2025-12-10 12:40:06.989 Loading license…
2025-12-10 12:40:06.990 [START] LoadLicense
2025-12-10 12:40:07.096 [END] LoadLicense - 104 ms
2025-12-10 12:40:07.096 Loading PDF…
2025-12-10 12:40:07.097 [START] LoadPdf
2025-12-10 12:40:07.251 [END] LoadPdf - 153 ms
2025-12-10 12:40:07.252 Creating watermark…
2025-12-10 12:40:07.253 [START] CreateWaterMark
2025-12-10 12:40:07.257 [END] CreateWaterMark - 3 ms
2025-12-10 12:40:07.258 Stamping pages…
2025-12-10 12:40:07.258 [START] StampPages
2025-12-10 12:40:07.527 [END] StampPages - 268 ms
2025-12-10 12:40:07.528 Saving PDF to memory…
2025-12-10 12:40:07.528 [START] SavePdf
2025-12-10 12:40:19.487 [END] SavePdf - 11958 ms
2025-12-10 12:40:19.489 [END] SetWaterMark - TOTAL - 12500 ms
2025-12-10 12:40:26.395 [START] SetWaterMark - TOTAL
2025-12-10 12:40:26.397 Loading license…
2025-12-10 12:40:26.397 [START] LoadLicense
2025-12-10 12:40:26.506 [END] LoadLicense - 108 ms
2025-12-10 12:40:26.507 Loading PDF…
2025-12-10 12:40:26.508 [START] LoadPdf
2025-12-10 12:40:26.760 [END] LoadPdf - 251 ms
2025-12-10 12:40:26.761 Creating watermark…
2025-12-10 12:40:26.762 [START] CreateWaterMark
2025-12-10 12:40:26.767 [END] CreateWaterMark - 2 ms
2025-12-10 12:40:26.767 Stamping pages…
2025-12-10 12:40:26.768 [START] StampPages
2025-12-10 12:40:27.110 [END] StampPages - 341 ms
2025-12-10 12:40:27.111 Saving PDF to memory…
2025-12-10 12:40:27.112 [START] SavePdf
2025-12-10 12:40:44.305 [END] SavePdf - 17192 ms
2025-12-10 12:40:44.306 [END] SetWaterMark - TOTAL - 17909 ms
2025-12-10 13:16:22.904 [START] SetWaterMark - TOTAL
2025-12-10 13:16:22.906 Loading license…
2025-12-10 13:16:22.907 [START] LoadLicense
2025-12-10 13:16:23.026 [END] LoadLicense - 118 ms
2025-12-10 13:16:23.027 Loading PDF…
2025-12-10 13:16:23.027 [START] LoadPdf
2025-12-10 13:16:23.268 [END] LoadPdf - 240 ms
2025-12-10 13:16:23.269 Creating watermark…
2025-12-10 13:16:23.270 [START] CreateWaterMark
2025-12-10 13:16:23.273 [END] CreateWaterMark - 2 ms
2025-12-10 13:16:23.274 Stamping pages…
2025-12-10 13:16:23.275 [START] StampPages
2025-12-10 13:16:23.512 [END] StampPages - 237 ms
2025-12-10 13:16:23.513 Saving PDF to memory…
2025-12-10 13:16:23.514 [START] SavePdf
2025-12-10 13:16:39.859 [END] SavePdf - 16345 ms
2025-12-10 13:16:39.860 [END] SetWaterMark - TOTAL - 16954 ms
2025-12-10 14:07:59.843 [START] SetWaterMark - TOTAL
2025-12-10 14:07:59.844 Loading license…
2025-12-10 14:07:59.845 [START] LoadLicense
2025-12-10 14:08:00.293 [END] LoadLicense - 446 ms
2025-12-10 14:08:00.294 Loading PDF…
2025-12-10 14:08:00.294 [START] LoadPdf
2025-12-10 14:08:00.846 [END] LoadPdf - 550 ms
2025-12-10 14:08:00.847 Creating watermark…
2025-12-10 14:08:00.848 [START] CreateWaterMark
2025-12-10 14:08:00.852 [END] CreateWaterMark - 3 ms
2025-12-10 14:08:00.852 Stamping pages…
2025-12-10 14:08:00.853 [START] StampPages
2025-12-10 14:08:01.291 [END] StampPages - 437 ms
2025-12-10 14:08:01.292 Saving PDF to memory…
2025-12-10 14:08:01.292 [START] SavePdf
2025-12-10 14:08:19.446 [END] SavePdf - 18153 ms
2025-12-10 14:08:19.447 [END] SetWaterMark - TOTAL - 19603 ms
2025-12-10 14:09:25.614 [START] SetWaterMark - TOTAL
2025-12-10 14:09:25.615 Loading license…
2025-12-10 14:09:25.616 [START] LoadLicense
2025-12-10 14:09:25.735 [END] LoadLicense - 118 ms
2025-12-10 14:09:25.736 Loading PDF…
2025-12-10 14:09:25.737 [START] LoadPdf
2025-12-10 14:09:25.941 [END] LoadPdf - 203 ms
2025-12-10 14:09:25.942 Creating watermark…
2025-12-10 14:09:25.943 [START] CreateWaterMark
2025-12-10 14:09:25.948 [END] CreateWaterMark - 4 ms
2025-12-10 14:09:25.949 Stamping pages…
2025-12-10 14:09:25.949 [START] StampPages
2025-12-10 14:09:26.379 [END] StampPages - 429 ms
2025-12-10 14:09:26.380 Saving PDF to memory…
2025-12-10 14:09:26.382 [START] SavePdf
2025-12-10 14:09:43.474 [END] SavePdf - 17090 ms
2025-12-10 14:09:43.475 [END] SetWaterMark - TOTAL - 17859 ms
The log on physical hardware server for the same pdf:
2025-12-10 13:30:55.766 [START] SetWaterMark - TOTAL
2025-12-10 13:30:55.767 Loading license…
2025-12-10 13:30:55.767 [START] LoadLicense
2025-12-10 13:30:55.768 Loading PDF…
2025-12-10 13:30:55.769 [START] LoadPdf
2025-12-10 13:30:55.785 [END] LoadPdf - 15 ms
2025-12-10 13:30:55.786 Creating watermark…
2025-12-10 13:30:55.786 [START] CreateWaterMark
2025-12-10 13:30:55.787 [END] CreateWaterMark - 0 ms
2025-12-10 13:30:55.788 Stamping pages…
2025-12-10 13:30:55.789 [START] StampPages
2025-12-10 13:30:55.798 [END] StampPages - 8 ms
2025-12-10 13:30:55.799 Saving PDF to memory…
2025-12-10 13:30:55.799 [START] SavePdf
2025-12-10 13:30:55.835 [END] SavePdf - 34 ms
2025-12-10 13:30:55.836 [END] SetWaterMark - TOTAL - 68 ms
2025-12-10 13:31:10.488 [START] SetWaterMark - TOTAL
2025-12-10 13:31:10.489 Loading license…
2025-12-10 13:31:10.490 [START] LoadLicense
2025-12-10 13:31:10.491 Loading PDF…
2025-12-10 13:31:10.492 [START] LoadPdf
2025-12-10 13:31:10.496 [END] LoadPdf - 3 ms
2025-12-10 13:31:10.497 Creating watermark…
2025-12-10 13:31:10.497 [START] CreateWaterMark
2025-12-10 13:31:10.499 [END] CreateWaterMark - 1 ms
2025-12-10 13:31:10.500 Stamping pages…
2025-12-10 13:31:10.500 [START] StampPages
2025-12-10 13:31:10.510 [END] StampPages - 8 ms
2025-12-10 13:31:10.511 Saving PDF to memory…
2025-12-10 13:31:10.511 [START] SavePdf
2025-12-10 13:31:10.536 [END] SavePdf - 23 ms
2025-12-10 13:31:10.536 [END] SetWaterMark - TOTAL - 46 ms
2025-12-10 13:35:54.121 [START] SetWaterMark - TOTAL
2025-12-10 13:35:54.122 Loading license…
2025-12-10 13:35:54.123 [START] LoadLicense
2025-12-10 13:35:54.124 Loading PDF…
2025-12-10 13:35:54.125 [START] LoadPdf
2025-12-10 13:35:54.128 [END] LoadPdf - 2 ms
2025-12-10 13:35:54.130 Creating watermark…
2025-12-10 13:35:54.131 [START] CreateWaterMark
2025-12-10 13:35:54.133 [END] CreateWaterMark - 1 ms
2025-12-10 13:35:54.134 Stamping pages…
2025-12-10 13:35:54.134 [START] StampPages
2025-12-10 13:35:54.146 [END] StampPages - 11 ms
2025-12-10 13:35:54.147 Saving PDF to memory…
2025-12-10 13:35:54.147 [START] SavePdf
2025-12-10 13:35:54.172 [END] SavePdf - 23 ms
2025-12-10 13:35:54.173 [END] SetWaterMark - TOTAL - 50 ms
2025-12-10 14:05:18.421 [START] SetWaterMark - TOTAL
2025-12-10 14:05:18.425 Loading license…
2025-12-10 14:05:18.425 [START] LoadLicense
2025-12-10 14:05:18.657 [END] LoadLicense - 231 ms
2025-12-10 14:05:18.658 Loading PDF…
2025-12-10 14:05:18.658 [START] LoadPdf
2025-12-10 14:05:18.844 [END] LoadPdf - 184 ms
2025-12-10 14:05:18.845 Creating watermark…
2025-12-10 14:05:18.845 [START] CreateWaterMark
2025-12-10 14:05:18.861 [END] CreateWaterMark - 14 ms
2025-12-10 14:05:18.861 Stamping pages…
2025-12-10 14:05:18.862 [START] StampPages
2025-12-10 14:05:18.916 [END] StampPages - 53 ms
2025-12-10 14:05:18.917 Saving PDF to memory…
2025-12-10 14:05:18.918 [START] SavePdf
2025-12-10 14:05:19.012 [END] SavePdf - 93 ms
2025-12-10 14:05:19.013 [END] SetWaterMark - TOTAL - 587 ms
2025-12-10 14:08:52.590 [START] SetWaterMark - TOTAL
2025-12-10 14:08:52.594 Loading license…
2025-12-10 14:08:52.595 [START] LoadLicense
2025-12-10 14:08:52.879 [END] LoadLicense - 283 ms
2025-12-10 14:08:52.880 Loading PDF…
2025-12-10 14:08:52.881 [START] LoadPdf
2025-12-10 14:08:53.076 [END] LoadPdf - 194 ms
2025-12-10 14:08:53.076 Creating watermark…
2025-12-10 14:08:53.077 [START] CreateWaterMark
2025-12-10 14:08:53.094 [END] CreateWaterMark - 16 ms
2025-12-10 14:08:53.094 Stamping pages…
2025-12-10 14:08:53.095 [START] StampPages
2025-12-10 14:08:53.152 [END] StampPages - 55 ms
2025-12-10 14:08:53.153 Saving PDF to memory…
2025-12-10 14:08:53.154 [START] SavePdf
2025-12-10 14:08:53.256 [END] SavePdf - 101 ms
2025-12-10 14:08:53.256 [END] SetWaterMark - TOTAL - 662 ms
2025-12-10 14:10:10.519 [START] SetWaterMark - TOTAL
2025-12-10 14:10:10.521 Loading license…
2025-12-10 14:10:10.522 [START] LoadLicense
2025-12-10 14:10:10.731 [END] LoadLicense - 208 ms
2025-12-10 14:10:10.732 Loading PDF…
2025-12-10 14:10:10.733 [START] LoadPdf
2025-12-10 14:10:10.902 [END] LoadPdf - 168 ms
2025-12-10 14:10:10.903 Creating watermark…
2025-12-10 14:10:10.904 [START] CreateWaterMark
2025-12-10 14:10:10.916 [END] CreateWaterMark - 11 ms
2025-12-10 14:10:10.917 Stamping pages…
2025-12-10 14:10:10.918 [START] StampPages
2025-12-10 14:10:10.971 [END] StampPages - 52 ms
2025-12-10 14:10:10.972 Saving PDF to memory…
2025-12-10 14:10:10.972 [START] SavePdf
2025-12-10 14:10:11.065 [END] SavePdf - 91 ms
2025-12-10 14:10:11.065 [END] SetWaterMark - TOTAL - 543 ms
2025-12-10 14:16:57.610 [START] SetWaterMark - TOTAL
2025-12-10 14:16:57.611 Loading license…
2025-12-10 14:16:57.612 [START] LoadLicense
2025-12-10 14:16:57.668 [END] LoadLicense - 54 ms
2025-12-10 14:16:57.668 Loading PDF…
2025-12-10 14:16:57.669 [START] LoadPdf
2025-12-10 14:16:57.683 [END] LoadPdf - 13 ms
2025-12-10 14:16:57.684 Creating watermark…
2025-12-10 14:16:57.685 [START] CreateWaterMark
2025-12-10 14:16:57.687 [END] CreateWaterMark - 1 ms
2025-12-10 14:16:57.687 Stamping pages…
2025-12-10 14:16:57.688 [START] StampPages
2025-12-10 14:16:57.698 [END] StampPages - 9 ms
2025-12-10 14:16:57.700 Saving PDF to memory…
2025-12-10 14:16:57.701 [START] SavePdf
2025-12-10 14:16:57.733 [END] SavePdf - 30 ms
2025-12-10 14:16:57.733 [END] SetWaterMark - TOTAL - 122 ms