翻譯|使用教程|編輯:李顯亮|2020-08-14 10:07:20.607|閱讀 517 次
概述:在本系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何在PDF文件中添加圖章。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PDF for .NET是一種高PDF處理和解析API,用于在跨平臺應用程序中執行文檔管理和操作任務。API可以輕松用于生成、修改、轉換、渲染、保護和打印PDF文檔,而無需使用Adobe Acrobat。此外,API還提供PDF壓縮選項,表格創建和操作,圖形和圖像功能,廣泛的超鏈接功能,印章和水印任務,擴展的安全控制和自定義字體處理。
在接下來的系列教程中,將為開發者帶來Aspose.PDF for .NET的一系列使用教程,例如進行文檔間的轉換,如何標記PDF文件,如何使用表單和圖表等等。本文將介紹如何在PDF文件中添加圖章。
>>Aspose.PDF for .NET更新至最新版v20.8,歡迎下載體驗。
使用Aspose.PDF for .NET可以使用ImageStamp類將圖像圖章添加到PDF文件。ImageStamp類提供創建基于圖像的圖章所需的屬性,例如高度,寬度,不透明度等。
要添加圖像圖章:
以下代碼段顯示了如何在PDF文件中添加圖章。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "AddImageStamp.pdf"); // Create image stamp ImageStamp imageStamp = new ImageStamp(dataDir + "aspose-logo.jpg"); imageStamp.Background = true; imageStamp.XIndent = 100; imageStamp.YIndent = 100; imageStamp.Height = 300; imageStamp.Width = 300; imageStamp.Rotate = Rotation.on270; imageStamp.Opacity = 0.5; // Add stamp to particular page pdfDocument.Pages[1].AddStamp(imageStamp); dataDir = dataDir + "AddImageStamp_out.pdf"; // Save output document pdfDocument.Save(dataDir);
將圖像添加為圖章對象時,可以控制圖像的質量。ImageStamp類的Quality屬性用于此目的。它以百分比表示圖像質量(有效值為0..100)。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "AddImageStamp.pdf"); // Create image stamp ImageStamp imageStamp = new ImageStamp(dataDir + "aspose-logo.jpg"); imageStamp.Quality = 10; pdfDocument.Pages[1].AddStamp(imageStamp); pdfDocument.Save(dataDir + "ControlImageQuality_out.pdf");
可以使用PdfPageStamp類將PDF頁面作為圖章添加到PDF文件中。PdfPageStamp類提供創建基于PDF頁面的圖章所需的屬性。可以將任何PDF文件的特定頁面傳遞給PdfPageStamp類的構造函數。為了添加基于頁面的圖章,需要使用必需的屬性創建Document對象和PdfPageStamp對象。之后,可以調用Page的AddStamp方法在PDF中添加圖章。以下代碼段顯示了如何在PDF文件中添加PDF頁面圖章。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "PDFPageStamp.pdf"); // Create page stamp PdfPageStamp pageStamp = new PdfPageStamp(pdfDocument.Pages[1]); pageStamp.Background = true; pageStamp.XIndent = 100; pageStamp.YIndent = 100; pageStamp.Rotate = Rotation.on180; // Add stamp to particular page pdfDocument.Pages[1].AddStamp(pageStamp); dataDir = dataDir + "PDFPageStamp_out.pdf"; // Save output document pdfDocument.Save(dataDir);
可以使用PageNumber Stamp類在PDF文件中添加頁碼戳。PageNumber Stamp類提供了創建基于頁碼的圖章所需的屬性,如格式,邊距,對齊方式,起始編號等。為了添加頁碼圖章,您需要使用必需的屬性來創建Document對象和PageNumber Stamp對象。之后,可以調用Page的AddStamp方法在PDF中添加圖章。還可以設置頁碼標記的字體屬性。以下代碼段顯示了如何在PDF文件中添加頁碼。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "PageNumberStamp.pdf"); // Create page number stamp PageNumberStamp pageNumberStamp = new PageNumberStamp(); // Whether the stamp is background pageNumberStamp.Background = false; pageNumberStamp.Format = "Page # of " + pdfDocument.Pages.Count; pageNumberStamp.BottomMargin = 10; pageNumberStamp.HorizontalAlignment = HorizontalAlignment.Center; pageNumberStamp.StartingNumber = 1; // Set text properties pageNumberStamp.TextState.Font = FontRepository.FindFont("Arial"); pageNumberStamp.TextState.FontSize = 14.0F; pageNumberStamp.TextState.FontStyle = FontStyles.Bold; pageNumberStamp.TextState.FontStyle = FontStyles.Italic; pageNumberStamp.TextState.ForegroundColor = Color.Aqua; // Add stamp to particular page pdfDocument.Pages[1].AddStamp(pageNumberStamp); dataDir = dataDir + "PageNumberStamp_out.pdf"; // Save output document pdfDocument.Save(dataDir);
可以使用TextStamp類在PDF文件中添加文本戳。TextStamp類提供創建基于文本的圖章所需的屬性,例如字體大小,字體樣式和字體顏色等。為了添加文本圖章,需要使用必需的屬性來創建Document對象和TextStamp對象。之后,可以調用Page的AddStamp方法在PDF中添加圖章。以下代碼段顯示了如何在PDF文件中添加文本戳。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "AddTextStamp.pdf"); string annotationText = string.Empty; annotationText = DateTime.Now.ToString("MM/dd/yy hh:mm:ss tt "); // Create text stamp TextStamp textStamp = new TextStamp(annotationText); // Set properties of the stamp textStamp.BottomMargin = 10; textStamp.RightMargin = 20; textStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right; textStamp.VerticalAlignment = VerticalAlignment.Bottom; // Adding stamp on stamp collection pdfDocument.Pages[1].AddStamp(textStamp); DefaultAppearance default_appearance = new DefaultAppearance("Arial", 6, System.Drawing.Color.Black); FreeTextAnnotation textAnnotation = new FreeTextAnnotation(pdfDocument.Pages[1], new Aspose.Pdf.Rectangle(0, 0, 0, 0), default_appearance); textAnnotation.Name = "Stamp"; textAnnotation.Accept(new AnnotationSelector(textAnnotation)); textAnnotation.Contents = textStamp.Value; // TextAnnotation.Open = true; // TextAnnotation.Icon = Aspose.Pdf.InteractiveFeatures.Annotations.TextIcon.Key; Border border = new Border(textAnnotation); border.Width = 0; border.Dash = new Dash(1, 1); textAnnotation.Border = border; textAnnotation.Rect = new Aspose.Pdf.Rectangle(0, 0, 0, 0); pdfDocument.Pages[1].Annotations.Add(textAnnotation); dataDir = dataDir + "AddDateTimeStamp_out.pdf"; // Save output document pdfDocument.Save(dataDir);
可以使用TextStamp類在PDF文件中添加文本戳。TextStamp類提供創建基于文本的圖章所需的屬性,例如字體大小,字體樣式和字體顏色等。為了添加文本圖章,需要使用必需的屬性來創建Document對象和TextStamp對象。之后,可以調用Page的AddStamp方法在PDF中添加圖章。以下代碼段顯示了如何在PDF文件中添加文本戳。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_AsposePdf_StampsWatermarks(); // Open document Document pdfDocument = new Document(dataDir+ "AddTextStamp.pdf"); // Create text stamp TextStamp textStamp = new TextStamp("Sample Stamp"); // Set whether stamp is background textStamp.Background = true; // Set origin textStamp.XIndent = 100; textStamp.YIndent = 100; // Rotate stamp textStamp.Rotate = Rotation.on90; // Set text properties textStamp.TextState.Font = FontRepository.FindFont("Arial"); textStamp.TextState.FontSize = 14.0F; textStamp.TextState.FontStyle = FontStyles.Bold; textStamp.TextState.FontStyle = FontStyles.Italic; textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Aqua); // Add stamp to particular page pdfDocument.Pages[1].AddStamp(textStamp); dataDir = dataDir + "AddTextStamp_out.pdf"; // Save output document pdfDocument.Save(dataDir);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn