Core Api Attach File

A forum for questions or concerns related to the PDF-XChange Core API SDK

Moderators: TrackerSupp-Daniel, Tracker Support, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, 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
edvschu
User
Posts: 127
Joined: Fri Jun 25, 2010 6:54 am

Core Api Attach File

Post by edvschu »

Hello,

i want attach a file to my pdf. Your documentation will help not really, because the interfaces are not really describe. Help needed.

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Hello edvschu,

Code: Select all

LIBNAME.IPXC_NameTree pTree =  pDoc.GetNameTree("EmbeddedFiles");
string sFilePath = "D:\\YourFile.txt";
LIBNAME.IPXC_FileSpec pFS = pDoc.CreateEmbeddFile(sFilePath);
LIBNAME.IPXC_EmbeddedFileStream pEFS = pFS.EmbeddedFile;
pEFS.UpdateFromFile2(sFilePath);
pTree.Add("YourFile", pFS.PDFObject);
pDoc.WriteToFile("D:\\YourNewFile.pdf");
HTH
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
edvschu
User
Posts: 127
Joined: Fri Jun 25, 2010 6:54 am

Re: Core Api Attach File

Post by edvschu »

Hi Sasha,

Code works. Create Support. Thanks a lot.

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

:wink:
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
edvschu
User
Posts: 127
Joined: Fri Jun 25, 2010 6:54 am

Re: Core Api Attach File

Post by edvschu »

With Property AFRelationship of IPXC_FileSpec the Relationships for associated files are described. Which Values stands for Source, Data, Alternative ... ?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

To work with atoms use

Code: Select all

//PDFXEdit.IPXS_Inst pSInt
pFS.AFRelationship = pSInt.StrToAtom("Alternative");
HTH
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
edvschu
User
Posts: 127
Joined: Fri Jun 25, 2010 6:54 am

Re: Core Api Attach File

Post by edvschu »

It seems to work. I have implemented it in VB.NET. Get a value of 6893.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17822
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Core Api Attach File

Post by Tracker Supp-Stefan »

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

If you have methods that take atom as an argument, you should use StrToAtom. That will convert atom string value (that is given in the PDF specification) into a number that is used by our inner code.

HTH
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
edvschu
User
Posts: 127
Joined: Fri Jun 25, 2010 6:54 am

Re: Core Api Attach File

Post by edvschu »

I try this:

Code: Select all

MsgBox(pxsInst.StrToAtom("Alternative"))
MsgBox(pxsInst.HasAtom("Alternative")
HasAtom returns True only StrToAtom is executed before. Why?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

The thing is that we don't have all of the atoms registered when the application starts. These few atoms are registered by the first usage of the StrToAtom function or by manual registering of the atom. But these atoms "live" only in the IPXS_Inst lifespan so do be careful with such atoms.

HTH
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
edvschu
User
Posts: 127
Joined: Fri Jun 25, 2010 6:54 am

Re: Core Api Attach File

Post by edvschu »

I attach a XML File on a PDF, but no MIME Type text/xml is included. How can resolve this?

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Hi, edvschu.

This is how you can change the Mime type:

Code: Select all

			//Getting structure document
			IPXS_Document pSDoc = pDoc.CosDocument;
			{
				//Locking document for writing
				pSDoc.LockDocumentExclusive();
				//Setting MimeType (Subtype)
				pEFS.PDFObject.Dict_SetName("Subtype", "text/xml");
				//Unlocking document
				pSDoc.UnlockDocumentExclusive();
			}
HTH
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
edvschu
User
Posts: 127
Joined: Fri Jun 25, 2010 6:54 am

Re: Core Api Attach File

Post by edvschu »

You're the Best, Sasha. Thanks a lot :D .
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

:wink:
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
edvschu
User
Posts: 127
Joined: Fri Jun 25, 2010 6:54 am

Re: Core Api Attach File

Post by edvschu »

Hello,

the PDF with attachment does not meet the requirements that I need.

The relationship of attachment is set alternative. But in PDF the object of the relationship, so the data structure to
which the relationship is to apply missing. Specifically, the Catalog object (root object) must have an /AF entry that
must point to an array of all "Associated Files". The appendix includes a sample PDF and a picture that explains what I mean.

Is there a way to implement it?

Best Regards,
edvschu
Attachments
Sample.zip
(32.73 KiB) Downloaded 320 times
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Hi edvschu,

I need a little clarification.
Currently your sample document has such a structure:
Image
Do you need to replace this structure with yours (provided in sample)? And where do you want your Catalog dictionary to be?
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
edvschu
User
Posts: 127
Joined: Fri Jun 25, 2010 6:54 am

Re: Core Api Attach File

Post by edvschu »

Hi Sasha,

Sorry. I have delivered only a partial section of the required specification. In the appendix you can find the full specification.
Thus, the PDF will be declared as valid, must meet the required specification.

Reference to the structure that you have posted, i see the attachment is completely embedded differently.

Is there a way to meet the required specification?

Regards,
edvschu
Attachments
Specification.zip
(63.13 KiB) Downloaded 318 times
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

This code will take all of the items from the EmbeddedFiles tree and copy them to the new AF array in the root variant:

Code: Select all

		private void AddAFArrayToRoot(LIBNAME.IPXC_Document pDoc, LIBNAME.IPXC_Inst pInst)
		{
			if (pDoc == null)
				return;

			//Getting IPXS_Inst if you don't have it
			LIBNAME.IPXS_Inst pSInt = (LIBNAME.IPXS_Inst)pInst.GetExtension("PXS");
			LIBNAME.IPXS_Document pSDoc;
			LIBNAME.IPXS_PDFVariant pRootVar;
			LIBNAME.IPXC_NameTree pNameTree;
			do
			{
				//Getting root variant
				pSDoc = pDoc.CosDocument;
				if (pSDoc == null)
					break;
				pSDoc.LockDocumentExclusive();
				do
				{
					pRootVar = pSDoc.Root;
					if (pRootVar == null)
						break;
					LIBNAME.IPXS_PDFVariant pAFVar = pSInt.NewVar_Array(0, pSDoc, false);
					if (pAFVar == null)
						break;
					uint nAFAtom = pSInt.StrToAtom("AF");
					pRootVar.Dict_SetA(nAFAtom, pAFVar, false);
			
					pNameTree = pDoc.GetNameTree("EmbeddedFiles");
					if (pNameTree == null)
						break;
					uint nCount = pNameTree.Count;

					for (uint i = 0; i < nCount; i++)
					{
						LIBNAME.IPXS_PDFVariant pItemVar;
						string sName;
						pNameTree.Item(i, out sName, out pItemVar);
						if (pItemVar == null)
							continue;
						pAFVar.Arr_Insert(pItemVar, i + 1, true);
					}
				} while (false);
				pSDoc.UnlockDocumentExclusive();
				pDoc.WriteToFile("D:\\YourNewFile.pdf");
		
			} while (false);
		}
HTH :wink:
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
edvschu
User
Posts: 127
Joined: Fri Jun 25, 2010 6:54 am

Re: Core Api Attach File

Post by edvschu »

It works. Incredible support. Thanks :D.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17822
Joined: Mon Jan 12, 2009 8:07 am
Location: London
Contact:

Re: Core Api Attach File

Post by Tracker Supp-Stefan »

:)
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Hi Sasha,

