I'm creating a pie chart using jFreechart and add the chart in pdf created in iText. The problem is chart is always added at the bottom of the page and not after the last line.
A sample code for regenrating the error is:
Document document = new Document();
PdfWriter writer;
File file = new File("c:/myPdf.pdf");
writer = PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
try {
DefaultPieDataset pieDataset = new DefaultPieDataset();
pieDataset.setValue("OPT 1", 10);
pieDataset.setValue("OPT 2", 0);
pieDataset.setValue("OPT 3", 17);
pieDataset.setValue("OPT 4", 11);
JFreeChart chart = ChartFactory.createPieChart3D("Option click count",
pieDataset, true, false, false);
final PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setNoDataMessage("No data to display");
chart.setTitle(new TextTitle("Option Click Count", new Font("Times New Roman", Font.PLAIN, 14)));
PdfContentByte pdfContentByte = writer.getDirectContent();
PdfTemplate pdfTemplateChartHolder = pdfContentByte.createTemplate(225,225);
Graphics2D graphicsChart = pdfTemplateChartHolder.createGraphics(225,225,new DefaultFontMapper());
Rectangle2D chartRegion =new Rectangle2D.Double(0,0,225,225);
chart.draw(graphicsChart,chartRegion);
graphicsChart.dispose();
pdfContentByte.addTemplate(pdfTemplateChartHolder,0,0);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
Here the options are fetched from database so not sure on the count of the option. I want to show chart right to the table. How can I do this?
You are adding the chart as a template, and per definition they are added with absolute coordinates.
If you are using floating elements, as I assume you are, you can use com.lowagie.itext.Image (version 2.1), and in newer versions com.itextpdf.text.Image.
You can use the Image class to create the template, and add it as a Element:
See here (iText API).
PdfContentByte pdfContentByte = writer.getDirectContent();
PdfTemplate pdfTemplateChartHolder = pdfContentByte.createTemplate(225,225);
Graphics2D graphicsChart = pdfTemplateChartHolder.createGraphics(225,225,new DefaultFontMapper());
Rectangle2D chartRegion = new Rectangle2D.Double(0,0,225,225);
chart.draw(graphicsChart,chartRegion);
graphicsChart.dispose();
Image chartImage = Image.getInstance(pdfTemplateChartHolder);
document.add(chartImage);
The code example above shows the gist of it. You should as often as possible use Element objects such as Image if you don't want to handle heights and positions absolutely.
Related
I am generating a PDF invoice report using OpenPDF. On the PDF, I have to set a rectangular block for header/footer on every page. I have used the HeaderFooter class to add header/footer on every page but this works only for a Phrase.
HeaderFooter header = new HeaderFooter(new Phrase("This is a Header."), false);
Is there any way to set a rectangular block with height and width for header/footer using HeaderFooter class?
This is what I am expecting on every page:
You can do this by creating your custom PdfPageEvent where you add the elements whenever a new page is finished (onEndPage-event). The simplest way of doing this is by extending PdfPageEventHelper in a standalone class or in an anonymous class. First, define and style your rectangles. Second, add them to the page using the PdfWriter inside the callback.
Here is a demo showing how to do it:
Document document = new Document(PageSize.A4, 40, 40, 200, 200);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"));
// footer
final Rectangle footer = new Rectangle(30, 30, PageSize.A4.getRight(30), 180);
footer.setBorder(Rectangle.BOX);
footer.setBorderColor(Color.BLACK);
footer.setBorderWidth(2);
// header
final Rectangle header = new Rectangle(footer);
header.setTop(PageSize.A4.getTop(30));
header.setBottom(PageSize.A4.getTop(180));
// content-box
final Rectangle box = new Rectangle(footer);
box.setTop(document.top());
box.setBottom(document.bottom());
// create and register page event to add the rectangles
writer.setPageEvent(new PdfPageEventHelper() {
#Override
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte cb = writer.getDirectContent();
cb.rectangle(header);
cb.rectangle(footer);
cb.rectangle(box);
}
});
document.open();
document.add(new Paragraph(LOREM_IPSUM)); // just some constant filler text
document.close();
The result looks like this:
I'm working in some Annotation stuff with iText.
I tried to draw a Stamp Annotation with text on an existing PDF file.
The Stamp Annotation did show on the PDF but when I moving the stamp (or resizing), the text (will all format) is disappear and can't gain it back anymore.
Here is my code
PdfReader reader = new PdfReader(SRC_FILE);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(DEST_FILE));
PdfWriter writer = stamper.getWriter();
BaseColor textColor = BaseColor.ORANGE;
BaseColor fillColor = BaseColor.GRAY;
Rectangle rectangle = new Rectangle(150, 250, 450, 320);
PdfAnnotation annotation = PdfAnnotation.createSquareCircle(writer, rectangle, null, true);
annotation.setColor(textColor);
PdfDictionary dict = new PdfDictionary();
PdfTemplate template = PdfTemplate.createTemplate(writer, rectangle.getWidth(), rectangle.getHeight());
template.setBoundingBox(rectangle);
template.setColorFill(fillColor);
//template.setColorStroke(textColor);
template.setLineWidth(4);
template.rectangle(rectangle.getLeft(), rectangle.getBottom(), rectangle.getWidth(), rectangle.getHeight());
template.closePathFillStroke();
template.setColorFill(textColor);
ColumnText columnText = new ColumnText(template);
columnText.setSimpleColumn(rectangle);
Paragraph p = new Paragraph("This is my test", new Font(Font.FontFamily.HELVETICA, 24, Font.BOLD));
p.setAlignment(Element.ALIGN_CENTER);
p.setLeading(50);
columnText.setAlignment(Element.ALIGN_CENTER);
columnText.addElement(p);
columnText.go();
writer.releaseTemplate(template);
dict.put(PdfName.N, template.getIndirectReference());
annotation.put(PdfName.AP, dict);
annotation.put(PdfName.F, new PdfNumber(4));
stamper.addAnnotation(annotation, 1);
stamper.close();
reader.close();
I attached the Screenshot this issue for your review too.
I did some research and if I change the line code
PdfAnnotation annotation = PdfAnnotation.createSquareCircle(writer, rectangle, null, true);
to
PdfAnnotation annotation= stamper.getWriter().createAnnotation(rectangle, PdfName.FREETEXT);
The text will still there when I moving the Stamp, but still disappear if I resize the Rectangle.
I am adding text to an already created pdf document using this method.
ITextSharp insert text to an existing pdf
Basically it uses the PdfContentByte and then adds the content template to the page.
I am finding that in some areas of the file, the text doesn't show up.
It seems that the text I am adding is showing up behind the content that is already on the page? I flattened the pdf document down to it just being images but I am still having the same issue happen with the flattened file.
Has anyone had any issues adding text being hidden using Itextsharp?
I also tried using DirectContentUnder as was suggested in this link to no avail..
iTextSharp hides text when write
Here is the code I am using...With this I am trying to basically overlay graph paper on top of the PDF. In this example, there is a box in the upper left corner of every page that doesn't get populated. There is an image in the original pdf in this spot. And on the 4th and 5th pages, there are boxes that don't get populated, but they don't seem to be images.
PdfReader reader = new PdfReader(oldFile);
iTextSharp.text.Rectangle size = reader.GetPageSizeWithRotation(1);
Document document = new Document(size);
// open the writer
FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write);
PdfWriter writer = PdfWriter.GetInstance(document, fs);
document.Open();
// the pdf content
PdfContentByte cb = writer.DirectContent;
for (int i = 0; i < reader.NumberOfPages; i++)
{
document.NewPage();
// select the font properties
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
cb.SetFontAndSize(bf, 4);
cb.SetColorStroke(BaseColor.GREEN);
cb.SetLineWidth(1f);
for (int j = 10; j < 600; j += 10)
{
WriteToDoc(ref cb, j.ToString(), j, 10);//Write the line number
WriteToDoc(ref cb, j.ToString(), j, 780);//Write the line number
if (j % 20 == 0)
{
cb.MoveTo(j, 20);
cb.LineTo(j, 760);
cb.Stroke();
}
}
for (int j = 10; j < 800; j += 10)
{
WriteToDoc(ref cb, j.ToString(), 5, j);//Write the line number
WriteToDoc(ref cb, j.ToString(), 590, j);//Write the line number
if (j % 20 == 0)
{
cb.MoveTo(15, j);
cb.LineTo(575, j);
cb.Stroke();
}
}
// create the new page and add it to the pdf
PdfImportedPage page = writer.GetImportedPage(reader, i + 1);
cb.AddTemplate(page, 0, 0);
}
// close the streams and voilá the file should be changed :)
document.Close();
fs.Close();
writer.Close();
reader.Close();
Thanks for any of the help you can provide...I really appreciate it!
-Greg
First of all: If you are trying to basically overlay graph paper on top of the PDF, why do you first draw the graph paper and stamp the original page onto it? You essentially are underlaying graph paper, not overlaying it.
Depending on the content of the page, your graph paper this way may easily get covered. E.g. if there is a filled rectangle in the page content, in the result there is a box in the upper left corner of every page that doesn't get populated.
Thus, simply first add the old page content, then add overlay changes.
This being said, for the task of applying changes to an existing PDF, using PdfWriter and GetImportedPage is less than optimal. This actually is a task for the PdfStamper class which its made for stamping additional content on existing PDFs.
E.g. have a look at the sample StampText, the pivotal code being:
PdfReader reader = new PdfReader(resource);
using (var ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(reader, ms))
{
PdfContentByte canvas = stamper.GetOverContent(1);
ColumnText.ShowTextAligned( canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 36, 540, 0 );
}
return ms.ToArray();
}
Is there a way to place 2 instances of a bitmap in a PDF for a single image? One to display when it is viewed on the screen and another when it prints?
The problem we have is rendering a chart to a bitmap. If we do 300 dpi then axis lines, borders, etc. disappear. If we do 96 dpi, then printing it looks bad.
thanks - dave
You can use Optional Content to do this. Supplying the usage application dictionaries with a 'Print' event causes the content to be appropriate for printing. (Note, not all printing applications will honour this).
See The PDF Reference Manual, in my 1.7 edition section 4.10 'Optional Content' beginning on page 364.
You can add an Alternate Image Dictionary (PDF Spec, section 8.9.5.4) which can specify an image to be used for printing.
Yes, there is a way, although I do not know it. We used it as a prank on a coworker, when printing a document, some totally other pictures appeared
You can also use 2 readonly textbox fields and draw the images on the field's appearance. Then for one field you set its visibility to VisibleNonPrintable and for the other HiddenButPrintable.
I implemented this (using iText). For anyone else who needs these here's the code. And you can download the source at my blog.
static void Main(string[] args)
{
Document document = new Document(new Rectangle(0, 0, 8.5f * 72.0f, 11 * 72));
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Path.GetFullPath(#"..\..\test_dotnet.pdf"), FileMode.OpenOrCreate, FileAccess.ReadWrite));
document.Open();
document.Add(new Paragraph("Visibility test"));
// not displayed on printer
PdfLayer layer = new PdfLayer("screen", writer);
layer.OnPanel = false;
layer.SetPrint("Print", false);
layer.View = true;
PdfContentByte cb = writer.DirectContent;
cb.BeginLayer(layer);
Image img = Image.GetInstance(Path.GetFullPath(#"..\..\building_01.png"));
img.SetAbsolutePosition(72, 72 * 7);
cb.AddImage(img);
cb.EndLayer();
// not displayed on screen
layer = new PdfLayer("print", writer);
layer.OnPanel = false;
layer.SetPrint("Print", true);
layer.View = false;
cb = writer.DirectContent;
cb.BeginLayer(layer);
img = Image.GetInstance(Path.GetFullPath(#"..\..\building_02.png"));
img.SetAbsolutePosition(72, 72 * 3);
cb.AddImage(img);
cb.EndLayer();
document.Close();
Console.Out.WriteLine("all done");
}
I'm using iTextSharp to create a PDF, how can I add a textField into PdfPCell?
You wouldn't really add a 'text field' to a PdfPCell, you'd create a PdfPCell and add text (or other stuff) to that.
mikesdotnetting.com might have the clearest example and there's always the iTextSharp tutorial.
Give this a try. It works for me.
Document doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f);
MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
doc.Open();
// Create your PDFPTable here....
TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(67, 585, 140, 800), "cellTextBox");
PdfPCell tbCell = new PdfPCell();
iTextSharp.text.pdf.events.FieldPositioningEvents events = new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tf.GetTextField());
tbCell.CellEvent = events;
myTable.AddCell(tbCell);
// More code...
I adapted this code from this post.
Edit:
Here is a full working console application that puts a TextBox in a table cell. I tried to keep the code to a bare minimum.
using System;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace iTextSharpTextBoxInTableCell
{
class Program
{
static void Main(string[] args)
{
// Create a PDF with a TextBox in a table cell
BaseFont bfHelvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false);
Font helvetica12 = new Font(bfHelvetica, 12, Font.NORMAL, Color.BLACK);
Document doc = new Document(PageSize.LETTER, 18f, 18f, 18f, 18f);
FileStream fs = new FileStream("TextBoxInTableCell.pdf", FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(doc, fs);
doc.Open();
PdfPTable myTable = new PdfPTable(1);
myTable.TotalWidth = 568f;
myTable.LockedWidth = true;
myTable.HorizontalAlignment = 0;
TextField tf = new TextField(writer, new iTextSharp.text.Rectangle(67, 585, 140, 800), "cellTextBox");
PdfPCell tbCell = new PdfPCell(new Phrase(" ", helvetica12));
iTextSharp.text.pdf.events.FieldPositioningEvents events =
new iTextSharp.text.pdf.events.FieldPositioningEvents(writer, tf.GetTextField());
tbCell.CellEvent = events;
myTable.AddCell(tbCell);
doc.Add(myTable);
doc.Close();
fs.Close();
Console.WriteLine("End Of Program Execution");
Console.ReadLine();
}
}
}
Bon chance
DaveB's answer works, but the problem is that you have to know the coordinates to place the textfield into, the (67, 585, 140, 800). The more normal method of doing this is to create the table cell and add a custom event to the cell. When the table generation calls the celllayout event it passes it the dimensions and coordinates of the cell which you can use to place and size the textfield.
First create this call, which is the custom event
public class CustomCellLayout : IPdfPCellEvent
{
private string fieldname;
public CustomCellLayout(string name)
{
fieldname = name;
}
public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
{
PdfWriter writer = canvases[0].PdfWriter;
// rectangle holds the dimensions and coordinates of the cell that was created
// which you can then use to place the textfield in the correct location
// and optionally fit the textfield to the size of the cell
float textboxheight = 12f;
// modify the rectangle so the textfield isn't the full height of the cell
// in case the cell ends up being tall due to the table layout
Rectangle rect = rectangle;
rect.Bottom = rect.Top - textboxheight;
TextField text = new TextField(writer, rect, fieldname);
// set and options, font etc here
PdfFormField field = text.GetTextField();
writer.AddAnnotation(field);
}
}
Then in your code where you create the table you'll use the event like this:
PdfPCell cell = new PdfPCell()
{
CellEvent = new CustomCellLayout(fieldname)
// set borders, or other cell options here
};
If you want to different kinds of textfields you can either make additional custom events, or you could add extra properties to the CustomCellLayout class like "fontsize" or "multiline" which you'd set with the class constructor, and then check for in the CellLayout code to adjust the textfield properties.