How to add header in iText 5.2.1? - java

I use iText 5.2.1 and I have already created Document object.
like this:
document.addHeader("hello");
Why don't I see the header on the PDF File?

Use Page Events. Many good answers have been provided already on StackOverflow, for example this one, or this other one.

try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("FicheClient.pdf"));
document.open();
document.add(new Paragraph("hello World",FontFactory.getFont(FontFactory.TIMES_BOLD,20,Font.BOLD,BaseColor.BLACK)));
document.add(new Paragraph("---------------------------------------"));
document.close();
JOptionPane.showMessageDialog(null, "Raport saved ");
} catch (Exception e) {
}

Related

The issue of Link API in iText7

When I generate a pdf document with Link API, there have some strange things. A rectangle always outside the link text. It looks like the cell rectangle, but I didn't set any cell in the document.
My code is like this:
try (PdfDocument pdfDocument = new PdfDocument(new PdfWriter(file));
Document document = new Document(pdfDocument);) {
PdfAction pdfAction = PdfAction.createURI("https://kb.itextpdf.com/home");
Link link = new Link("https://kb.itextpdf.com/home", pdfAction);
Paragraph paragraph = new Paragraph();
paragraph.add(link);
document.add(paragraph);
} catch (Exception e) {
System.out.println(e);
}
I resolved it like this:
PdfLinkAnnotation linkAnnotation = link.getLinkAnnotation();
linkAnnotation.setBorder(new PdfAnnotationBorder(0, 0, 0));

How to pack a pdf-file and get standard dialog box with java servlet?

How convert pdf to zip and get standard dialog box that asks the user what he wants to just showing the PDF in the browser or saved to the file system in zip-format?
public class PdfServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// Get the text that will be added to the PDF
String text = request.getParameter("text");
if (text == null || text.trim().length() == 0) {
text = "You didn't enter any text.";
}
// step 1
Document document = new Document();
// step 2
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
// step 3
document.open();
// step 4
document.add(new Paragraph(String.format(
"You have submitted the following text using the %s method:",
request.getMethod())));
document.add(new Paragraph(text));
// step 5
document.close();
// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());
// write ByteArrayOutputStream to the ServletOutputStream
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
}
catch(DocumentException e) {
throw new IOException(e.getMessage());
}
}
}
Once more, I have to refer you to the official documentation. For some reason you don't seem to be able to access the resources that are available on that site.
Take a look at the HelloZip example:
// creating a zip file with different PDF documents
ZipOutputStream zip =
new ZipOutputStream(new FileOutputStream(RESULT));
for (int i = 1; i <= 3; i++) {
ZipEntry entry = new ZipEntry("hello_" + i + ".pdf");
zip.putNextEntry(entry);
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, zip);
writer.setCloseStream(false);
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello " + i));
// step 5
document.close();
zip.closeEntry();
}
zip.close();
In this example, we create three PDF files that are added to a single ZIP file. In your case, you want to create a ZIP file in memory that contains only one ZIP file. That's easy:
// Creating a ByteArrayOutputStream
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// creating a zip outputstream stream
ZipOutputStream zip = new ZipOutputStream(baos);
// prepare entry
ZipEntry entry = new ZipEntry("my_document.pdf");
zip.putNextEntry(entry);
// create PDF
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, zip);
writer.setCloseStream(false);
document.open();
document.add(new Paragraph("Hello " + i));
document.close();
zip.closeEntry();
zip.close();
Now you can sent the bytes stored in the baos to your response object as explained in my answer to your previous question: Why doesn'n create pdf-documents in java servlet?
OutputStream os = response.getOutputStream();
baos.writeTo(os);
This answers the first part of your question "How convert pdf to zip" and I hope that this time you'll accept my answer. I already answered one question for you and although you said that my answer worked for you, you didn't accept my answer. (Why would anyone want to provide good answers if the person getting the answer doesn't accept correct answers?)
As for the second part of your question get standard dialog box that asks the user what he wants to just showing the PDF in the browser or saved to the file system in zip-format, that part is unanswerable. There is no standard box to do this. The server gets a request and sends a response. You can only send one single response so there's no way to send a PDF to show inline as well as a ZIP to open as an attachment at the same time so that the user can choose through a dialog box.
You need to give the user the option to choose for an inline PDF, a PDF as an atachment or a PDF in a ZIP file before sending the request to the server. There is nothing standard about that!

PDF with multiple paragraphs from Java Itext

I have a array of Strings as follows :-
String[] data = {“Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”}.
Now I want to write this data strings to a pdf file one below the other like :-
1. Sunday
2. Monday
3. Tuesday
4. Wednesday
5. Thursday
6. Friday
7. Saturday.
I am using itext to achieve this. Below is the code snippet I am using
for(int i= 0; i< data.length;i++)
{
Document document=new Document();
PdfWriter.getInstance(document, new FileOutputStream(directory));
document.open();
document.add(new Paragraph(data[i]));
document.add(Chunk.NEWLINE);
document.close();
}
Problem :-
The pdf file which I get has only :-
Saturday.
Please help.
The problem is, you are creating the document in the loop. Try this:
Document document=new Document();
PdfWriter.getInstance(document, new FileOutputStream(directory));
document.open();
for(int i= 0; i< data.length;i++)
{
document.add(new Paragraph(data[i]));
document.add(Chunk.NEWLINE);
}
document.close();
You might want to handle closing of the stream in case something happens.
With Java 7 or above you can achieve with this:
Document document=new Document();
try (FileOutputStream fos = new FileOutputStream(directory)) {
PdfWriter.getInstance(document, fos);
document.open();
for(int i= 0; i< data.length;i++)
{
document.add(new Paragraph(data[i]));
document.add(Chunk.NEWLINE);
}
//EDIT start
document.close();
//EDIT end
}
You are creating document in a loop and also closing same.
make sure document is open and close only once in its lifetime.
try
{
Document document=new Document();
PdfWriter.getInstance(document, new FileOutputStream(directory));
document.open();
for(int i= 0; i< data.length;i++)
{
document.add(new Paragraph(data[i]));
document.add(Chunk.NEWLINE);
}
}
finally{
document.close();
}

Making a paragraph in iText pdf not divided in two pages

I'm writing a pdf file with iText and I want the paragraphs divided across two different pages. How can I do this?
It is a lot easier to help if you could provide a more accurate example of the paragraphs and the document you are creating, but as far as I understand it is something like this:
Generate an ArrayList or other weapon of choise in making an iterable list of the paragraphs. Iterate through that list and call newPage() before adding content to page 2
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream([file]));
for (ArrayList<Paragraph> : theParagraph ) {
document.addElement(theParagraph)
document.newPage();
}
document.close();
This will automaticaly add new pages as content is added on the pdf-document, but with less controle over when the pagebreak occurs:
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream([file]));
document.open();
for(int i=0 ; i<100; i++){
document.add(new Paragraph("This is a very important message"));
}
document.close();

java pdf file write

I am working with Java and I was wondering how to do a page break in a PDF file? After 50 lines I want to start writing on a new page.
I am using the iText library.
You can use the iText library for creating a PDF in Java
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("HelloWorld.pdf"));
document.open();
document.add(
new Paragraph("Hello World"));
document.newpage();
// You are on the new page from here.
// Note that newpage method won't work on empty pages,
// so first you should add something to previous page and then call this method
// to create new page
} catch (Exception e) {
// handle exception
}
document.close();

Categories