it would be a great help for me, if you could provide a small sample project, that uses your famous code to attach a file to an existing pdf.
How do I regfree include the needed reference to PDFXCoreAPI.dll to my solution?

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Hello cew,

What coding language are you using?
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Hi Sasha,

I use c#.
Sasha - Tracker Dev Team wrote:Hello cew,

What coding language are you using?
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Hi cew,

So the problem is how to add PDFXCoreAPI.dll to your project as reference?
There is a video sample on how to attach the EditorSDK dll as reference - the Core API SDK dll should be added in the similar way.
https://www.youtube.com/watch?v=j3I0oteI13s

HTH
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Sorry for the missing information. :(
I have to use the lib regfree in a WPF project, not in WinForms.

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Hello cew,

Here's your sample :wink:

Cheers,
Alex
Attachments
WpfSimpleApp.rar
(163.58 KiB) Downloaded 304 times
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Hi Sasha,

great example, but how do I load an existing PDF instead of creating a new one by code to attach a document?

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Hello cew,

If all of your documents don't have a password specified - then you can open them like this:
https://forum.pdf-xchange.com/ ... 907#p94907
If you need to handle password protected documents - then use this:
https://forum.pdf-xchange.com/ ... 913#p94913

HTH,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Hello Sasha,

the following code looks fine, but how do I instantiate pInst?

Code: Select all

private void OpenDocumentFromFileSample(string sFilePath, LIBNAME.IPXC_Inst pInst)
{
	LIBNAME.IPXC_Document pDoc = pInst.OpenDocumentFromFile(sFilePath, null, null, 0, 0);
}
best regards,
cew
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Hello cew,

Look in the sample project I post you.

Code: Select all

public PDFXCoreAPI.PXC_Inst g_Inst = new PDFXCoreAPI.PXC_Inst();
And look how and where was it initialized and destroyed.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Hi Sasha,

Using the following method works fine, but only the first time.

Code: Select all

 public static void AttachFileToDocument(string document, string attachment, string pdfXChangeCoreApiKey) {
      if (!File.Exists(document)) {
        throw new FileNotFoundException(document);
      }
      if (!File.Exists(attachment)) {
        throw new FileNotFoundException(attachment);
      }

      PDFXCoreAPI.PXC_Inst g_Inst = new PDFXCoreAPI.PXC_Inst();
      g_Inst.Init(pdfXChangeCoreApiKey);
		
      var attachmentDisplayName = Path.GetFileName(attachment);

      try {
        PDFXCoreAPI.IPXC_Document pDoc = g_Inst.OpenDocumentFromFile(document, null, null, 0, 0);

        PDFXCoreAPI.IPXC_NameTree pTree = pDoc.GetNameTree("EmbeddedFiles");
        PDFXCoreAPI.IPXC_FileSpec pFS = pDoc.CreateEmbeddFile(attachment);
        PDFXCoreAPI.IPXC_EmbeddedFileStream pEFS = pFS.EmbeddedFile;
        
        pEFS.UpdateFromFile2(attachment);
        pTree.Add(attachmentDisplayName, pFS.PDFObject);

        pDoc.WriteToFile(document, null, 0);

        if (pDoc != null)
          pDoc.Close();
        pDoc = null;
     
      } finally{
	  g_Inst.Finalize();
	  g_Inst = null;
      }
    }
The second call leads to an exception:

Code: Select all

System.ArgumentException was unhandled
  HResult=-2147024809
  IsTransient=false
  Message=Der Wert liegt außerhalb des erwarteten Bereichs.
  StackTrace:   bei PDFXCoreAPI.IPXC_Inst.OpenDocumentFromFile(String lpszFileName, IPXC_DocAuthCallback pAuthCallback, IProgressMon pProgressMon, UInt32 nFlags, UInt32 nRestrictPerms)
What's wrong? :cry:

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Hello cew,

Check this thread:
https://forum.pdf-xchange.com/ ... 67&t=24344
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Great, it works!

At least there are two questions:
1. What do I have to do to get just the filename as name of the attachment instead of the complete filepath?

2. If I open an attached file, and close immidiatly the programm, started by the PDFXChange Viewer, the viewer prompts a hint, that the PDF has been changed. If I choose YES, I get an an exception, that the file could not be saved, because it is used by another process.
a) Why is the PDF detected as changed?
b) Why is the original pdf in use?


