PDFsharp icon indicating copy to clipboard operation
PDFsharp copied to clipboard

Merge Docs from Bytes

Open Kylar182 opened this issue 4 months ago • 4 comments

Resources

Examples proved in previous doc site used to work and match this method, new doc site does not have this example.

Reporting an Issue Here

Expected Behavior

Combined PDFs from Memory Stream output a concatenated / merged file.

Actual Behavior

Using this method, I get the a repeat of the first doc for every page in outputDoc. The imported files are stamped and flattened docs from memory streams. The imported files output correctly individually (although a much larger file size for some reason). This seems to be all versions 6 and above. I've tried variations of this method with doc.Pages[i].Clone(), etc. with variations in success.

    /// <summary>
    /// Merges multiple pdfs into a single document
    /// </summary>
    /// <param name="files">list of pdf documents as byte arrays</param>
    /// <returns>byte array of the combined pdf</returns>
    /// <remarks>Flattening will not work after merging</remarks>
    public static byte[] MergePdfs(IEnumerable<byte[]> files)
    {
        byte[] merged;
        PdfDocument outputDoc = new PdfDocument();

        foreach (byte[] file in files)
        {
            using (MemoryStream output = new MemoryStream(file, 0, file.Length))
            {
                using (PdfDocument doc = PdfReader.Open(output, PdfDocumentOpenMode.Import))
                {
                    outputDoc.Version = doc.Version;

                    for (int i = 0; i < doc.PageCount; i++)
                    {
                        PdfPage page = doc.Pages[i];
                        page.Size = PageSize.Letter;
                        outputDoc.AddPage(page);
                    }
                }
            }
        }

        using (MemoryStream output = new MemoryStream())
        {
            outputDoc.Save(output, false);

            output.Seek(0, SeekOrigin.Begin);
            merged = output.ToArray();

            try
            {
                outputDoc.Close();
            }
            catch (Exception) { }
        }

        return merged;
    }

Kylar182 avatar Sep 25 '25 16:09 Kylar182

I don't understand what the issue with your code snippet is.

ThomasHoevel avatar Sep 30 '25 06:09 ThomasHoevel

I don't understand what the issue with your code snippet is.

Prior to this code, each PDF outputs correctly individually. We use this code to merge the PDF pages from the previous PDFs into a new PDF. In the new PDF it just repeats the first Page over snd over with this code, instead of combining the individual Pages.

Kylar182 avatar Sep 30 '25 09:09 Kylar182

If I provide an example repo can this be tagged as a bug?

Kylar182 avatar Oct 07 '25 15:10 Kylar182

If I provide an example repo can this be tagged as a bug?

We'll tag it as a bug when we can confirm it is a bug.

Providing a ZIP file is recommended.
https://docs.pdfsharp.net/General/Issue-Reporting/About.html

With which version did it work? Which version is the first that breaks the desired behavior?

ThomasHoevel avatar Oct 13 '25 12:10 ThomasHoevel