empira / PDFsharp-1.5

A .NET library for processing PDF
MIT License
1.28k stars 589 forks source link

Question about Merge PDF Files #163

Closed Kensuke-Ota-SRA closed 2 years ago

Kensuke-Ota-SRA commented 2 years ago

I am editing a PDF file using .Net PDFSharp library. If enter in the Form and then merge it with another PDF file, The text I typed in the Form is no longer appears. The text seems to be saved because only visible while hover over the form. I'm wondering if the PDF merge process is wrong, can you give me some advice?

Merge process is like this,

using (MemoryStream outPdfStream = new MemoryStream())
using (PdfDocument outPdfDoc = new PdfDocument())
{
    using (MemoryStream orgPdfStream = new MemoryStream(ssOrgPDFFile))
    using (PdfDocument orgPdfDoc = PdfReader.Open(orgPdfStream, PdfDocumentOpenMode.Import))
    using (MemoryStream conPdfStream = new MemoryStream(ssConnectPDFFile))
    using (PdfDocument conPdfDoc = PdfReader.Open(conPdfStream, PdfDocumentOpenMode.Import))
    {
        for (int i = 0; i <= ssInsertPos; i++)
        {
            PdfPage pg1 = orgPdfDoc.Pages[i];
            outPdfDoc.AddPage(pg1);
        }
        for (int i = 0; i < ssConnectPageNum; i++)
        {

            PdfPage pg2 = conPdfDoc.Pages[ssConnectTargetPos + i];
            outPdfDoc.AddPage(pg2);
        }

        if (ssInsertPos < orgPdfDoc.PageCount)
        {
            for (int i = ssInsertPos + 1; i < orgPdfDoc.PageCount; i++)
            {
                PdfPage pg1 = orgPdfDoc.Pages[i];
                outPdfDoc.AddPage(pg1);
            }
        }
    }

    outPdfDoc.Save(outPdfStream, true);
    ssOutputPDFFile = outPdfStream.ToArray();
}
Kensuke-Ota-SRA commented 2 years ago

This question has been resolved. The cause was that the "/NeedAppearances" flag was lost when merging. When I tried to keep the "/NeedAppearances" flag even in the merged PDF file, the text was displayed normally.

Thank you for your help.