This behaviour only occurres, if a file has been attached by the api. Even if I attach another file to that pdf just using the viewer.

Best regards,
cew
Attachments
attachment.zip
(15.65 KiB) Downloaded 277 times
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Hi Sasha,

how can I use the core api regfree in our application?

I just found hints to manifests for the editor api.

Could you please enhance the sample with the necessary manifest or whatever is needed to lead to a regfree use of the core api?

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

1) The name of the Embedded file dictionary is specified when you do this:

Code: Select all

pTree.Add("Test", pFS.PDFObject);
The name that would be shown in the attachments view is specified here:

Code: Select all

PDFXCoreAPI.IPXC_FileSpec pFS = pDoc.CreateEmbeddFile("Test.tsst");
These code was taken from sample that I've sent you.
Image
Image
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Ok, it works. The sample has a little bug...

Code: Select all

string filePath = System.IO.Directory.GetParent(System.Environment.CurrentDirectory).Parent.FullName + "\\Test.txt";
PDFXCoreAPI.IPXC_FileSpec pFS = pDoc.CreateEmbeddFile(filePath);
But now i know what the method CreateEmbeddFile is for :-)

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Well the sample did not contain a bug - I've just modified it and screenshoted the code for better understanding - that' all =)
I did not manage to reproduce the modified file behavior that you've described in the post above.
And what did you mean "use the core api regfree in our application"?
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Hi Sasha,
Sasha - Tracker Dev Team wrote: And what did you mean "use the core api regfree in our application"?
We want to redistribute the core api using the "Registration-Free(Click-Once) Method" described in

https://sdkhelp.pdf-xchange.com/vie ... stribution

This page just shows examples for the PDFXEditCore.
How has the manifest for the PDFXCoreAPI.x86.dll to look like?

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Hello cew,

Maybe it's better to use "Using the C-export Method" described in the same link that you've post. Because you use the Core API and it does not have a manifest. So using LoadLibrary in your case will be better.
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Do I need a mapping class like Trackers XCPRO40Declares.cs for the C-Export Method

// Initialize PDF object
[DllImport("xcpro40")]
public static extern int PXCp_Init(out int pObject, string Key, string DevCode);
// Deinitialize PDF Object
[DllImport("xcpro40")]
public static extern int PXCp_Delete(int pObject);
...

If so, could you provide that?

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Hello cew,

We've updated the sample project for the manifest use. Note that the manifest file was added and modified and also, the project settings were updated - check on that.
All you need to do now is copy your dll to the exe file and it will work.

HTH,
Alex
Attachments
WpfSimpleApp.rar
(686.47 KiB) Downloaded 295 times
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
cew
User
Posts: 213
Joined: Tue Feb 01, 2011 8:14 am

Re: Core Api Attach File

Post by cew »

Great support, thank you!!!

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

Re: Core Api Attach File

Post by Sasha - Tracker Dev Team »

Glad that worked for you :wink:
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply