public MainWindow() { this.InitializeComponent(); this.document = new DocumentViewModel(); this.DataContext = this.document; this.document.PropertyChanged += this.DocumentOnPropertyChanged; Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image star = new Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image("star", "Resources//star.png", true); Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image smile = new Apitron.PDF.Kit.FixedLayout.Resources.XObjects.Image("smile", "Resources//smile.png", true); this.resourceManager = new ResourceManager(); resourceManager.RegisterResource(star); resourceManager.RegisterResource(smile); }
/// <summary> /// Adds a watermark on all pages of the specified document. /// </summary> /// <param name="doc">Document to process.</param> /// <param name="imageDataStream">Stream containing image data to be used as watermark. Caller is reposible for closing it.</param> /// <param name="outputStream">Output stream, optional. If not set, incremental save will be performed. Caller is responsible for closing it.</param> public static void Watermark(this FixedDocument doc, Stream imageDataStream, Stream outputStream = null) { if (doc == null) { throw new ArgumentNullException(nameof(doc)); } if (imageDataStream == null) { throw new ArgumentNullException(nameof(imageDataStream)); } // create and register image resource string imageResourceId = Guid.NewGuid().ToString("N"); Image imageResource = new Image(imageResourceId, imageDataStream); doc.ResourceManager.RegisterResource(imageResource); // register watermark XObject it will be referenced in all watermark annotations FixedContent watermarkContent = new FixedContent(Guid.NewGuid().ToString("N"), new Boundary(imageResource.Width, imageResource.Height)); watermarkContent.Content.AppendImage(imageResourceId, 0, 0, imageResource.Width, imageResource.Height); doc.ResourceManager.RegisterResource(watermarkContent); // add annotations to every page foreach (Page page in doc.Pages) { WatermarkAnnotation watermarkAnnotation = CreateWatermarkAnnotation(page.Boundary.MediaBox, watermarkContent); doc.ResourceManager.RegisterResource(watermarkAnnotation.Watermark); page.Annotations.Add(watermarkAnnotation); } // save to specified file or do an incremental update if (outputStream != null) { doc.Save(outputStream); } else { doc.Save(); } }