本文介绍了如何使用iTextSharp Library为pdf添加边框和水印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public FileStreamResult Pdf()
{
MemoryStream workStream = new MemoryStream();
Document document = new Document();
PdfWriter.GetInstance(document, workStream).CloseStream = false;
document.Open();
document.Add(new Paragraph("Hello World"));
document.Add(new Paragraph(DateTime.Now.ToString()));
document.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
return new FileStreamResult(workStream, "application/pdf");
}
推荐答案
PdfContentByte content = writer.DirectContent;
Rectangle rect = new Rectangle(doc.PageSize);
rect.Left += doc.LeftMargin;
rect.Right -= doc.RightMargin;
rect.Top -= doc.TopMargin;
rect.Bottom += doc.BottomMargin;
content.SetColorStroke(Color.BLACK);
content.Rectangle(rect.Left, rect.Bottom, rect.Width, rect.Height);
content.Stroke();
WATERMARK
添加类似onStartPage事件所以它会被调用每个新页面。
WATERMARK
Add similar to the onStartPage event so it gets called for each new page.
float fontSize = 48;
float xPos = 300;
float yPos = 400;
float angle = 25;
PdfContentByte wmark = writer.DirectContentUnder;
BaseFont baseFont = BaseFont.CreateFont(BaseFont.ARIAL, BaseFont.WINANSI, BaseFont.EMBEDDED);
wmark.BeginText();
wmark.SetColorFill(BaseColor.LIGHT_GRAY);
wmark.SetFontAndSize(baseFont, fontSize);
wmark.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "CONFIDENTIAL", xPos, yPos, angle);
wmark.EndText();
这篇关于如何使用iTextSharp Library为pdf添加边框和水印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!