Add watermark to pdf files- problem with using xor encrypt

first, i used the api to add watermark to a pdf file,

i got the pdf with watermark successful,

then, i encrypted this pdf using xor encrypt.

when i decrypt the file, appeared this problem in attachment. Weird, the watermark text have changed, and the font was incorrect too.I want to know what should i do?I will appreciate any reply.



ps: the watermark text is leo@watermarktwo.onmicrosoft.com,and not all the file can reproduce this problem.I have upload the file in attachment.

I found the real reason, when i save the pdfDocument to Stream, i got the bad stream.
if i save to a filepath, i got the correct stream.

Document pdfDocument = new Document(fileStream);
TextStamp textStamp = new TextStamp(contentStr);
pdfDocument.AddStamp(textStamp);
pdfDocument.Save(fileStream);//save to stream
pdfDocument.Save(filePath);//save to filePath

ps: I could reproduce this only using the attachment file, I don’t know this file have what special.Finally, I saved it to a filepath, and then get the stream from that file.It’s a little bit of a problem for me.

gengzhi1109:
I found the real reason, when i save the pdfDocument to Stream, i got the bad stream.
if i save to a filepath, i got the correct stream.

Document pdfDocument = new Document(fileStream);
TextStamp textStamp = new TextStamp(contentStr);
pdfDocument.AddStamp(textStamp);
pdfDocument.Save(fileStream);//save to stream
pdfDocument.Save(filePath);//save to filePath

ps: I could reproduce this only using the attachment file, I don’t know this file have what special.Finally, I saved it to a filepath, and then get the stream from that file.It’s a little bit of a problem for me.

Hi Dalonggeng,

Thanks for contacting support and sorry for the delayed response.

I have tested the scenario using Aspose.Pdf for .NET 10.1.0 where I have used following code snippet and I am unable to notice any issue. Can you please share some details regarding your working environment.

[C#]

Document pdfDocument = new Document(“c:/pdftest/%5bMS-OXPROTO%5d.pdf”);<o:p></o:p>

TextStamp textStamp = new TextStamp("contentStr");

pdfDocument.Pages[1].AddStamp(textStamp);

MemoryStream ms = new MemoryStream();

pdfDocument.Save(ms);

Console.WriteLine(ms.Length);

Hi Dalonggeng,

Can you please share some code snippet which can help us in replicating the above stated issue. We are sorry for your inconvenience.

Thanks for your reply.
Only need to pay attention to my second email, I think your code can reproduce my problem completely. It’s
nothing different from my code and yours.I just want to know could you test if the stream is what we want by the following code?Could you check the pdf file named abc.pdf if the watermark is correct.I had tested with the Aspose.Pdf for .NET 10.1.0, but i still got the wrong watermark.
Thanks again for your help.
[C#]

Document pdfDocument = new Document(“c:/pdftest/%5bMS-OXPROTO%5d.pdf”);

TextStamp textStamp = new TextStamp(“contentStr”);

pdfDocument.Pages[1].AddStamp(textStamp);

MemoryStream ms = new MemoryStream();

pdfDocument.Save(ms);

MemoryStream outStream = new MemoryStream();
using (Stream s = System.IO.File.OpenWrite(“C:\abc.pdf”))
{
byte[] buffer = new byte[2046];
int count = 1;
while ((count = outStream.Read(buffer, 0, buffer.Length)) != 0)
{
if (count < 2046)
{
s.Write(buffer, 0, count);
}
else
{
s.Write(buffer, 0, buffer.Length);
}
}
}

Hi there,


Thanks for your feedback. We have tested your code, it seems you are trying to write outStream to file instead ms memory stream, containing the original document. I have tested your code with a slight change and its working fine. If there is difference in you issue and our understanding then please share a sample console project here. We will look into it and guide you accordingly.


Document pdfDocument = new Document(myDir+"%5bMS-OXPROTO%5d.pdf");<o:p></o:p>

TextStamp textStamp = new TextStamp("contentStr");

pdfDocument.Pages[1].AddStamp(textStamp);

MemoryStream ms = new MemoryStream();

pdfDocument.Save(ms);

// MemoryStream outStream = new MemoryStream();

using (Stream s = System.IO.File.OpenWrite(myDir+"abc_watermark.pdf"))

{ byte[] buffer = new byte[2046];

int count = 1;

while ((count = ms.Read(buffer, 0, buffer.Length)) != 0)

{ if (count < 2046)

{ s.Write(buffer, 0, count); }

else

{ s.Write(buffer, 0, buffer.Length); }

}

}


We are sorry for the inconvenience caused.


Best Regards,

Thanks for your reply.

I use this test code , it is ok.
But, I think something wrong in my code caused this problem. I will do some testing and review my code.When I have some progress, i will tell you.

Thanks,

Hi Dalonggeng,


Please continue using our API and in the event of any further query, please feel free to contact.