When attempting to convert an SVG to pdf with PageSetup.Sizing types ScaleContent and Contain we get an exception. The other 2 types work fine. This doesn’t seen to be document related but I do have a sample.
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Aspose.SVG
StackTrace:
at ..Transform(IMatrix )
Code is simply:
using (SVGDocument doc = new SVGDocument(filename, new Aspose.Svg.Configuration()))
{
var saveOptions = GetSaveOptions();
using (PdfDevice pdfDevice = new PdfDevice(saveOptions, filename)
doc.RenderTo(pdfDevice);
}
private PdfRenderingOptions GetSaveOptions()
{
var saveOptions = new PdfRenderingOptions()
{
JpegQuality = 100,
FormFieldBehaviour = FormFieldBehaviour.Flattened
};
saveOptions.PageSetup.Sizing = SizingType.ScaleContent;
return saveOptions;
}
Scenario: Converting an SVG to PDF while using PageSetup.Sizing = SizingType.ScaleContent (or SizingType.Contain).
Observed behaviour: A System.NullReferenceException is thrown from the internal Transform method.
Why the exception occurs
SizingType.ScaleContent and SizingType.Contain require both the target page dimensions (width & height) and the SVG’s intrinsic dimensions to be known at the moment the rendering pipeline is built.
If the page size is not explicitly set, the internal layout engine receives a null value for the target size, which leads to the NullReferenceException you see.
How to fix it
Specify the page width and height (or a predefined PageSize) in PdfRenderingOptions.PageSetup.
(Optional) Set the page margins if you need a non‑zero border.
Keep the Sizing value as ScaleContent or Contain.
Minimal, working example
using Aspose.Svg;
using Aspose.Svg.Rendering;
using Aspose.Svg.Rendering.Pdf;
// ------------------------------------------------
// 1. Load the SVG document
// ------------------------------------------------
using (SVGDocument doc = new SVGDocument(@"C:\Input\sample.svg"))
{
// ------------------------------------------------
// 2. Prepare PDF rendering options
// ------------------------------------------------
PdfRenderingOptions pdfOptions = GetPdfOptions();
// ------------------------------------------------
// 3. Create the PDF device (output file is "sample.pdf")
// ------------------------------------------------
using (PdfDevice pdfDevice = new PdfDevice(pdfOptions, @"C:\Output\sample.pdf"))
{
// ------------------------------------------------
// 4. Render SVG → PDF
// ------------------------------------------------
doc.RenderTo(pdfDevice);
}
}
// ------------------------------------------------
// Helper: configure PdfRenderingOptions
// ------------------------------------------------
private static PdfRenderingOptions GetPdfOptions()
{
var opts = new PdfRenderingOptions
{
JpegQuality = 100,
FormFieldBehaviour = FormFieldBehaviour.Flattened
};
// ---- IMPORTANT: define the target page size ----
// You can use any unit you prefer (point, mm, inch, etc.)
// Here we use points (1 point = 1/72 inch):
opts.PageSetup.Width = 595; // A4 width = 210 mm ≈ 595 pt
opts.PageSetup.Height = 842; // A4 height = 297 mm ≈ 842 pt
// Choose the sizing mode
opts.PageSetup.Sizing = SizingType.ScaleContent; // or SizingType.Contain
// (Optional) remove margins if you want the SVG to occupy the whole page
opts.PageSetup.MarginTop = 0;
opts.PageSetup.MarginBottom = 0;
opts.PageSetup.MarginLeft = 0;
opts.PageSetup.MarginRight = 0;
return opts;
}
What the code does
Step
Explanation
Load SVG
SVGDocument parses the file and extracts its intrinsic size (width/height or viewBox).
Configure PdfRenderingOptions
PageSetup.Width & Height give the renderer a concrete target rectangle. The chosen SizingType then decides how the SVG is fitted into that rectangle.
Create PdfDevice
Binds the options to the output PDF file.
Render
doc.RenderTo(pdfDevice) performs the conversion without hitting the NullReferenceException.
Additional notes
Topic
Details
Supported units
PageSetup.Width/Height accept any SVG length unit (pt, mm, in, cm). The library automatically normalizes them.
Contain vs. ScaleContent
Contain scales the SVG to fit inside the page while preserving aspect ratio (may leave empty space). ScaleContent also preserves aspect ratio but fills the page, cropping if necessary.
Why the exception appears only on 25.8.0
In 25.8.0 the internal guard that validates the presence of page dimensions was missing for these two sizing modes. The issue was fixed in later builds (≥ 25.9.0).
Recommended version
If upgrading is possible, move to the latest stable release. The fix is included and you will also benefit from performance improvements and additional rendering features.
Add page dimensions (as shown above) to your existing GetSaveOptions() method.
Re‑run the conversion – the NullReferenceException should disappear.
(Optional) Upgrade to the newest Aspose.SVG for .NET version to get the bug fix and the latest enhancements.
If you still encounter issues (e.g., the SVG does not have a defined viewBox/width/height), please share a minimal SVG sample so we can investigate the intrinsic size handling further.
Happy coding! If you have any more questions or need assistance with other Aspose products, feel free to reply.