转载:https://www.freesion.com/article/81151100236/
使用试用版spire.Pdf生成书签的时候总是会有Evaluation Warning : The document was created with Spire.PDF for .NET.的警告字样,当时在网上也找了一些方法,例如:生成俩页再删掉第一页之类的,都没能很好的解决问题,遂换了一种思路,用一个白底的图片覆盖字样
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System;
using System.Drawing;
using System.IO;
using Spire.Pdf.Annotations;
using Spire.Pdf.Annotations.Appearance;
namespace Server.Utils
{
public class test
{
public void CreatePDF()
{
//创建一个PdfDocument类对象,并新添加一页到PDF文档
PdfDocument doc = new PdfDocument();
//添加一个空页
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4);
//创建一个PdfGrid对象
PdfGrid grid = new PdfGrid();
#region 添加白底图片覆盖字样
PdfRubberStampAnnotation logoStamp = new PdfRubberStampAnnotation(new RectangleF(new PointF(0, 0), new SizeF(350, 15)));
PdfAppearance logoApprearance = new PdfAppearance(logoStamp);
//本地路径
var logoPath = AppDomain.CurrentDomain.BaseDirectory + "white.jpg";
PdfImage image = PdfImage.FromFile(logoPath);
PdfTemplate template = new PdfTemplate(350, 15);
template.Graphics.DrawImage(image, 0, 0);
logoApprearance.Normal = template;
logoStamp.Appearance = logoApprearance;
page.AnnotationsWidget.Add(logoStamp);
#endregion
#region 添加标题
//文本、字体、颜色
string titleText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "测试PDF标题";
PdfTrueTypeFont titleFont = new PdfTrueTypeFont(new Font("宋体", 15), true);
PdfSolidBrush titleBrush = new PdfSolidBrush(Color.Black);
page.Canvas.DrawString(titleText, titleFont, titleBrush, 150, 20);
#endregion
//保存到文档D:\PDF文档
var path = AppDomain.CurrentDomain.BaseDirectory + @"PDF\";
var dif = new DirectoryInfo(path);
if (!dif.Exists)
dif.Create();
var name = "xxx" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
string savePath = path + name;
doc.SaveToFile(savePath);
}
}
}
白底图片:
———————————————————————————
———————————————————————————