Reading the same pdf

Hi,
 I have a bunch of pdf files that I read from a folder and concatenate in a big pdf file. This is in a multi user enviroment, meaning that some people might read the same file in the same time.
 From time to time (randomly) I get this following error:
Could not open C:\Sites\\Uploads\Pdfs\18602_425_Reference-MGS.pdf file for some reason
 ---> €.: Each key should be Pdf Name

at Aspose.Pdf.Facades.PdfFileEditor.(Exception )
at Aspose.Pdf.Facades.PdfFileEditor.Concatenate(Stream[] inputStream, Stream outputStream)
at Phoenix.DataAccess.Factories.Commands.GetFileCollectionFactoryBase`1.ExecuteAppendPdf(Stream targetStream, String targetFile)
— End of inner exception stack trace —

 The stack trace is not particularly helpful. Any thoughts on this?
Regards,

Hi Mel,

Thank you for the details. Which version of Aspose.Pdf for .NET are you using? Please download and try the latest version of Aspose.Pdf for .NET v6.7 and check if it works fine for you. If you still face any problem, please share your template files and sample code with us which will help us reproduce the issue at our end.

Sorry for the inconvenience.

Hi,


Thank you for your reply. I’ve downloaded the newest version but when I tested I have a new more serious issue, the concatenation only add’s the first document and empty pages for the rest.

Here is a code snippet on how I’ve implemented so far.

public void ExecuteAppendPdf(Stream targetStream, string targetFile)
{
var pdfFileEditor = new PdfFileEditor
{
AllowConcatenateExceptions = true,
CloseConcatenatedStreams = false
};

try
{
using (var fileStream = System.IO.File.OpenRead(targetFile))
{
pdfFileEditor.Concatenate(targetStream, fileStream, targetStream);
}
}
catch (FileNotFoundException exception)
{
Logger.Error(exception);
using (Stream pdfErrorPage = CreatePdfErrorPage(string.Format(CreateApplicationReportFactoryStrings.FileNotFound, Path.GetFileName(targetFile))))
{
pdfFileEditor.Concatenate(targetStream, pdfErrorPage, targetStream);
}
}
catch (Exception exception)
{
Exception wrapper = new Exception(string.Format(“Could not open {0} file for some reason\r\n”, targetFile), exception);
Logger.Error(wrapper);
using (Stream pdfErrorPage = CreatePdfErrorPage(string.Format(CreateApplicationReportFactoryStrings.FormatUnsupported, Path.GetFileName(targetFile))))
{
pdfFileEditor.Concatenate(targetStream, pdfErrorPage, targetStream);
}
}
}

Ideas?

Regards,

Hi Mel,

Please share the template and resultant PDF files with us. This will help us figure out the issue soon.

Sorry for the inconvenience,

Hi,


I’ve attached a arhive with documents created with v6.5 and documents created with 6.7 and the documents used, you can see that 6.5 works 6.7 will put blank pages.

Regards,

Hi Mel,

Thank you for sharing the template and resultant files.

I checked your issue with the latest version of Aspose.Pdf for .NET v6.7 and it works fine. I have attached the resultant PDF file for your reference. Please create a sample application and share with us. This will help us figure out the issue soon. Also, please let us know which OS, Visual Studio Version and .NET Framework are you using. This will help us narrow down the issue.

Sorry for the inconvenience,

Hi,


Attached is a solution that reproduces the issue.
In the libs folder you have 2 folders for version 6.5 (witch is fine) and 6.7(witch is bad) please copy the dll’s there reference them from the application and run, it will concatenate the 2 files and open the result.
I should mention that the project is basically a copy paste of the code in this threat, but if you say it works I wonder what the issue might be.

System: Win7 x64, .net 4.0, using Foxit pdf viewer.

What do you think?

Thank you,

Hi Mel,

Thank you for sharing the sample application.

I am able to generate the issue you mentioned. Actually, in my previous test, I simply concatenated two sample files you provided with each other and that worked fine. Now, as in your new application, you are concatenating / appending both the files into an empty PDF file and that is causing the problem. I have registered an issue in our issue tracking system with issue id: PDFNEWNET-33294. You will be notified via this forum thread regarding any update against your reported issue.

Sorry for the inconvenience,

Hi,


Do you happen to have a estimate on this fix?

Thank you,

Hi Mel,

I have asked the development team to provide an ETA after analyzing your issue. As soon as I get a response, I will update you via this forum thread.

Sorry for the inconvenience,

Hi there,


Hope you haven’t forgot about this issue, waiting on a ETA

Thank you,

Hi Mel,

I have received the following feedback from our development team which I would like to share with you. Hopefully, this will help you in resolving your issue.

In v6.5, all the data of stream objects of the document were loaded into memory when concatenation operation was performed. This approach required a large size of allocated memory.

To avoid extra memory allocation algorithm was improved after version 6.5: stream objects are not loaded into memory but just copied from source document into destination document at document save process.

This means that all data of source document must be accessible when result document is saved.

That’s why concatenating document and storing result into source stream may get incorrect result.

In other word the following code may work incorrectly:

PdfFileEditor pfe = new PdfFileEditor(); pfe.Concatenate(srcStream, otherStream, srcStream);

You should redesign your code to avoid this situation. e.g.

Modify AppendPdf to return new stream object:

public void Stream AppendPdf(Stream stream, string targetFile)
{
    Stream result = new MemoryStream();
    PdfFileEditor pfe = new PdfFileEditor();
    using(Stream fileStream = File.OpenRead(targetFile))
    {
        pfe.Concatenate(stream, fileStream, result);
    }
    return result;
}

Use these lines of code:

Stream targetStream = createEmptyFile();
targetStream = AppendPdf(targetStream, "file1.pdf");
targetStream = AppendPdf(targetStream, "file2.pdf");

But a better solution is to use concatenation of multiple files:

pfe.Concatenate(new string[] { "file1.pdf", "file2.pdf", "file3.pdf"}, resultFile); \ (or use version of concatenate method with streams).

This approach will be more efficient because it will not require multiple loading and saving PDF document from/to streams.

Please perform the above modification and let us know if you still face any issue.

Sorry for the inconvenience,

Hi,


I will try this and get back to you with feedback.

Regards,

The issues you have found earlier (filed as PDFNEWNET-33294) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.