Merge DOCX into PDF using memory stream in .NET

Getting “corrupt file” error message when merging docx (memory stream) into pdf, but not when merging same file from disc.

1 Like

@sboschi

Please note that you can merge same file formatted documents and the output will follow the same format.

ok, perhaps I misunderstood the documentation provided at below link:

Thanks for your reply

@sboschi

Sorry for the misunderstanding. Yes, API provides facility to merge multiple formatted documents to PDF.

Can you please share your sample code and also mention the API version that you are using (e.g. 20.5, 20.7).

looks like 20.8 via nuget in visual studio.

in below code docbytes is pdf in byte[]. file.File_Contents is byte[] of various file types (.pdf, .docx). The join of .docx memorystream fails, but the join to the same file on disc is fine.

using (Merger merger = new Merger(new MemoryStream(docbytes)))
{
foreach (var file in attchments)
{
merger.Join(new MemoryStream(file.File_Contents));
}

           // test same file on disc
           merger.Join(Server.MapPath("~/app_data/sample.docx"));

}

1 Like

@sboschi

Thank you for the details. We have logged this scenario in our internal issue tracking system with ID MERGERNET-936 for further investigation and resolution. You will be notified in case of any update.

Thank you for your support

1 Like

@sboschi

You’re welcome.

@sboschi

You described two different cases which are using the Merger object created with PDF document:

  1. Joining with PDF memory streams
  2. Joining with DOCX file from hard disk and that is already working fine

In the first case, API generates exception because it thinks that provided memory streams are PDF files not DOCX (As the Merger was initiated with PDF).
For now, API has no ability to define all the file types at run-time from bytes[] or MemoryStream. That is why API requires additional info about file type or extension. For example, if you use FileStream instead of MemoryStream then it will be enough. Because Merger will have additional info describe above.

ok, I’ll give that a try

Thank You

@sboschi

You are welcome.