C#利用iTextSharp組件給PDF文檔添加圖片/文字水印
最近在做關(guān)于PDF文檔添加水印的功能,折騰了好久,終于好了。以下做個記錄:
首先會用到iTextSharp組件,大家可以去官網(wǎng)下載,同時我也會在本文中附加進來。
代碼中添加引用為:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using iTextSharp.text.pdf; using System.IO; using iTextSharp.text;
創(chuàng)建一個顯示指定圖片的pdf
/// <summary>
/// 創(chuàng)建一個顯示指定圖片的pdf
/// </summary>
/// <param name="picPdfPath"></param>
/// <param name="picPath"></param>
/// <returns></returns>
public static bool CreatePDFByPic(string picPdfPath,string picPath)
{
//新建一個文檔
Document doc = new Document();
try
{
//建立一個書寫器(Writer)與document對象關(guān)聯(lián)
PdfWriter.GetInstance(doc, new FileStream(picPdfPath, FileMode.Create, FileAccess.ReadWrite));
//打開一個文檔
doc.Open();
//向文檔中添加內(nèi)容
Image img = Image.GetInstance(picPath);
//img.SetAbsolutePosition();
doc.Add(img);
return true;
}
catch (Exception ex)
{
return false;
throw ex;
}
finally
{
if (doc != null)
{
doc.Close();
}
}
}
為PDF文檔添加圖片水印
/// <summary>
/// 加圖片水印
/// </summary>
/// <param name="inputfilepath"></param>
/// <param name="outputfilepath"></param>
/// <param name="ModelPicName"></param>
/// <param name="top"></param>
/// <param name="left"></param>
/// <returns></returns>
public static bool PDFWatermark(string inputfilepath, string outputfilepath, string ModelPicName, float top, float left)
{
//throw new NotImplementedException();
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try
{
pdfReader = new PdfReader(inputfilepath);
int numberOfPages = pdfReader.NumberOfPages;
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;
pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
PdfContentByte waterMarkContent;
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ModelPicName);
image.GrayFill = 20;//透明度,灰色填充
//image.Rotation//旋轉(zhuǎn)
//image.RotationDegrees//旋轉(zhuǎn)角度
//水印的位置
if (left < 0)
{
left = width/2 - image.Width + left;
}
//image.SetAbsolutePosition(left, (height - image.Height) - top);
image.SetAbsolutePosition(left, (height/2 - image.Height) - top);
//每一頁加水印,也可以設(shè)置某一頁加水印
for (int i = 1; i <= numberOfPages; i++)
{
//waterMarkContent = pdfStamper.GetUnderContent(i);//內(nèi)容下層加水印
waterMarkContent = pdfStamper.GetOverContent(i);//內(nèi)容上層加水印
waterMarkContent.AddImage(image);
}
//strMsg = "success";
return true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (pdfStamper != null)
pdfStamper.Close();
if (pdfReader != null)
pdfReader.Close();
}
}
添加普通偏轉(zhuǎn)角度文字水印
/// <summary>
/// 添加普通偏轉(zhuǎn)角度文字水印
/// </summary>
/// <param name="inputfilepath"></param>
/// <param name="outputfilepath"></param>
/// <param name="waterMarkName"></param>
/// <param name="permission"></param>
public static void setWatermark(string inputfilepath, string outputfilepath,string waterMarkName)
{
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try
{
pdfReader = new PdfReader(inputfilepath);
pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
int total = pdfReader.NumberOfPages + 1;
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);
float width = psize.Width;
float height = psize.Height;
PdfContentByte content;
BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
for (int i = 1; i < total; i++)
{
content = pdfStamper.GetOverContent(i);//在內(nèi)容上方加水印
//content = pdfStamper.GetUnderContent(i);//在內(nèi)容下方加水印
//透明度
gs.FillOpacity = 0.3f;
content.SetGState(gs);
//content.SetGrayFill(0.3f);
//開始寫入文本
content.BeginText();
content.SetColorFill(BaseColor.LIGHT_GRAY);
content.SetFontAndSize(font, 100);
content.SetTextMatrix(0, 0);
content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 - 50,height / 2 - 50, 55);
//content.SetColorFill(BaseColor.BLACK);
//content.SetFontAndSize(font, 8);
//content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, 0, 0, 0);
content.EndText();
}
}catch (Exception ex)
{
throw ex;
}
finally
{
if (pdfStamper != null)
pdfStamper.Close();
if (pdfReader != null)
pdfReader.Close();
}
}
添加傾斜水印
/// <summary>
/// 添加傾斜水印
/// </summary>
/// <param name="inputfilepath"></param>
/// <param name="outputfilepath"></param>
/// <param name="waterMarkName"></param>
/// <param name="userPassWord"></param>
/// <param name="ownerPassWord"></param>
/// <param name="permission"></param>
public static void setWatermark(string inputfilepath, string outputfilepath, string waterMarkName, string userPassWord, string ownerPassWord, int permission)
{
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try
{
pdfReader = new PdfReader(inputfilepath);
pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
// 設(shè)置密碼
//pdfStamper.SetEncryption(false,userPassWord, ownerPassWord, permission);
int total = pdfReader.NumberOfPages + 1;
PdfContentByte content;
BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
gs.FillOpacity = 0.2f;//透明度
int j = waterMarkName.Length;
char c;
int rise = 0;
for (int i = 1; i < total; i++)
{
rise = 500;
content = pdfStamper.GetOverContent(i);//在內(nèi)容上方加水印
//content = pdfStamper.GetUnderContent(i);//在內(nèi)容下方加水印
content.BeginText();
content.SetColorFill(BaseColor.DARK_GRAY);
content.SetFontAndSize(font, 50);
// 設(shè)置水印文字字體傾斜 開始
if (j >= 15)
{
content.SetTextMatrix(200, 120);
for (int k = 0; k < j; k++)
{
content.SetTextRise(rise);
c = waterMarkName[k];
content.ShowText(c + "");
rise -= 20;
}
}
else
{
content.SetTextMatrix(180, 100);
for (int k = 0; k < j; k++)
{
content.SetTextRise(rise);
c = waterMarkName[k];
content.ShowText(c + "");
rise -= 18;
}
}
// 字體設(shè)置結(jié)束
content.EndText();
// 畫一個圓
//content.Ellipse(250, 450, 350, 550);
//content.SetLineWidth(1f);
//content.Stroke();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (pdfStamper != null)
pdfStamper.Close();
if (pdfReader != null)
pdfReader.Close();
}
}
調(diào)用方法的例子:
string path = "D://my.pdf";
HtmlToPDFUtil.HtmlToPdf(Request.Url.AbsoluteUri, path);
//HtmlToPDFUtil.HtmlToPdf(sbUrlList.ToString(), path);
PDFSetWaterMark.PDFWatermark(path, "D://my.pdf", Server.MapPath("/HtmlToPdf/Tools/sy.bmp"), 0, 0);
PDFSetWaterMark.setWatermark("D://my.pdf", "D://my2.pdf", "TEST");
//PDFSetWaterMark.setWatermark("D://my.pdf", "D://my2.pdf", "TEST", "", "", 1);
附件:iTextSharp
總結(jié)
到此這篇關(guān)于C#利用iTextSharp組件給PDF文檔添加圖片/文字水印的文章就介紹到這了,更多相關(guān)iTextSharp組件給PDF添加水印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
.NET使用C#設(shè)置Excel單元格數(shù)值格式
這篇文章主要為大家詳細介紹了如何使用C#在.NET程序中實現(xiàn)對Excel單元格數(shù)字格式的設(shè)置,幫助實現(xiàn)更完善的Excel文件處理,感興趣的小伙伴可以了解下2024-12-12
利用C#實現(xiàn)將小數(shù)值四舍五入為整數(shù)
在項目的開發(fā)中,遇到一些除法計算內(nèi)容會產(chǎn)生小數(shù)值,但是又需要根據(jù)項目的實際情況將這些小數(shù)內(nèi)容化為整數(shù),所以本文為大家整理了C#實現(xiàn)將小數(shù)值四舍五入為整數(shù)的方法,希望對大家有所幫助2023-07-07
C#將圖片和字節(jié)流互相轉(zhuǎn)換并顯示到頁面上
本文主要介紹用C#實現(xiàn)圖片轉(zhuǎn)換成字節(jié)流,字節(jié)流轉(zhuǎn)換成圖片,并根據(jù)圖片路徑返回圖片的字節(jié)流,有需要的朋友可以參考下2015-08-08

