PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Mon Jul 13, 2020 8:56 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 Nov 28, 2019 12:11 pm 

Joined: Tue Aug 06, 2019 10:45 am
Posts: 40
I asked a Similar question a while back, but I have a pdf that doesn't show correctly on the browser window.

The problem itself is that I have a pdf that has acrofields, but I copy that pdf, and fill the acroform acordingly to the data I have, the problem is when I try to show it to the user (through a web browser, since I'm this in ASP.NET).
the text doesn't come formatted correctly, but, if the user downloads the pdf and opens it on the computer, it is correctly formatted.

I don't know why this happens, the pdf I had filled the form using itextsharp shows correctly.


PdfSharp Example on browser (chrome and firefox were tested with the same results):
PdfSharpAcroOnChrome.png [ 175.85 KiB | Viewed 3196 times ]

iTextSharp Example on browser (chrome and firefox were tested with the same results):
iTextSharpAcroOnChrome.png [ 170.39 KiB | Viewed 3196 times ]

the acroform was filled using pdfsharp and heres a snippet of the code I'm using to make it work, just in case it helps with something:

Code Sample used to get the template copy and set the '/NeedAppearances' attribute
    using (var fs = new FileStream(ReportPath + newfile, FileMode.Create))
        // Get the template and copy it to another document
        PdfSharp.Pdf.PdfDocument myTemplate = PdfSharp.Pdf.IO.PdfReader.Open(pdfTemplateMod22, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify);
        PdfSharp.Pdf.PdfDocument newDoc = (PdfSharp.Pdf.PdfDocument)myTemplate.Clone();

        var acroFields = newDoc.AcroForm.Fields;

        //Ensure the new values are displayed
        if (newDoc.AcroForm.Elements.ContainsKey("/NeedAppearances"))
            newDoc.AcroForm.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
            newDoc.AcroForm.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));


        if (newDoc.AcroForm != null)
            newDoc.Flatten();   // NOTE: doc.AcroForm is null after flattening ! and the pdf is not editable

        url = "../relatorios/temporarios/" + newfile;

Code Sample on how I am putting value on the acro fields
    #region Página 1 Modelo 22
    DateTime dataTrb = GlobalFunctions.GetDate(period);

    #region Quadro 01
    // Preciso obter a DAT_INI_TRB
    DateTime dataIniTrb = dataTrb.AddYears(-1).AddDays(1);
    ((PdfTextField)pdfFormFields["DAT_INI_TRB_Ano"]).Value = new PdfString(dataIniTrb.ToString("yyyy"));
    (PdfTextField)pdfFormFields["DAT_INI_TRB_Mes"]).Value  = new PdfString(dataIniTrb.ToString("MM"));
    ((PdfTextField)pdfFormFields["DAT_INI_TRB_Dia"]).Value  = new PdfString(dataIniTrb.ToString("dd"));

    DateTime dataFimTrb = dataTrb;
    ((PdfTextField)pdfFormFields["DAT_FIM_TRB_Ano"]).Value = new PdfString(dataFimTrb.ToString("yyyy"));
    ((PdfTextField)pdfFormFields["DAT_FIM_TRB_Mes"]).Value = new PdfString(dataFimTrb.ToString("MM"));
    ((PdfTextField)pdfFormFields["DAT_FIM_TRB_Dia"]).Value  = new PdfString(dataFimTrb.ToString("dd"));

    ((PdfTextField)pdfFormFields["Ano"]).Value = new PdfString(Space(dataFimTrb.ToString("yyyy"), 3));
    #endregion Quadro 01

Some help regarding this matter would be appretiated, since the last time I asked something regarding this subject, It wasn't ansered.

Thank you in advance

Reply with quote  
PostPosted: Sat Nov 30, 2019 12:46 pm 
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 549
Location: CCAA
IRlyDunno wrote:
Some help regarding this matter would be appretiated, since the last time I asked something regarding this subject, It wasn't ansered.
It seems the majority here has no experience with AcroForms and cannot help.

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

