Showing image on a acro text field position - itext

I had a PDF document which has acro text fields which is already shared to the client. Now the client wants to insert signature image in one of the text fields. My manager asked me to try a way to do the same.My idea is to replace an image on top of the text field position and resize the text field as image size.
For replacing acro text field of pdf into image, i am trying as below
1.Finding the text field by its field id
String position = null;
List<FieldPosition> fieldPositons = form.getFieldPositions("50106");
for (FieldPosition position :fieldPositons) {
this.position = position.position;
}
2. Setting the image into that position of text field
Image image = Image.getInstance("ImageFileName");
Float dimensions = position.split("x");
image.setAbsolutePosition(dimensions[0], dimensions[1]);
content.addImage(image);
Based on the given image width and height i need to change the width and height of acro text field.
Can any one tried as below, Is my logic works with itext pdf library. Let me know if any idea to replace acro text field with image

It is never a good idea to change the dimensions of an AcroField as all the content in a PDF is added at absolute positions. When you change the dimension of a field, you risk that it will overlap with other content.
Hence, your best option is to adapt the size of the image to the size of the AcroField. As you already indicated, you can get the field position like this:
AcroFields.FieldPosition f = form.GetFieldPositions("50106")[0];
Note that it's not a good idea to make this a string. You can use the FieldPosition object like this:
int page = f.page;
Rectangle rect = f.position;
You can scale and position the image like this:
Image image = Image.getInstance("ImageFileName");
image.ScaleToFit(rect.Width, rect.Height);
image.SetAbsolutePosition(rect.Left, rect.Bottom);
The ScaleToFit() method will scale the image in such a way that it will fit the dimensions of the form field, respecting the original aspect ratio of the image (you don't want to add a stretched image).
You need to page variable to add the image to the correct page:
PdfContentByte canvas = stamper.GetOverContent(page);
canvas.AddImage(image);
Important:
If you add the image as described above, you need to remove the field (this happens automatically if you flatten the form).
If the form field is a button, then you shouldn't use the above code. Instead you should replace the icon of the button. This is described in my answer to the question How to change a Button Icon of a PDF Formular with itextsharp? This answer explains the standard way to do what you're trying to do, but it requires that the field where you want to add the image is a button field (and based on your question, it seems that it's a text field).

Related

Unity Resizing Dropdown Dynamically

I have a dropdown list on a filter panel which needs to stretch dynamically to fit the content as shown:
I have tried several content size fitters but cannot find anything, If possible I would like to set a max width it can expand to then truncate everything longer than that, I would also like it to expand only to the right with a right pivot point. I have found a similar example here: https://forum.unity3d.com/threads/resize-standard-dropdown-to-fit-content-width.400502/
Thanks!
Well. Lets start with the code from that Unity forum thread.
var widest = 0f;
foreach (var item in _inputMines.GetComponentsInChildren<Text>()) {
widest = Mathf.Max(item.preferredWidth, widest);
}
_inputMines.GetComponent<LayoutElement>().preferredWidth = widest + 40;
We want to have a max-width allowed, so any content longer than this will be truncated. So let's add that variable:
var maxWidth = 250f;
//or whatever value; you may wish this to be a property so you can edit it in the inspector
var widest = 0f;
foreach (var item in _inputMines.GetComponentsInChildren<Text>()) {
Now we use the smaller of the two width values. Then apply it to the content layout:
}
widest = Mathf.Min(maxWidth, widest);
_inputMines.GetComponent<LayoutElement>().preferredWidth = widest + 40;
The +40 should be retained because that deals with the scrollbar. Your maxWidth value should be chosen to account for this.
Finally we want the longer items to get cut off nicely.
Give each dropdown item a Rect Mask 2D component (Component -> UI -> Rect Mask 2D). The template object exists in the scene hierarchy before the game is run, just disabled. You can just add the component to the parent transform, the one with the image (so the text is clipped).
You'll need to make sure that the mask covers the same width as the image graphic and expands along with it, possibly slightly shorter on the X direction so the text gets cut off before the image border is drawn. This should happen automatically, but you will need to check and possibly make some alterations to the template object. Alternatively you can use an Image Mask, but you'll have to play with that one yourself, but it will allow for non-rectangular clipping.
That's it!

Drawing graphical objects (boxes and lines) inside a structured iText(Sharp) document (Chapters and Sections)

I'm creating a PDF document using iTextSharp, what I'm doing is generating all of my content in a c# List<Chapter> where the Chapters contain one or more Sections, and the Chapters have not yet been added to the document. I then enumerate through my List<Chapter> to generate a table of contents at the start of the document, and then add the Chapters to the document after my TOC.
That works great when my Sections contain text and images, but now I need to generate a Section containing boxes and lines. I don't want to draw my boxes and lines into an image and drop the image into the Section, that won't look as good as if I have actual PDF boxes and lines.
The Sections containing graphical elements can be intermixed with Sections containing text, so I need a way to add some kind of element to a Section such that that graphical Section works like text Sections in terms of going onto a new page only if necessary.
What's the best way to do this? I feel like it somehow involves PdfTemplates but I'm not sure how. Or maybe I need to create a PdfPTable and create my graphical elements in an IPdfPCellEvent?
You are on the right track when you want to involve PdfTemplate elements. PdfTemplate is an iText object that corresponds with the concept of Form XObjects in the PDF specification. We chose another name because the word Form is somewhat misleading (people confuse it with form fields, interactive forms, etc).
The content stream of a page in PDF is a sequence of PDF syntax, consisting of operands and operators. An XObject is an object that is external to this content stream. The content of an XObject is stored inside the PDF document only once, but it can be reused many times on the same page, on different pages.
There are different types of XObjects, but Image XObjects and Form XObjects are the most important ones.
Image XObjects are used when we work with raster images. You are absolutely right when you write: *"I don't want to draw my boxes and lines into an image and drop the image into the Section, that won't look as good as if I have actual PDF boxes and lines."
Form XObjects are used when we want to reuse PDF syntax. This is what you need: you want to define moveTo(), lineTo(), curveTo(), stroke(), fill(),... operations, and you want these lines and shapes to be stored as vector data.
The solution to your problem is to draw lines and shapes to a PdfTemplate object and to wrap the PdfTemplate object inside an Image object. When you add that Image object to a Section or a Chapter, it will be added as a Form XObject. You don't have to feat that it will be degraded into a raster image.
You can find some examples of this technique on the official web site. For instance in the answer to the question
How to generate 2D barcode as vector image?
Here we create a PdfTemplate with a bar code and we return it as an Image object. The screen shot that shows you the internals of the resulting PDF proves that the bar code is added as a vector image.
public Image createBarcode(PdfContentByte cb, String text,
float mh, float mw) throws BadElementException {
BarcodePDF417 pf = new BarcodePDF417();
pf.setText("BarcodePDF417 barcode");
Rectangle size = pf.getBarcodeSize();
PdfTemplate template = cb.createTemplate(
mw * size.getWidth(), mh * size.getHeight());
pf.placeBarcode(template, BaseColor.BLACK, mh, mw);
return Image.getInstance(template);
}
To create a PdfTemplate object, you need a PdfContentByte instance (e.g. using writer.getDirectContent()) and use the createTemplate() method passing a width and a height as parameters. Then you draw content to the PdfTemplate and turn it into an Image object using Image.getInstance().
You'll find more info on drawing lines and shapes in the chapter on Absolute positioning of lines and shapes and in the example section of Chapter 3 and Chapter 14 of my book.

How to manipulate size and position of image inside a digital signature rectangle? Itextsharp

I need to simultaneously stamp both a string (details of the user) and an image containing an actual signature in a pdfDocument. The string and image are successfully stamped on the document, however, the image of the actual signature is being stretched to fit the rectangle of the whole signature. how do i fix this and manipulate the size and position of the image?
I used both Layer2Text and Image function in one stamper declaration. Can anyone help me?
Thanks in advance
Instead of merely setting the Layer2Text and Image and letting iTextSharp arrange everything, you can create the signature appearance all by yourself on the PdfTemplate returned by GetLayer(2).
If you need some inspiration on how to fill that layer, you can look at the PdfSignatureAppearance.cs method GetAppearance(), more exactly the section between
if (app[2] == null) {
and
}
if (app[3] == null && !acro6Layers) {
This is where iTextSharp creates the default appearance on the PdfTemplate t. Your code may vary only slightly.

Adding text below text box

I have a text item on one of my plots, placed at a certain location, with a certain orientation:
th=text(x,y,'some text','HorizontalAlignment','right','rotation',rot);
Based on its position (get(th, 'Position')), or any other property, is there any way to compute the lower bound of its bounding box? I want to place another text just below it.
You could use the Extent property. You will probably need to tweak the alignments depending on what you're after, but something like this:
th = text(x,y,'some text','HorizontalAlignment','right','rotation',rot);
ex = get(th,'Extent');
text(ex(1),ex(2),'Text Below')

get the exact position of text from image in tesseract

Using GetHOCRText(0) method in tesseract I'm able to retrieve the text in html and on presenting the html in webview i'm able get the text but the postion of text in image is different from the output. Any idea is highly helpful.
tesseract->SetInputName("word");
tesseract->SetOutputName("xyz");
tesseract->Recognize(NULL);
char *utf8Text=tesseract->GetHOCRText(0);
and output image
If you have the hocr output, you should have a tag for each word. These tags should have class="ocrx_word" and name="bbox x1 y1 x2 y2" where the x and y are the top left and bottom right corner of the bounding box around the word. I don't think it's possible to automatically use this information to format a text document - would require translating pixel differences to number of tabs/spaces. But, you should be able to render text in the given location.
GetBoxText() method will return exact position of each characters in an array.
char *boxtext = _tesseract->GetBoxText(0);
NSString* aBoxText = [NSString stringWithUTF8String:boxtext];