Você está na página 1de 7

13/10/2015

Asp.netOpenPDFFileinWebBrowserusingC#,VB.NETASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQLServer,Ajax,SSRS,XMLe

.NET News Weekly


Everything .NET, delivered straight to your inbox, every week.

ASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQL
HOME
ASP.NET
AJAX
GRIDVIEW
JAVASCRIPT
SQL
JQUERY
OOPS CONCEPTS
INTERVIEW QUESTIONS
Server,Ajax,SSRS,XMLexamples
aspdotnetsureshoffersC#.netarticlesand
tutorials,csharpdotnet,asp.netarticlesand
tutorials,VB.NETArticles,Gridviewarticles,code
Asp.netOpenPDFFileinWebBrowserusingC#,VB.NET
examplesofasp.net2.0/3.5,AJAX,SQLServer
Articles,examplesof.nettechnologies
By:SureshDasariNov5,2012

TRACE MOBILE

FORUMS

SearchThisSite

Categories:Asp.net,C#.Net,General,VB.NET

Introduction
HereIwillexplainhowtoopenpdffileinwebbrowserinasp.netusingC#andVB.NET.
Description
InpreviousarticlesIexplainedExportGridviewdatatoPDF,Sendvaluesfromonepagetoanotherpage

CONTACT

Search

.NET News Weekly


Everything .NET, delivered
straight to your inbox, every
week.

usingQueryString,JoinsinSQLServer,HighlightGridviewrecordsbasedonsearchandmanyarticles
relatingtoGridview,SQL,jQuery,asp.net,C#,VB.NET.NowIwillexplainhowtoopenpdffileinweb
browserinasp.netusingC#andVB.NET.
Toimplementthisconceptfirstcreateonenewwebsiteandaddoneofyourexistingpdffiletoyour
websiteafterthatopenDefault.aspxpageandwritethefollowingcode

<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>OpenPDFFileinWebBrowserinasp.net</title>
</head>
<body>
<formid="form1"runat="server">
<div>
<asp:ButtonID="btnOpen"Text="1stWaytoShowPDFInBrowser"FontBold="true"runat="server"
onclick="btnOpen_Click"/>
<asp:ButtonID="btnpdf"Text="2ndWaytoShowPDFInBrowser"FontBold="true"runat="server"
onclick="btnpdf_Click"/>
</div>
</form>
</body>
</html>
NowopenDefault.aspxcodebehindfileandaddfollowingnamespaces

FollowUS

C#Code

usingSystem
usingSystem.Net
Oncenamespacesaddedwritethefollowingcode

protectedvoidPage_Load(objectsender,EventArgse)
{
}
//FirstWaytoshowPDFinbrowser
protectedvoidbtnOpen_Click(objectsender,EventArgse)
{
Response.Redirect("SiteAnalytics.pdf")
}
//SecondwaytoShowPDFinbrowserbysettingContentTypeoftheResponseobjectandaddthe
binaryformofthepdfintheheader
protectedvoidbtnpdf_Click(objectsender,EventArgse)
{
stringpath=Server.MapPath("SiteAnalytics.pdf")
WebClientclient=newWebClient()
Byte[]buffer=client.DownloadData(path)

Seleccionaridioma
Conlatecnologade

Traductor

if(buffer!=null)
{
Response.ContentType="application/pdf"

http://www.aspdotnetsuresh.com/2012/11/aspnetopenpdffileinwebbrowser.html

1/7

13/10/2015

Asp.netOpenPDFFileinWebBrowserusingC#,VB.NETASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQLServer,Ajax,SSRS,XMLe
Response.AddHeader("contentlength",buffer.Length.ToString())
Response.BinaryWrite(buffer)
}
}

.NET News Weekly


Everything .NET, delivered
straight to your inbox, every
week.

VB.NETCode

Tweet

