How to display page count in PDF? - asp.net

I am using ItextSharp for creating Pdf in my application. When the user clicks on submit the pdf is generated on the fly and displayed to the user. The problem am facing is i am not able to display the total number of pages present in that pdf. I am using an image as a header and in footer am again using an image, i don't want to display the page number in the footer as per the requirements. So i want something like
"Total number of pages: 2(pages present in that pdf)" on the first page of my pdf. Is it possible to do this Please suggest something.

//Create instance for the PDF reader
PdfReader pdf_fie = new PdfReader(filepath);
//read it's pagecount
page_count = pdf_fie.NumberOfPages;
//close the file
pdf_fie.Close();

Related

How can I get the current PDF page number from PDF.js iframe?

I have the viewer hosted on my local webserver and the iframe points and loads up the pdf.
I then want to press a button that 'logs' the page number to a text file, I read this question and answer which seems to suggest you can use pdf.getPage to get the page number but how can i access the PDFJS when it is being ran from inside of the iframe?
Thank you.
var iFrame = document.getElementById('iframe_id');
if ( iFrame.contentDocument ) {
currentPageNum= iFrame.contentDocument.getElementById('pageNumber').value;
}
alert(currentPageNum);
This will access the iframe and then search the viewer.html for the element that contains the page number. All that needs doing now is to pass currentPageNum to the form.

How to display image on web page from ADODB.stream object

I have a classic ASP question.
I need to show images on my webpage.
But because of the security reasons, I cannot simply put all of my images in the images folder in my website folder and I need to put them outside of my website folder.
For example, my website folder is located at:
C:\Inetpub\wwwroot\mysite\
But I need to put images (which I want to show on my web pages) at:
C:\images\
I am trying to use ADODB.stream object to pull the images using ASP vb codes as shown below:
<%
Response.ContentType = "image/png"
Set adoStream = Server.CreateObject("ADODB.Stream")
adoStream.Open
adoStream.Type = 1
FPath = "C:\images\1.png"
adoStream.LoadFromFile FPath
Response.BinaryWrite adoStream.Read
adoStream.Close
Set adoStream = Nothing
Response.End
%>
When I launch this webpage in internet explorer, the page shows a File Download window/message for downloading the image file "1.png" rather than displaying the image on the web page.
How can I fix the code to show the image on the web page rather than downloading the image file?
That code generates an image as the return type, so there is no HTML to display the image in, if you call this code directly say it's called image.asp then you would do something like this in another page that displays HTML.
<img src="image.asp" />
This will call your code and output the raw image binary as this is what <img> expects.
If you want to produce multiple images from the file system or database pass a querystring parameter such as ?id=yourimage and change the code to select different images.

Generate aspx file one below other in single tab

I am having a complete .aspx page which contains header,footer,textbox,tables everything. Values in these all fields come from code behind .cs file.
For different set of data i am generating different .aspx page file in tabs. so that user can take print of it.
i was doing something like this to open multiple tabs
var cust_prop_id = $('#hdnPropertyNo').val().split(',');
$.each(cust_prop_id, function (i, val) {
var myWindow = window.open('UserInfo.aspx?Prop=' + val, '', '');
});
PROBLEM STATEMENT
All of sudden client said user wont go to each tab and say "Print".
Instead generated all .aspx one below other in single tab. so in one single click user can able to print all generated .aspx files
What is the best way to do it ?
You can do this in many ways here are two most common.
Method 1
Create user control for each page which you want to be printed add these user controls in the page where user will print the page.Load each user control in separate div and place in main container and at print time get css of container div which will include all data.
Method 2
Create div for each page which you were opening in new window in a main container .Load data in divs at page load and when user press Print get all html of container div and print it.

Where to store and how to remove images in a photo gallery?

I'm working on a photo gallery using ASP.NET.
I'm storing user's images in a SQL database. I'm not sure how should displaying images look like.
Let's say there is 1 picture per user, I was doing something like that:
get image from database
save it on server's disc as "file.jpg"
ASP:Image.uri = "file.jpg"
And that worked fine until I found out that If few users loads that page at the very same time, It might not work properly.
Then I though changing "file.jpg" into some random string would help me:
get image from database
save it on server's disc as "ABCDUDHDJSAKFHADGJKHAKADFAD.jpg"
ASP:Image.uri = "ABCDUDHDJSAKFHADGJKHAKADFAD.jpg"
File.Delete("~/ABCDUDHDJSAKFHADGJKHAKADFAD.jpg");
But it wasnt possible to delete this file because it was still being used by a server.
What would be the proper way to solve my problem? User in my photo gallery will eventually see 12 photos at the same time.
What I usually do is serve images from DB using a generic handler (ashx file).
What you need to do is create an ashx file and use something along these lines:
context.Response.BinaryWrite(myImage);
The you create image tags like so:
<img alt="whatever" src="/images.ashx?iid=1243 />
Where iid is the unique ID of the image in the DB.
You should not store the file actually. You should stream it. For example create a handler which returns the image from an user with a specific ID and call it with that ID like image.ashx?id=1. The handler could look like
Image image; // image from your database
using(MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.WriteTo(context.Response.OutputStream);
}
Response.ContentType = "image/png";
This handler you can use like a static image file. So, you might even use it for a background-image:
<div style="background-image: url(image.ashx?id=1)">Username</div>

CSS not rendering while converting webpage to pdf using convertApi.dll

I have a large pdf with 50-60 pages. I converted the pdf into web page using ConvertApi web2pdf online api and then added some input controls on the pages wherever required.
When user filled all the required input and submit I rendered the entire page and show the preview to the user to check whether user has filled the inputs correctly. After that when User click submit then it stores into database and generate the pdf of the same.
I used convertApi.dll and wrote the following code to generate pdf.
MemoryStream outStream = new MemoryStream();
Web2Pdf convertApi;
convertApi = new Web2Pdf();
convertApi.SetPageSize("A4");
convertApi.SetPageWidth("100%");
convertApi.ConvertHtml(HtmlText, outStream);
My problem is when we generate the pdf it does not take all the styles used in the page. And pdf look different. I need to provide the very similar pdf which client provided. Can any one suggest me to do it in better way.
Thanks.

Resources