Aspose.Pdf.Facades.PdfFileEditor 11.5 Merging pdf files created by Aspose.Words are empty

Hi!

I have a situation where we are converting word documents to pdf -files and then merging these converted pdfs’ to a single pdf -file using Aspose.Pdf.Facades.PdfFileEditor

This process has worked well with the Aspose.Pdf version 10.9 that we have used previously, but we with the lates version 11.5 the resulting document is 0 bytes in length.

Aspose.Words version is 16.3.0.0
Aspose.Pdf versions tried:
* 10.9.0.0 (working as expected)
* 11.5.0.0 (Not working)

Attached are two files: document1.docx and document2.docx. Copy these files to C:\Temp\PdfConcat -folder, edit the program code to reflect your environment and run it

using Aspose.Pdf.Facades;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace PdfConcat
{
    class Program
    {
        private static Aspose.Pdf.License _pdfLicense;
        private static Aspose.Words.License _wordsLicense;

        private static string _inputFile1Path = @"C:\Temp\PdfConcat\document1.docx";
        private static string _inputFile2Path = @"C:\Temp\PdfConcat\document2.docx";
        private static string _outputFilePath = @"C:\Temp\PdfConcat\pdf_concat_version_" + Assembly.LoadFrom("Aspose.Pdf.dll").GetName().Version.ToString() + ".pdf";

        //path to your Aspose.pdf license file
        private static string _pdfLicensePath = @"";

        //path to your Aspose.Words license file
        private static string _wordsLicensePath = @"";

        static Program()
        {
            _pdfLicense = new Aspose.Pdf.License();
            _pdfLicense.SetLicense(_pdfLicensePath);

            _wordsLicense = new Aspose.Words.License();
            _wordsLicense.SetLicense(_wordsLicensePath);
        }

        static void Main(string[] args)
        {
            List documents = new List
            {
                docxToPdf(File.ReadAllBytes(_inputFile1Path)),
                docxToPdf(File.ReadAllBytes(_inputFile2Path))
            };
            
            PdfFileEditor pdfEditor = new PdfFileEditor();
            using (MemoryStream outputStream = new MemoryStream())
            {
                var inputStreams = new MemoryStream[documents.Count];

                try
                {
                    for (int i = 0; i < documents.Count; i++)
                    {
                        inputStreams[i] = new MemoryStream(documents[i]);
                    }

                    pdfEditor.Concatenate(inputStreams, outputStream);   
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    //0 bytes written with Aspose.Pdf version 11.5.0.0
                    //Works as expected with Aspose.Pdf version 10.9.0.0
                    File.WriteAllBytes(_outputFilePath, outputStream.ToArray());
                    for (int i = 0; i < inputStreams.Count(); i++)
                    {
                        inputStreams[i].Dispose();
                    }
                }

                Console.ReadLine();
            }
        }

        private static byte[] docxToPdf(byte[] bytes)
        {
            using (var outputStream = new MemoryStream())
            {
                using (var inputStream = new MemoryStream(bytes))
                {
                    var doc = new Aspose.Words.Document(inputStream);
                    doc.Save(outputStream, Aspose.Words.SaveFormat.Pdf);
                    return outputStream.ToArray();
                }
            }
        }
    }
}

Hi Antti

Thanks for contacting support.

I have tested the scenario and have observed that once the Word files are converted to PDF format, the size of returning stream is 0KB so it appears that incorrect file size is being saved in List byte[] array object. Can you please double check the code at your end and update the code to return correct byte stream. We are sorry for this inconvenience.
codewarior:
I have tested the scenario and have observed that once the Word files are converted to PDF format, the size of returning stream is 0KB so it appears that incorrect file size is being saved in List byte[] array object. Can you please double check the code at your end and update the code to return correct byte stream. We are sorry for this inconvenience.

Unfortunately I am unable to reproduce this issue. The bytearrays do contain data on my computer. Can you make sure that the example documents are readable by the program?

Attached a screenshot mirroring the same state you had on your computer.


I am experiencing this same problem. I generate an Aspose.Words document, convert it to pdf and then try to append an existing pdf to the end.



If an effort to solve this, I’ve switch my code to try just adding pages. This doesn’t work either. It seems that the code for Aspose.PDF to load from a stream is broken.



Aspose.Pdf.Document output_pdf = new Aspose.Pdf.Document();



//page 1

Aspose.Words.Saving.SaveOptions page1_opts = Aspose.Words.Saving.SaveOptions.CreateSaveOptions(Aspose.Words.SaveFormat.Pdf);



System.IO.MemoryStream page1_pdf_stream = new System.IO.MemoryStream();

page1.Save(page1_pdf_stream, page1_opts);



Aspose.Pdf.Document page1_pdf = new Aspose.Pdf.Document(page1_pdf_stream);

output_pdf.Pages.Add(page1_pdf.Pages);



The following error is thrown:



Object reference not set to an instance of an object.



Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.



Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.



Source Error:



Line 22:

Line 23: Aspose.Pdf.Document page1_pdf = new Aspose.Pdf.Document(page1_pdf_stream);

Line 24: output_pdf.Pages.Add(page1_pdf.Pages);

Line 25:

Line 26: //page 2

AnttiKA:
codewarior:
I have tested the scenario and have observed that once the Word files are converted to PDF format, the size of returning stream is 0KB so it appears that incorrect file size is being saved in List byte[] array object. Can you please double check the code at your end and update the code to return correct byte stream. We are sorry for this inconvenience.

Unfortunately I am unable to reproduce this issue. The bytearrays do contain data on my computer. Can you make sure that the example documents are readable by the program?

Attached a screenshot mirroring the same state you had on your computer.


Hi Antti,

As a workaround, I have converted Word files to PDF format and tried concatenating the PDF documents using Aspose.Pdf for .NET and as per my observations, an Exception is being generated. For the sake of correction, I have logged it as PDFNEWNET-40710 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Has this issue been resolved? We are experiencing the same problem. Aspose.PDF will not merge PDF files created by Aspose.Words. We receive a NullReferenceException.

Hi David,


Thanks for contacting support.

The earlier reported PDFNEWNET-40710 has been recently noticed and is pending for review. However the concatenation issue depends upon the structure and complexity of input PDF files used. Can you please share the resource files which you are using and facing this problem, so that we can test the scenario in our environment. We are sorry for this inconvenience.

The issues you have found earlier (filed as PDFNET-40710) have been fixed in Aspose.PDF for .NET 22.3.