iTextSharp add pre-existing PDF as a layer to another PDF - c#

I have reports that are being converted into PDFs. Some of these reports have information missing simply because we don't track it. I have created another PDF with the shell of the report and placed input fields controls on it. I would like to know if there is a way to apply the shell PDF to the converted PDF so users can enter information in those blank fields without having to print them out and hand write them? I have done this manually through Adobe Acrobat Pro 9.3 by applying the generated PDF to the shell PDF as a Layer. I have done as much poking around with iTextSharp concerning Layers, but I still haven't found anything that has worked.
Thank you in advanced!

1) Layers won't work with fields. PDF Layers are a part of the page contents. Form fields, as with all annotations, float above the page.
2) Having said that, you can hide and reveal form fields using Acrobat/Reader JavaScript. The "doc" object is usually "this" in field and page entry points, so to show a given field, it's just:
var fld = this.getField("fieldName");
fld.hidden = false;
There are quite a few different places you can add JS to a PDF. Various field events, page events, and document events. You can also set a layer's action to some javaScript. Heck you can set a bookmark's action to be javascript instead of a "go over there" action.
Note that layers are called "Optional Content Groups" (OCGs) in PDF tech-speak. If you really want to create a layer, it looks like it would go something like this:
// layer implements PdfOCG
PdfLayer layer = new PdfLayer("MyLayer", writer);
PdfContentByte cb = getAContentByteFromSomewhere();
cb.beginLayer(layer); // takes PDFOCG object
/* draw stuff to be part of that layer */
cb.endLayer();
There are a number of examples on the iText site corresponding to "iText In Action, 2nd edition" (I don't get paid, the author is a friend). The aforementioned examples can be found here.
This bears repeating: Fields cannot be part of an OCG (layer). They can however be scripted to act like they are.

Related

How to Embed iTextSharp PDF in View (C# - MVC 5)

Greetings fellow coders!
I've done tons of research on this topic and can't manage to find any resources that truly depict how this process can be done (or if it can be done). So, I'm turning to you all for some advice!
The Issue:
So right now, I have a form that sends the filled out data back to the controller via HttpPost. At this point, I pass that form data to the model function to create the PDF. Now at this point, I have it returning the PDF file if the model function was successful.
PDFMemStream memstream = new PDFMemStream();
CreatePDFinfo formdata = new CreatePDFinfo();
if (ModelState.IsValid)
{
memstream = CreatePDF.Populate_PDFcontent(formdata);
if (memstream.retval.success)
{
// if creating pdf is successful, return file
return File(memstream.mstream, "application/pdf");
}
}
ViewBag.PageTitle = "Cool PDF Project";
ViewBag.TabTitle = "Really Cool PDF Project";
return View(formdata);
I can display this created PDF in a separate tab if I use the code below in the view. This assumes that the current view is called "createPDF". So this part works all fine and dandy.
$('#form').submit(function () {
if ($('#form').valid())
{
window.open('', 'createPDF');
this.target = 'createPDF';
}
});
If I don't use the above code, after the button click, it will remain on the current page and reload the page with only the pdf content in the native browser PDF viewer.
The Needed Solution:
As I stated, everything works perfectly if I wanted the PDF to be populated in at new tab or the existing one. HOWEVER, what I need, is a way to view the PDF on the same page either in a partial view or embedded on the existing page. I could even deal with the PDF being embedded in a new view as long as I could pass the model data to the new view for other sections of that page.
Resources & Articles:
Here's all of the articles and resources I've already looked into and read (so please do not post the same ones):
using ITextSharp to extract and update links in an existing PDF
Displaying a pdf to webpage using iTextSharp?
http://developers.itextpdf.com/content/itext-7-jump-start-tutorial/sharp/chapter-1-introducing-basic-building-blocks
MVC Application to Display embedded PDF documents
https://www.codeproject.com/Questions/890372/How-Can-I-Display-A-Pdf-From-Byte-Array-In-Mvc
https://www.codeproject.com/Articles/66948/Rendering-PDF-views-in-ASP-MVC-using-iTextSharp
C# ASP.NET MVC Create PDF from view Rotativa or iTextSharp?
Convert PartialView Html to String for ITextSharp HtmlParser
How to open a PDF file in an <iframe>?
How to render an ASP.NET MVC View in PDF format
Now some of the articles aren't completely relevant but I included them just in case they may be helpful.
UPDATE:
In case anyone else runs into a similar issue, what I ended up doing was processing the non-PDF related method first, returned a new view with the model data, and then made an AJAX call to a route that creates/generates the PDF. Hopefully that makes sense. Haha. :)
Any suggestions, tips or advice?...

How to create interactive PDF allowing the user to fill in form fields

Hi i am having a strange requirement where Client wants the PDF to be User interactive(.i.e to have fillable form fields) like entering the name in text box inside pdf and several other fields. we usually work with Activereports 9 to generate reports. I have searched a lot its not possible with Activereports9 and i gone through iTextframe library and started working with it but no clue how to get required functionality.
I was sucessful to add the textbox to the pdf but co-ordinates to place the textbox on the pdf am facing trouble.
my code is like this,
iTextSharp.text.pdf.TextField oTextField = new iTextSharp.text.pdf.TextField(oPdfStamper.Writer, new iTextSharp.text.Rectangle(171, 195, 562, 404), "txtName");
Need asssitance guys,using any other tool or iTextFrame how i can achieve the functionality.

print a form with texts in c# that looks like a word document

i am trying to create a contract (document) that I can print like a normal word document.
I have a form which is my 'entry form') where i can type in details such as customer name, amount,date of contract etc. these information is then saved in ms access.
i have another form (which i call 'contract doc'). it has labels in it and the label holds the information i typed in my 'entry form'. I assigned one label to get the values that i entered in the textbox in the entry form.
contractdoc.contract_date_label.Text = contract_date_tb.Text;
contractdoc.deposit_label.Text = deposit_tb.Text;
contractdoc.customer_name_label.Text = customer_name_tb.Text;
i also added labels and typed the rest of the documents body and position them in the 'contract doc form ' to look like an actual contract.
but i dont know how to print it like a document. i iused :
printForm1.Print();
but what happens is, it asks me to save in xls format and only a messege box that says:"printing page 1 of document" with a cancel button.
I hope you can help. thank you in advance
I presume from the name printForm1, that you're using the PrintForm class provided as part of the Visual Basic PowerPack.
In that case, you need to make sure that the PrintAction property is set appropriately. To print to a physical printer, you need to set it to PrintAction.PrintToPrinter. Depending on what printer is configured as the system default, you might also need to set the PrinterSettings property. Then when you call the Print method, it will print to the correct printer.
It's probably prompting you to save the file in XLS format because your system default printer is a virtual one that generates XLS files.

How can I generate a PDF and load the contents of an ASCX asp.net user control?

I have an ASP.NET User control, .ascx and I need to generate a PDF with the contents of the user control. Is this possible, if so can someone point me in the right direction?
Thanks
There are many third-party PDF creation tools out there. One we've used at my work that works well is PdfSharp
As far as outputting the user control's as text, this is what you need to do:
Page page = new Page();
HtmlForm form = new HtmlForm();
[USERCONTROL] uc = (ReportHeaderInfoUC)page.LoadControl("~/UserControls/[NAME OF YOUR USER CONTROL].ascx");
page.Controls.Add(form);
form.Controls.Add(uc);
//call the function on the user control that populates the control and pass in any data needed (if any)
uc.Populate(data);
StringWriter stringWriter = new StringWriter();
this.Server.Execute(page, stringWriter, false);
What you are basically doing here is creating a new WebForm, adding the user control to it and having the server render the output to a stream, which is basically what ASP.NET does for a regular page request.
To write the contents of the outputted user control to a file use:
File.WriteAllText([FILE PATH], stringWriter.ToString());
As long as the user control is only text based, it should not be difficult to output as a pdf with any pdf creation library out there (search for generating pdf, or pdf creation). If your usercontrols have more complex html tags then you basically need to buy a Html 2 Pdf library. I have not been able to find an acceptable free library that supports html to pdf.
You can also build up tables in PDF, but it would be very difficult to convert from html tables to pdf tables generically without buying a library to do that in my experience. You could also code the building of the PDF tables separately from the usercontrol, but again very difficult to combine that function into it.

Replacement for Acrobat method AFormAutLib.setButtonIcon

An existing process uses the Adobe Acrobat COM object AFormAutLib to open and fill form items. There are over 500 forms, and they all have a form field of type Button at the top. The method AFormAutLib.setButtonIcon is used to set the path of another PDF file to be used as the image on the button.
I am looking for an alternative. I have looked at iTextSharp, activePDF Tookit, and others, but have been unable to find anything that can replace this functionality.
Thanks in advance.
The solution was to use activePDF Toolkit in a different way...
APToolkitNET.FieldInfo myFI = aTK.FieldInfo(x.Key.ToString(),1);
aTK.PrintImage(logoPath, myFI.Left, myFI.Bottom, myFI.Width, myFI.Height, true, 1);
aTK.DeleteFormField(x.Key.ToString());
The button had the right location and dimensions, so the FieldInfo class is used to get those values. Then PrintImage is called with the path to the image and the locations, before the button is deleted.

Categories

Resources