This question already has answers here:
Bold a single word within a sentence with iTextSharp
(3 answers)
Closed 7 years ago.
I am writing a simple program on CSharp. What it does, is that you write a text(in a richTextBox), and it saves it's content on a .pdf in a specific path. Now, i want the user to target a specific word/phrase, and by clicking a button, the specific word/phrase will go bold. Is this possible ?? And if yes, how ??
First of all check this out : http://www.mikesdotnetting.com/article/81/itextsharp-working-with-fonts
What basically you need to do is to call the SetFieldProperty function
You take your old pdf, edit it and out put a new PDF with a bolded textbox.
MemoryStream stream = new MemoryStream(); //Memory stream to with new pdf with changed bold text
PdfReader pdfReader = new PdfReader("file.pdf"); //The original PDF
PdfStamper stamper = new PdfStamper(pdfReader, stream); //A stamper to create the pdf
SetFieldProperty("fieldName","textfont",BaseFont.COURIER_BOLD,null); //Change textbox properties
SetField("fieldName","TEXT"); //Change field text
stamper.Close(); //Save and close
byte[] newFile = stream.ToArray(); //Here you have your new file with the bolded text
This code will give you a new file with the bolded text
I think you can input an html to itextsharp. This way you can control many styling attributes using html. for ex: This is bold text. can be input as :
This is <b>bold</b> text.
You can use Html to customize the styling and color of the text like this. Although I haven't tried using external stylesheets using itextsharp, but the inline styling works fine.
Related
I have a PDF template with form fields where I used iText 7.1.0 to replace some placeholder text inside different form fields which is working fine:
PdfReader reader = new PdfReader(pdf_template);
PdfDocument pdf = new PdfDocument(reader, new PdfWriter(pdf_output));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
form.GetField("Vertragsnummer").SetValue("12345");
form.FlattenFields();
pdf.Close();
reader.Close();
Now I have a form field for which option is set to allow rich text format as there are some parts with bold font etc..
Inside the form field standard text there are again placeholders which I want to replace.
Currently I've got the following code:
PdfFormField tf_maintext = form.GetField("maintext");
tf_maintext.SetRichText(tf_maintext.GetRichText());
Now I need to replace the placeholders but I didn't find any working solution for C#.
For normal form fields I can use
PdfFormField kdnr = form.GetField("Kundennummer");
kdnr.SetValue(kdnr.GetValueAsString().Replace("7777777777", "1111111111"));
the normal Replace function to achieve this.
But I need a solution to replace text in the rich text format field to ensure that the formatting is still there afterwards.
How can I do this?
I've got a WPF control with several text boxes on it. The number of controls is variable depending on what the user does in the program. What I need to do is take the text from the text boxes and write it to a XPS doc. What the question really boils down to is "How do I write lines of text to a XPS doc? Does anybody know of a library I can incorporate or what the best way to do this is?
Here is simplest sample I saved to my OneNote some time ago (sorry I dont remember the source):
PrintDocumentImageableArea area = null;
XpsDocumentWriter wr = PrintQueue.CreateXpsDocumentWriter(ref area);
var text = new TextBlock() {Text = "Hello there"};
text.Margin = new Thickness(area.OriginWidth, area.OriginHeight, 0, 0);
Size outputSize = new Size(area.MediaSizeWidth, area.MediaSizeHeight);
text.Measure(outputSize);
text.Arrange(new Rect(outputSize));
text.UpdateLayout();
wr.Write(text);
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.
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 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...