Converting from PDF Kit to PDF

I initially developed a solution to merge multiple pdf documents in C# using PDF kit.I used the following method:

private void DisplayApplication(Stream[] psaPDFs)
{
MemoryStream msPDFout = new MemoryStream();
//create a new PDF Kit object
PdfFileEditor pfe = new PdfFileEditor();
//concatenate the Attachments
pfe.Concatenate(psaPDFs, msPDFout);
//make a new Byte[] to pass to the output screen
int intLength = (int)msPDFout.Length;
byte[] baPDFout = new byte[intLength];
msPDFout.Read(baPDFout, 0, intLength);
//Context.Session.Add("ApplicationPDF", baPDFout);
//Context.Response.Redirect("DisplayPDF.aspx", false);
Response.ContentType = "Application/pdf";
Context.Response.OutputStream.Write(baPDFout, 0, intLength);
}
I think that Aspose.Pdf.Facades.Concatenate() doesn't support the empty memory stream. Please tell me if this is true and what method/syntax I should use to to concatenate the pdfs into a single document.

Thanks,

Geoffrey

Hi Geoffrey,

Thanks for using our products.

I tested the scenario with below mentioned source code using Aspose.Pdf for .NET version 6.8 and unable to notice any problem with Aspose.Pdf.Facades.Concatenate() method. Kindly try to use the latest version of Aspose.Pdf for .NET v6.8 and check if it fits your need.

