Free Support Forum - aspose.com

'System.ArgumentOutOfRangeException' with SVG in Header with Table

I am trying to add a logo to the header of a PDF. When I use images they always look bad no matter how I set up the size and quality. They are always pixelated so I decided to use a SVG version of the logo.


I want my header to have the logo on the left and words on the right centered with the logo. I cannot use the ImageStamp because it doesn’t work with SVG files so I am trying to use a table in the logo with the SVG in it but I get

Exception thrown: ‘System.ArgumentOutOfRangeException’ in mscorlib.dll
Additional information: StartIndex cannot be less than zero.
when the file is saved.

If I remove the cell.Paragraphs.Add(image); everything is fine, but that doesn’t help me :slight_smile:

This is my code:

private void AddPageHeader(string title)
{
var header = new HeaderFooter
{
Margin = new MarginInfo { Left = 36, Right = 36, Top = 5, Bottom = 5 },
};
Image image = null;
var stream = typeof(SimpleReportGenerator).Assembly.GetManifestResourceStream("Eeg.SimpleReporting.assets.eec.svg");
if (stream != null)
{
	image = new Image
	{
		FileType = ImageFileType.Svg,
		ImageStream = stream,
		FixWidth = 75,
		FixHeight = 40
	};
}

var table = new Table { ColumnWidths = "100 100" };
var row = table.Rows.Add();

var cell = row.Cells.Add();
cell.Paragraphs.Add(new TextFragment(title));

cell = row.Cells.Add();
cell.Paragraphs.Add(image);

header.Paragraphs.Add(table);

_Page.Header = header;

}

Hello Timothy,


Thanks for contacting support.

TimothyBlue:
I want my header to have the logo on the left and words on the right centered with the logo. I cannot use the ImageStamp because it doesn’t work with SVG files so I am trying to use a table in the logo with the SVG in it but I get

Exception thrown: ‘System.ArgumentOutOfRangeException’ in mscorlib.dll
Additional information: StartIndex cannot be less than zero.

I have tried to add SVG image inside table and I was unable to observe the exception which you have mentioned. Though the image was not visible in the resultant PDF document after code was executed, so I have logged an issue as PDFNET-42748 in our issue tracking system with my sample output document.

Concerning to the issue which you have mentioned, we will appreciate if you can share a sample input document (if any) along with the SVG image, so that we can try to replicate the issue in our environment and address it accordingly.

TimothyBlue:
I am trying to add a logo to the header of a PDF. When I use images they always look bad no matter how I set up the size and quality. They are always pixelated

I have also tried to add JPG image in the PDF document through your code and I was unable to notice the bad pixel/quality issue (Please check the attached document). Please note that the image will look improper or bad if changing its height and width disturbs its aspect ratio. In order to maintain the pixel quality of the image you need to maintain its aspect ratio.

Please check following code snippet to change the height and width of the image so that its aspect ratio would remain same.


FileStream stream = new FileStream(dataDir + “sampleimage.jpg”, FileMode.Open);<o:p></o:p>

System.Drawing.Image img = new Bitmap(stream);<o:p></o:p>

img = ScaleImage(img, (int)pdfImageSection.PageInfo.Width,(int)pdfImageSection.PageInfo.Height);<o:p></o:p>

MemoryStream ms = new MemoryStream();<o:p></o:p>

img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);<o:p></o:p>

ms.Seek(0, SeekOrigin.Begin);<o:p></o:p>

var image = new Aspose.Pdf.Image { ImageStream = ms };


public static System.Drawing.Image ScaleImage(System.Drawing.Image image, int maxWidth, int maxHeight)<o:p></o:p>

{<o:p></o:p>

var ratioX = (double)maxWidth / image.Width;<o:p></o:p>

var ratioY = (double)maxHeight / image.Height;<o:p></o:p>

var ratio = Math.Min(ratioX, ratioY);<o:p></o:p>

var newWidth = (int)(image.Width * ratio);<o:p></o:p>

var newHeight = (int)(image.Height * ratio);<o:p></o:p>

var newImage = new Bitmap(newWidth, newHeight);<o:p></o:p>

using (var graphics = Graphics.FromImage(newImage))<o:p></o:p>

graphics.DrawImage(image, 0, 0, newWidth, newHeight);<o:p></o:p>

return newImage;<o:p></o:p>

}



In case if you still face image quality issue, please share your sample input file(s), so that we can also try to test the scenario in our environment and address it accordingly.


Best Regards,

The issues you have found earlier (filed as PDFNET-42748) have been fixed in Aspose.PDF for .NET 18.8.