Writing to absolute position while using direct content - itext

I am creating a pdf-document. First I add a table and some Texts to the PdfWriter. Now I want to add a costum template (including images and texts): I have to get the direct Content, which ist a layer over the PdfWriter-layer:
over= PdfWriter.getDirectContent();
I want to set the template exactly after the content on PdfWriter-layer.
I can use
writer.getVerticalPosition(true)
for my calculation of y-Position on PdfWriter-layer.
This way I can add the costum template to the upper layer at that position. Now back to PdfWriter-layer how can I set the position of PdfWriter-layer after the tempalte on over-layer?!
Can somebody help?
Thanks in advance.

Mixing content added with document.add() and direct content us always a delicate operation. You need to know the height of the custom template and then add some white space that matches that height. However: what are you going to do if the content of the custom template doesn't match the page?
I would advise against your plans, and I would recommend another approach.
I am assuming that your custom template is a PdfTemplate object. In that case, you can wrap this object inside an Image object, and use document.add() to add the template.
See for instance: Changing Font on PDF Rotated Text
In this example, we create a PdfTemplate with a bar code and some text:
PdfTemplate template = canvas.createTemplate(rect.getWidth(), rect.getHeight() + 10);
ColumnText.showTextAligned(template, Element.ALIGN_LEFT,
new Phrase("DARK GRAY", regular), 0, rect.getHeight() + 2, 0);
barcode.placeBarcode(template, BaseColor.BLACK, BaseColor.BLACK);
We want to add this template to a document with document.add(), so we wrap the template inside an Image object:
Image image = Image.getInstance(template);
We can now add this image to the document:
document.add(image);
Afterwards, we can add extra content:
Paragraph p3 = new Paragraph("SMALL", regular);
p3.setAlignment(Element.ALIGN_CENTER);
document.add(p3);
That content is added under the template at the right position. You don't need to worry anymore about getting the Y position with getVerticalPosition() and you don't need to worry about setting the Y position after adding the template. If the template doesn't fit on the current page, it will automatically be moved to the next page.
Important: Maybe you are worried about the resolution of your template. You shouldn't be. If the template consists of vector data, wrapping that data inside an Image object won't change it into raster data. The vector data will be preserved. This is important in the bar code example, because you don't want the quality of your bar code to deteriorate by making it a raster image.

Related

SetFixedPosition of Parapgraph or Image results in wrong display order

With itext7 in VB.NET i create a PDF.
There I have this code-snippet:
Dim img As New Image(ImageDataFactory.Create(imagePath))
img.SetWidth(varImgWidth)
img.SetHeight(varImgHeight)
img.SetFixedPosition(100,100)
doc.add(img)
doc.add(new Paragraph("This is a test"))
The set position of the image overlaps with the text of the parapgraph. I expected, that the image is set to the document at the given position and then - afterwards - the text is "printed" above the image, because it is defined "after" the image in the code.
But what I see is, that the image positioned with SetFiexedPosition is always above other elements. I get the behaviour when positioning a text via SetFixedPosition on the Paragraph containing the text.
My question is: Is there any possibility to tell the element which is positioned, that it should be behind other elements.
Or - better question - is there any "positioning possibility" which allows a positioning of an element and considering the "order" in which it is "printed" in the code.
Do you understand what I mean?
(Sorry, it´s not easy for me to express it in English)
Thank you for any help
Regards
Benjamin

How to insert a non-inline picture into a word document using VB6?

