水印到 pdf 不能正常工作
watermark to pdf not working perfectly
我有在用户上传文件时创建水印的代码。水印在中间页和页脚页中创建。但我的问题是水印工作并不完美,具体取决于上传文件内容的内容。有关更多详细信息,请注意下图:
这是我的代码:
public bool InsertWaterMark(string path)
{
bool valid = true;
string FileDestination = AppDomain.CurrentDomain.BaseDirectory + "Content/" + path;
string FileOriginal = AppDomain.CurrentDomain.BaseDirectory + "Content/" + path.Replace("FileTemporary", "FileOriginal");
System.IO.File.Copy(FileDestination, FileOriginal);
string watermarkText = "Controlled Copy";
PdfReader reader = new PdfReader(FileOriginal);
using (FileStream fs = new FileStream(FileDestination, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
int pageCount = reader.NumberOfPages;
for (int i = 1; i <= pageCount; i++)
{
PdfContentByte cbCenter = stamper.GetUnderContent(i);
PdfGState gState = new PdfGState();
cbCenter.BeginText();
iTextSharp.text.Rectangle rect = reader.GetPageSize(i);
cbCenter.SetColorFill(BaseColor.GRAY);
gState.FillOpacity = 0.15f;
cbCenter.SetGState(gState);
cbCenter.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 80);
cbCenter.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, 45f);
cbCenter.EndText();
PdfContentByte cbFooter = stamper.GetUnderContent(i);
PdfGState gStateFooter = new PdfGState();
cbFooter.BeginText();
cbFooter.SetColorFill(BaseColor.RED);
gStateFooter.FillOpacity = 1f;
cbFooter.SetGState(gStateFooter);
cbFooter.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_OBLIQUE, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12);
cbFooter.ShowTextAligned(PdfContentByte.ALIGN_CENTER, '"' + "When printed, this documents are considered uncontrolled" + '"', 300.7f, 10.7f, 0);
cbFooter.EndText();
}
}
}
reader.Close();
return valid;
}
请更正我的代码并给出解决方案。
提前致谢
您正在使用 stamper.GetUnderContent(i)
,这意味着您正在 现有内容下添加内容。
换句话说:水印被现有内容覆盖。如果现有内容不透明,则水印将不可见。 Ctrl+A可以看到形状,打印页面时看不到。
在示例 ALFA 中,您会在现有文本下方看到水印,因为现有文本没有背景。在示例 CARLIE 中,您看不到它,因为文本具有覆盖水印的白色背景。
如何解决这个问题?使用 stamper.GetOverContent(i)
而不是 stamper.GetUnderContent(i)
.
另外:为什么您仍在使用 iTextSharp 版本 5 或更早版本?当前版本是 iText 7 for .NET。请升级!
我有在用户上传文件时创建水印的代码。水印在中间页和页脚页中创建。但我的问题是水印工作并不完美,具体取决于上传文件内容的内容。有关更多详细信息,请注意下图:
这是我的代码:
public bool InsertWaterMark(string path)
{
bool valid = true;
string FileDestination = AppDomain.CurrentDomain.BaseDirectory + "Content/" + path;
string FileOriginal = AppDomain.CurrentDomain.BaseDirectory + "Content/" + path.Replace("FileTemporary", "FileOriginal");
System.IO.File.Copy(FileDestination, FileOriginal);
string watermarkText = "Controlled Copy";
PdfReader reader = new PdfReader(FileOriginal);
using (FileStream fs = new FileStream(FileDestination, FileMode.Create, FileAccess.Write, FileShare.None))
{
using (PdfStamper stamper = new PdfStamper(reader, fs))
{
int pageCount = reader.NumberOfPages;
for (int i = 1; i <= pageCount; i++)
{
PdfContentByte cbCenter = stamper.GetUnderContent(i);
PdfGState gState = new PdfGState();
cbCenter.BeginText();
iTextSharp.text.Rectangle rect = reader.GetPageSize(i);
cbCenter.SetColorFill(BaseColor.GRAY);
gState.FillOpacity = 0.15f;
cbCenter.SetGState(gState);
cbCenter.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 80);
cbCenter.ShowTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, rect.Width / 2, rect.Height / 2, 45f);
cbCenter.EndText();
PdfContentByte cbFooter = stamper.GetUnderContent(i);
PdfGState gStateFooter = new PdfGState();
cbFooter.BeginText();
cbFooter.SetColorFill(BaseColor.RED);
gStateFooter.FillOpacity = 1f;
cbFooter.SetGState(gStateFooter);
cbFooter.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_OBLIQUE, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12);
cbFooter.ShowTextAligned(PdfContentByte.ALIGN_CENTER, '"' + "When printed, this documents are considered uncontrolled" + '"', 300.7f, 10.7f, 0);
cbFooter.EndText();
}
}
}
reader.Close();
return valid;
}
请更正我的代码并给出解决方案。 提前致谢
您正在使用 stamper.GetUnderContent(i)
,这意味着您正在 现有内容下添加内容。
换句话说:水印被现有内容覆盖。如果现有内容不透明,则水印将不可见。 Ctrl+A可以看到形状,打印页面时看不到。
在示例 ALFA 中,您会在现有文本下方看到水印,因为现有文本没有背景。在示例 CARLIE 中,您看不到它,因为文本具有覆盖水印的白色背景。
如何解决这个问题?使用 stamper.GetOverContent(i)
而不是 stamper.GetUnderContent(i)
.
另外:为什么您仍在使用 iTextSharp 版本 5 或更早版本?当前版本是 iText 7 for .NET。请升级!