(How to add Watermark in PDF file using iTextSharp in ASP.NET) – Để bảo vệ bản quyền tác giả đối với các tài liệu khi xuất bản, tác giả thường sử dụng  chức năng Watermark. Khi chèn Watermark vào tài  liệu, dòng chữ này sẽ hiển thị mờ ở nền mà không ảnh hưởng đến nội dung. Bài viết dưới đây,chúng tôi sẽ giới thiệu với các bạn cách sử dụng thư viện iTextSharp để chèn Watermark vào file PDF. Chương trình sẽ cho phép người sử dụng 1 trong 2 cách đó là sử dụng ký tự (Text) hoặc sử dụng hình ảnh (Image) để chèn Watermark.


Nghe những bài hát đỉnh nhất về Thấy cô giáo - Nghe trên Youtube



Code Example C#, Code Example VB.NET
Code Example C#, Code Example VB.NET



B1: Tạo Project trong Microsoft Visual Studio 2010

B2: Download  thư viện iTextSharp tại đây

B3: References  itextsharp.dll trong thư mục vừa giải nén vào Project.

B4: Mở file Default.aspx dưới dạng HTML và  nhập mã HTML
C#
<%@ PageTitle="Using iTextSharp to Drawing shapes and Graphics in ASP.NET" Language="C#"MasterPageFile="~/Site.master"AutoEventWireup="true"CodeBehind="Default.aspx.cs"Inherits="AddWatermarkToPDFUsingiTextSharp._Default"%>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <table cellpadding="3"cellspacing="5"border="0"width="50%">
        <tr>
            <td>
                <divclass="panel panel-default">
                    <divclass="panel-heading">
                        <asp:label id="lblHeader" runat="server" Text="Using iTextSharp to Drawing shapes and Graphics in ASP.NET"></asp:label>
                    </div>
                    <divclass="panel-body">
                      
                    </div>
                    <divclass="modal-footer">
                        <div class="btn-group">
                            <asp:LinkButton id="cmdDrawing"runat="server"CssClass="btn btn-small" OnClick="cmdDrawing_Click" ValidationGroup="Text" Causesvalidation="true">
                                <i class="icon-insert"></i>&nbsp;&nbsp;<asp:label id="lblInsert" runat="server" Text="Drawing shapes and Graphics"></asp:label>
                            </asp:LinkButton>
                        </div>
                    </div>
                </div>
            </td>
        </tr>       
    </table>
</asp:Content>
VB.NET Code
<%@ PageTitle="Using iTextSharp to Drawing shapes and Graphics in ASP.NET" Language="vb"MasterPageFile="~/Site.Master"AutoEventWireup="false"CodeBehind="Default.aspx.vb"Inherits="DrawingshapesGraphicsUsingiTextSharp._Default"%>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <table cellpadding="3"cellspacing="5"border="0"width="50%">
        <tr>
            <td>
                <divclass="panel panel-default">
                    <divclass="panel-heading">
                        <asp:label id="lblHeader" runat="server" Text="Using iTextSharp to Drawing shapes and Graphics in ASP.NET"></asp:label>
                    </div>
                    <divclass="panel-body">
                      
                    </div>
                    <divclass="modal-footer">
                        <div class="btn-group">
                            <asp:LinkButton id="cmdDrawing"runat="server"CssClass="btn btn-small" ValidationGroup="Text" Causesvalidation="true">
                                <i class="icon-insert"></i>&nbsp;&nbsp;<asp:label id="lblInsert" runat="server" Text="Drawing shapes and Graphics"></asp:label>
                            </asp:LinkButton>
                        </div>
                    </div>
                </div>
            </td>
        </tr>       
    </table>
</asp:Content>

B5: Viết Code cho file Default.aspx
C# Code
//Visit http://www.laptrinhdotnet.com for more ASP.NET Tutorials
using Microsoft.VisualBasic;
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Web.UI;
using System.Web;
using iTextSharp.text.html;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;

namespace AddWatermarkToPDFUsingiTextSharp
{
    public partial class _Default : System.Web.UI.Page
    {

        #region"Drawing shapes and Graphics"

