When I try merge the pdf into word the pdf content corrupted

@alexey.noskov
Target document for file 3 .pdf (290.5 KB)

please find the attachment

@JassarMahmoud The problem occurs because header/footer is inherited from the target document. You can avoid this using code like this:

Document target = new Document(@"C:\Temp\Target document for file 3 .pdf");
Document src = new Document(@"C:\Temp\File 3.pdf");
src.FirstSection.HeadersFooters.LinkToPrevious(false);
target.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);
target.UpdatePageLayout();
target.Save(@"C:\Temp\out.pdf");

The issues you have found earlier (filed as WORDSNET-24358) have been fixed in this Aspose.Words for .NET 22.10 update also available on NuGet.

Dear @alexey.noskov
We faced many issues with marge’s files
there is a way to append PDF files as an image (workaround)

@JassarMahmoud Sure, you can convert PDF to images and append them to the the document. Please try using the following code:

Document dst = new Document(@"C:\Temp\in.docx");

using (FileStream pdfStream = File.OpenRead(@"C:\Temp\in.pdf"))
    AppendPdfAsImages(dst, pdfStream);

dst.Save(@"C:\Temp\out.docx");
private static void AppendPdfAsImages(Document dst, Stream pdfStream)
{
    // Convert PDF to images.
    Aspose.Words.Pdf2Word.FixedFormats.PdfFixedRenderer pdfRenderer = new Aspose.Words.Pdf2Word.FixedFormats.PdfFixedRenderer();
    IReadOnlyList<Stream> images = pdfRenderer.SavePdfAsImages(pdfStream);

    // Create a document from images.
    Document subDocument = new Document();
    DocumentBuilder builder = new DocumentBuilder(subDocument);
    for (int frameIdx = 0; frameIdx < images.Count; frameIdx++)
    {
        using (Stream imgStream = images[frameIdx])
        {
            // Insert a section break before each new page, in case of a multi-frame TIFF.
            if (frameIdx != 0)
                builder.InsertBreak(BreakType.SectionBreakNewPage);

            // Insert the image into the document and position it at the top left corner of the page.
            Shape s = builder.InsertImage(imgStream);
            s.Width = s.ImageData.ImageSize.WidthPoints;
            s.Height = s.ImageData.ImageSize.HeightPoints;
            s.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
            s.RelativeVerticalPosition = RelativeVerticalPosition.Page;
            s.Left = 0;
            s.Top = 0;
            s.WrapType = WrapType.None;

            // We want the size of the page to be the same as the size of the image.
            PageSetup ps = builder.PageSetup;
            ps.PageWidth = s.Width;
            ps.PageHeight = s.Height;
        }
    }

    dst.AppendDocument(subDocument, ImportFormatMode.KeepSourceFormatting);
}

@alexey.noskov
thank you for your response, but the line
IReadOnlyList<Stream> images = pdfRenderer.SavePdfAsImages(pdfStream)
return exception

System.IO.FileLoadException: 'The file cannot be opened. It might have unsupported format or be corrupted.'
InnerException
ArgumentOutOfRangeException: Non-negative number required. Arg_ParamName_Name

Please note that we use aspose .net word and we sure the pdf not corrupted

@JassarMahmoud Could you please attach the PDF document that causes the exception on your side? We will check the issue and provide you more information.

Really I can’t upload the attached, it contains sensitive information we need many approvals :sweat_smile: to upload it
I’ll try to provide more information about the file and I’ll try to get approval to upload it

Please note that
The file is protected against comments, content copying, modification, signature, etc.
Please see the screenshot showing the restrictions applied to the file

@JassarMahmoud Unfortunately, it is difficult to say what the problem is without the document. Please let us know once you get a permission to share the problematic document for testing. Alternatively, you can remove sensitive data from the document and share it here for testing, if this allow us to reproduce the problem.

The issues you have found earlier (filed as WORDSNET-24359) have been fixed in this Aspose.Words for .NET 23.6 update also available on NuGet.