We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replacing Image in PDF file

We’re working on an application that takes data from our database and fills out government required forms. I can do the form fill with either the FillField or ImportXML, I’ve gotten both to work. My problem is that I need to put a PDF417 barcode on all the forms and some of them I have to put two different PDF417, as well as a code39 barcode on some too.
We already have a component that will create the barcode as a bmp file so all I need to do is replace the one in the PDF file. I’m using the FillImageField which I think should work but I can’t figure out what the name of the image object is in Adobe and/or can’t get an image object created in the PDF correctly.
Using only the Aspose.PDF.Kit is it possible to take a given image and drop it on top the PDF at a certain X-Y location? Here’s some of my code I’ve been playing with:

Dim templatePDF As String = “c:\source\MV-140.pdf”
Dim outputFile As String = “C:\Source\bcOutput.pdf”
Dim form As Form = New Form(templatePDF, outputFile)
Dim barcode As New PDF417Lib.PDF

’ This creates the barcode, this works fine
barcode.DataToEncode = “This is a test” 'aamva.EmbedData
barcode.FixedResolutionCM = 0
barcode.PDFColumns = 8
barcode.PDFErrorCorrectionLevel = 2
barcode.SetPixelsXY(400, 200)

’ Load the form from an XML document, this works fine
form.ImportXml(File.Open(“C:\Source\Input.xml”, FileMode.Open))

’ This doesn’t work, at least one problem that I know is that the fieldname “text34” isn’t the right
’ name for the image object.
form.FillImageField(“text34”, “c:\barcode.bmp”)


I’m trying to make 100% certain that I can get this one component to do everything I need to before I put in the budget request. I think this is about the last thing I need to figure out.



Thank you for considering Aspose.

You can use AddImage method of PdfFileMend to add image to the page of the PDF document at any specified location (determined by lower left X-axis, lower left Y-axis, upper right X-axis and upper right Y-axis) in a specified page number of the PDF document.

For more information please consult http://www.aspose.com/Wiki/default.aspx/Aspose.Pdf.Kit/AdhereImage.html


Okay, I’m using the FileMend as shown in the code below and I’ve gotten it to work with a jpg file that is on the harddrive, put the PNG file gives the error code that is below it. Both of the AddImage methods give the same error and I have verified that the memory stream does contain the PNG file.


Dim mend As New PdfFileMend(outputFile, outputFile2)
mend.AddImage(“g:\aes\barcode.png”, 10, 10, 50, 50, 1)
mend.AddImage(memStream, 1, 300, 800, 400, 1000)

---- See exception detail below

x833cb12bef83f1f9.xc048bd652dc413fb was unhandled
Message=“Unknown PNG filter!”
at Aspose.Pdf.Kit.PdfFileMend.AddImage(Stream imageStream, Int32 pageNum, Single lowerLeftX, Single lowerLeftY, Single upperRightX, Single upperRightY)
at ActivePDF.Form1.Button3_Click(Object sender, EventArgs e) in C:\Source\ActivePDF1\ActivePDF1\Form1.vb:line 196
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at ActivePDF.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

Dear Allen,

Can you please provide complete code and images that can reproduce this error?

Dear Allen,

Thanks for considering our product :)
Firstly, I would like to explain FillImageField functionality to you. This function can paste an existing image (for example, the barcode c:\barcode.bmp you created by other tools) to a pre-designed Button in PDF templates, looking like filling the Button with the image. In AcroForm (such as document from Adobe 6 and below), this Button is supposed to be a normal push button field; while in XfaForm (such as document from Adobe 7 and above), it is supposed to be an image button field. More detail please refer to the corresponding API or wiki.
As to your project, Aspose.Pdf.Kit can 100% fullfill your requirement if you can include those Buttons (i.e., push button in AcroForm or image button in XfaForm) in your PDF documents beforehand. Otherwise, there is another alternative to add a push button field by invoking FormEditor.AddField(). The only problem is that it only works in AcroForm. The support for XfaForm image field adding is under developing, but I don't think it will be released soon.
Please feel free to post it if you have any other question. Best regards.

'Here’s the code and I’ve attached a zip file with the PDF file and two images.
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim inputFile As String = “g:\aes\Barcodes\mv-140.pdf”
Dim outputFile As String = “g:\aes\BarCodes\mv-140barcode.pdf”

Dim mend As New PdfFileMend(inputFile, outputFile)
’ This jpg was created by converting the png file to a jpg file using Photoshop Elements
mend.AddImage(“g:\aes\barcodes\elememstream.jpg”, 1, 370, 700, 550, 800)
’ This png file is the one that was converted to the jpg file in the above line.
’ It was originally created using the .NET Bitmap object and saving it to a png file.
’ Ultimately I’d like to get the memory stream working to cut down on the temporary files used.
mend.AddImage(“g:\aes\barcodes\memstream.png”, 10, 10, 50, 50, 1)
'mend.AddImage(memStream, 1, 300, 800, 400, 1000)
End Sub

Dear Allen,

Sorry for the delaying response.

After investigation, I found the reason lies in the special format of your PNG image file. It seems that a uncommon filter is adopted, which cannot be supported well till now in Aspose. And I do not think this will be provided soon.

My suggestion is: change the image file type, such as jpg or bmp. Furthermore, when I convert your JPG image into PNG with mspaint, it works well, too.

BTW, mend.AddImage("g:\aes\barcodes\memstream.png", 10, 10, 50, 50, 1)
should be:

mend.AddImage("g:\aes\barcodes\memstream.png", 1, 10, 10, 50, 50)