Free Support Forum - aspose.com

Add SVG Image to a Page

I am trying to complete the evaluation and just have this one last requirement.

This is a function that will receive the PDF, SVG Image, and the field name to replace with the image.

If I use a jpg, the function will work, if I use a SVG, the function will not work.

How do I get this function to work with a SVG image?

private Stream ProcessHandSignatures(Stream pdfReader, Stream signatureFileStream, string signatureFieldName)
        {            
                                                             
            if (signatureFileStream !=null)
            {
                Document pdfDocument = new Document(pdfReader);

                // Add SVG to Signature field in PDF                   
                PdfFileMend mender = new PdfFileMend(pdfDocument);

                Field result = Array.Find(pdfDocument.Form.Fields, p => p.FullName == signatureFieldName);

                if (result != null)
                {
                    // Get coordinates of signature field
                    int pageIndex = pdfDocument.Form[signatureFieldName].PageIndex;
                    Rectangle sigRectangle = pdfDocument.Form[signatureFieldName].GetRectangle(false);

                    // Add image over signature field
                    mender.AddImage(signatureFileStream, pageIndex, (float)sigRectangle.LLX, (float)sigRectangle.LLY, (float)sigRectangle.URX, (float)sigRectangle.URY);

                    // Remove signature field
                    removeField(pdfDocument, signatureFieldName);

                    // Save
                    pdfDocument.Save(pdfReader);
                }                                               
            }                                    

            return pdfReader;
        }

Hi There,


Thanks for using our API.

I have tested the entire scenario using Aspose.Pdf (DOM) approach, because Aspose.Pdf.Facades approach is going to be obsolete soon and I observed that API threw an exception while adding SVG into PDF document. Therefore, for the sake of correction, I have logged an issue as PDFNET-42576 in our issue tracking system. We will further look into the details of the issue and keep you informed on the status of its resolution. Moreover, you can check the following code snippet which I have used to test the scenario.

FileStream pdfReader = new FileStream(dataDir + “input.pdf”, FileMode.Open);<o:p></o:p>

FileStream signatureFileStream = new FileStream(dataDir + “sample.svg”, FileMode.Open);<o:p></o:p>

string signatureFieldName = “sign1”;<o:p></o:p>

Document pdfDocument = new Document(pdfReader);<o:p></o:p>

Page page = pdfDocument.Pages[1];<o:p></o:p>

page.Resources.Images.Add(signatureFileStream);<o:p></o:p>

page.Contents.Add(new Operator.GSave());<o:p></o:p>

Aspose.Pdf.Rectangle rectangle = pdfDocument.Form[signatureFieldName].GetRectangle(false);<o:p></o:p>

Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });<o:p></o:p>

page.Contents.Add(new Operator.ConcatenateMatrix(matrix));<o:p></o:p>

XImage ximage = page.Resources.Images[page.Resources.Images.Count];<o:p></o:p>

page.Contents.Add(new Operator.Do(ximage.Name));<o:p></o:p>

page.Contents.Add(new Operator.GRestore());<o:p></o:p>

pdfDocument.Save(pdfReader);<o:p></o:p>


Please be patient and spare us a little time. We are sorry for the inconvenience.


Best Regards,

Hi John,


Thanks for contacting support.

Adding more to Asad’s comments, as a workaround, you may search the form field inside PDF file, get its related information (coordinates etc) and in order to render the SVG inside PDF, you may consider Add SVG Object to Table Cell (may be you can create a table with one row and one column to accomplish this requirement).

In case you still face any issue, you may consider Converting SVG To Raster Format using Aspose.Imaging for .NET and then use the same raster image inside PDF document. For further details, please visit Add Image to Existing PDF File
On this line
page.Resources.Images.Add(signatureFileStream);

I keep getting the following error:
An unhandled exception of type 'System.ArgumentException' occurred in Aspose.Pdf.dll

Additional information: Invalid image stream (Parameter is not valid.)

I just used my SVG instead of the one you have.

Adding the SVG with a table did not work. The table was there, but no image.


I did try the image library with my first solution and I did get the SVG to show up on the PDF.

I would still like to see the other solutions work that were suggested.

Hi John,


Thanks for writing back.

