How to attach Javascript to a PDF Hyperlink - c#

I got a large PDF which is embedded in a homepage and should have interactive links with custom javascript commands in it.
This document should be maintained by some students and it is not possible for them to go through all sites and set the javascript commands after a change in the original word file. So i want them to set hyperlinks in word where an action should occur.
After that i want to remove the hyperlink with iTextSharp and set a javascript action instead.
I can communicate with HTML, find the hyperlinks correctly and can change their url like in this post
Also tried to add this to the annotation action:
string destination = AnnotationAction.GetAsString(PdfName.URI).ToString();
AnnotationAction.Put(PdfName.JAVASCRIPT, new PdfString("this.hostContainer.postMessage(\"" + destination + "\");"));
But it is always calling the original hyperlink before it is executing the javascript. So how can i remove the hyperlink without losing the formation of the text within the document?

You just need to change the value of /S to /JAVASCRIPT and set your JavaScript command as the value of /JS. Instead of changing things, however, I'm just going to blow away the original action and create a new one, just to make sure there isn't any baggage.
The first three line below are from my original answer that you linked to, after that I've killed off the URL changing code and substituted it with the JavaScript code. Since you understood my other post this should make sense hopefully, too.
//Make sure this annotation has an ACTION
if (AnnotationDictionary.Get(PdfName.A) == null)
continue;
//Remove our old action entry just so nothing weird hangs around
AnnotationDictionary.Remove(PdfName.A);
//Create a new action entry
var a = new PdfDictionary(PdfName.A);
//Set it as an action
a.Put(PdfName.TYPE, PdfName.ACTION);
//Set it as JavaScript
a.Put(PdfName.S, PdfName.JAVASCRIPT);
//Set the JavaScript
a.Put(PdfName.JS, new PdfString(#"app.alert('Hello World');"));
//Add it back to the annotation
AnnotationDictionary.Put(PdfName.A, a);

Related

Selenium - Uploading image to website when input doesn't exist in HTML code and web uses SelectFileDialog

I have this problem for I can't find proper solution.
On website trovo.live I'm trying to change profile picture with code. There is 'Upload' button, which opens SelectFileDialog.
When I try to find input tag via document.getElementsByTagName("input") it doesn't find any input for uploading. (So I can't just do element.SendKeys() )
It works this way, when I select file in dialog then website converts it into data URI and set it as < img > src + it sets style = transform: matrix(values params)
So I found this solution that I convert image into data URI and set it as src parameter like website do it. Also I calculate matrix parameter values.
My problem is when I click save button later, website for some reason resets my css style and then picture is saved with bad positioning
After I change src by code (transform style remains from previous avatar)
This is how it looks when I upload manualy by Upload button or I can matrix() values
But when I click save button, it saves like that first picture, like it wouldn't count in my matrix() values. Can I somehow get into save button function and force in my matrix() edit? Or simulate Upload function without pressing button?
Or how can I handle selectFileDialog in selenium when
No < input > in HTML code
I can't use System.Windows.Forms.SendKeys.SendWait(path);
I also tried to use AutoItX, but I had some problems with their library in my project and I would like to handle it without libraries if possible.
Thank you very much for all answers :)
**edit
I found .js file which contains code for it, but I have no idea how to call for example method for resizing on method image code for javascript

Asp.Net data attributes dynamically updated through JS/jQuery but the updated attributes aren't updated when retrieved through code behind