ImportsSystem.Net
PartialClassVBCodetoShowPDF
InheritsSystem.Web.UI.Page
ProtectedSubPage_Load(senderAsObject,eAsEventArgs)
EndSub
'FirstWaytoshowPDFinbrowser
ProtectedSubbtnOpen_Click(senderAsObject,eAsEventArgs)
Response.Redirect("SiteAnalytics.pdf")
EndSub
'SecondwaytoShowPDFinbrowserbysettingContentTypeoftheResponseobjectandaddthe
binaryformofthepdfintheheader
ProtectedSubbtnpdf_Click(senderAsObject,eAsEventArgs)
DimpathAsString=Server.MapPath("SiteAnalytics.pdf")
DimclientAsNewWebClient()
DimbufferAs[Byte]()=client.DownloadData(path)

RecentPosts

GetLatestarticlesinyourinboxfor
free.
Enteryouremailaddress:

IfbufferIsNotNothingThen
Response.ContentType="application/pdf"
Response.AddHeader("contentlength",buffer.Length.ToString())
Response.BinaryWrite(buffer)
EndIf
EndSub
EndClass

Subscribe

Demo

Aspdotnet-Suresh
Seguir

+1

+ 2,045

DownloadSampleCodeAttached

Ifyouenjoyedthispost,pleasesupporttheblogbelow.It'sFREE!
GetthelatestAsp.net,C#.net,VB.NET,jQuery,Plugins&CodeSnippetsforFREEbysubscribingtoour
Facebook,Twitter,RSSfeed,orbyemail.

Tags

Asp.netJQuery GeneralC#.NetVB.NET

SubscribebyRSS

SubscribebyEmail

Code Snippets Javascript SQL Server


Gridview JQuery Plugins Errors Interview
QuestionsFileuploadAjaxc#DropdownListAngularJS
Google API validations JSON Google MAPS
DatePicker CSS IISServer Windows Application
Authentication AutoComplete CheckBox
Reports Modalpopup ExcelSheet OOPS Concepts
SharePoint HTML Membership ExportGridviewData
jQuery UI XML SendMail UpdatePanel WebService
AjaxModalPopupExtender Google Maps API InternetTips

http://www.aspdotnetsuresh.com/2012/11/aspnetopenpdffileinwebbrowser.html

2/7

13/10/2015

Asp.netOpenPDFFileinWebBrowserusingC#,VB.NETASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQLServer,Ajax,SSRS,XMLe
SlideShowWCFMenuProgressbarSQLJoinsToolTip

.NET News Weekly

mvc

DataGridview

YouTube

web.config

DataList

LightBoxEffect RDLC Report RadioButtonList Visual Studio

Everything .NET, delivered


straight to your inbox, every
week.

loading Image mvc Charts CheckBoxList Cookie Dynamic


Controls EncryptionandDecryption Facebook Global.asax
RepeaterSSRSTwitterWindowsServicejQueryMenu
DatabaseAccordionMenuAjaxAsyncFileUploadBlogStatistics
FixedHeaderonScrollIEnumerableListBoxPolymorphism
Grid ZIP UNZIP Files jQuery Cookie setInterval setTimeOut
360

Degree

View

Plugins

ASP

AjaxAutoCompleteExtender

AjaxTabContainerAverageratingBootstrapCaptchaFlipEffect
KeyBoardKeyCodesLinkedinMultilineTextbox News Ticker in jQuery
PDF Viewers Product Reviews Resize Image Reviews
Constraints SQL Server 2008 R2 Session Timeout SiteMap
Media Bookmark Plugins ThumbnailsGeneration UserName Check
Visitors Count app.config jQuery Media Plugins 3TierArchitecture
ADO.NET

AbstractVsInterface

ActiveDirectory

Advertise

Calendarextender Ajax ConfirmbuttonExtender AjaxAccordionControl


AjaxCalendarExtender

Youmightenjoyreading:

AjaxDragPanelExtender

AjaxCollapsiblePanelControl
AjaxPasswordStrength

AjaxRatingControl

AjaxSlideshowExtender Arraylist Assembly Authorization


