0

I have an existing process that builds various PDF documents in memory using aspose.pdf.generator. The process then concatenates the PDF files and returns one PDF to the user at the end.

I now have a requirement to insert existing Word documents into the concatenation process. I can import the Word docs into memory as Aspose.Words documents. How can I insert the Aspose.Words document into my Aspose.PDF.Generator documents at various locations? If I could convert each Aspose.Words document to a Aspose.PDF.Generator document I could insert the method cleanly into my process.

This object var msDoc = new Aspose.Words.Document(ms); needs inserted into the tdocs.pdf property which is of type Aspose.PDF.Generatorr.Pdf.

        PdfFileEditor pdfEditor = new PdfFileEditor();
        MemoryStream outStream = new MemoryStream();
        MemoryStream[] streamArray = new MemoryStream[tdocs.Count];

        int i = 0;
        foreach (var tdoc in tdocs)
        {
            MemoryStream inputStream1 = new MemoryStream();
            tdoc.Pdf.Save(inputStream1);
            streamArray[i] = inputStream1;
            i++;
        }
        pdfEditor.Concatenate(streamArray, outStream);

        using (FileStream file = new FileStream(filePath, FileMode.Create, System.IO.FileAccess.Write))
        {
            byte[] bytes = new byte[outStream.Length];
            outStream.Read(bytes, 0, (int)outStream.Length);
            file.Write(bytes, 0, bytes.Length);
            outStream.Close();
        }

        return fileName;
Scott
  • 3
  • 4

1 Answers1

0

You can save the Word document to PDF (stream) using Aspose.Words.Save(stream, Aspose.Words.SaveFormat.Pdf) method. And use this stream in pdfEditor.Concatenate().

I assume that tdoc is your class to contain the information about pdf documents. You can modify it to handle both PDF and Word documents by adding 1 more boolean member (IsPdf).

foreach (var tdoc in tdocs)
{
    if (tdoc.IdPdf)
    {
        MemoryStream inputStream1 = new MemoryStream();
        tdoc.Pdf.Save(inputStream1);
        streamArray[i] = inputStream1;

    }
    else
    {
        // If it is a Word document, use Aspose.Words.Document.Save()
        Aspose.Words.Document wordDoc = new Aspose.Words.Document(tdoc.wordFilePath);
        streamArray[i] = new MemoryStream();
        wordDoc.Save(streamArray[i], Aspose.Words.SaveFormat.Pdf);
    }
    i++;
}
Saqib Razzaq
  • 1,408
  • 1
  • 10
  • 10