Embed javascript to existing pdf - c#

Hi I am embedding pdf file to my asp.net page like this
<object data="..." type="application/pdf" width="300" height="200">
</object>
The problem is that I have created my own toolbar for pdf, and want to zoom in, out document from my toolbar image buttons. I know that I can embed JavaScript to pdf document, when creating document from scratch, but how can I embed javascript to pdf in html having only the path of that document?

Let the pdf document load. Once the document is ready, inject Javascript.
However, note that Javascript, as executed by your browser is different from the Javascript executed by the Adobe reader plugin for your browser. The structure is similar but the DOM is entirely different.
Further, Javascript in your HTML document cannot directly access/modify the DOM of the loaded pdf document. For that you will need to go through the reader plugin. I am sorry I have not used the reader plugin in this way. Take a look at the API:
http://www.adobe.com/devnet/acrobat/pdfs/js_api_reference.pdf
and also read this guide:
http://www.adobe.com/devnet/acrobat/pdfs/Acro6JSGuide.pdf
These may be your best bet because very few people on SO seem to be working on Javascript for pdf. I have asked a few questions before but never got any responses :(

Related

Convert asp.net page to Word document

I have built several PDF documents dynamically from ASP.NET pages (HTML/CSS) using plugins like Winnovative HtmlToPDFconverter. It has always been a successful outcome using the built-in functionality for those plugins, like merging existing PDF documents with dynamic content and adding pre-defined headers and footers, adding page margins, page numbers and so forth. The HTML content has overall been rendered as expected in the final PDF document(s).
Is there any way/any advice for a similar .NET plugin that can render HTML/CSHTML to a Microsoft Word document (.docx) in the same way – or is it too difficult to render native HTML5 and CSS into a desirable layout for a Microsoft Word Document?
I have googled around and found some suggestions, but I'm looking for recommendations for maybe a specific plugin – or a warning if it is a no-go
and too difficult to get the desired layout 1:1 from HTML to a Word document because of incompatibility between markups?
Devexpress HTML editor can export its HTML content to different formats including docx and rtf. Not sure about its limitations (e.g. script and canvas export, etc.), but in the common case it works well.

How to save html document as pdf?

I want to open a web page and save it as pdf. I have to use chrome's "Save as Pdf" feature. I dont want to use any paid library. Is it possible to open html page in chrome browser and save it as pdf programatically in c#.
No you can not save html page as a PDF without any third party DLL like iTextsharp and many others which are available in Free.
So, you can use this Free DLL for save as PDF.

Saving a Web Page with all its content in C#

I am trying to save a web page (just like we do in browsers) along with all its content and
formatting. I tried WebClient, WebRequest examples but they can only download the text part and sometimes javascript. But no css and images etc.
Is there any api for this in .Net, or any 3rd party api for .net?
It is possible, I think it because a lot applications are running for offline reading, and they show the saved pages with the same formatting and styling.
How it is done?
Any ideas ?
EDIT 1:
Web pages can be parsed and saved using HtmlAgilityPack. But is there any way to get the main article and other contents like ads, other external links separated. Is there any way to differentiate between the contents which are relevant and which are not?
(I am sorry, if this question is not clear).
Also can any one give some suggestion that how these offline reading applications (like read later/pocket etc) save a web page and format it.
Is there any way to do the same in C#?
You can download a Page text as Html, then parse it and get <link rel="stylesheet" type="text/css" href="..."> or <img src="..."/> elements and download link of attributes like href or src separately.
HtmlAgilityPack is a reliable and useful library for parsing Htmls.
You can use Wget
https://www.gnu.org/software/wget/manual/html_node/Recursive-Download.html#Recursive-Download
You could have a look at trying to save the page as an mht file.
These files bundles the web page and all of its references, into a single compact file (.mht)
Stackoverflow topic about mht via c#
Note: MHT was introduced by Microsoft. Not all browsers comply with this format. Opera is the only other popular browser that has the MHT save. Firefox users though can call upon two add-ons to handle this file standard, Mozilla Archive Format & UnMHT. Both these add-ons can be installed and used to open and save complete webpages.

Generate PDF in ASP.NET from Fully Rendered Page

Does anyone know of a component (open source or 3rd party) that would allow you to export a fully rendered HTML page to PDF in c#? We have a page that has its DOM modified with jquery but the methods we have tried (ABCpdf.NET, WebClient, etc) don't register any DOM changes from javascript in the PDF. We need to programmatically export that rendered HTML (post-jquery) to PDF on the fly.
ExpertPDF HtmlToPdf Converter v7.0
I was looking for something similar many months ago and as far as I can remember, it's not possible with any free third-party controls. There are paid ones available. The closest you can get is iTextSharp. It will allow you to export the contents of specific html tads and user controls but it's a bit of a pain to deal with
I'm never tried is but there's an open source solution called wkhtmltopdf that renders a PDF from HTML/JavaScript/CSS using the WebKit engine. This post talks a little bit about using it. If it works I'd like to know because I've heard this request a couple of times here.

Response.BinaryWrite DIV

Is there a way to write PDF to a div from DataBase i.e. Retrieve a Byte[] from Database and Reponse.BinaryWrite to a div.
We do similar thing for Images using src = "anotherpage.aspx" where image is written on anotherpage.
Is it possible with PDF without using IFrame?
If what you're trying to do is show a PDF file inside a DIV, you're going down the wrong path. You either need to:
Convert the PDF to Flash (ala Flash Paper)
or
Convert the PDF to HTML (like Scribd does using HTML 5).
Then you can embed the PDF inside a DIV. But no browser I know of supports directly embedding PDFs.
Otherwise you have to put the PDF in an IFRAME, but how this is shown is PDF plug-in dependent.
No. The reason it works with a src=otherpage.aspx request is that the src attribute results in the user's web browser making a completely separate request for the other resource. You're serving up an additional page to make that happen. Writing a PDF file directly is trying to inject the PDF into the same request as your page - not really "similar" to your img src at all. In fact, what is most similar to the "src=otherpage.aspx" method is the iframe approach that you mentioned.
As a side note, you our "AnotherPage.aspx" example should really be changed to "AnotherPage.ashx". Note the letter 'h' in there. That means you're using a handler rather than a page, which will perform better.

Categories