I am trying to insert an image into a word document using the Microsoft word 15.0 objects library included with VB^ and the only way I've seen to insert a graphics file is through this:
oDoc.Range.InlineShapes.AddPicture ("C:\Users\name\Desktop\file.jpg")
But, I want a picture that can be positioned over the text and where I want it in the document... Is there any way to do this using VB6 Code?
Word has two different ways to manage images and other embedded objects: as InlineShapes and as Shapes. The first are treated the same as characters in the text flow; the latter have text wrap foramtting and "live" in a different layer from the text.
To insert a graphics file as a Shape:
Dim shp as Word.Shape
Set shp = oDoc.Shapes.AddPicture(FileName, LinkToFile, _
SaveWithDocument, Left, Top, Width, Height, Anchor)
The AddPicture method returns a Shape object. Often, this is useful when additional properties need to be set after the object has been inserted. For example in order to specify the text wrap formatting. If no Shape object is required a Shape can be inserted without assigning to an object. In this case, leave out the parentheses:
oDoc.Shapes.AddPicture FileName, LinkToFile, _
SaveWithDocument, Left, Top, Width, Height, Anchor
While only the FileName argument is required, the last argument - Anchor - is very important if you want to control where the image is positioned when it's inserted.
It's also possible to insert as an InlineShape then use ConvertToShape in order to have a Shape object to which text wrap formatting can be applied.
Every Shape must be associated with a Range in the document. Unless otherwise specified, this will be the first character of the paragraph wherein the current selection is. I strongly recommend passing a Range to the Shapes.AddPicture method in the Anchor argument for this reason.
Note that once a Shape has been inserted there's no direct way to change the anchor position. It can be done using cut & paste. Another possibility is to use the ConvertToInlineShape method so that you can work with the Range to move the graphic, then ConvertToShape to turn it back into a Shape, but in this case a number of positioning and wrap properties may need to be reset. Here an example of using the "convert" methods:
Sub MoveShapeToOtherRange()
Dim oDoc As Word.Document
Dim shp As Word.Shape
Dim ils As Word.InlineShape
Dim rngEnd As Word.Range, rngStart As Word.Range
Set oDoc = ActiveDocument
Set rngStart = oDoc.content
rngStart.Collapse wdCollapseStart 'start of document
Set rngEnd = Selection.Range
Set shp = oDoc.shapes.AddPicture(fileName:="C:\Test\icons\Addin_Icon16x16.png", _
Top:=0, Left:=10, anchor:=rngStart)
Set ils = shp.ConvertToInlineShape
Set rngStart = ils.Range
rngEnd.FormattedText = rngStart.FormattedText
rngStart.Delete
Set ils = oDoc.InlineShapes(1)
Set shp = ils.ConvertToShape
End Sub
By default, a Shape will insert with MoveWithText activated. That means the position on the page is not set, editing will affect the vertical position. If you want the Shape to always be centered on the page, for example, set this to false. Note, however, that if the anchor point moves to a different page, the Shape will also move to that page.
On occasion, the Left and Top arguments don't "take" when adding a Shape - you may need to set these again as properties after adding it.
Ok, What I ended up doing that worked was this:
Dim a As Object
On Error Resume Next
a = oDoc.Shapes.AddPicture("C:\Users\name\Desktop\file.jpg", , , 25, 25, 25, 25)
For some reason, This places the image at the position and size. When I looked at the docs for ".AddPicture" I realized it returns a Shapes object. So I just had it store it in a throw-away object. For some reason it then would respond with an error, but would end up placing on the doc anyways. So I used:
On Error Resume Next
This skips the error. After that, the picture places as expected and the rest of the doc is made as expected
Thank you for your answers

Access Form layout and design: Header: How do I make my header section look like Google

