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;
}
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.
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).
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.
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.
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);
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.