Installato tramite NuGet
PM> Install-Package Spire.PDF
Link correlati
Una filigrana di immagine è solitamente un logo o un segno che appare sullo sfondo di documenti digitali, indicando il proprietario del copyright del contenuto. La filigrana del documento PDF con un'immagine può impedire il riutilizzo o la modifica dei dati. Questo articolo illustra come aggiungere una filigrana immagine al PDF in C# e VB.NET utilizzando Spire.PDF for .NET.
Installa Spire.PDF for .NET
Per cominciare, è necessario aggiungere i file DLL inclusi nel pacchetto Spire.PDF for.NET come riferimenti nel progetto .NET. I file DLL possono essere scaricati da questo link o installato tramite NuGet.
- Package Manager
PM> Install-Package Spire.PDF
Aggiungi una filigrana immagine al PDF
Di seguito sono riportati i passaggi principali per aggiungere una filigrana immagine a un documento PDF.
- Creare un oggetto PdfDocument e caricare un file PDF di esempio utilizzando il metodo PdfDocument.LoadFromFile().
- Carica un file immagine usando il metodo Image.FromFile().
- Scorrere le pagine del documento e ottenere la pagina specifica tramite la proprietà PdfDocument.Pages[].
- Impostare l'immagine come immagine di sfondo/filigrana della pagina corrente tramite la proprietà PdfPageBase.BackgroundImage. Impostare la posizione e le dimensioni dell'immagine tramite la proprietà PdfPageBase.BackgroundRegion.
- Salvare il documento in un file PDF diverso utilizzando il metodo PdfDocument.SaveToFile().
- C#
- VB.NET
using Spire.Pdf;
using System.Drawing;
namespace AddImageWatermark
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument document = new PdfDocument();
//Load a sample PDF document
document.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
//Load an image
Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\logo.png");
//Get the image width and height
int imgWidth = image.Width;
int imgHeight = image.Height;
//Loop through the pages
for (int i = 0; i < document.Pages.Count; i++)
{
//Get the page width and height
float pageWidth = document.Pages[i].ActualSize.Width;
float pageHeight = document.Pages[i].ActualSize.Height;
//Set the background opacity
document.Pages[i].BackgroudOpacity = 0.3f;
//Set the background image of current page
document.Pages[i].BackgroundImage = image;
//Position the background image at the center of the page
Rectangle rect = new Rectangle((int)(pageWidth - imgWidth) / 2, (int)(pageHeight - imgHeight) / 2, imgWidth, imgHeight);
document.Pages[i].BackgroundRegion = rect;
}
//Save the document to file
document.SaveToFile("AddImageWatermark.pdf");
document.Close();
}
}
}
Imports Spire.Pdf
Imports System.Drawing
Namespace AddImageWatermark
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim document As PdfDocument = New PdfDocument()
'Load a sample PDF document
document.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")
'Load an image
Dim image As Image = Image.FromFile("C:\Users\Administrator\Desktop\logo.png")
'Get the image width and height
Dim imgWidth As Integer = image.Width
Dim imgHeight As Integer = image.Height
'Loop through the pages
Dim i As Integer
For i = 0 To document.Pages.Count- 1 Step i + 1
'Get the page width and height
Dim pageWidth As single = document.Pages(i).ActualSize.Width
Dim pageHeight As single = document.Pages(i).ActualSize.Height
'Set the background opacity
document.Pages(i).BackgroudOpacity = 0.3f
'Set the background image of current page
document.Pages(i).BackgroundImage = image
'Position the background image at the center of the page
Dim rect As Rectangle = New Rectangle(CType((pageWidth - imgWidth) / 2,(Integer)(pageHeight - imgHeight) / 2,imgWidth,imgHeight, Integer))
document.Pages(i).BackgroundRegion = rect
Next
'Save the document to file
document.SaveToFile("AddImageWatermark.pdf")
document.Close()
End Sub
End Class
End Namespace

Richiedi una licenza temporanea
Se desideri rimuovere il messaggio di valutazione dai documenti generati o eliminare le limitazioni delle funzioni, per favore richiedere una licenza di prova di 30 giorni per te.