
iTextSharp的确好用,但我加水印一般在初始化的时候写两行就行了,不需要这么麻烦,只是图片得自己做淡一些
if(System.IO.File.Exists("./waterMark/waterMark.JPG") == false)
MessageBox.Show("waterMark加载失败,请确定文件waterMark.JPG放在waterMark文件夹下!")
else
{
Watermark watermark = new Watermark(iTextSharp.text.Image.getInstance("./waterMark/waterMark.JPG"), wm_left, wm_top)
document.Add(watermark)
}
wm_left, wm_top是水印的位置,从第几页开始显示水印就把上面的代码加到第几页
补充:watermark是iTextSharp里的,不需要其他引用
iTextSharp.text.watermark 检查是否输入正确
.\itextsharp-0.04-src\iTextSharp\text\watermark.cs 。。。。。。
或者你可以搜索一下?
代码全文:
using System
/*
* $Id: Watermark.cs,v 1.3 2003/05/15 01:49:58 geraldhenson Exp $
* $Name: $
*
* Copyright 2000, 2001, 2002 by Bruno Lowagie.
*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (the "License")you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the License.
*
* The Original Code is 'iText, a free JAVA-PDF library'.
*
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
* All Rights Reserved.
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
*
* Contributor(s): all the names of the contributors are added in the source code
* where applicable.
*
* Alternatively, the contents of this file may be used under the terms of the
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
* provisions of LGPL are applicable instead of those above. If you wish to
* allow use of your version of this file only under the terms of the LGPL
* License and not to allow others to use your version of this file under
* the MPL, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the LGPL.
* If you do not delete the provisions above, a recipient may use your version
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
*
* This library is free softwareyou can redistribute it and/or modify it
* under the terms of the MPL as stated above or under the terms of the GNU
* Library General Public License as published by the Free Software Foundation
* either version 2 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTYwithout even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
* details.
*
* If you didn't download this code from the following link, you should check if
* you aren't using an obsolete version:
* http://www.lowagie.com/iText/
*/
namespace iTextSharp.text {
/// <summary>
/// A <CODE>Watermark</CODE>is a graphic element (GIF or JPEG)
/// that is shown on a certain position on each page.
/// </summary>
/// <seealso cref="T:iTextSharp.text.Element"/>
/// <seealso cref="T:iTextSharp.text.Jpef"/>
/// <seealso cref="T:iTextSharp.text.Gif"/>
/// <seealso cref="T:iTextSharp.text.Png"/>
public class Watermark : Image, IElement {
// membervariables
/// <summary>This is the offset in x-direction of the Watermark. </summary>
private float offsetX = 0
/// <summary>This is the offset in y-direction of the Watermark. </summary>
private float offsetY = 0
// Constructors
/// <summary>
/// Constructs a <CODE>Watermark</CODE>-object, using an <CODE>Image</CODE>.
/// </summary>
/// <param name="image">an <CODE>Image</CODE>-object</param>
/// <param name="offsetX">the offset in x-direction</param>
/// <param name="offsetY">the offset in y-direction</param>
public Watermark(Image image, float offsetX, float offsetY) : base(image) {
this.offsetX = offsetX
this.offsetY = offsetY
}
// implementation of the Element interface
/// <summary>
/// Gets the type of the text element.
/// </summary>
/// <value>a type</value>
public override int Type {
get {
return type
}
}
// methods to retrieve information
/// <summary>
/// Returns the offset in x direction.
/// </summary>
/// <value>a value</value>
public float OffsetX {
get {
return offsetX
}
}
/// <summary>
/// Returns the offset in y direction.
/// </summary>
/// <value>an offset</value>
public float OffsetY {
get {
return offsetY
}
}
}
}
恩,给你发过去了,带有word教程和示例代码,国庆快乐
如果是把图片转换为pdf格式的,使用如下代码:首先要下载 iTextSharp.dll文件,之后添加到引用中
提供一个类 :
public void ExportDataIntoPDF(string pathName, String path)
{//导出至PDF
iTextSharp.text.Document document = new iTextSharp.text.Document()
try
{
iTextSharp .text .pdf .PdfWriter .GetInstance (document, new FileStream(pathName, FileMode.CreateNew ))
document.Open()
iTextSharp.text.pdf.BaseFont bfChinese = iTextSharp.text.pdf.BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.NOT_EMBEDDED)
iTextSharp.text.Font Titlefont = new iTextSharp.text.Font(bfChinese, 16, iTextSharp.text.Font.BOLD , new iTextSharp.text.Color(0, 0, 0))
document.Add(new Paragraph(" 将图片转成PDF:" + "\n", Titlefont))
iTextSharp.text.Image je = iTextSharp.text.Image.GetInstance(path)//path就是函数入参,即你要转成PDF的图片路径
document.Add(je)
}
catch (Exception de)
{
MessageBox .Show (de.ToString())
}
document.Close()
}
调用这个类就ok了
如果转换为swf格式的话 就需要使用商业的dll了
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)