0
votes

I use the PDFsharp project to merge many pdf documents into one file which works perfectly and smooth. But I also need to call this method from classic ASP server pages.

Works as well, but the strange thing is handling the param values by calling the method.

C# definition:

public void MergeMultiplePDF(object[] files, string outFile)
{
  // note: get an array from vbscript, so files need to be a object array, not string array.

  // Open the output document
  PdfDocument outputDocument = new PdfDocument();

  // Iterate files
  foreach (string file in files)
  {
    // Open the document to import pages from it.
    PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);

    // Iterate pages
    int count = inputDocument.PageCount;
    for (int idx = 0; idx < count; idx++)
    {
      // Get the page from the external document...
      PdfSharp.Pdf.PdfPage page = inputDocument.Pages[idx];
      // ...and add it to the output document.
      outputDocument.AddPage(page);
    }
  }

  // Save the document...
  outputDocument.Save(outFile);
  outputDocument.Dispose();
}

Call from classic ASP:

Dim l_sPath : l_sPath = "D:\test\"
oPDF.MergeMultiplePDF Array(l_sPath & "sample1.pdf", l_sPath & "sample2.pdf", l_sPath & "sample3.pdf" ), l_sPath & "output.pdf"

Works fine, as array is a object VARIANT and I handle the array inside the .NET class.

But if I have a "dynamic" array in classic ASP I get the usual error that the argument is not correct like you can find in many posts here...

Sample:

Dim myFiles(10)
For i = 0 To UBound(myFiles)
  myFiles(i) = "test" & i & ".pdf"
Next
oPDF.MergeMultiplePDF myFiles, l_sPath & "output.pdf"

This run into an argument error.

My workaround:

oPDF.MergeMultiplePDF Split(Join(myFiles,","),","), l_sPath & "output.pdf"

Then it works.

Both are objects of type Array().

So anyone has a clue why this is handled different?

2
They one who down votes can tell me why?YvesR
The code you post should work VarType(Array...)=VarType(myFiles)=VarType(Split...)=8204 (array of variant) . Are you sure the problem doesn't come from your C# implementation?Simon Mourier
@SimonMourier VarType() of both myFiles and Split/Join of myFiles are 8204, that is correct. But set myFiles as param like I wrote in my sample I get an error 800a0005 (invalid argument). This is why I post my question to find a answer for this strange situation. In c# it is just defined as object[], so this should't matter and the call fails, not the code inside the method.YvesR
I understand what you say, but I've tested it with a .VBS file and it works for me (check this out: pastebin.com/q4eZsKbU), and I don't see why it shouldn't. So is the shown code really the one that fails?Simon Mourier
I added my C# function, maybe that helps. But if the code produce an error I would get an exception and not an invalid argument error. I moved my code from asp classic to vbscript.vbs file as well, same error there. I try to reduce my code (take your sample) to investigate ...YvesR

2 Answers

0
votes

The code you post should work as in VBScript

VarType(Array(...)) = VarType(myFiles) = VarType(Split(...)) = 8204

8204 = 0x200C, which is VT_ARRAY | VT_VARIANT which translate indeed to object[] in .NET

So, the actual code is different than the sample shown here.

0
votes

Defining a dynamic array in ASP like ReDim myFiles(max_count) where max_count is a numeric value causes the problem. Dim myFiles(10) as test for example work like Simon tested as well.

@Simon, set your comments as an answer please so I can accept it.