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.
@chiraagbangera
This to update you that we are going to add descriptions to API that document is processed from the current position in the stream. We think that it will be enough because working with positions in streams is a regular practice described in Microsoft documentation. Besides you are able to load documents from compound streams where position of document within the stream can be more than 0.
I have an additional question related to the same project when used as a managed library inside Unity3D. When I am loading the SVGDocument either through a stream or a file, it doesn’t seem to fully initialize. I end up with an object that is empty. The ready state says loading and doesn’t seem to change to complete. This is only in Unity3D, the unit tests work perfectly fine.
image.png (22.8 KB)
Is there something I am missing ?
@chiraagbangera
It is hard to tell the reason behind such behavior by the API. Would you please share the steps to replicate the same issue in our environment. Also, please share your sample SVG file with us. We will test the scenario in our environment and address it accordingly.
Hi Asasd, i have created a sample unity3d project and a sample .net standard library that is used in the unity project. I still have the same issue when using the sample file. I have attached everything needed to test with this message.
When inspecting the svgdocument object after creating a new instance via stream, the ReadyState still says loading. I already tried loading the document asynchronously with the example code from
but that creates an infinite loop at the documentEvent.WaitOne(); line and unity freezes obviously.
Also,
I tried loading a pdf/svg directly from the unity3d project with the code below, no luck. Changing unity player settings such as scripting backend and .net version also yielded no results.
var svgDocuments = new List();
var document = new Document(pdf);
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
using ( var ms = new MemoryStream() )
{
pageDocument.Save(ms, SaveFormat.Svg);
// Reset stream position to 0
ms.Seek(0, SeekOrigin.Begin);
var svgDocument = new SVGDocument(ms, ".");
svgDocuments.Add(svgDocument);
}
}
// Load SVG Directly from file
var testSvg = new SVGDocument(".\\Assets\\temp.svg"); //same issue here
I do want to add that the pdf loads fine, i am able to save it as a file pdf/svg and it looks okay to me, it’s only when creating the svgdocument i have issues.
@chiraagbangera
We are checking the scenario in our environment with Unity3D and will get back to you shortly.
Hi, any updates to this issue yet ?