I am trying to save a file from the Document Viewer after applying a watermark.
My objective is to load the document into the Document Viewer, apply a watermark, and then save the file to a specified path.
I attempted to achieve this by loading the document, obtaining a stream, and copying the stream to create a new file, but unfortunately, this approach did not work.
I have also reviewed the documentation but couldn't find any methods or guidelines for performing this operation.
Here is a sample of the code I have tried:
var documentViewer = new DocumentViewer
{
Width = 960,
Height = 720,
Resizable = true,
Document = FilePath,
DocumentOptions = new DocumentOptions
{
Format = DocumentFormat.Pdf,
Watermarks = {
new TextWatermark
{
Text = "Contoso",
Rotation = -45,
Opacity = 50,
FontColor = Color.Red,
Width = 50,
Height = 50,
SizeIsPercentage = true
}
}
}
};
documentViewer.Hidden = true;
using (var fileStream = File.Create("D:\\PDF_POC\\Updated\\file.pdf"))
{
using (var stream = documentViewer.Document.OpenWrite())
{
//stream.Seek(0, SeekOrigin.Begin);
//stream.CopyTo(fileStream);
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = stream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, len);
}
}
}
Let me know if you'd like further adjustments!