johnshort:
On this line
page.Resources.Images.Add(signatureFileStream);

I keep getting the following error:
An unhandled exception of type ‘System.ArgumentException’ occurred in Aspose.Pdf.dll

Additional information: Invalid image stream (Parameter is not valid.)

I just used my SVG instead of the one you have.

As shared earlier, it seemed that API is creating issue while adding SVG image into page resources and for the sake of correction, I already have logged an issue in our issue tracking system. We will look into the detail of the issue and keep you updated on the status of its resolution.

johnshort:
Adding the SVG with a table did not work. The table was there, but no image.

I have tried to add SVG image in a table inside PDF document and I did not observe the issue which you have mentioned. We will really appreciate if you please share a sample code snippet which you have used to add SVG image inside a table so that we can try to test it in our environment and respond you accordingly. For your reference, I have used following code snippet.

FileStream pdfReader = new FileStream(dataDir + “input.pdf”, FileMode.Open);<o:p></o:p>

Document pdfDocument = new Document(pdfReader);

Page page = pdfDocument.Pages[1];

Aspose.Pdf.Image img = new Aspose.Pdf.Image();

img.FileType = Aspose.Pdf.ImageFileType.Svg;

img.File = dataDir + "sample.svg";

img.FixWidth = 50;

img.FixHeight = 50;

Aspose.Pdf.Table table = new Aspose.Pdf.Table();

table.ColumnWidths = "100";

Aspose.Pdf.Row row = table.Rows.Add();

Aspose.Pdf.Cell cell = row.Cells.Add();

cell.Paragraphs.Add(img);

page.Paragraphs.Add(table);

pdfDocument.Save(pdfReader);


Best Regards,

Adding a table to a paragraph solution: Even if the image showed, I am not sure how I could specify the rectangle to put the image in. The image has to be put over the existing signature field in the form.

Hi John,


Thanks for your patience.

I have tried to add table at specific position using the feature of placing FloatBox but I got no success. You may check the following code snippet which I have used to achieve the functionality. It seemed that sizing the SVG image is not working (i.e highlighted part) and floating box also did not appear at expected position.


FileStream pdfReader = new FileStream(dataDir + “input.pdf”, FileMode.Open);<o:p></o:p>

string signatureFieldName = "sign1";

Document pdfDocument = new Document(pdfReader);

Field result = Array.Find(pdfDocument.Form.Fields, p => p.FullName == signatureFieldName);

if (result != null)

{

Rectangle sigRectangle = pdfDocument.Form[signatureFieldName].GetRectangle(false);

Aspose.Pdf.Image img = new Aspose.Pdf.Image();

img.FileType = Aspose.Pdf.ImageFileType.Svg;

img.File = dataDir + "sample.svg";

img.FixWidth = sigRectangle.Width;

img.FixHeight = sigRectangle.Height;

Aspose.Pdf.Table table = new Aspose.Pdf.Table();

table.ColumnWidths = sigRectangle.Width.ToString();

Aspose.Pdf.Row row = table.Rows.Add();

Aspose.Pdf.Cell cell = row.Cells.Add();

table.ColumnAdjustment = ColumnAdjustment.Customized;

cell.Paragraphs.Add(img);

FloatingBox aBox = new FloatingBox((float)sigRectangle.Width, (float)sigRectangle.Height);

aBox.Left = 5;

aBox.Top = 5;

aBox.Border = new BorderInfo(BorderSide.All, Aspose.Pdf.Color.Red);

aBox.BackgroundColor = Aspose.Pdf.Color.Yellow;

aBox.Paragraphs.Add(table);

pdfDocument.Pages[pdfDocument.Form[signatureFieldName].PageIndex].Paragraphs.Add(aBox);

pdfDocument.Save(pdfReader);

}



Therefore I have logged issue(s) as PDFNET-42590 (SVG image size) and PDFNET-42591 (Add table at specified location) in our issue tracking system. We will further process it for investigation and keep you updated on the status of its correction. Please be patient and spare us a little time.

Furthermore as shared earlier, you may consider Converting SVG To Raster Format using Aspose.Imaging for .NET and then use the same raster image inside PDF document using the earlier code snippet which I have shared in my early reply.

We are sorry for the inconvenience caused.


Best Regards,

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