PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 5:22 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Nov 29, 2019 9:32 am 
Offline

Joined: Fri Nov 29, 2019 8:51 am
Posts: 3
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;
}


Top
 Profile  
Reply with quote  
PostPosted: Sat Nov 30, 2019 12:50 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 01, 2019 2:23 am 
Offline

Joined: Fri Nov 29, 2019 8:51 am
Posts: 3
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);
}


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 149 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group