I have an aspx page of images that, when selected, a popup appears prompting for various information tid bits that I then store the information as data attributes on hidden labels through use of jQuery (i.e. data-id="####", data-difficulty="###", etc.). I acknowledge that this isn't the best way to do it necessarily but I've tried other things (see below) and nothing has worked yet.
I've been attempting, and to no avail, to retrieve the dynamically updated data attributes so the various items can be stored to my local ms sql database. The updating of the attributes works perfectly in that I can view the items being updated properly in Chrome's developer tools. Despite this when I try to pull the same attributes I can see as being updated I'm unable to retrieve the updated values in the code behind and keep getting back the initial values (generally an empty string "").
Here's the implementation on the aspx page:
<asp:Label runat="server" ID="lblSelection" data-id="" data-count="" data-price="" data-difficulty="" CssClass="selected-items" />
and here's the relevant method being called when the "Submit" button is clicked further down on the same page:
protected void SubmitClicked(object sender, EventArgs e)
{
var currentID = lblSelection.Attributes["data-id"];
var currentCount = lblSelection.Attributes["data-count"];
var currentPrice = lblSelection.Attributes["data-price"];
var currentDifficulty = lblSelection.Attributes["data-difficulty"];
if (currentID == null || currentID == "")
{
// stop and throw an informative message to the user
}else{
// keep processing ...
}
}
The trouble is that when I run the project in debug mode and inspect those elements (again, making sure that I can visually see that the attributes are actually updated in the developer tools) they're all the initial value of "". My only guess is that there's some kind of post back issue but I wouldn't think that would happen until my method had been called and fully processed. As a note, I'm populating the images onto the page and updating their attributes already through a sql call based on the id of the item:
<img runat="server" src="" data-id="12345" />
The initial loading all works perfectly so I can clearly set the properties from the code behind fine. For whatever reason though I am unable to pick up the updated attribute values in the code behind after the jQuery updates the label's attributes (following some clicking and whatnot). Any tips or thoughts on this would be greatly appreciated.
What you are trying to do cannot work because:
The value of custom attributes is not posted back to the server (as discussed here).
Even if you set the text of the Label in client code, it would also not be available in the Text property in code-behind. A Label is rendered as a span element in the page, and the content of that type of element is not posted back to the server.
A nice list of properties included in a postback is given in this topic: Which values browser collects as a postback data?
As suggested by mshsayem, you should use HiddenFields. If, for some reason, you want to do it differently, you could use hidden TextBoxes, set their value in client code, and retrieve it with the Text property in code-behind. In other words: HiddenFields are not the only controls for which a value set in client-code can be retrieved in code-behind (but they are the obvious choice if the control is not to be displayed).

GET different url based on inputbox

I'm using the asp.net MVC pattern of having a url like controller/action/id. This works fine if I navigate to the url directly.
I want to have a page where the user can input the id and click a button (or link) which takes them to the correct url.
Do I need to somehow make an action link that points to a varying url based on the input using some client-side script?
Or can I render a Form that GETs the correct url?
Is what I'm doing unusual; should I be doing something else? (Note that the number of ids is too long to list).
Assuming you have the default route set up, you can do the following client-side script (make sure it's in the same file though):
var yourLink = '#Url.Action("controller", "action")';
//should output controller/action/
Then assuming your button has the id myButton and the textbox has the id myTextBox, you can do this jQuery:
$("#myButton").click(function () {
location.href = yourLink + $("#myTextBox").val();
//should output controller/action/5 for example, although you might
//want to make sure they've put a "correct" value in here
});

How to copy all data from a HTML doc and save it to a string using C#

I need to create a data index of HTML pages provided to a service by essentially grabbing all text on them and putting them in a string to go into a storage system.
If this were GUI based, I would simply Ctrl+A on the HTML page, copy it, then go to Notepad and Ctrl+V. Simples. If I can do it via good old point n' click, then surely there must be a way to do it programmatically, but I'm struggling to find anything useful.
The HTML docs in question are being loaded for rendering currently using the System.Windows.Controls.WebBrowser class, so I wonder if its somehow possible to grab the data from there?
I'm going to keep hunting, but any pointers would be very appreciated.
Note: We don't want the HTML source code, and would also really rather not have to parse all the source code to get the text unless we absolutely have to.
If I understand your problem correctly, you will have to do a bit of work to get the data.
WebBrowser browser=new WebBrowser(); // This is what you have
HtmlDocument doc = browser.Document; // This gives you the browser contents
String content =
(((mshtml.HTMLDocumentClass)(doc.DomDocument)).documentElement).innerText;
That last line is the browser's view of the rendered content.
This looks like it might be quite helpful.

URL and Query management Asp.Net C#

Ok so while back I asked question Beginner ASP.net question handling url link
I wanted to handle case like this www.blah.com/blah.aspx?day=12&flow=true
I got my answer string r_flag = Request.QueryString["day"];
Then what I did is placed a code in Page_Load()
that basically takes these parameters and if they are not NULL, meaning that they were part of URL.
I filter results based on these parameters.
It works GREAT, happy times.... Except it does not work anymore once you try to go to the link using some other filter.
I have drop down box that allows you to select filters.
I have a button that once clicked should update these selections.
The problem is that Page_Load is called prior to Button_Clicked function and therefore I stay on the same page.
Any ideas how to handle this case.
Once again in case above was confusing.
So I can control behavior of my website by using URL, which I parse in Page_Load()
and using controls that are on the page.
If there is no query in URL it works great (controls) if there is it overrides controls.
Essentially I am trying to find a way how to ignore parsing of url when requests comes from clicking Generate button on the page.
Maybe you can put your querystring parsing code into IsPostBack control if Generate button is the control that only postbacks at your page.
if (!IsPostBack)
{
string r_flag = Request.QueryString["day"];
}
As an alternative way, at client side you can set a hidden field whenever user clicks the Generate button, then you can get it's value to determine if the user clicked the Generate button and then put your logic there.

Categories

Resources