Add SVG in PDF as ImageStamp in C# using Aspose.PDF for .NET

Hi support,

I tried adding an SVG image to an ImageStamp. This results in a “Parameter is not valid” error. How do I fix this?

using (var imageStream = new MemoryStream(UC.PDF.Business.Properties.Resources.iconen))
{
var image = new Aspose.Pdf.Image
{
FileType = ImageFileType.Svg,
ImageStream = imageStream,
FixWidth = 178.58,
FixHeight = 28.35
};
imageStamp = new ImageStamp(image.ImageStream);
foreach (var page in document.Pages)
{
page.AddStamp(imageStamp);
}
}

Best regards,
Marcel

@Snaak1968

Thank you for contacting support.

Would you please share your sample SVG file as ZIP so that we may try to reproduce and investigate it in our environment.

svg.zip (4.3 KB)

@Snaak1968

We have tested the scenario in our environment using following code snippet which is correct way to add Image Stamp inside PDF and faced an OOM (Out Of Memory) exception.

Add Image Stamp Using Aspose.PDF

Document pdfDocument = new Document(dataDir + "Flatten.pdf");
ImageStamp imageStamp = new ImageStamp(dataDir + "iconen.svg");
imageStamp.Background = true;
imageStamp.Opacity = 0.5;
imageStamp.HorizontalAlignment = HorizontalAlignment.Center;
imageStamp.VerticalAlignment = VerticalAlignment.Center;
// Add stamp to particular page
for (int i = 1; i <= pdfDocument.Pages.Count; i++)
    pdfDocument.Pages[i].AddStamp(imageStamp);
pdfDocument.Save(dataDir + "ImageStamp_out1true.pdf");

We have logged an issue as PDFNET-47463 in our issue tracking system for further investigation. We will check this issue in details and keep you posted with the status of its correction. Please be patient and spare us little time.

We are sorry for the inconvenience.

Hi, @asad.ali! Any progress on this? Still get “out of memory” for svg. Aspose.Pdf 20.8.0

@mgkcortyw

Regretfully, the ticket is still unresolved. It is related to performance of the API and performance related issues are complex in nature. The issue needs more time to get fully analyzed and resolved. We will let you know as soon as we have additional updates regarding its fix. Please give us some time.

We are sorry for the inconvenience.

Hello,
Has any update been set for this issue? Still hitting the OOM exception in 22.9.0.

Thanks

@awulff

Regretfully, the issue has not been yet resolved. However, would you please share your sample files and code snippet with us so that we can test the case using 22.9 version of the API for your files and address it accordingly.

@asad.ali, Thank you for the reply.

Unfortunately, most of our executables use a good amount of startup libraries we had built and without adding external access to our nuget server (which I am not allowed to expose) I cannot send a full executable. However, the method I had tried was done using the attached svg (in a zip since .svg is not a supported upload extension) and the code you had provided on December 2019.header.zip (6.0 KB)

I am sorry I forgot to mention this:

We have a work around. Given our subscription includes Aspose.Words I can add my SVGs to a word document, save and then save again as PDF; interestingly enough, if I don’t do a save to a file system before the transform to PDF, the SVG images are not in the rendered PDF.

Attached is part of our code for the work around logic:

        public PdfDocument GeneratePdf(Model.Notification notification, Template template)
        {
            var wordDoc = MergeNotificationWithTemplate(notification, template);
            if (wordDoc == null)
                return null;

            wordDoc.SetFontAsArial();

            string tempWordFileName = null;

            // SVG headers / footers need added to the file while it is still in docx.  When we hit PDF, we explode on out of memory exceptions.
            if (template.Header.FileExtension.ToLower() == "svg" || template.Footer.FileExtension.ToLower() == "svg")
            {
                var builder = new DocumentBuilder(wordDoc);
                var pageWidth = wordDoc.GetPageInfo(0).WidthInPoints;

                if (template.Header.FileExtension.ToLower() == "svg")
                {
                    using (var imgStream = new MemoryStream(template.Header.Document))
                    {
                        builder.InsertImage(imgStream,
                            Aspose.Words.Drawing.RelativeHorizontalPosition.Margin,
                            Math.Max(pageWidth - (2 * template.Header.XPosition) - decimal.ToDouble(template.Header.SVGWidth.Value), 0),
                            Aspose.Words.Drawing.RelativeVerticalPosition.Margin,
                            0,
                            decimal.ToDouble(template.Header.SVGWidth.Value),
                            decimal.ToDouble(template.Header.SVGHeight.Value),
                            Aspose.Words.Drawing.WrapType.Square);
                    }
                }

                if (template.Footer.FileExtension.ToLower() == "svg")
                {
                    // because the width may be under sized, set a proportion
                    var widthProportion = pageWidth / decimal.ToDouble(template.Footer.SVGWidth.Value);

                    using (var imgStream = new MemoryStream(template.Footer.Document))
                    {
                        builder.InsertImage(imgStream,
                            Aspose.Words.Drawing.RelativeHorizontalPosition.Margin,
                            0,
                            Aspose.Words.Drawing.RelativeVerticalPosition.BottomMargin,
                            Math.Max((2 * template.Footer.YPosition) - decimal.ToDouble(template.Footer.SVGHeight.Value), 0),
                            decimal.ToDouble(template.Footer.SVGWidth.Value),
                            decimal.ToDouble(template.Footer.SVGHeight.Value),
                            Aspose.Words.Drawing.WrapType.Square);
                    }
                }

                tempWordFileName = $@"C:\Outputs\image\Dev\20220923\test-{notification.ID}.docx";

                wordDoc.Save(tempWordFileName);

                wordDoc = new WordDocument(tempWordFileName);
            }

            var stream = new MemoryStream();

            wordDoc.Save(stream, Aspose.Words.SaveFormat.Pdf);

            var pdf = new PdfDocument(stream, isManagedStream: true);

            pdf = AddPDFHeaderFooter(pdf, template, notification);

            if (!string.IsNullOrWhiteSpace(tempWordFileName))
                File.Delete(tempWordFileName);

            return pdf;
        }

@awulff

Thanks for sharing the requested information. Another ticket as PDFNET-52633 has been logged in our issue tracking system for your file separately. We will also look into its details and let you know as soon as it is resolved. Please spare us some time.

We are sorry for the inconvenience.

@awulff

Unfortunately ImageStamp doesnot support svg format, but you can use Aspose.Pdf to convert svg to pdf and after pdf to png, for example:

using (var svgDocument = new Document("header.svg", new SvgLoadOptions()))
{
    svgDocument.Save("header.pdf");
}

using (var pdfDocument = new Document("header.pdf"))
{
    new PngDevice().Process(pdfDocument.Pages[1], "header.png");
}

ImageStamp imageStamp = new ImageStamp("header.png", true);