Is there is a way to tell an iText paragraph contents to write from right to left?
I know it can be done with ColumnText or a table, but I need it in a paragraph (multiple pages can be written).
Here is just an example.
Document document = new Document(PageSize.A1.rotate(), 20, 20, 20, 20);
document.addTitle("YourReport");
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("c:\\ITextTest.pdf"));
document.open();
Paragraph TitleHeading = new Paragraph(targetFileName);
TitleHeading.setAlignment(writer.RUN_DIRECTION_RTL);
document.add(TitleHeading);
Related
I created a PDF file in Java using OpenPDF and inserted a paragraph. The problem is that I want to place it in the middle, not on the left. How can this be done?
Second question: How can I place a word with specific formatting in the test?
For example: "Hello and welcome" (welcome should be bold)
This is my code:
Document document = new Document();
String PDFPath = "output.pdf";
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(PDFPath));
Font font_bold = new Font(Font.TIMES_ROMAN, 16, Font.BOLD, Color.BLACK);
Font font_normal = new Font(Font.TIMES_ROMAN, 15, Font.NORMAL, Color.BLACK);
Paragraph p1 = new Paragraph("Ordonnance", font_bold);
document.open();
document.add(p1);
document.close();
You need to define the alignment if you want to have the paragraph centred:
p1.setAlignment(Element.ALIGN_CENTER);
To apply formatting to some parts of the text, you can divide it into several chunks. A Chunk in OpenPDF is a string with a certain Font. Instead of adding a String (unformatted) to the paragraph, you add a Chunk-object like new Chunk("Hello and ", fontNormal) which defines the text and the font to be used.
Here is the code for what you want to do according to the question:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
document.open();
Font fontNormal = new Font(Font.TIMES_ROMAN, 15, Font.NORMAL);
Font fontBold = new Font(Font.TIMES_ROMAN, 16, Font.BOLD);
Paragraph p1 = new Paragraph();
p1.setAlignment(Element.ALIGN_CENTER);
p1.add(new Chunk("Hello and ", fontNormal));
p1.add(new Chunk("welcome", fontBold));
document.add(p1);
document.close();
The result looks like this:
Your first line had some weird declaration, but this should work:
Document document = new Document();
String PDFPath = "D:\\test.pdf";
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(PDFPath));
Font font_bold = new Font(Font.TIMES_ROMAN, 16, Font.BOLD, Color.BLACK);
Font font_normal = new Font(Font.TIMES_ROMAN, 15, Font.NORMAL, Color.BLACK);
Paragraph p1 = new Paragraph("Ordonnance", font_bold);
p1.setAlignment(Element.ALIGN_CENTER);
document.open();
document.add(p1);
document.close();
You just have to set the alignment of the paragraph
I am in the process of converting the contents page of my PDF from using page numbers as a hyperlink, to anchors because of a few circumstantial limitations and the linking needs to be more dynamic.
I have omitted outer-loop code, but I am attempting to create a hyperlinked entry in the contents page using the following:
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
Anchor anchor = new Anchor("page18 link");
anchor.setReference("#page18");
p.add(anchor);
cell.addElement(p);
table.addCell(cell);
Once the contents page has been generated (i.e. all the rows have been added), I then use the writeSelectedRows on the table:
table.writeSelectedRows(0, -1, PageSize.A4.getWidth()*.05f, PageSize.A4.getHeight()-100, stamper.getOverContent(prevSectionPageCount+currentIndexPage+1));
On doing this, I get the following exception:
Cause Exception was: Error in StamperPDFPlugin. null
java.lang.NullPointerException at
com.itextpdf.text.pdf.internal.PdfAnnotationsImp.addPlainAnnotation(PdfAnnotationsImp.java:125)
at com.itextpdf.text.pdf.PdfDocument.localGoto(PdfDocument.java:2115)
at
com.itextpdf.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:1612)
at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:1025) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:877) at
com.itextpdf.text.pdf.ColumnText.goComposite(ColumnText.java:1381) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:882) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:877) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:866) at
com.itextpdf.text.pdf.PdfPRow.writeCells(PdfPRow.java:549) at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:767)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:897)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:845)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:823)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.transform(StamperPDFPlugin.java:584)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.transform(StamperPDFPlugin.java:328)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.executeProfile(StamperPDFPlugin.java:171)
On seeing the stack trace entry for localGoto, I took out the line anchor.setReference("#18.pdf"); and it completed fine without error (but obviously with the absence of the hyperlinks - only plain text).
What is going wrong here? Am I adding the anchor to the cell incorrectly?
Thanks
Please take a look at LinkInPositionedTable:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Anchor target = new Anchor("top");
target.setName("page18");
document.add(target);
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(500);
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
Anchor anchor = new Anchor("page18 link");
anchor.setReference("#page18");
p.add(anchor);
cell.addElement(p);
table.addCell(cell);
table.writeSelectedRows(0, -1, 36, 700, writer.getDirectContent());
document.close();
}
In this example, I create an anchor with name page18 (although it just refers to the top of the page) and a reference to that anchor added to a PdfPTable using your code snippet.
You can find the result here: link_in_positioned_table.pdf
This works for me, when using iText 5.5.7 (which is the most recent version).
I am converting html data into pdf,but I got white space where some large data will insert, I'd like to trim off the whitespace for following data
note: header is coming from jasper report
'Document document = new Document(PageSize.A4, 40, 72, getMargins(1),20);`
document.setMarginMirroringTopBottom(true);`
String path = dao.gettomcatserverPath1() + "webapps/wordimages/disch" +patid+ ".pdf";`
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
`String html = "";
List list1 = dao.executeQuery("select wordole from ip.dischargetemplate where patid='"+patid+"'");
html = ((Map)list1.get(0)).get("WORDOLE").toString();
InputStream _ishtml = new ByteArrayInputStream(html.getBytes());
HeaderFooter event = new HeaderFooter();`
writer.setPageEvent(event);`
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));`
document.open();`
Font font = FontFactory.getFont("Times-Roman");
XMLWorkerHelper.getInstance().parseXHtml(writer, document, _ishtml);`
document.close();`
I am generating charts using jfreechart.Now as per my requirement i need to export that chart into pdf using itext in java.Here i am able to export jfreechart to pdf but it is coming at the bottom of the pdf file whereas i want it to be top.
Here is my code..
PdfWriter writer = null;
Document document = new Document();
try {
writer = PdfWriter.getInstance(document, new FileOutputStream(
fileName));
document.open();
PdfContentByte contentByte = writer.getDirectContent();
PdfTemplate template = contentByte.createTemplate(width, height);
Graphics2D graphics2d = template.createGraphics(width, height,
new DefaultFontMapper());
Rectangle2D rectangle2d = new Rectangle2D.Double(0, 0, width,
height);
chart.draw(graphics2d, rectangle2d);
graphics2d.dispose();
contentByte.addTemplate(template, 0, 0);
How to set PdfWriter to write at the top of the pdf file.Please help me.
You are adding the template at the bottom of the page:
contentByte.addTemplate(template, 0, 0);
There are different options you can choose from.
You can add the template at another coordinate:
contentByte.addTemplate(template, 36, 400);
This will add the document at position x = 36 and y = 400. In this case, you need to do your math: instead of 400, you should take the top coordinate of the page (e.g. y = 842) minus a margin (e.g. 36) minus the height of the image.
If can be easier to choose a different option:
Image img = Image.getInstance(template);
document.add(img);
Now you're template is added in the flow of the document, just like all other high-level objects (just like Paragraphs and PdfPTables).
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();