Reply with quote  
PostPosted: Fri Dec 13, 2019 10:59 am 

Joined: Tue Aug 06, 2019 10:45 am
Posts: 40
So, does that mean there isn't a solution that the PdfSharp team's can provide me or make at least me understand the presented problem?

Reply with quote  
PostPosted: Tue Dec 17, 2019 10:08 am 

Joined: Tue Sep 30, 2014 12:29 pm
Posts: 26
Are you using vanilla PdfSharp or some modified version ?
One observation seem to suggest the latter is the case.
AFAIK the vanilla version of PdfSharp does not support Text-Fields, that are made up of "Combs".
(see https://helpx.adobe.com/acrobat/using/pdf-form-field-properties.html)
But since the characters in your screenshots show obviously some spacing related to the combs, it looks like you're using some modified version.
The spacing of the characters look correct, just the start-position is a bit off.
Can you share the document ?
Since I'm tasked to solve similar Form-Field related problems every now and then at work, i would like to have a look at it.

Reply with quote  
PostPosted: Tue Dec 17, 2019 12:25 pm 

Joined: Tue Aug 06, 2019 10:45 am
Posts: 40
First of all, thanks for your response.

Now, I'm using a modefied-ish version of MigraDoc/PdfSharp, with this I mean that I'm using the vanilla libs, but using some of my code as intermediary, for example:

I have a class called Paragraph that inherits from MigraDoc.DocumentObjectModel.Paragraph.
so that I can add some functions to the paragraph class.

This works and the document I'm using and rendering is with the vanilla libs.

I had no Idea that PdfSharp didn't support Combs, thanks for clearing that up.
The spacing you are seeing is hand made with a 'Space' method I made to add X spaces between each char of a string, that was the way I managed to make it work.

I'll give the link to the original pdf that I'm using/filling, since he is too big to upload here

Any help or solution regarding the matter will be appretiated

NOTE: I'd like to give emphasis to that fact that this only happens on browsers, if the pdf is downloaded and opened with acrobat, for example, the chars are all in the right places (because of the spacing I'm giving between each char).
But since you told that the textfield in PdfSharp doesn't support combs, that should be the reason as to why it doens't work on the browser. I'll try and find a way to make it work in the meantime, and update this thread if I come up with an answer.

Just in case that you need the Space method I talked about up there I'll post it here:
private static string Space(object obj, int spaces)
    if ((obj != null) && (obj.ToString() == ""))
        return null;

    string text = obj.ToString();
    string newText = "";

    for (int i = 0; i <= text.Length - 1; i++)
        newText += text[i] + new string(' ', spaces);

    return newText;

Reply with quote  
PostPosted: Tue Jan 28, 2020 3:38 pm 

Joined: Tue Aug 06, 2019 10:45 am
Posts: 40
If anyone get an issue like mine, there are a few things that I have discovered.
1st - PdfSharp does support combs, you just have to set it in the form and it should all be good.
2nd - It's possible to even alter the field, and with this I mean, It's possible to make a regular PdfTextField turn into a 'comb', all that's needs to be done is insert these 2 lines of code

// this property sets the /MaxLen attribute, this sais that the field can have X chars, and inserts them in an equidistant point from the adjacent chars, making the comb effect
  ((PdfTextField)acroCollection[field]).MaxLength = maxFieldLength;
  // The /Ff it's the attribute on pdf that contains the flags that tell what type the field is. (i.e: textfield = X, radio = Y, etc...) and, apperantlly the value that sets the field to be a comb is '29360128'. this is important, from what I have tested this doesn't work if this flag isn't set, even if you set the maxLength property to what ever it is that you need
  ((PdfTextField)acroCollection[field]).Elements.Add(PdfSharp.Pdf.AcroForms.PdfAcroField.Keys.Ff, new PdfSharp.Pdf.PdfInteger(29360128));

this was all that needed to be added for the code to work just fine.

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 12 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