Free Support Forum - aspose.com

Not able to convert from PDF to SVG via stream

I’m trying to convert a pdf document into svg pages. My first attempt was to load the pdf document and save it as a stream and then load that supposedly converted svg stream as a SVGDocument, this however produces a garbage object.

pageDocument.Save(ms, SaveFormat.Svg);
pageDocument.Save("temp.svg", SaveFormat.Svg);

// Object is invalid
var svgDocument1 = new SVGDocument(ms, ".");

// Create a FileStream object
using (var stream = new FileStream("temp.svg", FileMode.Open, FileAccess.Read))
{
   // Valid object
   var svgDocument2 = new SVGDocument(stream, ".");
}

By slightly modifying the code to save the pdf page as a svg image and then loading that as a file stream works perfectly, what am I doing wrong here ?

Needles to say, I want to try and avoid creating unnecessary files just for the sake of conversion.

@chiraagbangera

We tested the scenario with one of our sample file and did not notice any issue while using following code snippet:

Aspose.Pdf.Document doc = new Aspose.Pdf.Document(dataDir + @"BlockHighlightButton.pdf");
Aspose.Pdf.SvgSaveOptions saveOptions = new Aspose.Pdf.SvgSaveOptions();
MemoryStream ms = new MemoryStream();
doc.Save(ms, saveOptions);
//ms.Seek(0, SeekOrigin.Begin);
var svg = new Aspose.Svg.SVGDocument(ms, ".");

Could you please try using SvgSaveOptions to convert your PDF into SVG. Also, please make sure that you use the latest version of the API. Feel free to let us know in case you face any issues.

Hi Asasd, thanks for the quick reply, I tried setting svgsave options and i still get an empty/invalid svgdocument, i could verify that the memory stream is indeed populated with data.

image.png (34.8 KB)

and inspecting the svgdocument produced from the stream, this is what it shows me.

image.png (33.1 KB)

I am using the latest version of Aspose.SVG 20.12.0.

@chiraagbangera

Would you please confirm if you are using the API with a valid license. If so and still you are facing the issue, please share your sample source file with us along with complete sample code snippet. We will again test the scenario in our environment and address it accordingly.

Hi Asasd, I am using a Aspose.Total license file and I could verify that the license was applied because the evaluation watermark is gone. I won’t be able to share the exact test file I am testing but I will try to source a similar one. I can tell you that the pdf document has 5 pages with vector graphics in each page. The file was created with Adobe Illustrator. In the past we used LeadTools to do the same operations as now and it was working fine before.

in the meanwhile here is the code snippet

            // Convert each pdf into svg documents
            foreach ( var filePath in filePaths )
            {
                var document = new Document(filePath);

                foreach (var page in document.Pages)
                {
                    // Extract each page as a separate pdf document for conversion
                    var pageDocument = new Document();
                    pageDocument.Pages.Add(page);

                    // Convert page into svg document for manipulation
                    var ms = new MemoryStream();
                    pageDocument.Save(ms, new SvgSaveOptions());

                    var svgDocument = new SVGDocument(ms, ".");
                    svgDocuments.Add(svgDocument);
                    ms.Dispose();
                }
            }

@chiraagbangera

As shared earlier, we were unable to reproduce the issue at our end. We again tested the scenario in our environment and did not observe any issue with the same code snippet. Please try to share a sample file with us using which we can again test the scenario and try to replicate the issue.

Hi Asasd, can you please try the pdf attached with this message. On my computer i still have the same issue.

Sample_Tech_Pack_Advanced.pdf (637.4 KB)

@chiraagbangera

Could you kindly share the screenshot of the error with us. We tested the scenario using your file and did not notice any error or exception at var svgDocument = new SVGDocument(ms, ".");. We spectated the object and found that ChildNodes and Title Properties were NULL. While loading the SVG file from a file, the Title Property of the SVGDocument had some value.

Hi Asasd, I did not get any error, but when inspecting the svgdocument object, most of the properties were null including “ChildNodes”, “RootElement”. This is what lead me to believe that the svgdoucment object being created is empty/invalid. However, saving the file as a svg and then loading the file in as a file stream works perfectly.

image.png (33.1 KB)

image.png (37.2 KB)

@chiraagbangera

Please add following line of code before loading the SVGDocument from MemoryStream and let us know if issue still persists:

ms.Seek(0, SeekOrigin.Begin);

Hi Asad, that worked! Since this was not documented in the official documentation would this classify as a bug ?

@chiraagbangera

This would not be a bug in the API as seeking the memory stream back to the beginning resolved the issue. However, we will surely update our documentation accordingly so that such a situation can be prevented in the future.