Format alignment is not correct (only saved in stream)

Hello,

I want to format TextFormFields in a PDF file. With the following code it works if I save it into a file. If I save it as a stream which I convert to a byte-array and response it in a webseite, it only works partly, the alignment doesn’t work:

Aspose.Pdf.Document docPdf = new Aspose.Pdf.Document(“Tarifoptimierung.pdf”);
Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form(docPdf);
System.Collections.Hashtable facades = new System.Collections.Hashtable();

foreach (string fieldname in pdfForm.FieldNames)
{
if (pdfForm.GetField(fieldname) == ‘____________’)
{
pdfForm.FillField(fieldname, “”);
FormFieldFacade formFieldFacade = pdfForm.GetFieldFacade(fieldname);
formFieldFacade.Alignment = FormFieldFacade.AlignRight; //Alignment in response is not ok, its still left
formFieldFacade.BackgroundColor = System.Drawing.ColorTranslator.FromHtml("#CED7FF"); //Backgroundcolor in response is ok
facades[fieldname] = formFieldFacade;
}
}
pdfForm.Document.Save(outStream, Aspose.Pdf.SaveFormat.Pdf);

FormEditor fe = new FormEditor(docPdf);
foreach (string fieldname in facades.Keys)
{
fe.Facade = facades[fieldname] as FormFieldFacade;
fe.DecorateField(fieldname);
}
fe.Save(“Tarifoptimierung_out.pdf”); //save in file -> anything is ok
fe.Save(outStream); ////save in stream -> aligment is not right

outStream.Position = 0;
return outStream.ToArray();


The backgroundcolor in response is ok. The alignment in response is not ok, its still left.
Saved directly in the file both is ok.

What could be the problem?
In the attachment I send you the input file “Tarifoptimierung.pdf” and the 2 output files.
“Tarifoptimierung_out.pdf” is saved directly as file.
“Tarifoptimierung_out_saved_from_browser.pdf” is saved in stream -> ToArray -> response in browser.

Regards
Ringo

Hi Ringo,


Thanks for your inquiry. I am afraid I am unable to notice any issue in formfiled alignment while checking your shared PDF files in Adobe Reader. Can you please share screenshot of the difference? So we will check it at our end and provide you more information accordingly.

Moreover, might be it is PDF reader issue in browser. Please make Acrobat Reader as default viewer to view PDF files in your browser. Hopefully it will help you to resolve the issue.

please follow these steps to set Acorbat Reader as default viewer in Chrome and Firefox:

  • Chrome
    • Run chrome://plugins/ in browser’s prompt
    • Disable Chrome PDF Viewer option
    • Enable Adobe PDF Plug-In For Firefox and Netscape XX.X.XX.XX (the version may differ) option
    • Restart the browser
  • Firefox
    • At the top of the Firefox window, click on the Firefox button and then select Options
    • Select the Applications panel
    • Find Portable Document Format (PDF) in the list and click on it to select it
    • Click on the drop-down arrow in the Action column for the above entry and select Use Acrobat Reader (in Firefox)
    • Restart the browser

Best Regards,

Hello,

thank you for your reply.
The difference is at the alignment of the editable fields. I send you here the screenshot with the difference between the two output files which are saved in different ways.

I tried also to change the pdf-reader in firefox and chrome. Its still the same issue in the file Tarifoptimierung_out_saved_from_browser.pdf.

Regards
Ringo

Hi Ringo,


Thanks for sharing additional information. As per my observation you are saving document to stream before applying DecorateField(). So I have tested your code by commenting out the highlighted line and it is working as expected for both stream and file. Hopefully it will help you to accomplish the requirements.

Aspose.Pdf.Document docPdf = new Aspose.Pdf.Document("E:/Data/Tarifoptimierung.pdf");

Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form(docPdf);

System.Collections.Hashtable facades = new System.Collections.Hashtable();

foreach (string fieldname in pdfForm.FieldNames)

{

if (pdfForm.GetField(fieldname) == "____________")

{

pdfForm.FillField(fieldname, "");

FormFieldFacade formFieldFacade = pdfForm.GetFieldFacade(fieldname);

formFieldFacade.Alignment = FormFieldFacade.AlignRight; //Alignment in response is not ok, its still left

formFieldFacade.BackgroundColor = System.Drawing.ColorTranslator.FromHtml("#CED7FF"); //Backgroundcolor in response is ok

facades[fieldname] = formFieldFacade;

}

}

// pdfForm.Document.Save(outStream, Aspose.Pdf.SaveFormat.Pdf);

FormEditor fe = new FormEditor(docPdf);

foreach (string fieldname in facades.Keys)

{

fe.Facade = facades[fieldname] as FormFieldFacade;

fe.DecorateField(fieldname);

}

fe.Save("E:/data/Tarifoptimierung_test.pdf");//save in file -> anything is ok

fe.Save(outStream);

byte[] downloadBytes = outStream.ToArray();

HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");

HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + documentTitle + ".pdf");

//HttpContext.Current.Response.AddHeader("Content-Disposition", "inline; filename=" + documentTitle + ".pdf");

HttpContext.Current.Response.BinaryWrite(downloadBytes);

HttpContext.Current.Response.End();

Please feel free to contact us for any further assistance.


Best Regards,

Thank you for your reply.

Just to remove the saving of the pdfForm as outstream after edit the pdfForm didn’t work, in this case the fillField did not work.
But I have found a solution. I changed the saving a bit.
Before I open the file (sorry it was a stream) with Aspose.Pdf I had edit it with Aspose.Word and saved it as a stream. Now I save this not in outstream but in another stream which I open with Aspose.Pdf. And I changed the pdfForm.Document.Save(outStream) to pdfForm.Save(outStream).

So this works for me:

Aspose.Pdf.Document docPdf = new Aspose.Pdf.Document(outStreamTemp1);
Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form(docPdf);
System.Collections.Hashtable facades = new System.Collections.Hashtable();

foreach (string fieldname in pdfForm.FieldNames)
{
if (pdfForm.GetField(fieldname) == strEmptyFieldValue)
{
pdfForm.FillField(fieldname, “”);
FormFieldFacade formFieldFacade = pdfForm.GetFieldFacade(fieldname);
formFieldFacade.Alignment = FormFieldFacade.AlignRight;
formFieldFacade.BackgroundColor = System.Drawing.ColorTranslator.FromHtml("#CED7FF");
facades[fieldname] = formFieldFacade;
}
}
pdfForm.Save(outStream); //without this save it doesn’t work as requested

FormEditor fe = new FormEditor(docPdf);
foreach (string fieldname in facades.Keys)
{
fe.Facade = facades[fieldname] as FormFieldFacade;
fe.DecorateField(fieldname);
}
fe.Save(filenameSaveTemp3);
fe.Save(outStream);

Best regards
Ringo

Hi Ringo,


Thanks for your feedback. It is good to know that you have managed to resolve the issue.

Please keep using our API and feel free to ask any question or concern, we will be glad to extend our support.

Best Regards,