Hi, While converting a PDF document to SVG and traversing through that document I notice that we are consuming a lot of memory for operations like Saving svgdocument to png stream, getting total length of svg path element, computing x,y coordinates of svgpath etc. Resharper’s profiler is showing me memory allocations at around 6 GBs for these operations. Please find code snippets for these operations below.
Converting SVGDocument to PNG stream
image.png (218.2 KB)
and the code that i wrote is
foreach ( var filePath in filePaths )
{
using (var document = new Document(filePath))
{
foreach ( var page in document.Pages )
{
// Extract each page as a separate pdf document for conversion
using (var pageDocument = new Document())
{
pageDocument.Pages.Add(page);
pageDocument.Flatten();
// Convert page into svg document for manipulation
using ( var ms = new MemoryStream() )
{
pageDocument.Save(ms, SaveFormat.Svg);
//Reset position of stream to beginning
ms.Seek(0, SeekOrigin.Begin);
var svgDocument = new SVGDocument(ms, ".");
//Reset position of stream to beginning
ms.Seek(0, SeekOrigin.Begin);
using ( var pngStream = new MemoryStream() )
{
using ( var image = Aspose.Imaging.Image.Load(ms) )
{
image.Save(pngStream, new PngOptions()
{
VectorRasterizationOptions = new SvgRasterizationOptions()
});
svgPageTextures.Add((image.Width, image.Height, pngStream.ToArray()));
}
}
svgDocumentData.Add(((int) svgDocument.RootElement.Width.BaseVal.Value, (int) svgDocument.RootElement.Height.BaseVal.Value, svgDocument));
}
}
}
}
}
Parsing SVG to extract coordinates of SVGPath elements,
image.png (235.3 KB)
and this is how i get those points
foreach ( SVGPathElement pathElement in pathElements )
{
var pathPattern = pathElement.GetAttribute(“d”).Replace(“L”, " L").Trim(’ ', ‘Z’, ‘z’);
var segments = pathPattern.Count(x => x == ‘L’);
var totalLength = pathElement.GetTotalLength();
var ctm = pathElement.GetScreenCTM();
var firstPoint = pathElement.GetPointAtLength(0);
var lastPoint = pathElement.GetPointAtLength(totalLength);
// Similarly for points along the path element
var pathSegList = pathElement.PathSegList;
var lengthPerSegment = totalLength / pathSegList.Length;
var points = new List<DblVector2D>();
for ( ulong i = 0 ; i < pathSegList.Length ; i++ )
{
var point = pathElement.GetPointAtLength(lengthPerSegment * i);
points.Add(new DblVector2D( point.X, point.Y));
}
}
Can anyone help optimizing this ?