Plugins CodingStandards Custom Right Click Menu Dapper
Error Log Forums Generic List Google Charts Gzip Compression
Address ImportContacts LINQ MCC Award MVP Award Northwind
Database Panorama Image Viewer Plugins Print DIV
QueryString RSSFeeds Read/Write text file ReadOnlyValues

BindSpecific
Columnsof
Datatableto
Datagridviewin
C#Windows...

AutoSuggestion
SearchTextBox
inMasterPage
usingJQuery&
WebServices...

ExportGridview
Datawith
ImagestoWord,
ExcelinAsp.net
using...

Asp.netMVC
ExampleDemo
Project
Applicationwith
Database
Download...

CreateRead
(Get)Cookie
valuesinAsp.net
usingC#,
VB.NETwith...

RichTextBox Scrollbar Setup File Spell Checker Testimonial Example


Testing Trace Mobile Number Try Catch VBScript Virtual Keyboard
WPF Windows 8 bulk copy contactus delegates dynamically page
creategeneratescriptjQueryAudioPluginsjQueryMobilejQuery Video
PluginsjqGridviewscrolltop/bottomofdivsliderfromfoldersorting
view

C#Convert
String(Text)to
TitleCase(Proper
Case),Upper
Case,...

InsertUpdate
Deletein
Gridviewin
Asp.netusing
Stored
Procedure...

GenerateOne
TimePassword
(OTP)inAsp.net
usingC#,
VB.NET...

C#e:Isa
PhysicalPathbut
aVirtualPath
wasExpectedin
Asp.net...

jQuerySend
ReceiveJSON
Objectsfrom
Web...

ENGAGEYA

36comments:
MehtabAlisaid...

nicepost..ButifUserdonotwanttolettheotherstodownload/Save.pdffilefromWebsite..then...Howisit
possible??

November5,2012at1:17AM
ramesh.jsaid...

howtoredirecttositeAnalytics.pdf..wherewehavetocreatepdffile....

November20,2012at12:40AM
Anonymoussaid...

Thanks....buthowicanadtextinpdffilesusingitextsharp.dlll

November25,2012at1:38AM
Anonymoussaid...

HowtodisabletoRightClickandprintoptionandcontrolinkeyonpdffiles

December28,2012at6:30AM

http://www.aspdotnetsuresh.com/2012/11/aspnetopenpdffileinwebbrowser.html

3/7

13/10/2015

Asp.netOpenPDFFileinWebBrowserusingC#,VB.NETASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQLServer,Ajax,SSRS,XMLe
Anonymoussaid...

inthispage"facebook"popsupwhileonmouseovereffectandthengetsactualpositionaftermouseover
completes.Iwanttoaddsucheffectinmywebsitebutcannotidentifywhichcontrolisusedoverthere.plzletme
knowaboutitwithdetail.

January3,2013at12:20AM
Anonymoussaid...

Howtodisablerightclickinpdffileonbrowser

January4,2013at8:27AM
Anonymoussaid...

Howtodisplayxmlfileinbrowserusingasp.netwithc#..

April3,2013at3:05AM
Anonymoussaid...

sirhowcanigetdatafromwebsitebutthissiteisopenonlyinInternetexplorerandthenhowcaniautomatically
openinternetandgotologginusingsendingcredentialafterthatiwanttoextractdataandstoreintodatabase
usingC#lang.pleasehelpmetiachive

April26,2013at2:46AM
pratibhasaid...

Thiscommenthasbeenremovedbytheauthor.
May2,2013at4:59AM
pratibhasaid...

10

Sir,idontwanttodownloadpdf/docfiles,onlywanttoviewthefilesonclickeventofabuttonthatisstoredinsql
server2008inbytes..typeofcolumninsqlserverisvarbinary(max)....plshelp...

May2,2013at5:07AM
sakthidevelopersaid...

11

itsveryusefultomesir.howtodiablethesaveoptioninthepdffile.
thanksinadvance.

May5,2013at8:23PM
AnkitBansalsaid...

12

