read from filestream and save back to filestream

PDF-XChange Editor SDK for Developers

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

read from filestream and save back to filestream

Post by chavas »

Hi,

I am using PDF xchange to watermark my files which are fetched from database in memory stream(C#). I am working without any user interface so no pdfxchange editor is there on my window. This process is working in background service. objFile.FileMemoryStream is the object which contains file in memorystream. I want to apply watermark and save it back to either same memory stream or different. I am able to save the file back to a physical location(as you can see in this code) but not to memory stream. I have checked your FULLDEMO project and I am using IStreamWrapper class but could not solve my problem. So I need some help with the code here on how to save the file back to memory stream.

Here is my code-
PDFXEdit.PXV_Inst m_Inst;
PDFXEdit.IPXC_Inst m_pxcInst;

InitPDFControl(ref m_Inst,ref m_pxcInst);


var tempGuid = Guid.NewGuid().ToString() + "/";
Directory.CreateDirectory(Server.MapPath(@"Temp/" + tempGuid));
string strfilepath = Server.MapPath(@"Temp/" + tempGuid + "/" + TempFileName);
PDFXEdit.IAFS_Inst fsInst = (PDFXEdit.IAFS_Inst)(m_Inst.GetExtension("AFS"));
PDFXEdit.IAFS_Name impPath = fsInst.DefaultFileSys.StringToName(strfilepath);
IStreamWrapper srcIStream = new IStreamWrapper(objFile.FileMemoryStream);

PDFXEdit.IPXC_Inst pxcInst = (PDFXEdit.IPXC_Inst)(m_Inst.GetExtension("PXC"));
PDFXEdit.IPXC_Document resDoc = pxcInst.OpenDocumentFrom(srcIStream, null);

int nID = m_Inst.Str2ID("op.document.addWatermarks", false);
PDFXEdit.IOperation Op = m_Inst.CreateOp(nID);
var input = Op.Params.Root["Input"];
input.v = resDoc;
PDFXEdit.ICabNode options = Op.Params.Root["Options"];
options["PagesRange.Type"].v = "All";
options["Text"].v = "My Watermark " + DateTime.Now.Date.ToString("dd-MMM-yyyy"); ;
options["Rotation"].v = 33;
Op.Do();
resDoc.WriteTo(impPath); I want to save file to memory stream
resDoc.Close();


Rgds
Charu
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: read from filestream and save back to filestream

Post by Sasha - Tracker Dev Team »

Hello chavas,

The IStreamWrapper should help with this - what is the problem with it exactly?

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: read from filestream and save back to filestream

Post by chavas »

Hi,

I need help with the code..what to write to save the file back to the same meorystream.
I tried this-
resDoc.WriteTo(srcIStream);

but throws this exception-
System.Runtime.InteropServices.COMException (0x82141009): Exception from HRESULT: 0x82141009
at PDFXEdit.IPXC_Document.WriteTo(Object pDest, IProgressMon pProgress, UInt32 nFlags)
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: read from filestream and save back to filestream

Post by Sasha - Tracker Dev Team »

Hello chavas,

Please read the forum rules at the top of this page and use the FomatHRESULT method to see what the error is.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: read from filestream and save back to filestream

Post by chavas »

Hi Alex,

My bad...sorry abt that...here is the exception that is returned by FormatHRESULT()
var auxInst = (PDFXEdit.IAUX_Inst)m_Inst.GetExtension("AUX");
var msg= auxInst.FormatHRESULT(ex.HResult);


the error is- Error [Pdf Structure Library]: Invalid object structure.

Rgds
Charu
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: read from filestream and save back to filestream

Post by Sasha - Tracker Dev Team »

Hello chavas,

Hmm, have you tried other files? Maybe the file is corrupted or something like that.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: read from filestream and save back to filestream

Post by chavas »

Hi Alex,

The file is not corrupt. I have tried various files but result is same.

Rgds
Charu
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: read from filestream and save back to filestream

Post by Sasha - Tracker Dev Team »

Hello chavas,

Can you please make a small sample project that shows the problem that we can debug?

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: read from filestream and save back to filestream

Post by chavas »

Hi Alex,

On further digging I realised that this problem is only with registration free installation of pdfxchange editor sdk. (using manifest files).May be there are some dependent files that I have missed. Here is a sample project for you. I have removed my license keys from this project.
link to my sample project- http://drops.docsvault.com/plJA3U

Rgds
Charu
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: read from filestream and save back to filestream

Post by chavas »

Hi Alex,

Any updates on this issue? Let me know if you need any more information from me,


Rgds
User avatar
Paul - Tracker Supp
Site Admin
Posts: 6900
Joined: Wed Mar 25, 2009 10:37 pm
Location: Chemainus, Canada
Contact:

Re: read from filestream and save back to filestream

Post by Paul - Tracker Supp »

HI chavas,

Alex took a look and has indicated that this requires deeper investigation. I believe he will prioritize this as best he can and get back to you here on this thread.

I hope that this will be possible before the release of V9 but cannot say for sure when he will be able to get to this.
Best regards

Paul O'Rorke
Tracker Support North America
http://www.tracker-software.com
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: read from filestream and save back to filestream

Post by chavas »

Hi,

Thank you..I am eagerly waiting for your response. We are also planning our next version and the release is stuck because of this issue.

Best Rgds
Charu
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: read from filestream and save back to filestream

Post by Sasha - Tracker Dev Team »

Hello chavas,

Investigated this one even deeper and also spoke to my colleague about the error that occurs in the inner code.
1) If you want to write a modified file back into the stream that you've allocated before, you want be able to do that in many cases, as the resulting file can become bigger after the modification (an you don't know the precise resulting size at this point).
2) The IStreamWrapper is a fixed memory wrapper - meaning it's not meant to be rewritten.
3) You can't write to the same place that you are reading the PDF file from. In case of files, we detect the rewrite and first write into the temporary file and then we rewrite the original. In case of a stream (even if it can modify it's size) you will have to manually set the DocWriteFlag_UseTempFile that will result in temporary file creation.

So basically you have two ways of doing this:
1) Write to temporary file first
2) Write into the different memory stream that can do memory realloc.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
chavas
User
Posts: 141
Joined: Tue Mar 15, 2016 12:21 pm

Re: read from filestream and save back to filestream

Post by chavas »

Hi Alex,

We can work with the second option- Write into the different memory stream that can do memory realloc.
can you guide me how to go about it? Do you have some code sample for me to study?
We are working with vb.net/c# (visual studio2019).

Rgds
Charu
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: read from filestream and save back to filestream

Post by Sasha - Tracker Dev Team »

Hello chavas,

Well we don't have a sample for that, and as this is not a part of the SDK support I can only suggest searching the stackoverflow or codeproject for samples to create such a class for a stream.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply