Right now I am using iTextSharp to create a PDF. We have a need to put a few text boxes on this PDF for the user to fill out. The PDF if being generated dynamically since it depends on user input as to what is shown, which can increase or decrease page count depending on what is selected. When we use iTextSharp, adobe sees the fields, but says "Currently there are no form fields in this PDF....". If I hit cancel on this dialog, I see the fields are on the PDF, but something is not right. I loaded up the PDF in a trial of Aspose.PDF and it can't see the fields either. I am adding the fields in the code sample below.
var txtName1 = writer.AcroForm.AddSingleLineTextField("Name1","",_avenirDefault,Font.NORMAL, 72, writer.GetVerticalPosition(false) - 12, 275,writer.GetVerticalPosition(false) + 5);
txtName1.SetFieldFlags(PdfFormField.FF_REQUIRED);
I have also tried,
var txtName1 = new TextField(writer,
new Rectangle(72, writer.GetVerticalPosition(false) - 12, 275,
writer.GetVerticalPosition(false) + 5), "Name1") { Options = BaseField.REQUIRED };
writer.AddAnnotation(txtName1 );
Neither of these prevented the error from showing in Adobe or finding the fields in the Aspose library. Any idea why these fields are shown but not detected as Form Fields for adobe?
Related
I am trying to create a form field in a PDF where the user can insert an image file and save the document so that the image is persistent (in a new PDF document, as opposed to altering an existing document). I know this is possible, because I've seen it done in other PDFs, but I can't work out how it's supposed to be done in iText 7 for .NET/C#.
I found this on Google, which seems to at least provide the JavaScript and outline of a solution, but I don't know how to edit the "Layout" of an iText PdfButtonFormField object. I have also tried this answer from the iText website, but it's geared towards adding to an existing document, and I couldn't get it to work anyway (some more elusive System.NullReferenceException errors).
Using the idea of creating a button and replacing the image, so far I have tried:
PdfWriter writer = new PdfWriter("myfile.pdf");
PdfDocument document = new PdfDocument(writer);
PdfPage pdfPage = document.AddNewPage(PageSize.A4);
PdfCanvas canvas = new PdfCanvas(pdfPage);
PdfAcroForm form = canvas.GetForm();
PdfButtonFormField button = PdfFormField.CreateButton(document, new Rectangle(50, 50), 0);
button.SetAction(PdfAction.CreateJavaScript("event.target.buttonImportIcon();"));
form.AddField(button); // <-- Error on this line
document.Close();
writer.Close();
In the hope that the buttonImportIcon() would be enough to override the buttons appearance. But I get a System.NullReferenceException: 'Object reference not set to an instance of an object.' error at the indicated line (unfortunately it is no more specific than that), with a slightly unhelpful stacktrace:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at iText.Forms.PdfAcroForm.AddField(PdfFormField field, PdfPage page)
at iText.Forms.PdfAcroForm.AddField(PdfFormField field)
at ReplaceIcon.Main(String[] args) in ReplaceIcon.cs:line 65
I also tried replacing the CreateButton with CreatePushButton, as in:
PdfButtonFormField button = PdfFormField.CreatePushButton(document, new Rectangle(50, 50), "name", "caption");
Using which the code compiles, and I get a "Select Image" dialogue box when I click on the button in the PDF, but the button remains just a grey square with "caption" written on it, rather than being replaced by the selected image. But I suspect that a generic button is required so you can overwrite the layout (somehow).
If anyone knows how this is supposed to be done, either using this button approach or another way, I would greatly appreciate some pointers. As I said, I am specifically interested in creating these fields in a newly generated PDF document, using iText 7 in a C# program.
But I get a System.NullReferenceException: 'Object reference not set to an instance of an object.'
This is the bug in the Acroform#addField method. NPE is being thrown every time it gets a nameless field as a parameter.
To avoid it, just set the field name before adding to the form (field#setName).
Using which the code compiles, and I get a "Select Image" dialogue box when I click on the button in the PDF, but the button remains just a grey square with "caption" written on it, rather than being replaced by the selected image. But I suspect that a generic button is required so you can overwrite the layout (somehow).
the PdfFormField.CreateButton method does not give you any advantages here. That method in iText creates an empty PdfButtonFormField (appearance and behavior should be defined by the developer after a field creation).
On the other side, CreatePushButton does almost what you need.
The only thing must be adjusted is a layout. By default, the created push button has the "label only" layout.
public void Generate()
{
PdfWriter writer = new PdfWriter("myfile.pdf");
PdfDocument document = new PdfDocument(writer);
PdfAcroForm form = PdfAcroForm.GetAcroForm(document, true);
PdfButtonFormField button = PdfFormField.CreatePushButton(document, new Rectangle(20, 500, 50, 50), "btn",
"load");
button.SetAction(PdfAction.CreateJavaScript("event.target.buttonImportIcon();"));
//change the layout type.
PdfDictionary widget = (PdfDictionary) button.GetKids().Get(0).GetIndirectReference().GetRefersTo();
widget.GetAsDictionary(PdfName.MK).Put(PdfName.TP, new PdfNumber((int) PushButtonLayouts.ICON_ONLY));
form.AddField(button); // <-- Error on this line
document.Close();
}
enum PushButtonLayouts
{
LABEL_ONLY = 0, //No icon; caption only
ICON_ONLY = 1, //No caption; icon only
ICON_TOP_LABEL_BOTTOM = 2, // Caption below the icon
LABEL_TOP_ICON_BOTTOM = 3, // Caption above the icon
ICON_LEFT_LABEL_RIGHT = 4, //Caption to the right of the icon
LABEL_LEFT_ICON_RIGHT = 5, //Caption to the left of the icon
LABEL_OVER = 6 // Caption overlaid directly on the icon
}
I use PDF Clown to create PDF files containing text and different shapes. Saving the files brings the desired result. But when I print the pages or render them to bitmaps, only the shapes are visible and the text elements are missing.
I tried already different versions of the library with multiple files, always getting the same result.
Maybe someone can give me a hint on this issue.
EDIT:
This is a simplified form of the source code I use (with same result as described above; see the image at the end):
File file = new File();
Document document = file.Document;
document.PageSize = PageFormat.GetSize(PageFormat.SizeEnum.A4, PageFormat.OrientationEnum.Portrait);
Page page = new Page(document);
document.Pages.Add(page);
PrimitiveComposer composer = new PrimitiveComposer(page);
//draw a rectangle
composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.LightSalmon));
composer.DrawRectangle(new RectangleF(30, 42, 300, 32));
composer.Fill();
//draw some text
composer.SetFillColor(DeviceRGBColor.Get(System.Drawing.Color.Black));
composer.SetFont(new StandardType1Font(document, StandardType1Font.FamilyEnum.Courier, true, false), 32);
composer.ShowText("Hello World!", new PointF(32, 48));
composer.Flush();
//save the file
file.Save(#"..\document.pdf", SerializationModeEnum.Standard);
//and print it
Renderer renderer = new Renderer();
renderer.Print(file.Document, false);
Result of the above code (the printed version was created with virtual printer Adobe PDF; also tested with XPS Document Writer):
(I don't have enough points to add a comment so I put this comment as an answer.) If you look in the source for the RenderingSample class, you will see this comment:
This sample demonstrates how to render a PDF page as a raster image.
Note: rendering is currently in pre-alpha stage; therefore this sample is
nothing but an initial stub (no assumption to work!).
I don't think Stephano Chizzolini got around to finishing it.
There is another NuGet download, PDFClown.Net version 2.0.0, by Matthieu. It has tags for PDF-To-Image, Rasterizer and PDF, but I have not been able to get it to work either. I cannot find documentation for it. Inspection of the properties for the downloaded NuGET assembly shows version 0.1.2.0 instead of 2.0.0.
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.
I want to create a fillable PDF using Open Office or something which is open source. With Open Office I created the fillable PDF and it works fine with Foxit Reader; we can also save it. Now the problem is I have an image in the PDF which should also be fillable, like the other fields. User should be able to put his/her image in the image box and save it.
Later I will be reading the PDF using iTextSharp to retrieve the field's values and save it in the database. Other than the image everything works fine. I tried to create the image box with Open Office but when I open it in the PDF reader I cannot change the picture and moreover how will I read the image using iTextSharp and display it on the picture box so that user can save all the data in the database in future?
I tried with this but its showing null value
string pdfTemplate = #"c:\Temp\PDF\Untitled 1.pdf";
var reader = new PdfReader(pdfTemplate);
var output = new MemoryStream();
var stamper = new PdfStamper(reader, output);
//textBox2.Text = stamper.AcroFields.GetField("f1_09(0)");
Bitmap bimg = new Bitmap(stamper.AcroFields.GetField("ImageControl"));
System.Drawing.Image tempimg = bimg;
pictureBox1.Image = tempimg;
stamper.FormFlattening = true;
stamper.Close();
reader.Close();
It is not possible to 'put an image into a fillable form'. The PDF specification only supports fillable text, not images...
Here is a list of the general types for interactive AcroForm elements which are available according to the PDF-1.7 ISO specification:
Button fields represent interactive controls on the screen that the user can manipulate with the mouse. They include pushbuttons, check boxes, and radio buttons.
Text fields are boxes or spaces in which the user can enter text from the keyboard.
Choice fields contain several text items, at most one of which may be selected as the field value. They include scrollable list boxes and combo boxes.
Signature fields represent digital signatures and optional data for authenticating the name of the signer and the document’s contents.
As you can see, there are no images in this list...
I need to display PdfPage of a PDF Document in a WPF form. I am using Docotic.pdf.dll and want to stick to it. The PdfPage.Thumbnail is also giving me null with all PDFs.
Just confirmed from Bit Miracle, this feature has been added in Docotic.Pdf 3.4 (available on their site) and currently it is in beta phase .
Below is the code that will save the image from the Pdf.
using (PdfDocument pdf = new PdfDocument("your_file.pdf"))
{
PdfDrawOptions options = PdfDrawOptions.CreateZoom(150);
options.BackgroundColor = new PdfRgbColor(255, 255, 255); // white background, transparent by default
options.Format = PdfDrawFormat.Jpeg;
pdf.Pages[0].Save("result.jpg", options);
}
There is also Draw and print PDF group of samples that might worth to look into.
As far as I know Docotic.Pdf cannot display PDF files (at least the current version), it can only create them. The PdfPage.Thumbnail refers to the page thumbnail embedded in the PDF file, which usually is missing, this is why the property returns null.