hi,itsworkingonlyforpdffile,butwantstoshowallfileaslikedoc,xlsfile.

June13,2013at3:00AM
NeerajKumarsaid...

13

Hello,Canbesetfilenamewhilesavingthefileusingsavebutton.

June13,2013at5:37AM
Anonymoussaid...

14

howtoshowmultiplepdffilesinaslider

June30,2013at4:35AM
Anonymoussaid...

15

ihvmultiplepdffilesinafoldernineedtoshowallfilesinasliderwithpaging(inC#).Plsgivemesome
suggestion...Parth:)

June30,2013at4:45AM
ashwanisaxenasaid...

http://www.aspdotnetsuresh.com/2012/11/aspnetopenpdffileinwebbrowser.html

16
4/7

13/10/2015

Asp.netOpenPDFFileinWebBrowserusingC#,VB.NETASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQLServer,Ajax,SSRS,XMLe
postisgoodbutihaveaproblemthatiwanttoopendocxfileonthebrowserwithoutusingmicrosoftinterlop
library.plzhelpme

July1,2013at10:04PM
Anonymoussaid...

17

HowcanIgetthepagenumberwhichi'mreadingfromthebroweser?Ineedthecurrentpagetobeextractedasa
separatepdffile

July21,2013at9:19PM
ArielPlonsaid...

18

Thxforthecodeandsample.HowwouldyoushowthePDFinsideajquerydialogbox?IhavethePDFbytes()butI
don'tknowhotoshowtheminsideaDIV/dialogbox.
Thanks

July23,2013at1:50PM
vinaysaid...

19

ihavescanned10personalfilesofemployees.basedontheidnumberthepdflinkshouldopen.thnxplzgive
suggestions

July25,2013at9:42AM
HiteshKumarsaid...

20

iwanttodisplaythatdatainpdffilethatareshowinrepeaterusingasp.netwithC#.whatisthemethodforthis.

August16,2013at11:12PM
Niteshyadavsaid...

21

helloIammakingaprojectwhichinvolvestheadmintoviewpdffilesfromthedatabaseandapprovethem...hencei
amaskinghowcanicodesothatthefileswouldbedisplayedinagridviewandadmincanviewthem
NOTEthefilesshouldbepresentinthedatabaseandnotliketheabovementionedcode.

September21,2013at3:21AM
Niteshyadavsaid...

22

helloIammakingaprojectwhichinvolvestheadmintoviewpdffilesfromthedatabaseandapprovethem...hencei
amaskinghowcanicodesothatthefileswouldbedisplayedinagridviewandadmincanviewthem
NOTEthefilesshouldbepresentinthedatabaseandnotliketheabovementionedcode.

September21,2013at3:22AM
Anonymoussaid...

23

howtoopenanyformatfile?

September23,2013at12:37AM
VishalSutharsaid...

24

howtoopentwofilesthisway?iamgettingerrorinopenedfilewhileiamtryingtoopentwofileatthesametime

September26,2013at10:36PM
Vinodbsaid...

25

HowtoPrintPDFFileusingWindow.Print("D:/a.pdf")?

September30,2013at5:59AM
sasikalasaid...

26

Isthereanypossibilitytogetthecurrentlyviewingpdffilepagenumberinatextbox.Whichmeanswheniam
viewing4thpageofpdffile,mytextboxshouldshow4.afterscrollingtonextpage,textboxshdshow5..likewisei
wanted.kindlyhelpme..weareinurgenttogetthiscode.wefoundpossibilityofinsertingtext,linkandalloptions
inpdf..Wecanalsoabletofindtotalpagescount.Buthowtocapturecurrentpdfpagenumberintextbox.

http://www.aspdotnetsuresh.com/2012/11/aspnetopenpdffileinwebbrowser.html

5/7

13/10/2015

Asp.netOpenPDFFileinWebBrowserusingC#,VB.NETASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQLServer,Ajax,SSRS,XMLe
October6,2013at10:39PM
chandanasaid...

27

