如何将图像背景作为水印添加到pdf文件?



我创建了ExportToPDF按钮将Gridview导出为PDF文件,那么我如何将图像背景添加为PDF文件的水印?

protected void ExportToPDF(object sender, EventArgs e)
{
using (StringWriter sw = new StringWriter())
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
gvOrders.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
}
}

一个解决方案是;

private static Image watermark_600
{
get
{
if (_watermark_600 == null)
_watermark_600 = Image.FromFile(HttpContext.Current.Server.MapPath(@"~/content/Images/watermark600.png"));
return _watermark_600;
}
}

,

public static byte[] AddWaterMark(byte[] fileContent)
{
if (fileContent == null)
return null;
//create a image object containing the photograph to watermark            
using (var img = new ImageFactory())
using (var imgWatermark = new ImageLayer())
{
img.Load(fileContent);
if (img.Image.Width < 750 && img.Image.Height < 750)
imgWatermark.Image = (Image)watermark_200.Clone();
else if (img.Image.Width < 1500 && img.Image.Height < 1500)
imgWatermark.Image = (Image)watermark_400.Clone();
else if (img.Image.Width < 2500 && img.Image.Height < 2500)
imgWatermark.Image = (Image)watermark_600.Clone();
else
imgWatermark.Image = (Image)watermark_800.Clone();
int x = Math.Abs((img.Image.Width - imgWatermark.Image.Width)) / 2;
int y = Math.Abs((img.Image.Height - imgWatermark.Image.Height)) / 2;
imgWatermark.Position = new Point(x, y);
imgWatermark.Opacity = 90;
img.Overlay(imgWatermark);
using (MemoryStream msbyte = new MemoryStream())
{
img.Save(msbyte);
return msbyte.ToArray();
}
}
}

相关内容

  • 没有找到相关文章

最新更新