PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sat Apr 27, 2024 5:58 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Thu Sep 21, 2023 8:23 pm 
Offline

Joined: Fri Sep 08, 2023 5:27 pm
Posts: 12
Hi - I attempted a search on this topic in Bugs and Support and didn't find anything. If this is a duplicate, please forward a link to the thread(s) & I'll read thru and see if I can find an answer to my issue.

I've got a PDF template that I open and fill in using PDFSharp's Acrofields objects/features. But after flattening, saving and opening in Acrobat to view, the text entered into the fields that were filled in disappears until the field in question is clicked on.

One important point is that my company has been using a different 3rd party PDF tool to create/fill in PDFs up until now, but we're thinking of changing to PDFSharp & I'm working out if this tool will work for us. I've tried a couple of solutions mentioned online but the first caused a secondary issue that I can't have: adding the "\NeedAppearances" attribute with a value of true. This does fix the issue!!! but causes Acrobat to demand the PDF be saved after opening and then attempting to close it in Acrobat. So this solution doesn't work for us.

Other important info:
1. Neither the disappearing text or Acrobat save issues happen if the PDF template is filled by using another 3rd party's PDF tool to open & then fill the form. -- this issue is a deal killer for us. I can't make the case that we should use PDFSharp or MigraDoc (using that for other needs in the same projects) if I can't get this solved.

2. Viewing the PDFSharp filled form in Chrome displays the filled in & flattened text w/o issue.

3. Another suggestion online also didn't work: There is a fork someone had created of the PDFSharp.dll called PDFSharpFlatten that was supposed to fix this. However, it generated errors back to my project when attempting to use it. It's older than the current PDFSharp.dll anyway and seems to be 1/2 baked, so that's not an option for me either.

4. Here's the short version of the C# code I use to open, fill and save the file. Thanks for your response! - Chris B

public string[] RenderWithWatermark(string sourceFileName, string outputFileName, Dictionary<string, string> dict, string watermarkText) {

File.Copy(sourceFileName, outputFileName);
PdfDocument doc = PdfReader.Open(outputFileName, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify);
PdfSharp.Pdf.AcroForms.PdfAcroForm acroFormDict = doc.AcroForm;
FileInfo fi = new FileInfo(AppStd.PDFStorageDir outputFileName);

bool bFieldsFound = false;
string _fieldName;
List<string> l_sNotFound = new List<string>();
for (int i = 0; i < acroFormDict.Fields.Count; i++) {
PdfAcroField fld = acroFormDict.Fields[i];
_fieldName = fld.Name;
if (_fieldName.EndsWith("Image") && dict.ContainsKey(_fieldName) && dict[_fieldName].StartsWith("*IMAGE:")) {
/* ...
* code goes here that grabs data and places it in the image fld
*/
bFieldsFound = true;
} else if (dict.ContainsKey(_fieldName)) {
string fldValueToProcess = dict[_fieldName];
fld.Value = new PdfString(fldValueToProcess);
bFieldsFound = true;
} else {
l_sNotFound.Add(_fieldName);
}
}
if (!bFieldsFound) {
acroFormDict.Fields.Elements.Clear();
}
if (!string.IsNullOrEmpty(watermarkText)) {
PdfPage page = doc.Pages[0];
bool bWatermarkOnTop = (AppStd.GetConfigIntWithCache("WATERMARK_ON_TOP", -1) != 0);
PdfSharp.Drawing.XImage waterMarkImage = PdfSharp.Drawing.XImage.FromFile(this._watermarkFile);
XGraphics gfx;
if (bWatermarkOnTop) {
gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
} else {
gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);
}
gfx.DrawImage(waterMarkImage, 0, 0);
gfx.Dispose();
}

if (_formFlatten) {
doc.Flatten();
}

doc.Save(outputFileName);
doc.Close();

return l_sNotFound.ToArray();
}
}


Top
 Profile  
Reply with quote  
PostPosted: Sat Sep 23, 2023 12:52 pm 
Offline

Joined: Sat Sep 23, 2023 12:27 pm
Posts: 6
having the same issue,

