SVG Background error 'Invalid image stream (Image export failed.)'

I’m working on a POC to create a pdf, the issue I have is when I try to add a svg image as background with this code:

BackgroundArtifact background = new BackgroundArtifact();
background.BackgroundImage = File.OpenRead(image);
background.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
background.ArtifactVerticalAlignment = VerticalAlignment.Center;
background.Position = new Aspose.Pdf.Point(0, 0);
page.Artifacts.Add(background);

the background for the first page works fine but for all the other pages send the error Invalid image stream (Image export failed.), 2 weeks ago I test this code and works fine, actually if I use other type of image works, the problem is only with svg files.

could you help me to understand what is the issue?

@armando.rodriguez,

Since I do not see the whole code I don’t know if this code is been iterated or not. But at first look, it seems like you are only adding the background to a single page object.

I have a code sample for you to see with the files used included as attachments.

private void Logic()
{
    Document doc = new Document($"{PartialPath}_input.pdf");

    // Create Background Artifact object
    BackgroundArtifact background = new BackgroundArtifact();

    // Specify the image for backgroundartifact object
    background.BackgroundImage = File.OpenRead($"{PartialPath}.jpg");

    // Add backgroundartifact to artifacts collection of page
    foreach (var page in doc.Pages)
    {
        page.Artifacts.Add(background);
    }

    doc.Save($"{PartialPath}_output.pdf");
}

AddingBackgroundImage.jpg (544.1 KB)
AddingBackgroundImage_input.pdf (3.0 KB)
AddingBackgroundImage_output.pdf (3.2 MB)

Hi @carlos.molina, the code is iterated and my problem is only with svg files the second time or subsequent I try to add an svg image as background using the code I sent you, I´m trying to use a different background image for each page.

do you need an snipped of all the code?

@armando.rodriguez,

I will need your code snippet and the image as well.

this is the code snippet

        string pdfSource = Path.Combine(Directory.GetCurrentDirectory(), "pdfFiles");
        if (!Directory.Exists(pdfSource))
        {
            Directory.CreateDirectory(pdfSource);
        }
        string filePdf = Path.Combine(pdfSource, "testPDF_SVGFile.pdf");
        Document doc = pdfAspose.CreateFile(filePdf);

        for (int i = 0; i < 20; i++)
        {
            int pagNum = 0;
            foreach (int pg = 1; pg < 10; pg++)
            {
                Page nPag = doc.Pages.Add();

                int pagfin = doc.Pages.Count;
                Page page = doc.Pages[pagfin];
                BackgroundArtifact background = new BackgroundArtifact();
                background.BackgroundImage = File.OpenRead(svgFiles[pagNum]);
                background.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
                background.ArtifactVerticalAlignment = VerticalAlignment.Center;
                background.Position = new Aspose.Pdf.Point(0, 0);
                page.Artifacts.Add(background);
                background.Dispose();

                doc.Save(doc.FileName);
                pagNum++;
            }
        }

        var optimizeOptions = new Aspose.Pdf.Optimization.OptimizationOptions
        {
            //RemoveUnusedObjects = true,
            //RemoveUnusedStreams = true,
            //AllowReusePageContent = true,

            RemoveUnusedObjects = true,
            RemoveUnusedStreams = true,
            AllowReusePageContent = true,
            LinkDuplcateStreams = true,

        };

        doc.OptimizeResources(optimizeOptions);
        doc.Optimize();

        doc.Save(doc.FileName);
        doc.Dispose();

SVGPictures.zip (48.3 KB)

@armando.rodriguez,

I need the image and the pdf document. I need to replicate the issue. If you saw my code someple, the functionality works. So There must be something on the PDF or the image itself.

Please provide those 2 elements.

SVGPictures.zip (48.3 KB)

testPDF_SVGFile.pdf (573.2 KB)

images and pdf sample

@armando.rodriguez,

Can you provide the pdf please.

Just so we dont go back and forth every time let me explain how we usually work. What we required is the code snipped and the input files, meaning PDF and any other extra file used in the process you describe in this forums that you need help with.

testPDF_SVGFile.pdf (573.2 KB)

@armando.rodriguez,

I tried the following code and worrks, it doesnt look good because the SVG are bigger tham the rendered area but it does not fails.

private void Logic()
{
    Document doc = new Document();

    for (int i = 1; i <= 10; i++)
    {
        // Create Background Artifact object
        BackgroundArtifact background = new BackgroundArtifact();

        // Specify the image for backgroundartifact object
        background.BackgroundImage = File.OpenRead($@"{prefixPath}\Images\BackgroundMultipleSvgImages\page{i}.svg");
        background.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
        background.ArtifactVerticalAlignment = VerticalAlignment.Center;
        background.Position = new Aspose.Pdf.Point(0, 0);

        var page = doc.Pages.Add();

        page.Artifacts.Add(background);

        Console.WriteLine($"Page {i} done.");
    }            

    doc.Save($"{PartialPath}_output.pdf");
}

I did this code with a brand new document.

Here is the output:
BackgroundMultipleSvgImages_output.pdf (388.6 KB)

you say me that if I change the size of the page the svg could be shown with all the quality of the image, including the text in the correct position and not blurred, is correct?

@armando.rodriguez,

I apologize for not explaining this correctly. When I said bigger, I mean in pixels. Not in storage size.

So if you put the image into a bigger canvas(page) it should display properly.

Also, if you are allowed to manipulate the format, I suggest you to change from SVG to PNG.

Then you could use the class PngDevice, which gives you some extra tools, like quiality control.