PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Concatenate documents creates duplicate pages
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4068
Page 1 of 1

Author:  ifroggy2 [ Fri Nov 29, 2019 9:32 am ]
Post subject:  Concatenate documents creates duplicate pages

Hi Support,

I'm using pdfsharp in asp.net c# web application and am concatenating two documents created from the same base file but the first document gets appended twice. Has anyone ran into this issue before? The idea is populate Summary.pdf with data for a list of customers and then concatenate each customer's Summary.pdf into one document. But when I do this, the first Summary.pdf gets added customers.count() times. So if I want to create Summary.pdf for 3 different customers, the Summary.pdf for the first customer gets concatenated 3 times. Thanks in advance for your help.

Here's some code.

public MemoryStream GetReport()
{
foreach(var customer in customeritemList)
{
var ms = new MemoryStream();

ms = GetData(customer);

PdfDocument inputDocument = PdfReader.Open(ms, PdfDocumentOpenMode.Import);

// Iterate pages
int count = inputDocument.PageCount;
for (int idx = 0; idx < count; idx++)
{
// Get the page from the external document...
PdfPage page = inputDocument.Pages[idx];
// ...and add it to the output document.
outputDocument.AddPage(page);
}

}

var result = new MemoryStream();
outputDocument.Save(result, false);
outputDocument.Close();

return result;
}

public MemoryStream GetData(customer)
{
var data = GetData(customer);
var file = await GetWorksheetPDF("Summary.pdf");
PdfDocument doc = PdfReader.Open(file);
PdfAcroForm form = doc.AcroForm.NeedAppearances();
form.Fields["name"].SetValue(data.name, "T");

for (var i = 0; i < form.Fields.Count(); i++)
{
form.Fields[i].ReadOnly = true;
}

MemoryStream ms = New MemoryStream();
doc.Flatten();
doc.Save(ms, false);

return ms;
}

Author:  TH-Soft [ Sat Nov 30, 2019 12:50 pm ]
Post subject:  Re: Concatenate documents creates duplicate pages

ifroggy2 wrote:
Has anyone ran into this issue before?
I've seen questions like this before. There was a bug in the user code.

ifroggy2 wrote:
Here's some code.
Quite often the bug is in the code noobs do not share on this site.
http://forum.pdfsharp.net/viewtopic.php?f=2&t=832

Author:  ifroggy2 [ Sun Dec 01, 2019 2:23 am ]
Post subject:  Re: Concatenate documents creates duplicate pages

The problem with merge/concatenate forms in PDF is that form field names are globally shared within the same document. So if multiple pages in the same document share the same form field names, all the fields will have the same value. To fix this issue is to rename the fields for each document before merging/concatenating.

public void RenameAcroField(PdfAcroField field, string newFieldName)
{
field.Elements.SetString("/T", newFieldName);
}

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/