SO i have a created a PDF template form with textbox which are editable fields. I am able to generate the pre-populated PDF with values from my database into the template through MVC 4.0 Application. Which works fine. Now i want to add a image from a folder into the PDF which will distinguished one form with another form. The image will depend on the user in-put. Image will go at the bottom of the PDF. I don't see any image-box or image container as a filed option. Only one i can see are text-box,checkbox,radio,list box etc but nothing like a iimage holder.
Dose any one know how to add image dynamically into PDF?
You can find the answer to your question in the official documentation, more specifically in chapter 8. In section 8.2.3, entitled "Pushbuttons", I explain that we usually use buttons as placeholders for images, because buttons can have an icon.
The ReplaceIcon example shows how you can replace the icon of an exising button. As you are using C#, you may want to take a look at ReplaceIcon.cs:
PdfReader reader = new PdfReader(aPdf);
using (MemoryStream ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms)) {
AcroFields form = stamper.AcroFields;
PushbuttonField ad = form.GetNewPushbuttonFromField("button_name");
ad.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
ad.ProportionalIcon = true;
ad.Image = Image.GetInstance(yourImage);
form.ReplacePushbuttonField("button_name", ad.Field);
}
}
// ms will contain your PDF in memory
}
reader.Close();
Note that the line ad.ProportionalIcon = true; will scale your image so that it fits the button.
Related
Question Revised and clarified (Thanks to Bruno for point me in the right direction)-
This post was originally made under great sleep deprivation and after reading, I see how it could be confusing. I want to make sure that others have this solution in the future and not have to spend loads of time trying to figure it out.
Here is my question and there is an answer below that solves it.
I have a pdf form and I need to add an image to a specific location where I have put a place holder. How can I do this?
Just for future people who may be looking for the answer on how to best add an image to a pre-defined specific location into a pre-existing .pdf form, here is the answer.
/* This codes works for those who have a pre-created pdf form with a button in the location and size you want your image.
I reccomend acrobat pro, thought it has a monthly cost. There are additional free PDF editors
instantiate a new PdfStamper that will create a new file from my existing form. The Reader reads in your template and the stamper makes a copy
FileToPath and FileFromPath are strings i created above that hold the path
*/
using (PdfStamper stamper = new PdfStamper(new PdfReader(fileFromPath), File.Create(FileToPath)))
{
//get my buttons positions current location and height btn1 is the name of my button that exists in the pdf already
AcroFields.FieldPosition fieldPosition = stamper.AcroFields.GetFieldPositions("btn1")[0];
//create a new button utilze the field position to set it's location. FYI this is a rectangle. I reccomend you read about those
//it will save you tons of time to understand them.
PushbuttonField imageField = new
//btn1Replaced is what I named the new button that will overwrite the old place holder button
PushbuttonField(stamper.Writer, fieldPosition.position, "btn1Replaced");
//Here I set they layout from my old button to my new one
imageField.Layout = PushbuttonField.LAYOUT_ICON_ONLY;
//grab the image you want in your pdf imgPath is a string I wrone above to grab the image
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance("imgPath");
//set your buttons image property to be your image you just grabbed
imageField.Image = img;
//always scale to the size of the button
imageField.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
imageField.ProportionalIcon = false;
//make sure your button is read only and then it will not act like a button, it will act like an image.
imageField.Options = BaseField.READ_ONLY;
//Get rid of the old button
stamper.AcroFields.RemoveField("btn1");
//add my button and make sure it is on the correct page
stamper.AddAnnotation(imageField.Field, fieldPosition.page);
stamper.Close();
}
I'm facing a problem when filling a PDF form using iTextSharp, I'm using the following code to fill the PDF form:
PdfReader pdfReader = new PdfReader(Properties.Resources.ConfirmationFees);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(folderPath + "\\" +fileName, FileMode.Create));
AcroFields pdfFFields = pdfStamper.AcroFields;
pdfFFields.SetFieldProperty("Text1", "textsize", 10.0f, null);
pdfFFields.SetField("Text1", serialNumber.ToString("D6") + "№");
pdfStamper.FormFlattening = false;
// close the pdf
pdfStamper.Close();
When I open the PDF, I have to select the textField and go to Properties, select border color or fill color, and click on "No Color". or just simply add a character to the textField.
I've tried to set the border and background color of the textField to null, but without luck.
So, how can I solve this problem without doing the mentioned way?
How did you create your form? If with Open/Libre Office, then the forms are a little bit crappy. You may need to add this line:
pdfFFields.setGenerateAppearances(true);
In your specific C# snippet, that would be:
pdfFFields.GenerateAppearances = true;
See also:
AcroForm values missing after flattening
PdfCopy and form values with iText: form values not visible
If this doesn't solve your problem, you need to tell us which version of iTextSharp you're using. If it's older than 5.5.1, please upgrade.
I'm using Aspose.Pdf for .NET.
What I'm trying to do is:
Make pdf with 1 layer (best solution would be if before generate pdf all used text and bacground image would be as picture and then generated pdf)
I need it for prohibit changing content in my pdf simple secure pdf file isn't enough, because even online pdf to doc converters enable to change content.
So is there a way to do this now?
Or is there any way for make image from content before put it on site of pdf?
I was able to generate pdf but with multiple layers (2 in my scenario).
I've use dll version 5.0.0 for .net 4.0 Client.
Thanks in advance:)
Using Aspose.Pdf, you can set several privileges on the PDF documents to control their use. Specify the security options as follows using Aspose.Pdf and the Pdf document you have generated will behave as a readonly document which could not be edited:
//Instantiate Pdf instance by calling its empty constructor
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
//Assign a security instance to Pdf object
pdf1.Security = new Aspose.Pdf.Generator.Security();
//Restrict annotation modification
pdf1.Security.IsAnnotationsModifyingAllowed = false;
//Restrict contents modification
pdf1.Security.IsContentsModifyingAllowed = false;
//Restrict copying the data
pdf1.Security.IsCopyingAllowed = false;
//Allow to print the document
pdf1.Security.IsPrintingAllowed = true;
//Restrict form filling
pdf1.Security.IsFormFillingAllowed = false;
//Add a section in the Pdf
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
//Create a text paragraph and set top margin
Aspose.Pdf.Generator.Text text1 = new Aspose.Pdf.Generator.Text(sec1,"this is text content");
text1.Margin.Top = 30;
//Add image
Aspose.Pdf.Generator.Image img = new Aspose.Pdf.Generator.Image();
img.ImageInfo.File = "asposelogo.png";
img.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png;
//Add the text paragraph and image to the section
sec1.Paragraphs.Add(text1);
sec1.Paragraphs.Add(img);
//Save the Pdf
pdf1.Save("test.pdf");
As far as creating the whole content of PDF as an embedded image, it is not directly supported in Aspose.Pdf. You can however use some way around or some other component to generate image with your content and then can use this image to create Pdf file using Aspose.Pdf as follows:
Generate PDF with your content (A multi layer PDF as you have created earlier)
Export the PDF to image as described in "Working with Images" section of Aspose.Pdf documentation.
Use exported image to create PDF as described in "Working with Images(Generator)" and distribute your document.
My name is Iqbal and I am developer evangelist at Aspose.
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.