        private voidDrawingshapesGraphicsUsingiTextSharp(stringFileName)
        {
            Document oDoc = newDocument(PageSize.A4, 10f, 10f, 5f, 0f);
            oDoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
            System.IO.MemoryStream msReport = new System.IO.MemoryStream();
            Paragraph pLegend = newParagraph();

            try
            {
                PdfWriter writer = PdfWriter.GetInstance(oDoc, msReport);
                oDoc.AddAuthor("laptrinhdotnet.com");
                oDoc.AddSubject("Add Watermark To PDF");
                oDoc.Open();

                PdfContentByte ContentUnder = writer.DirectContentUnder;
                ContentUnder.SetCMYKColorStroke(255, 255, 0, 0);

                //Drawing Rectangle
                ContentUnder.Rectangle(100f, 430f, 150f, 50f);
                ContentUnder.Stroke();

                //Drawing Ellipse
                ContentUnder.Ellipse(100f, 420f, 400f, 300f);
                ContentUnder.Stroke();

                //Drawing Circle
                ContentUnder.SetCMYKColorFill(0, 255, 255, 0);
                ContentUnder.Circle(500f, 480f, 50f);
                ContentUnder.Fill();

                //Drawing Triangle
                ContentUnder.MoveTo(500f, 260f);
                ContentUnder.LineTo(500f, 410f);
                ContentUnder.LineTo(650f, 260f);
                ContentUnder.ClosePathStroke();

                //Drawing Square
                ContentUnder.Rectangle(100f, 200f, 60, 60);
                ContentUnder.Stroke();

                //Drawing a line
                ContentUnder.MoveTo(180f, 250f);
                ContentUnder.LineTo(280f, 180f);
                ContentUnder.Stroke();

                string sText = "Using iTextSharp to Drawing shapes and Graphics";
                Chunk beginning = new Chunk(sText, new Font(Font.HELVETICA, 16f));
                Phrase p1 = newPhrase(beginning);

                pLegend.IndentationLeft = 220;
                pLegend.Add(p1);
                oDoc.Add(pLegend);
            }
            catch
            {
            }
            oDoc.Close();
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".pdf");
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(msReport.ToArray());
            Response.End();
        }

        #endregion

        #region"Event Handles"

        protected voidcmdDrawing_Click(object sender, System.EventArgs e)
        {
            DrawingshapesGraphicsUsingiTextSharp("Drawing");
        }

        #endregion
    }
}
VB.NET Code
'Visit http://www.laptrinhdotnet.com for more ASP.NET Tutorials
Imports iTextSharp.text.html
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Imports System.IO

Namespace DrawingshapesGraphicsUsingiTextSharp

    Public Class _Default
        Inherits System.Web.UI.Page

#Region "Drawing shapes and Graphics"

        Private SubDrawingshapesGraphicsUsingiTextSharp(ByValFileName As String)
            Dim oDoc As New Document(PageSize.A4.Rotate, 20, 20, 30, 20)
            oDoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate())
            Dim msReport As New System.IO.MemoryStream()
            Dim pLegend As New Paragraph()

            Try
                Dim writer AsPdfWriter = PdfWriter.GetInstance(oDoc, msReport)
                oDoc.AddAuthor("laptrinhdotnet.com")
                oDoc.AddSubject("Add Watermark To PDF")
                oDoc.Open()

                Dim ContentUnder As PdfContentByte = writer.DirectContentUnder
                ContentUnder.SetCMYKColorStroke(255, 255, 0, 0)

                'Drawing Rectangle
                ContentUnder.Rectangle(100.0F, 430.0F, 150.0F, 50.0F)
                ContentUnder.Stroke()

                'Drawing Ellipse
                ContentUnder.Ellipse(100.0F, 420.0F, 400.0F, 300.0F)
                ContentUnder.Stroke()

                'Drawing Circle
                ContentUnder.SetCMYKColorFill(0, 255, 255, 0)
                ContentUnder.Circle(500.0F, 480.0F, 50.0F)
                ContentUnder.Fill()

                'Drawing Triangle
                ContentUnder.MoveTo(500.0F, 260.0F)
                ContentUnder.LineTo(500.0F, 410.0F)
                ContentUnder.LineTo(650.0F, 260.0F)
                ContentUnder.ClosePathStroke()

                'Drawing Square
                ContentUnder.Rectangle(100.0F, 200.0F, 60, 60)
                ContentUnder.Stroke()

                'Drawing a line
                ContentUnder.MoveTo(180.0F, 250.0F)
                ContentUnder.LineTo(280.0F, 180.0F)
                ContentUnder.Stroke()

                Dim sText AsString = "Using iTextSharp to Drawing shapes and Graphics" & vbCrLf
                Dim beginning AsNew Chunk(sText, New Font(Font.HELVETICA, 16.0F))
                Dim p1 AsNew Phrase(beginning)

                pLegend.IndentationLeft = 220
                pLegend.Add(p1)
                oDoc.Add(pLegend)

            Catch e As Exception
                Console.Error.WriteLine(e.Message)
            End Try
            oDoc.Close()
            Response.Clear()
            Response.AddHeader("content-disposition", "attachment;filename=" & FileName & ".pdf")
            Response.ContentType = "application/pdf"
            Response.BinaryWrite(msReport.ToArray())
            Response.End()
        End Sub

#End Region

#Region "Event Handles"

        Private SubcmdDrawing_Click(ByVal sender As Object, ByVal e As System.EventArgs) HandlescmdDrawing.Click
            DrawingshapesGraphicsUsingiTextSharp("Drawing")
        End Sub

#End Region

    End Class

End Namespace

Code Example C#, Code Example VB.NET
Code Example C#, Code Example VB.NET




Chúc các bạn thành công!

Quang Bình

0 comments Blogger 0 Facebook

Post a Comment

 
lập trình đốt nét © 2013. All Rights Reserved. Powered by Blogger
Top