Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, May 10, 2018 2:08:01 PM(UTC)
Toadfish

Groups: Registered
Posts: 20


Hey all,

When I try converting a page from PDF to SVG, the resulting total SVG pages is bigger than the originial PDF. The original PDF is 2MB and when all the pages are converted to SVG, each is about 0.2-0.3MB. Resulting in a total of about 45MB getting sent from the API.

Here is some example code of converting the first page of the document into a byte[]. The resulting pageByteArray is usually larger than the original document.
Code:

...
MemoryStream stream = new MemoryStream(documentData); //documentData is base64, 280 page, 2MB PDF
var document = DocumentFactory.LoadFromStream(stream, new LoadDocumentOptions());
var documentPage = document.Pages[0];

if (documentPage.IsSvgSupported)
{
    var pageByteArray = CreateSVGPage(documentPage);
}
...
public static byte[] CreateSVGPage(DocumentPage documentPage)
{
    using (var svgDocument = documentPage.GetSvg(new CodecsLoadSvgOptions()))
    {
        if (svgDocument != null)
        {
            if (!svgDocument.IsFlat)
                svgDocument.Flat(null);

            if (!svgDocument.IsRenderOptimized)
                svgDocument.BeginRenderOptimize();

            var svgBounds = svgDocument.Bounds;
            if (!svgBounds.IsValid)
                svgDocument.CalculateBounds(false);
        }

        if (svgDocument != null)
        {
            var gzip = false;
            var gzipString = ServiceHelper.GetSettingValue(ServiceHelper.Key_Svg_GZip);
            if (!string.IsNullOrEmpty(gzipString))
            {
                if (!bool.TryParse(gzipString, out gzip))
                    gzip = false;
            }

            var stream = ToStream(svgDocument, gzip);

            var currentContext = HttpContext.Current;
            if (currentContext != null)
            {
                if (gzip)
                    currentContext.Response.Headers.Add("Content-Encoding", "gzip");

                currentContext.Response.ContentType = "image/svg+xml";
                currentContext.Response.Headers.Add("ContentLength", stream.Length.ToString());
            }

            MemoryStream ms = new MemoryStream();
            stream.CopyTo(ms);
            return ms.ToArray();
        }
        else
        {
            return null;
        }
    }
}
...

private static Stream ToStream(SvgDocument svgDocument, bool gzip)
{
var ms = new MemoryStream();

var svgSaveOptions = new SvgSaveOptions();

if (gzip)
{
using (var compressed = new GZipStream(ms, CompressionMode.Compress, true))
{
using (var tempStream = new MemoryStream())
{
svgDocument.SaveToStream(tempStream, svgSaveOptions);
tempStream.Position = 0;
ServiceHelper.CopyStream(tempStream, compressed);
}
}
}
else
{
svgDocument.SaveToStream(ms, svgSaveOptions);
}

ms.Position = 0;
return ms;
}
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

#2 Posted : Tuesday, May 15, 2018 7:05:38 AM(UTC)
Christopher

Groups: Registered, Tech Support, Administrators
Posts: 89

Was thanked: 4 time(s) in 4 post(s)

Hi,

The size of each of the pages converted from your PDF to SVG is dependent on the type of data that is on each of the pages in the PDF.
I ran a quick test in our LEADTOOLS convert demo and the resulting conversion showed that the pages that held graphical images were much larger in size when compared with those pages with just text.

Have you tried to use the LEADTOOLS convert demo to test the results there?
This would allow you to compare the results in your application with that of the demo.

This demo can be found in the following default file path from the v19 SDK download:
C:\LEADTOOLS 19\Shortcuts\Document Converter\.NET Class Libraries
or v20
C:\LEADTOOLS 20\Shortcuts\Document Converter\.NET Framework Class Libraries

Also, you could look into the ConvertToSvg Method that is part of the Leadtools.Pdf namespace in our LEADTOOLS Online Documentation.

Chris Thompson
Developer Support Engineer
LEAD Technologies, Inc.

LEAD Logo
 
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.081 seconds.