[C#]

private void DisplayApplication(Stream[] psaPDFs)
{
MemoryStream msPDFout = new MemoryStream();
PdfFileEditor pfe = new PdfFileEditor();
pfe.Concatenate(psaPDFs, msPDFout);
byte[] baPDFout = msPDFout.ToArray();
FileStream output = new FileStream(@"d:\pdffiles\merged_output.pdf", FileMode.Create, FileAccess.Write);
output.Write(baPDFout, 0, baPDFout.Length);
output.Close();
}

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,

I have upgraded to Aspose.Pdf for .NET v6.9 and still get the following error:

File does not begin with '%PDF-'.

The issue is very much like the problems described in 355299 in reply to 347958355299 in reply to 347958

BTW... the paste function does not work in this editor.

I am beginning to lose confidence in Aspose.

Please respond as soon as possible.

PS: I (Like Robert Ruggio in thread 347958) am using MEMORY STREAMS NOT FILE STREAMS.

Hi Geoffrey,


Thanks for sharing the details and sorry for replying you late.

Please note that I have further investigated this problem and I am able to notice the same problem File does not begin with ‘%PDF-’” during PDF concatenation. For the sake of correction, I have logged this problem as PDFNEWNET-33679 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are really sorry for this inconvenience.

Hi Geoffrey,


Thanks for your patience.

We have further investigated this issue and have found that the reason of this problem can be the fact that the Seek call to memory stream msPDFout object is missing which eventually causes empty resultant data array (baPDFOut). Please try using the following code snippet with latest release version of Aspose.Pdf for .NET 7.0.0 and in case you still face the similar problem or you have any further query, please feel free to contact.


[C#]

FileStream[] inputStreams = new FileStream[2];<o:p></o:p>

inputStreams[0] = new FileStream("D:\\pdftest\\OutputPDF.pdf", FileMode.Open);

inputStreams[1] = new FileStream("D:\\pdftest\\input (3).pdf", FileMode.Open);

MemoryStream msPDFout = new MemoryStream();

//create a new PDF Kit object

PdfFileEditor pfe = new PdfFileEditor();

//concatenate the Attachments

pfe.Concatenate(inputStreams, msPDFout);


//make a new Byte[] to pass to the output screen

int intLength = (int)msPDFout.Length;

byte[] baPDFout = new byte[intLength];

//move pointer of the stream to begin

msPDFout.Seek(0, SeekOrigin.Begin);

msPDFout.Read(baPDFout, 0, intLength);

//Context.Session.Add("ApplicationPDF", baPDFout);

//Context.Response.Redirect("DisplayPDF.aspx", false);

Response.ContentType = "Application/pdf";

Context.Response.OutputStream.Write(baPDFout, 0, intLength);


Besides this, if you need to display the resultant PDF in client's browser, you may simply call pfe.Concatenate(inputStreams,Response); overloaded method where you can simply pass Response object as an argument and it will save you from storing the resultant file in MemoryStream object, create a Byte array and then pass the contents of Byte array to Response object. When using this approach, the code will be simplified as shown below


[C#]

FileStream[] inputStreams = new FileStream[2];

inputStreams[0] = new FileStream("D:\\pdftest\\OutputPDF.pdf", FileMode.Open);

inputStreams[1] = new FileStream("D:\\pdftest\\input (3).pdf", FileMode.Open);

MemoryStream msPDFout = new MemoryStream();

//create a new PDF Kit object

PdfFileEditor pfe = new PdfFileEditor();

//concatenate the Attachments

pfe.Concatenate(inputStreams,Response);


In case you still need to use MemoryStream, then the more efficient way is not to create a new array but use MemoryStream.ToArray(). Please take a look over following code snippet.


[C#]

FileStream[] inputStreams = new FileStream[2];

inputStreams[0] = new FileStream("D:\\pdftest\\OutputPDF.pdf", FileMode.Open);

inputStreams[1] = new FileStream("D:\\pdftest\\input (3).pdf", FileMode.Open);

MemoryStream msPDFout = new MemoryStream();

//create a new PDF Kit object

PdfFileEditor pfe = new PdfFileEditor();

//concatenate the Attachments

pfe.Concatenate(inputStreams, msPDFout);

//make a new Byte[] to pass to the output screen

int intLength = (int)msPDFout.Length;

// Remove this code portion

//byte[] baPDFout = new byte[intLength];

//move pointer of the stream to begin

//msPDFout.Read(baPDFout, 0, intLength);

//Context.Session.Add("ApplicationPDF", baPDFout);

//Context.Response.Redirect("DisplayPDF.aspx", false);

Response.ContentType = "Application/pdf";

Context.Response.OutputStream.Write(msPDFout.ToArray(), 0, intLength);

Nayyer,

The final solution required a modification to code as per the attached files.

In order to formulate this solution, I had to test my code by writing each of the intermediate pdfs to a file and attempting to view them with Adobe. The pdfs produced by Aspose.Word were apparently the problem files. What I don't understand at this point is: Why did this code work with Aspose.Pdf.kit and NOT with the newer Aspose.pdf?!?

Regardless, I am including a test file that I was able to use to solve the problem... but you'll have to use a couple of .doc or .docx files for the Aspose.Words portion.

Thanks,

Geoffrey

Here is the necessary file:

Hi Geoffrey,


Thanks for sharing the resource files. We are working over this query and will get back to you soon. We are sorry for this delay and inconvenience.

Hi Geoffrey,

Thanks for sharing the sample source code with us. I tested the scenario with your provided source code using Aspose.Pdf for .NET v7.0 and unable to find any issue. Sample source code, source and resultant documents are attached for your reference. Please find the attached documents and source code and check if it works fine at your end. If you still face any issue, we would appreciate, kindly create a sample application along with template documents and post here to show the issue. This will help us to regenerate the exact issue and get the cause of the issue soon. We apologize for your inconvenience.

Thanks & Regards,

Rashid,

I appreciate your response and I have found the folks at Aspose to be pleasant.

If you had read the post I submitted... you'd know that I said that I'd fixed the issue and had posted a comment along with my file. I am quite disappointed in your team's apparently lack of attention to posts. This makes me quite doubtful regarding your usefulness with regards to support issues.

Looking forward to a REAL respons to the post.

Geoffrey

Hello Geoffrey,


Sorry for delay in response.

I have tested the code snippet which you have shared in first post of this thread and its seems to work correctly with Aspose.Pdf.Kit for .NET 6.0.0 however when tested with Aspose.Pdf for .NET 7.0.0, its generating the error message. I have intimated the development to further look into the details of this problem and as soon as I have some updates, I will let you know.

Now concerning to Rashid’s reply, in fact he tested the updated code snippet which you have shared ad he was not able to reproduce the problem so do I. During testing, its been observed that PDF files are being merged and DisplayApplication2(…) method is properly displaying resultant PDF file is Web Browser.

Please note that every query posted in Aspose product support forums is equally important and we deeply investigate the problem/scenario/requirement before replying to the customer. However there are some occasions where support person could not get on the same page with the customer, in first attempt. But it’s very rare. Anyways, please accept our humble apologies in this regard and we will try to be more careful while replying.

We are sorry for your inconvenience.

Hello Geoffrey,


Thanks for your patience.

After further investigation, we have observed that in code of Aspose.Pdf.Kit for .NET, the save method moves the position of stream to then end of stream, after writing it. We are going to implement the same approach in Aspose.Pdf.Facades so that the legacy code works with new release versions of Aspose.Pdf for .NET. Please be patient and spare us little time. We are really sorry for this inconvenience.