I am trying to write some tests for PDF files and rather than testing the binary output of a cached PDF against one that has just been generated I am looking into generating the Document and then examining it against a set of known parameters such as positions.
The version of itext that is being used is 7.0.0.
Is it possible to retrieve all of this information from the Document object? I can see that the Document has a childElements object which I cannot access as it is protected.
As an example, there is a iText div added which I would like to retrieve and then extract the positioning values from it.
public class Address extends Div
{
public Address()
{
final Paragraph address = new Paragraph(
"Address 1\n" +
"Address 2\n");
address.setMarginTop(top);
add(address);
}
}
Document document;
document.add(new Address());
I have tried to keep the example as concise as possible without adding in unneccessary code as the there is a lot of code to build the PDF.
What I would like to achieve is to retrieve information from the Address div such as the margin top value using the document object.
Related
Simple problem but can't find a solution: I have a Thymeleaf form used to add a new object, say of a Book class. It works perfectly well and I only need that particular form for adding new objects, not editing the existing ones. The question is: how can I put several objects of the Book class in the same single form? So, purely for convenience, instead of filling form for a single book and clicking Send you can fill form for several books at once and only then click Send, have them all inserted into the database (in whatever order) and also have the option to fill the form partially (e.g. the form has room for 5 books but it will also accept 1, 2, 3 or 4 and you can leave the rest blank).
Edit: I've tried passing a list of object to the Thymeleaf template with the form bound to the whole list and iteration inside, but Thymeleaf throws BingingResultError upon rendering it.
You need to use a wrapper object to realize what you want.
Something like:
public class BooksCreationDto {
private List<Book> books;
// default and parameterized constructor
public void addBook(Book book) {
this.books.add(book);
}
// getter and setter
}
Then you need to pass this object as a model attribute in your controller:
BooksCreationDto booksForm = new BooksCreationDto();
model.addAttribute("form", booksForm);
bind fields using index property
th:field="*{books[__${itemStat.index}__].title}"
and get back the result with
#ModelAttribute BooksCreationDto form
in your controller.
For a complete and detailled explaination visit: https://www.baeldung.com/thymeleaf-list
I have a code generating a full report but I want to add an image in the header so I created a template and tried out. But now the word file only gives me the name of the objects in the report and not the actual content like this:
Do anyone know where does this problem comes from?
My code is
%% init
import mlreportgen.report.*
import mlreportgen.dom.*
rpt = Document('Report','docx', 'template.dotx');
moveToNextHole(rpt)
%% chapter
ChapterRegression = Chapter;
ChapterRegression.Title = 'Summary';
append(rpt, ChapterRegression)
close(rpt)
rptview(rpt)
My dotx template just have one Rich Text Content Control hole.
Thank you in advance!
My question has been answered on Mathwork forum. But let me sum it up here as well:
I am using the Document object and we can not add Report API objects like TitlePage to a DOM Document. Instead and that was what I was doing at first I should use mlreportgen.report.Report of the DOM Document. You can add both DOM and Report API objects to a Report object.
Then to use all the functions relative to the Document object like moveToNextHole() the Report object has an underlying DOM Document object that can be used.
Use it like this for example:
doc = rpt.Document;
doc.moveToNextHole();
I have a question about hyperlinks within pdf documents created with itext. Currently, using the following code written in java, I am able to successfully create links. However, when I hover over the link, the link text is displayed. The client does not want the link text to appear upon hover-over. How can I either remove the hover-over, or give it alternate text to display (e.g. "Course Info")? I am using itext version 5.5.9. I have looked at "iText in Action" chapter 7 but was not able to find what I needed. Is there a better way to create the links? Any help and examples will be appreciated. Thanks.
package edu.ucsd.act.academic.studente2t.util;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfAction;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfBorderArray;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfWriter;
class LinkInCellEvent implements PdfPCellEvent
{
protected String url;
public LinkInCellEvent(String url)
{
this.url = url;
}
public void cellLayout(PdfPCell cell, Rectangle position,
PdfContentByte[] canvases)
{
PdfWriter writer = canvases[0].getPdfWriter();
PdfAction action = new PdfAction(url);
PdfAnnotation link = PdfAnnotation.createLink(writer, position,
PdfAnnotation.HIGHLIGHT_INVERT, action);
PdfBorderArray border = new PdfBorderArray(0, 0, 0);
link.setBorder(border);
writer.addAnnotation(link);
}
}
This is not an iText problem. It's inherent to PDF. The PDF specification (ISO-32000-1) doesn't say anything about the way viewers should present tool tips for link annotations.
Your client (who probably should also be our client), may be confused by the following concepts:
Additional actions
The only occurrence of the word "tool tip" is in a NOTE when the E (enter) and X (exit) event are described in the section about additional actions. One can use additional actions, for instance on a widget annotation, to have a custom tool tip appear / disappear when someone hovers over a widget annotation.
When you study the PDF standard, you will see that there are several instances where you can define additional action (/AA), but link annotations aren't one of them.
Alternative field name
There's also the /TU entry (formerly known as the user name entry), which is (I quote the spec) an alternative field name that shall be used in place of the actual field name wherever the field shall be identified in the user interface (such as in error or status messages referring to the field). This text is also useful when extracting the document’s contents in support of accessibility to users with disabilities or for other purposes. The value of the /TU entry is often used by viewers as a tool tip, but as you can tell from the description, the /TU entry is specific for fields, not for annotations. It can only be used in a field dictionary, not in an annotation dictionary.
Conclusion:
Whatever is shown when someone hovers over a link annotation is not described in the specification. Every vendor of a PDF viewer may decide what to show (if anything) when a user hovers over a link annotation. There is no way to add something to the PDF that can force the viewer to show something else (or nothing).
I am using iTextSharp 5.5.3 i have a PDF with named fields i created with Adobe lifecycle I am able to fill the fields using iTextSharp but when i change the textcolor for a field it does not change. i really dont know why this is so. here is my code below
form.SetField("name", "Michael Okpara");
form.SetField("session", "2014/2015");
form.SetField("term", "1st Term");
form.SetFieldProperty("name", "textcolor", BaseColor.RED, null);
form.RegenerateField("name");
If your form is created using Adobe LifeCycle, then there are two options:
You have a pure XFA form. XFA stands for the XML Forms Architecture and your PDF is nothing more than a container of an XML stream. There is hardly any PDF syntax in the document and there are no AcroForm fields. I don't think this is the case, because you are still able to fill out the fields (which wouldn't work if you had a pure XFA form).
You have a hybrid form. In this case, the form is described twice inside the PDF file: once using an XML stream (XFA) and once using PDF syntax (AcroForm). iText will fill out the fields in both descriptions, but the XFA description gets preference when rendering the document. Changing the color of a field (or other properties) would require changing the XML and iText(Sharp) can not do that.
If I may make an educated guess, I would say that you have a hybrid form and that you are only changing the text color of the AcroForm field without changing the text color in the XFA field (which is really hard to achieve).
Please try adding this line:
form.RemoveXfa();
This will remove the XFA stream, resulting in a form that only keeps the AcroForm description.
I have written a small example named RemoveXFA using the form you shared to demonstrate this. This is the C#/iTextSharp version of that example:
public void ManipulatePdf(String src, String dest)
{
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create));
AcroFields form = stamper.AcroFields;
form.RemoveXfa();
IDictionary<String, AcroFields.Item> fields = form.Fields;
foreach (String name in fields.Keys)
{
if (name.IndexOf("Total") > 0)
form.SetFieldProperty(name, "textcolor", BaseColor.RED, null);
form.SetField(name, "X");
}
stamper.Close();
reader.Close();
}
In this example, I remove the XFA stream and I look over all the remaining AcroFields. I change the textcolor of all the fields with the word "Total" in their name, and I fill out every field with an "X".
The result looks like this: reportcard.pdf
All the fields show the letter "X", but the fields in the TOTAL column are written in red.
I finally found a way, guess the problem was coming from using Adobe LC, so i switched to Open Office it all worked but when i flatten the form everything disappears. I found a solution to that here ITextSharp PDFTemplate FormFlattening removes filled data
Thanks Mr Lowagie for your help
I use itext.dll for genrating a PDF using ASP.NET and I want a footer in my document in the form of:
Page 1 of 6
HeaderFooter footer = new HeaderFooter(new Phrase("Page"), new Phrase(" of 6"));
footer.setBorder(Rectangle.NO_BORDER);
footer.setAlignment(Element.ALIGN_CENTER);
document.setFooter(footer);
Is this possible without hardcoding the total number of pages? I.e. is there a method to get the total number pages in a document?
I have found that there are (at least) 2 ways of doing this.
One is to create the document without the footer and after that use PdfStamper to stamp the page numbers with total on it. But that raised some problems with me when I output the stampers product to MemoryStream and there seems to be no way of closing the stamper without closing the stream at the same time.
The other way is to create one instance of PdfTemplate that will represent the total page count and add that to every page to footer or where ever you want it.
Next you can use your own PdfPageEventHelper class and implement OnCloseDocument method where you can fill the template with total page count:
public override void OnCloseDocument(PdfWriter writer, Document document)
{
PageCountTemplate.BeginText();
PageCountTemplate.SetFontAndSize(HeaderFont.BaseFont, HeaderFont.Size);
PageCountTemplate.ShowText((writer.CurrentPageNumber - 1).ToString());
PageCountTemplate.EndText();
}
I personally also use OnOpenDocument to create the template and OnEndPage to write it on each page.
EDIT:
To answer Jan's question, OnCloseDocument is called only once when the whole doc has been written. When Doc.Close() is called I mean.