CSharp: Spire.PdfViewer

发布时间 2024-01-11 08:50:51作者: ®Geovin Du Dream Park™

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SpirePDFdemo.aspx.cs" Inherits="WebAppPdfDemo.SpirePDFdemo" %>
<%@ Register Assembly="Spire.PdfViewer.Asp" Namespace="Spire.PdfViewer.Asp" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pdf view</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <cc1:PdfViewer ID="PdfViewer1" runat="server" Width="800px" Height="800px">
        </cc1:PdfViewer>
    </div>
    </form>
</body>
</html>

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using Spire.Pdf;
using Spire.PdfViewer;
using Spire.Pdf.Graphics;
using Spire.Pdf.Tables;
using Spire.Pdf.Fields;
using Spire.Pdf.Annotations;
using Spire.Pdf.Actions;
using Spire.Pdf.General;
using Spire.Pdf.General.Find;
using Spire.PdfViewer.Asp;

namespace WebAppPdfDemo
{

    /// <summary>
    /// 
    /// </summary>
    public partial class SpirePDFdemo : System.Web.UI.Page
    {


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Creates a pdf document
                PdfDocument doc = new PdfDocument();

                // Creates a new page
                PdfPageBase page = doc.Pages.Add();

                string s1 = "Spire.PDF for .NET is a professional PDF component applied to creating, writing, "
                            + "editing, handling and reading PDF files without any external dependencies within "
                            + ".NET application. Using this .NET PDF library, you can implement rich capabilities "
                            + "to create PDF files from scratch or process existing PDF documents entirely through "
                            + "C#/VB.NET without installing Adobe Acrobat.";
                string s2 = "Many rich features can be supported by the .NET PDF API, such as security setting "
                            + "(including digital signature), PDF text/attachment/image extract, PDF merge/split "
                            + ", metadata update, section, graph/image drawing and inserting, table creation and "
                            + "processing, and importing data etc.Besides, Spire.PDF for .NET can be applied to easily "
                            + "converting Text, Image and HTML to PDF with C#/VB.NET in high quality.";

                // Get width and height of page
                float pageWidth = page.GetClientSize().Width;
                float pageHeight = page.GetClientSize().Height;

                PdfBrush brush = PdfBrushes.Black;
                PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 12f);
                PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Justify);

                // Draw text
                page.Canvas.DrawString(s1, font, brush, new RectangleF(0, 20, pageWidth / 2 - 8f, pageHeight), format);
                page.Canvas.DrawString(s2, font, brush, new RectangleF(pageWidth / 2 + 8f, 20, pageWidth / 2 - 8f, pageHeight), format);


                String result = Server.MapPath("CreateTwoColumnPDF_out.pdf");

                //Save the document
                doc.SaveToFile(result);
                //Launch the Pdf file
               // PDFDocumentViewer(result);

                //The interval time of emptying caching, specified in seconds.
                this.PdfViewer1.CacheInterval = 1200;
                //Caching time of an image, specified in seconds.
                this.PdfViewer1.CacheTime = 1000;
                //Cache the number of Image, default is 1000.
                this.PdfViewer1.CacheNumberImage = 1000;
                //The time of responding to event after stopping scrolling, specified in milliseconds.default is 500.
                this.PdfViewer1.ScrollInterval = 300;
                //Zoom value of page, default is 1.
                this.PdfViewer1.ZoomFactor = 1;
                this.PdfViewer1.CustomErrorMessages = "Error information";
                this.PdfViewer1.LoadFromFile("CreateTwoColumnPDF_out.pdf");

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        private void PDFDocumentViewer(string fileName)
        {
            try
            {
                System.Diagnostics.Process.Start(fileName);
            }
            catch { }
        }
    }
}