1.使用Aspose.Pdf.BackgroundArtifact 插入底层水印
Aspose.Pdf.Document document = new Aspose.Pdf.Document(pdfPath);
for (int i = 1; i <= document.Pages.Count; i++)
{
Aspose.Pdf.BackgroundArtifact artifact = new Aspose.Pdf.BackgroundArtifact();
if (document.Pages[i].PageInfo.IsLandscape)//是否横版
{
artifact.BackgroundImage = File.OpenRead(watermarkLandscapeImgage);
}
else
{
artifact.BackgroundImage = File.OpenRead(watermarkImgage);
}
artifact.Opacity = opacity;
artifact.Rotation = rotation;
artifact.ArtifactHorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
artifact.ArtifactVerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
document.Pages[i].Artifacts.Add(artifact);
}
document.Save(pdfPath);
document.Dispose();
2.使用Aspose.Pdf.WatermarkArtifact 添加水印
Aspose.Pdf.Document document = new Aspose.Pdf.Document(pdfPath);
for (int i = 1; i <= document.Pages.Count; i++)
{
Aspose.Pdf.WatermarkArtifact artifact = new Aspose.Pdf.WatermarkArtifact();
artifact.Rotation = rotation;
artifact.Opacity = opacity;
artifact.ArtifactHorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
artifact.ArtifactVerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
artifact.Type = Aspose.Pdf.Artifact.ArtifactType.Undefined;
//artifact.Subtype = Aspose.Pdf.Artifact.ArtifactSubtype.Background;
if (document.Pages[i].PageInfo.IsLandscape)//是否横版
{
artifact.SetImage(File.OpenRead(watermarkLandscapeImgage));
}
else
{
Image imgPhoto = Image.FromFile(watermarkImgage);
artifact.SetImage(File.OpenRead(watermarkImgage));
}
document.Pages[i].Artifacts.Add(artifact);
}
document.Save(pdfPath);
document.Dispose();
3. 使用Aspose.Pdf.ImageStamp(印章)
Aspose.Pdf.Document document = new Aspose.Pdf.Document(pdfPath);
for (int i = 1; i <= document.Pages.Count; i++)
{
Image imgPhoto = Image.FromFile(watermarkImgage);
var Width = (int)(document.PageInfo.Width / 4);
var Height = (int)(document.PageInfo.Height / 4);
Aspose.Pdf.ImageStamp imageStamp = new Aspose.Pdf.ImageStamp(watermarkImgage);
imageStamp.Quality = 100;
imageStamp.Background = false;
imageStamp.XIndent = Width;
imageStamp.YIndent = Height;
imageStamp.Height = imgPhoto.Height*0.72;
imageStamp.Width = imgPhoto.Width*0.72;
// imageStamp.Rotate = Rotation.on270;
imageStamp.Opacity = 0.7;
document.Pages[i].AddStamp(imageStamp);
}
document.Save(pdfPath);
document.Dispose();