iTextSharp - Force text below image - itext

My issue with images and iTextSharp is that I want to put a header in and the have the text below that point.
I can display the image and wrap the text but not get it to display below, even when adding New paragraph.
I am assuming there is some command around the aligmnent but I am struggling to find it even when looking at tutorials such as http://www.mikesdotnetting.com/Article/87/iTextSharp-Working-with-images
'Import Image
Dim vImagePath = "C:\sites\images\" & "Logo.jpg"
Dim jpg As Image = Image.GetInstance(vImagePath)
jpg.ScaleToFit(139.0F, 106.0F)
jpg.Alignment = Image.ALIGN_RIGHT Or Image.TEXTWRAP
jpg.IndentationLeft = 7.0F
jpg.SpacingAfter = 9.0F
jpg.BorderWidthTop = 6.0F
jpg.BorderColorTop = Color.WHITE
document.Add(jpg)
'##Main Document Header
Dim tableHeader As New PdfPTable(1) ' Number in brackets is number of columns
tableHeader.DefaultCell.Border = Rectangle.NO_BORDER
Dim phFirst As Phrase = New Phrase("Title", HeaderFont)
tableHeader.AddCell(phFirst)
document.Add(tableHeader)

Related

Can I increase a number by a macro in LibreOffice Impress?

I want to make a counter with Libreoffice Impress.
Each Time I click on a rectangle, the number inside increases.
Dim Doc As Object
Dim Page As Object
Dim Rect As Object
Doc = ThisComponent
Page = Doc.drawPages.getByName("Test")
Rect = Page.getByIndex(0)
Rect.String = CInt(Rect.String) + 1
It works everywhere except in presentation mode.
Is it possible to make it work in presentation mode ?

Why does Rotation change not change the crop box?

I want to crop / remove / cut away a part of a pdf page.
Somebody provided a code for me to do that in this question.
2 new problems occured:
I want to print the PDF in a different orientation
I want to cut away more of the blank space around the DHL label
Because dealing with the coordinates in iTextSharp requires (0,0) based coders to twist their mind a bit, I first wanted to rotate the PDF so that I can follow it along more easily.
I did that using the following code:
Dim testFile As String = "new pdf1.pdf"
Dim resultFile As String = "new pdf1-Rotated.pdf"
Using pdfReader As PdfReader = New PdfReader(testFile)
Using pdfStamper As PdfStamper = New PdfStamper(pdfReader, File.Create(resultFile))
For i As Integer = 1 To pdfReader.NumberOfPages
Dim rotate As Integer = pdfReader.GetPageRotation(i)
Dim pageDictionary As PdfDictionary = pdfReader.GetPageN(i)
pageDictionary.Put(PdfName.ROTATE, New PdfNumber(90))
Next
End Using
End Using
It does work.
I expected that the page rotation change would automatically affect the crop box.
However, it does not.
Dim testFile As String = "new pdf1.pdf" 'they both give the same cropbox
Dim testFile As String = "new pdf1-Rotated.pdf"'they both give the same cropbox
Using pdfReader As PdfReader = New PdfReader(testFile)
For i As Integer = 1 To pdfReader.NumberOfPages
Dim cropBox As Rectangle = pdfReader.GetCropBox(i)
Why is that so?

iText -- How do I get the rendered dimensions of text?

I would like to find out information about the layout of text in a PdfPCell. I'm aware of BaseFont.getWidthPointKerned(), but I'm looking for more detailed information like:
How many lines would a string need if rendered in a cell of a given width (say, 30pt)? What would the height in points of the PdfPCell be?
Give me the prefix or suffix of a string that fits in a cell of a given width and height. That is, if I have to render the text "Today is a good day to die" in a specific font in a PdfPCell of width 12pt and height 20pt, what portion of the string would fit in the available space?
Where does iText break a given string when asked to render it in a cell of a given width?
This is with regard to iText 2.1.6. Thanks.
iText uses the ColumnText class to render content to a cell. This is explained in my book on page 98-99. This means that, just like with ColumnText, you need to make the distinction between text mode and composite mode.
In any case, ColumnText measures the width of the characters and tests if they fit the available width. If not, the text is split. You can change the split behavior in different ways: by introducing hyphenation or by defining a custom split character.
I've written a small proof of concept to show how you could implement custom "truncation" behavior. See the TruncateTextInCell example.
Instead of adding the content to the cell, I have an empty cell for which I define a cell event. I pass the long text "D2 is a cell with more content than we can fit into the cell." to this event.
In the event, I use a fancy algorithm: I want the text to be truncated in the middle and insert "..." at the place where I truncated the text.
BaseFont bf = BaseFont.createFont();
Font font = new Font(bf, 12);
float availableWidth = position.getWidth();
int contentLength = content.length();
int leftChar = 0;
int rightChar = contentLength - 1;
availableWidth -= bf.getWidthPoint("...", 12);
while (leftChar < contentLength && rightChar != leftChar) {
availableWidth -= bf.getWidthPoint(content.charAt(leftChar), 12);
if (availableWidth > 0)
leftChar++;
else
break;
availableWidth -= bf.getWidthPoint(content.charAt(rightChar), 12);
if (availableWidth > 0)
rightChar--;
else
break;
}
String newContent = content.substring(0, leftChar) + "..." + content.substring(rightChar);
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(position);
ct.addElement(new Paragraph(newContent, font));
ct.go();
As you can see, we get the available width from the position parameter and we check how many characters match, alternating between a character at the start and a character at the end of the content.
The result is shown in the resulting PDF: the content is truncated like this: "D2 is a c... the cell."
Your question about "how many lines" can be solved in a similar way. The ColumnText class has a getLinesWritten() method that gives you that information. You can find more info about positioning a ColumnText object in my answer to your other question: Can I tell iText how to clip text to fit in a cell

How do I insert right justified text in itextsharp?

I have a PDF into which I need to define very precise rectangles into which I need to inject text. i.e. the PDF is a template and I need to be able to specify a rectangular area and then have text aligned to the bottom of the rectangle and to the right.
I have tried something like this:-
PdfContentByte over = ps.GetOverContent(1);
font = new Font(bf, 47, Font.NORMAL, new Color(190, 210, 49));
ct = new ColumnText(over);
myText = new Phrase("3.5", font);
ct.Alignment = Element.ALIGN_RIGHT | Element.ALIGN_BOTTOM;
ct.SetSimpleColumn(myText, 446.5f, 314.5f, 536.5f, 294.5f, 47, Element.ALIGN_RIGHT | Element.ALIGN_BOTTOM);
ct.Go();
Not sure what I am doing wrong but it doesn't work....
Instead of giving absolute positions to the text, why dont you develop a table structure and then place text inside the cells.
Give the alignments and all to the cells
Use PdfPCell and PdfPTable

Add Header with image in word corrupts document

I have a Header with a table (1 row , 3 cells).In first cell there is a image and in 2nd and 3rd there are some text paragraphs
I append header to doc dynamically like this:
HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>();
string rId = mainPart.GetIdOfPart(headerPart);
HeaderReference headerRef = new HeaderReference();
headerRef.Id = rId;
headerRef.Type = HeaderFooterValues.Default;
sectionProps.RemoveAllChildren<HeaderReference>();
sectionProps.Append(headerRef);
headerPart.Header = HeaderCreator.GenerateHeader();
headerPart.Header.Save();
In document there is a page with a group of shapes.
Everything works ok if I
Remove the page with shapes
Remove the logo from Header
Any suggestions?