HowcanIgetthepagenumberwhichi'mreadingfromthebrowser?Ineedthecurrentpagenumbertobe
extractedinatextbox..kindlyhelpmewiththecode.

October7,2013at10:35PM
Anonymoussaid...

28

Thankyousureshsomuch,Urjustbrilliant

December9,2013at9:42PM
RahulGunjalsaid...

29

Greathelp.ButitrequiresPDFReadertobeinstalledonclientmachine.AlsoIdontwantanyonetodownloadmy
files.Isthereanypdfviewerwhichcanhelpmeinthis?
Thanksinadvance

January29,2014at8:14PM
ChandrikaVeedhisaid...

30

nicepost..ButifUserdonotwanttolettheotherstodownload/Save.pdffilefromWebsite..then...Howisit
possible??

March12,2014at12:29AM
NityanandYadavsaid...

31

itnotworkingwithupdatepanel

April1,2014at1:37AM
Anonymoussaid...

32

Butitsgivinganerrorwhensizeofpdffileislarge

May8,2014at11:31PM
Anonymoussaid...

33

Howtostayonsamepageandopenadialoguetosavepdffile.Pleasereplyfasturgent.
Thanksinadvance

August28,2014at5:56AM
Anonymoussaid...

34

Itisworkingnice.
ButIwantopenpdffileinpanelusingchoosefilecontrolinsteadofiframesrcinasp.netusingc#

March21,2015at4:26AM
Anonymoussaid...

35

hi,canyoupleasehelpme??I'mnewbieinASP.net.I'mtryingtoopenaPDFfileusingASP.netbutIcan'tseemto
workitout.IwanttoviewthePDFfilewhichisstoredin(1)localmachineand(2)webserver.Thanks.

April27,2015at2:15AM
Anonymoussaid...

36

Thankyousomuchforpostingthis!IwasusingthisonIIS6withoutissuebutwhenImovedtoIIS7.5webegan
seeingproblemswithInternetExplorerdisplayinggibberishwhentryingtoloadanotherPDFornavigatetoanother
page.FoundthatIneededtoaddthefollowingattheendoftheabovetocorrecttheproblem:
Response.End()

August3,2015at12:40PM

GiveyourValuableComments
http://www.aspdotnetsuresh.com/2012/11/aspnetopenpdffileinwebbrowser.html

6/7

13/10/2015

Asp.netOpenPDFFileinWebBrowserusingC#,VB.NETASP.NET,C#.NET,VB.NET,JQuery,JavaScript,Gridview,SQLServer,Ajax,SSRS,XMLe

Enteryourcomment...

Commentas:

Publish

GoogleAccount

Preview

NewerPost

Home

OlderPost

Subscribeto:PostComments(Atom)

OtherRelatedPosts
Interview Questions in ASP.NET,C#.NET,SQL Server,.NET Framework
Asp.net insert, Edit, update, delete data in gridview
3 tier architecture example in asp.net with C#
Introduction to Object Oriented Programming Concepts (OOPS) in C#.net
Simple login form example in asp.net Check Username and Password availability in database
Introduction to WCF WCF tutorial | WCF Tutorial Windows Communication Foundation | WCF Example |
WCF Sample code in asp.net 3.5 | Basic WCF Tutorial for Beginners
how to insert images into database and how to retrieve and bind images to gridview using asp.net (or)
save and retrieve images from database using asp.net
jQuery 360 Degrees Image Display Plugins Examples with Tutorial
Best Login Page Design in HTML, CSS with Source Code
Asp.net WebService or Creating and Consuming WebService in asp.net or Create and call webservice in
asp.net or how to create webservice and how to use webservice in asp.net

2015AspdotnetSuresh.com.AllRightsReserved.
ThecontentiscopyrightedtoSureshDasariandmaynotbereproducedonotherwebsiteswithoutpermissionfromtheowner.

http://www.aspdotnetsuresh.com/2012/11/aspnetopenpdffileinwebbrowser.html

7/7

Você também pode gostar