add text to text fields in template file
add signature image
NeedApparances set
Flatten file
save

fields show empty primarily for ios native pdf reader and adobe app in app store.


Top
 Profile  
Reply with quote  
PostPosted: Sat Sep 23, 2023 4:57 pm 
Offline

Joined: Sat Sep 23, 2023 12:27 pm
Posts: 6
Just an update on the Flatten() function of PDFsharp.

Even on the latest 1.5 beta Flatten() does not work truly. I hit the issue as described above where output pdf does not open properly in mainstream readers.

When I was checking ABCPDF where it has .Stamp() function similar to Flatten, realized it does remove the fields .

So when I searched forums about this I came across the post from (void)
viewtopic.php?f=2&t=3369 (instruction on how to apply his patch to the main branch)
viewtopic.php?f=3&t=3778 (further updates with link to github)

and downloaded his branch https://github.com/packdat/PDFsharp/tree/AcroForms
installed 4.6.1 net runtime devpack and compiled release

replacing the PdfSharp.dll file with this and re-testing doc.Flatten() does indeed did the trick and truly Flatten the document.

Now I can open the file in ios pdf reader or Acrobat app properly - as expected.

I am not sure why the post and feedback by (void) was not taken into consideration after many years....


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 25, 2023 5:58 pm 
Offline

Joined: Fri Sep 08, 2023 5:27 pm
Posts: 12
AKEILO wrote:
Just an update on the Flatten() function of PDFsharp.

Even on the latest 1.5 beta Flatten() does not work truly. I hit the issue as described above where output pdf does not open properly in mainstream readers.

When I was checking ABCPDF where it has .Stamp() function similar to Flatten, realized it does remove the fields .

So when I searched forums about this I came across the post from (void)
viewtopic.php?f=2&t=3369 (instruction on how to apply his patch to the main branch)
viewtopic.php?f=3&t=3778 (further updates with link to github)

and downloaded his branch https://github.com/packdat/PDFsharp/tree/AcroForms
installed 4.6.1 net runtime devpack and compiled release

replacing the PdfSharp.dll file with this and re-testing doc.Flatten() does indeed did the trick and truly Flatten the document.

Now I can open the file in ios pdf reader or Acrobat app properly - as expected.

I am not sure why the post and feedback by (void) was not taken into consideration after many years....


This solution will not work for me: I've already discovered that flattening is not an issue if I set the "/NeedAppearances" attribute to true. I'm using the most current Nuget for PdfSharp + MigraDoc as of a week ago (1.50.5147). However, adding "/NeedAppearances" causes a secondary issue: opening the saved PDF in Acrobat and attempting to close will prompt the user to Save the form tho they have changed nothing (and cannot because it's flattened). Opening the template file in Acrobat does not cause the "Save" issue. And neither does filling the template form using a different 3rd party PDF tool. Something is wrong when PDFSharp saves the filled in form. I need to find a solution to the issue of disappearing/ghosted text where I don't have to add "/NeedAppearances" if we're to use PDFSharp.

I posted a simplified version of our C# code that fills in a form earlier in this thread. Thank you for your help!
Chris


Last edited by chris.ball on Mon Sep 25, 2023 6:03 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 25, 2023 6:02 pm 
Offline

Joined: Sat Sep 23, 2023 12:27 pm
Posts: 6
I suspect the issue you are experiencing is partly because .Flatten is not removing the fields hence when its opened again it attempts to Save it.

You can always temporarily copy the DLL from the branch I have linked, and try .Flatten again to see if Save prompt disappears


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 25, 2023 7:28 pm 
Offline

Joined: Fri Sep 08, 2023 5:27 pm
Posts: 12
AKEILO wrote:
I suspect the issue you are experiencing is partly because .Flatten is not removing the fields hence when its opened again it attempts to Save it.

You can always temporarily copy the DLL from the branch I have linked, and try .Flatten again to see if Save prompt disappears


Thank you! - this might work. Still a bit up in the air as I'm getting some errors that are being thrown by the dll, but I might be able to track them down. :D


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 344 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