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!