I would like to rip off Google's design for my Continuous Form. The detail section of the form is set up to display N number of records resulting from a search, and thus cannot be used to create this effect (i think). Everything must go in the header section.
there are 2 primary issues I would like to address in this question:
Two toned background. The header section should have a grey stripe and a white stripe. This stripe needs to extend the full width of the form, which is variable and will depend on the user. (i'm using tabs not pop-ups)
How to right justify certain elements of the header so that they stay close to the right edge, wherever that may fall, just like your account information on Google.
The "Search Results" in the detail section are loaded by setting the form's recordSource to the results of a query defined in VBA, which takes parameters from the search box. The form is continuous.
Any ideas how to hack this into place?
Recent versions of MS Access provide improved form layout features when using the ACCDB database file format.
The screen captures below are based on a form in Access 2010. The second image is after the form width was expanded, but it's scaled down for display on this web page. However you can open those images directly to compare their relative widths.
The grey color is from the form header's Back Color property. The white box is a simple text box whose Back Color is white and Back Style is Normal (not Transparent).
The text box's Horizontal Anchor property is Both, and its Can Grow property is Yes. The other 3 items ("?", "Button 2", and "Button 3") are command buttons. Their Horizontal Anchors are set to Right and their Can Grow properties are No.
The result of those properties is that when the form expands, those command buttons maintain their size are are kept right-aligned within the form. And the text box stretches to fill the remaining available space.
Note this behavior is accomplished without any VBA code.
I think these layout capabilities were introduced in Access 2007 and perhaps refined in 2010.
For the background, use two rectangles with transparent borders, one back color gray, one white. You can size them to the form by using the form's InsideWidth property. For example:
Private Sub Form_Resize()
rect1.Width = Me.InsideWidth
rect2.Width = Me.InsideWidth
End Sub
I would do a similar thing for the buttons/images/etc you want right justified. Set their Left property relative to the form's width:
mySettingsButton.Left = Me.InsideWidth - 300
Keep in mind all the measurements are twips (1440 twips/inch)

Text not fitting into form fields (iTextSharp)

I created a .PDF file using Adobe Acrobat Pro. The file has several text fields. Using iTextSharp, I'm able to populate all the fields and mail out the .PDF.
One thing is bugging me - some of the next will not "fit" in the textbox. In Adobe, if I type more that the allocated height, the scroll bar kicks in - this happens when font size is NOT set to auto and multi-line is allowed.
However, when I attempt to set the following properties:
//qSize is float and set to 15;
//auto size of font is not being set here.
pdfFormFields.SetFieldProperty("notification_desc", "textsize", qSize, null);
// set multiline
pdfFormFields.SetFieldProperty("notification_desc", "setfflags", PdfFormField.FF_MULTILINE, null);
//fill the field
pdfFormFields.SetField("notification_desc", complaintinfo.OWNER_DESC);
However upon compilation and after stamping, the scroll bar does not appear in the final .PDF.
I'm not sure if this is the right thing to do. I'm thinking that perhaps I should create a table and flood it with the the text but the documentation makes little or no reference to scroll bars....
When you flatten a document, you remove all interactivity. Expecting working scroll bars on a flattened form, is similar to expecting working scroll bars on printed paper. That's why you don't get a lot of response to your question: it's kind of absurd.
When you fill out a rectangle with text, all text that doesn't fit will be omitted. That's why some people set the font size to 0. In this case, the font size will be adapted to make the text fit. I don't know if that's an option for you as you clearly state that the font size must be 15 pt.
If you can't change the font size, you shouldn't expect the AcroForm form field to adapt itself to the content. ISO-32000-1 is clear about that: the coordinates of a text field are fixed.
Your only alternative is to take control over how iText should fill the field. I made an example showing how to do this in the context of my book: MovieAds.java/MovieAds.cs. In this example, I ask the field for its coordinates:
AcroFields.FieldPosition f = form.GetFieldPositions(TEXT)[0];
This object gives you the page number f.page and a Rectangle f.position. You can use these variables in combination with ColumnText to add the content exactly the way you want to (and to check if all content has been added).
I hope you understand that:
it's only normal that there are no scroll bars on a flattened form,
the standard way of filling out fields clips content that doesn't fit,
you need to do more programming if you want a custom result.
For more info: please consult "iText in Action - Second Edition".

Is it possible to "shrink" a PdfPtable?

I am currently working with Itextsharp and I have some trouble with PDfPtables.
Sometimes they get too big for a page and, when added to a document, are broken up on multiple pages.
Sadly ths rational behviour is not acceptable for some of my superiors - they keep insisting that the table shall be "shrunk" to a page. Is there a way to achieve this? There are some tantalizing hints that i could be possible - but hints are all that i have.
What is the alternative? Perhaps I could delete the fat table from the document and build the table again with smaller Fonts and Cells, but that would be very cumberosme - I would prefer to "zoom out", in lack of a better word.
My current code:
Dim test As PdfContentByte = mywriter.DirectContent
Dim templ = test.CreateTemplate(mywriter.PageSize.Width, mywriter.PageSize.Height)
Table.WriteSelectedRows(0, Table.Rows.Count - 1, 0.0F, mywriter.PageSize.Height, templ)
Dim myimage = Image.GetInstance(templ)
' myimage.ScaleAbsolute(mywriter.PageSize.Width, mywriter.PageSize.Height)
would scaleabsolute be necessary?
myimage.SetAbsolutePosition(0, 0)
test.AddImage(myimage)
This code puts something one the page, but it has the height of the page and the width is about a quarter of the page - wi will try to find the bug...
Create the table and define a 'total width'. As soon as iText knows the width of the table, you can calculate the height of all the rows. Once you know the height, you can check:
Does the table fit the page? Just add it as is. Maybe using WriteSelectedRows if you don't want to take any page margins into account.
Isn't there enough space on the page? Add the table to a PdfTemplate (there's more than one way to do this), wrap the PdfTemplate inside an Image. Scale the image, and add it to the document.