翻譯|使用教程|編輯:李顯亮|2021-01-11 10:09:23.483|閱讀 511 次
概述:PDF文件中經(jīng)常使用水印。只需幾個簡單的步驟即可在PDF文件中插入或刪除文本和圖像水印。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
PDF文件中經(jīng)常使用水印。只需幾個簡單的步驟即可在PDF文件中插入或刪除文本和圖像水印。同樣可以輕松控制用于處理水印的許多屬性。例如,不透明度,位置,旋轉(zhuǎn)度,顏色,字體,圖像等,等等。同樣,可以使用快速API調(diào)用為水印設(shè)置許多其他屬性。讓我們通過以下各節(jié)進(jìn)一步探討該主題:
.NET API的Aspose.PDF可以創(chuàng)建,編輯和處理PDF文檔,可以輕松使用在PDF文檔中添加或刪除水印的功能。 您可以從“新發(fā)行版”部分下載最新的DLL文件,
使用C#以編程方式在PDF文件中插入文本水印
PDF文檔中經(jīng)常使用文本水印來顯示文檔的目的或性質(zhì)。例如,某些組織可能會根據(jù)文檔的性質(zhì)將文本水印添加為“分類”或“機(jī)密”。同樣,可以控制字體大小,字體樣式,顏色,旋轉(zhuǎn)度,位置等。可以通過以下步驟在PDF文件中添加文本水印:
以下代碼段說明了如何使用C#在PDF文件中添加或插入文本水印:
// Open document
Document pdfDocument = new Document(dataDir + "Input.pdf");
// Create text stamp
TextStamp textStamp = new TextStamp("CONFIDENTIAL");
// Set origin
textStamp.XIndent = 25;
textStamp.YIndent = 400;
// Set text properties
textStamp.TextState.Font = FontRepository.FindFont("Arial");
textStamp.TextState.FontSize = 72.0F;
textStamp.TextState.FontStyle = FontStyles.Italic;
textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Gray);
textStamp.Opacity = 50;
// Set Stamp ID for text watermark to later identify it
textStamp.setStampId(123456);
// Add stamp to particular page
pdfDocument.Pages[1].AddStamp(textStamp);
dataDir = dataDir + "Add_Text_Watermark.pdf";
// Save output document
pdfDocument.Save(dataDir);
使用C#以編程方式在PDF文件中插入圖像水印
圖像包含許多有意義的信息,因此圖像水印也經(jīng)常在PDF文件中使用。例如,某個組織或機(jī)構(gòu)發(fā)布的文檔可能在頁面背景中帶有其徽標(biāo)。您可以按照以下步驟使用使用C#在PDF文檔中插入圖像水印的功能:
下面的代碼段顯示了如何使用C#在PDF文件中插入圖像水印:
// Load input PDF document
Document pdfDocument = new Document(dataDir + "Input.pdf");
// Access any page of the input PDF
Page testpage = pdfDocument.Pages[1];
// Create image stamp
ImageStamp imageStamp = new ImageStamp(dataDir + "aspose-logo.png");
imageStamp.Background = true;
imageStamp.Height = 300;
imageStamp.Width = 300;
// Center adjust the image watermark based on page dimensions
imageStamp.XIndent = (testpage.PageInfo.Width / 2) - (imageStamp.Width / 2);
imageStamp.YIndent = (testpage.PageInfo.Height / 2) - (imageStamp.Height / 2);
imageStamp.Opacity = 0.5;
// Set stamp id for deleting the watermark later, if required
imageStamp.setStampId(12345678);
// Add stamp to particular page
//pdfDocument.Pages[1].AddStamp(imageStamp);
// Add stamp to each page of PDF
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(imageStamp);
}
dataDir = dataDir + "Add_Image_Watermark.pdf";
// Save output document
pdfDocument.Save(dataDir);
可以在屏幕截圖中注意到PDF文件的居中對齊圖像水印:
使用C#刪除PDF文件中的圖像或文本水印
可以從PDF文檔中刪除/刪除水印。可以使用上面示例中在添加水印時設(shè)置的圖章id屬性來標(biāo)識圖像水印或文本水印。您需要按照以下步驟從PDF文件中刪除水印:
以下代碼顯示了如何從C#中的PDF文件中刪除圖像或文本水印:
// Initialize PdfContentEditor class object PdfContentEditor contentEditor = new PdfContentEditor(); // Call BindPdf method contentEditor.BindPdf(dataDir + "Watermark.pdf"); // Delete watermark using specific id contentEditor.DeleteStampById(12345678); // Save output PDF with removed watermark contentEditor.Save(dataDir + @"Remove_Watermark.pdf");
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn