Fuzzy text in pdf on a transparent png generated by Aspose.SVG

Hello,

I’m using Aspose.SVG to generate a PNG from SVG, and then I use this PNG to add it to a PDF created by Aspose.PDF.

The text in the generated PNG looks ok, but when it’s added to the PDF file, it looks fuzzy:
image.png (26.8 KB)

Note that the SVG doesn’t have a background color set. When I set it (i.e. I add style on the div
background-color: white;), the issue is no longer present.

Aspose.SVG version: 24.6.0
Aspose.PDF version: 24.6.0
C# target framework: .NET 8

Svg in question:

<svg xmlns='http://www.w3.org/2000/svg' width='300' height='300' viewBox='0 0 300 300'>
	<foreignObject width='300' height='300'>
		<div xmlns='http://www.w3.org/1999/xhtml' style='width: 100%; height: 100%; font-family: Arial;'>
			<style type='text/css'/>
			<div style='width:100%;font-size:16px;text-align:center;font-family: Arial;color: #ebcece;'>Some text</div>
			<div style='width:100%;text-align:center;font-family: Arial;color: #bcf5ec;'>Some longer text. Something here as well.</div>
		</div>
	</foreignObject>
</svg>

Code used to generate the pdf:


using Aspose.Svg;
using Aspose.Svg.Saving;
using Aspose.Svg.Converters;
using Aspose.Svg.Rendering.Image;
using Aspose.Pdf;


Document d = new Document();
Aspose.Pdf.Page p = d.Pages.Add();
p.SetPageSize(8.3 * 72, 11.7 * 72); // A4 in pt

using (var svgMemStream = new MemoryStream())
{

    using Aspose.Svg.SVGDocument svg = new Aspose.Svg.SVGDocument(@"path to the source SVG");
    ImageRenderingOptions imageOptions = new(ImageFormat.Png);
    imageOptions.PageSetup.Sizing = Aspose.Svg.Rendering.SizingType.FitContent;
    imageOptions.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    using (ImageDevice device = new ImageDevice(imageOptions, svgMemStream))
    {
        svg.RenderTo(device); // render svg to png in memory
    }

    ImageStamp imgCroppedStamp = new ImageStamp(svgMemStream) // use png in memory to add to pdf
    {
        HorizontalAlignment = HorizontalAlignment.Left,
        VerticalAlignment = VerticalAlignment.Top,
        LeftMargin = 72,
        TopMargin = 109.5,
        Width = 112,
        Height = 112
    };

    p.AddStamp(imgCroppedStamp);
}

d.ProcessParagraphs();

d.Save(@"output PDF file");

Let me know if you need more info.

Thanks!

@pavle.aleksov

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-57623

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

1 Like

@pavle.aleksov

This problem is not related to our library, if you run only your code related to rendering an SVGDocument into PNG (without referencing Aspose.PDF), you’ll see that the converted PNG already has fuzzy outlines around the text. Please create a topic in Aspose.SVG forum category, as we’re not able to do anything to fix the image improperly converted by the Aspose.SVG library.

Also, please note that you can add this SVG to the document, using Aspose.PDF only, with no reference to Aspose.SVG, employing the Aspose.Pdf.Image class. Please try the following code snippet and see if it satisfies requirements:

using Aspose.Pdf;


Document d = new Document();
Aspose.Pdf.Page p = d.Pages.Add();
p.SetPageSize(8.3 * 72, 11.7 * 72); // A4 in pt
// Set zero page margins, so that they don't interfere with the image positioning
p.PageInfo.Margin = new MarginInfo(0, 0, 0, 0);

Aspose.Pdf.Image image = new Image
{
    HorizontalAlignment = HorizontalAlignment.Left,
    VerticalAlignment = VerticalAlignment.Top,
    File = @"path to the source SVG",
    // It's important to set the correct FileType
    FileType = ImageFileType.Svg,
    FixWidth = 112,
    FixHeight = 112,
    Margin = new MarginInfo(72, 0, 0, 109.5)
};

p.Paragraphs.Add(image);

d.ProcessParagraphs();

d.Save(@"output PDF file");

57623_out.pdf (7.9 KB)

1 Like

My bad, the reason I didn’t see the fuzzy outlines when generating just the SVG was cause my img viewer (Microsoft photos) has black background:
image.png (9.6 KB)

But when I edit the png in mspaint, I do see it.
image.png (9.2 KB)

I won’t be reporting the issue to Aspose.SVG since I believe it would be the same GDI problem as in Aspose.SVG renders an imaginary line between two foreignObject divs
The similar workaround (adding a background) fixes it as well.

Thanks, for the update!

@pavle.aleksov

Thanks for your kind feedback. Please feel free to create a new topic in case you need any kind of assistance.

1 Like