I looked at this Stackoverflow question, iTextSharp multiple actions for pushbuttonfield, but I still cannot get this working correctly. This is a proof of concept before we buy the iTextSharp license.
So here is the background. I am working on a web forms web application written in vb.net. I open a new browser window to display the PDF in an iframe. The PDF contains form fields, which the user can fill out. At the bottom of the form, there will be a submit button to process the form and a save progress button to save their current changes. After the form is processed/saved, it's supposed to redirect to an HTML page that says "Form Submitted Successfully". I have this all working in Google Chrome, but it's not doing the redirect to the HTML page in IE.
We will be getting LOTS of PDFs from our customers. When I create the PDF forms in Acrobat, I add the 2 buttons to the bottom of the PDF. This ensures the correct placement of the buttons, based on where it fits for that PDF. So the buttons will always have the same names, "btnSaveProgress" and "btnProcessForm".
In the codebehind for the source of the iframe, I have this iTextSharp code:
Using myPdfReader As New PdfReader(strFullFilePathName)
Context.Response.Clear()
Context.Response.ContentType = "application/pdf"
Using stamper As PdfStamper = New PdfStamper(myPdfReader, Response.OutputStream)
stamper.ViewerPreferences = PdfWriter.HideToolbar
Dim hostUrl As String = Request.Url.GetLeftPart(UriPartial.Authority)
Dim btnArray() As String = {"btnSaveProgress", "btnProcessForm"}
Dim intCounter = 0
For Each btnName As String In btnArray
Dim saveButtonField As PushbuttonField = stamper.AcroFields.GetNewPushbuttonFromField(btnName)
Dim pff As PdfFormField = saveButtonField.Field
Dim myAction As PdfAction = PdfAction.CreateSubmitForm("../../Common/Services/StaticFormSubmissionHandler.ashx?submissionType=" & intCounter & "&formID=" & intFormID & "&resultID=" & intResultID & "&templateName=" & Server.UrlEncode(sShortName), Nothing, PdfAction.SUBMIT_EXCL_F_KEY)
myAction.Next(New PdfAction(hostUrl & "/Main/Pages/FormSub.htm"))
'pff.SetAdditionalActions(PdfName.U, myAction)
pff.Action = myAction
stamper.AcroFields.ReplacePushbuttonField(btnName, pff)
intCounter += 1
Next
End Using
End Using
The save/process submission works, but the redirect only works in Chrome. In IE, it makes the iframe blank. I'm not seeing any errors or anything. Also, in IE, the PDF has the menu bars displayed. I understand if this is strictly related to the Reader in the IE browser, and I have no control over it. But it would be nice if the stamper.ViewerPreferences = PdfWriter.HideToolbar would work.
Thank you so much in advance for any advice/input.
Related
I'm using itextsharp in vb.net to stamp some backgrounds (ie other single page pdfs) onto another pdf.
So I have a 5 page 'blank' pdf, on which I stamp page 1 with the first page from a file called page1.pdf, then I put the first page of page2.pdf as a background to page 2 etc etc.
It's worked fine so far, but I've come across a problem with stamping a particular pdf onto my 'blank' - the issue appears to be with a file I'll call 'page4.pdf' and it seems likely it's because page4.pdf has been designed as a fillable form.
When I stamp page4 on and open up the blank file in Adobe reader, I get the message:
There was an error processing a page. There was a problem reading this document (18)
Could anyone suggest a way I can stamp a pdf with a form pdf as the source without this issue?
Thanks!
Here's an extract from the code I'm using for stamping (it does other stuff and involves a loop to go through the pages of the pdf, but I've just put the actual stamp bit below to keep things simple):
Dim background As PdfContentByte
Dim page As PdfImportedPage = Nothing
Dim reader As PdfReader = New PdfReader(sourcepdf)
Dim stamper As New itextsharp.text.pdf.PdfStamper(reader, New System.IO.FileStream(outputpdf, System.IO.FileMode.Create))
Dim s_reader As New PdfReader(backfile)
page = stamper.GetImportedPage(s_reader, 1)
background.AddTemplate(page, 0, 0)
stamper.Close()
reader.Close()
s_reader.Close()
Here's the code I've been trying out to convert the form pdf to a 'non-form', which I was hoping would eliminate the stamping problem (no success so far):
Dim pdfReader As PdfReader = New PdfReader(inputpdf)
Dim pdfStamper As itextsharp.text.pdf.PdfStamper = New PdfStamper(pdfReader, New FileStream(outputpdf, FileMode.Create))
pdfStamper.AnnotationFlattening = True
pdfStamper.FreeTextFlattening = True
pdfStamper.FormFlattening = True
pdfStamper.Close()
Issue was nothing to do with the pdf being a form, but a problem that seems to crop up when I repeatedly stamp the same output file. Solution is just to explicity delete the former output file before re-running process.
I found this code from here:
Show Pictures Directly From URLs in Access Forms and Reports
I have this code on my form:
Public Function PeopleImage()
'FUNCTION TO SET THE IMGPEOPLEPIC WITH THE CURRENT PERSON'S PHOTO, IF THE PHOTO CAN BE FOUND. OTHERWISE A STOCK IMAGE SAYING NO PIC FOUND WILL SHOW
Dim PeoplePic As String
Dim NoPeoplePic As String
Dim ImgDwnld As String
PeoplePic = "https://TheWebAddress.com/People%20Photos/" & Nz(Me.PersonID, 0) & ".jpg"
ImgDwnld = "\\AppServer\AppDir\Storage\Images\" & Nz(Me.PersonID, 0) & ".jpg"
DownloadFile PeoplePic, ImgDwnld, False
NoPeoplePic = "\\AppServer\AppDir\Storage\NoPeoplePic.jpg"
If FileExists(ImgDwnld) Then
Me.ImgPeoplePic.Picture = ImgDwnld
Else
Me.ImgPeoplePic.Picture = NoPeoplePic
End If
Me.ImgPeoplePic.Requery
End Function
When I added the image control ImgPeoplePic I set it to the NoPeoplePic, so that a picture would be set. Now, when I load the form and navigate, it looks like the photo is reloading (it blinks), but it just keeps showing the stock NoPeoplePic.
Anyone know what I'm missing? I feel like I'm so close. I just don't understand what it's not putting in the image that it downloads. I verified that the image downloads to the directory, and that it is there. I debug the path and image and it pulls up with no problem.
Any help is appreciated!
I have a PDF which is created by scanning software. One image per page and hidden OCR'ed text.
I want to remove the images and make the text visible.
I found info how to remove images (replace by another image) but found no way for making the invisible text visible.
Sample PDF with image and hidden text
I tried below method, but it does not work:
Public Shared Sub UnhideText(ByVal strFileName As String)
Dim pdf As iTextSharp.text.pdf.PdfReader = New iTextSharp.text.pdf.PdfReader(strFileName)
Dim stp As iTextSharp.text.pdf.PdfStamper = New iTextSharp.text.pdf.PdfStamper(pdf, New IO.FileStream("e:\out.pdf", IO.FileMode.Create))
'This does not work, text remains unvisible. I guess SetTextRenderingMode applies only for new added text.
For pageNumber As Integer = 1 To pdf.NumberOfPages
Dim cb As iTextSharp.text.pdf.PdfContentByte = stp.GetOverContent(pageNumber)
cb.SetTextRenderingMode(iTextSharp.text.pdf.PdfContentByte.TEXT_RENDER_MODE_FILL)
Next
stp.Close()
End Sub
I used the code below to successfully get a copy of each picture inside a page loaded using webbrowser control.
Dim doc As IHTMLDocument2 = DirectCast(wb.Document.DomDocument, IHTMLDocument2)
Dim imgRange As IHTMLControlRange = DirectCast(DirectCast(doc.body, HTMLBody).createControlRange(), IHTMLControlRange)
For Each img As IHTMLImgElement In doc.images
imgRange.add(DirectCast(img, IHTMLControlElement))
imgRange.execCommand("Copy", False, Nothing)
Using bmp As Bitmap = DirectCast( _
Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
bmp.Save(img.nameProp)
End Using
Next
I got the code from here: Copy an image from cache of web browser control present in VB.NET
However, the picture I am interested in is inside an iFrame.
I tried changing:
Dim doc As IHTMLDocument2 = DirectCast(wb.Document.DomDocument, IHTMLDocument2)
to
Dim doc As IHTMLDocument2 = DirectCast(wb.Document.Window.Frames(iFrameID).Document.DomDocument, IHTMLDocument2)
but I am getting an "Access Denied" Error. I guess (not sure) its because the iframe's src is on a different domain.
Is there a way around this problem?
Thanks!
If the iFrame has content from a domain that is different from the the parent then your out of luck. You could attempt a different solution and retrieve the iFrame's page via Ajax and parse out the image src. Another alternative is to do it server side with a program like PhantomJs (or VB, PHP etc) to retrieve the page and parse it for images to retrieve.
I need to get some text from this web page. I want to use the trade feed for my program to analyse the sentiment of the markets.
I used the browser control and the get element command but its not working. The problem is that whenever my browser starts to open the page I get Java scripts errors.
I tried with DOM but seems that i dont quite understand what i need to do :)
Here is the code:
Dim code As String
Using client As New WebClient
code = client.DownloadString("http://openbook.etoro.com/ahanit/#/profile/Trades/")
End Using
Dim htmlDocument As IHTMLDocument2 = New HTMLDocument(code)
htmlDocument.write(htmlDocument)
Dim allElements As IHTMLElementCollection = htmlDocument.body.all
Dim allid As IHTMLElementCollection = allElements.tags("id")
Dim element As IHTMLElement
For Each element In allid
element.title = element.innerText
MsgBox(element.innerText)
Next
Update: So I tried the HTML Agility pack, as suggested in the comments, and I am stuck again on this code
Dim plain As String = String.Empty
Dim htmldoc As New HtmlAgilityPack.HtmlDocument
htmldoc.LoadHtml("http://openbook.etoro.com/ahanit/#/profile/Trades/")
Dim goodnods As HtmlAgilityPack.HtmlNodeCollection = htmldoc.DocumentNode.SelectNodes("THE PROBLEM")
For Each node In goodnods
TextBox1.Text = htmldoc.DocumentNode.InnerText
Next
Any advice what to now?
Ok I think I know what the problem is somehow the div that I need is hidden and its not loaded when I load the web page just the source code. Does someone knows how to load all the hidden divs ??
Here is my new code
Dim doc As New HtmlAgilityPack.HtmlDocument
Dim web As New HtmlWeb
doc = web.Load("http://openbook.etoro.com/ahanit/#/profile/Trades/")
Dim nodes As HtmlNode = doc.GetElementbyId("feed-items")
Dim id As String = nodes.WriteTo()
TextBox1.Text = TextBox1.Text & vbCrLf & id
user1336635,
Welcome to SO! Something you might try is to check out his source code, figure out what javascript function is populating the field you want (using firebug - I assume it's the one that "trades result in profit" next to it), and then embedding that script into a web page that your webbrowser control loads. That's where I'd try to start. I checked his source code and searched for "trades result in profit" and didn't find anything which leads me to believe hunting for the element 'might' not be possible. Just a starting place until someone with more experience with this chimes in!! Best!
-sf