How to keep the text while moving (resizing) the Stamp Annotation iText - pdf

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.

Related

Draw form isolated using PDFBOX

I am trying to draw an XObject from type form in a new PDF page, I did the following:
this.cell = (PDFormXObject) xobject;
PDDocument document = new PDDocument();
PDPage page = new PDPage();
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.transform(new Matrix(1, 0, 0, 1, -cell.getBBox().getLowerLeftX(),-cell.getBBox().getLowerLeftY()));
contentStream.drawForm(cell);
contentStream.close();
document.addPage(page);
document.save("D://temp//blankPage.pdf");
document.close();
The result is as desired, but the xobject has larger border with empty areas, I only need to isolate the internal box with (which I add yellow border to it), here is a ss:

ITextSharp - Field's font size to be change after click on the field

I try to fill data to PDF form using iTextSharp. When I use Adobe Reader to open that PDF, i can see the data has been filled into the field. But when I click on the field, the font size will be changed and if i modify the content, the font size will be fixed. Here is my PDF file
Additional Information:
If i set the field font to "Times Roman", it won't have this issue.
BaseFont bfChinese = BaseFont.CreateFont("c:\\windows\\fonts\\mingliu.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
using (MemoryStream ms = new MemoryStream())
{
PdfReader pdfReader = new PdfReader(file);
PdfStamper pdfStamper = new PdfStamper(pdfReader, ms, '\0', true);
AcroFields pdfFormFields = pdfStamper.AcroFields;
pdfFormFields.AddSubstitutionFont(bfChinese);
pdfFormFields.SetField("Text1", "Testing Message");
pdfFormFields.SetField("Text2", "Testing Message");
pdfStamper.Close();
pdfReader.Close();
return ms.ToArray();
}

How to add an image to a pdf file in APPEND mode using PdfStamper?

I'm using the iTextSharp library version 5.5.6.0.
This file contains customizable text fields and it's necessary to keep an interactive text form fields: https://yadi.sk/i/yoUvDI9EmtVhc .
But I can't adding an image in PdfTemplate object.
The code in c# at this stage is:
string outpath = #"D:\pdf_\output.pdf";
string inpath = #"D:\pdf_\input.pdf";
string stamp = #"D:\pdf_\img.png";
This method does'nt add the image, but the text boxes are active.
public static void onlyInteractive()
{
using (MemoryStream os = new MemoryStream())
using (PdfReader pdfReader = new PdfReader(inpath))
//APPEND mode
using (PdfStamper stamper = new PdfStamper(pdfReader, os, '\0', true))
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(stamp);
image.SetAbsolutePosition(0, 0);
PdfTemplate template = PdfTemplate.CreateTemplate(stamper.Writer, image.Width, image.Height);
template.AddImage(image);
stamper.GetOverContent(1).AddTemplate(template, 150, 200, true);
os.WriteTo(new FileStream(outpath, FileMode.Create, FileAccess.ReadWrite));
}
}
The behavior of this method back to the first.
public static void onlyImage()
{
using (Stream output = new FileStream(outpath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
using (PdfReader reader = new PdfReader(inpath))
using (var stamper = new PdfStamper(reader, output, '\0', true))
{
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(stamp);
image.SetAbsolutePosition(0, 0);
PdfTemplate template = PdfTemplate.CreateTemplate(stamper.Writer, image.Width, image.Height);
template.AddImage(image);
stamper.GetOverContent(1).AddTemplate(template, 150, 200, true);
}
}
onlyInteractive
The issue with this code is that you grab the output PDF before it is finished:
using (PdfStamper stamper = new PdfStamper(pdfReader, os, '\0', true))
{
[...]
os.WriteTo(new FileStream(outpath, FileMode.Create, FileAccess.ReadWrite));
}
When the stamper is getting closed (here implicitly at the end of its using block), some not yet stored PDF objects are written and the internal cross references and the file trailer are written.
You write the os contents to file before that. Thus, your result document is incomplete. Adobe Reader upon opening it repairs it which results in essentially your original document.
onlyImage
This code by itself is correct, it stamps the image onto the document and stores it correctly.
Your problem here is that the document itself is Reader-enabled, i.e. it is signed with a so called usage rights signature. Such signatures tell Adobe Reader upon opening a file to make additional features available displaying editing the document in question.
But when checking the signature on the document with the image, Adobe Reader sees that the document has been changed in a way that is not compatible with the usage rights granted by the signature: An image has been added to the page content which is something not granted by the signature. Thus, Adobe Reader revokes the granted features, in your case form editing.
Removing the usage rights signature
One option in this situation is to remove that signature. In that case form editing is not granted anymore by means of that signature. But in newer Adobe Reader versions (since version XI if I recall correctly) form editing has been granted to all documents by default! In your case that feature is removed due to the invalidated signature!
This can be done as follows:
using (Stream output = new FileStream(outpath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
using (PdfReader reader = new PdfReader(inpath))
using (var stamper = new PdfStamper(reader, output))
{
reader.RemoveUsageRights();
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(stamp);
image.SetAbsolutePosition(0, 0);
PdfTemplate template = PdfTemplate.CreateTemplate(stamper.Writer, image.Width, image.Height);
template.AddImage(image);
stamper.GetOverContent(1).AddTemplate(template, 150, 200, true);
}
You can now edit the PDF with image in newer Adobe Readers.
Unfortunately, though, there is an error upon saving the document. I don't know whether they have to do with the fact that the source document is partially invalid (Adobe Preflight complains about a number of issues, foremost the use of an undefined encoding name Win1251Encoding) or whether something else gets broken.
Removing the usage rights signature in append mode
Working in append mode we have to manually remove the usage rights signature. Actually, we'll remove the whole Perms dictionary from the Catalog:
using (Stream output = new FileStream(outpath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
using (PdfReader reader = new PdfReader(inpath))
using (var stamper = new PdfStamper(reader, output, '\0', true))
{
reader.Catalog.Remove(PdfName.PERMS);
stamper.MarkUsed(reader.Catalog);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(stamp);
image.SetAbsolutePosition(0, 0);
PdfTemplate template = PdfTemplate.CreateTemplate(stamper.Writer, image.Width, image.Height);
template.AddImage(image);
stamper.GetOverContent(1).AddTemplate(template, 150, 200, true);
}
Now you can edit the form and save the file (at least I can in Adobe Reader DC).
PS: The correct coordinates
In a comment the OP shared another PDF and stated that it
for the other file is impossible to place a picture on a page with landscape orientation.
There indeed is an issue in the OP's code:
stamper.GetOverContent(1).AddTemplate(template, 150, 200, true);
The fixed coordinates 150, 200 are a sign that the OP assumes the lower left page corner to be the origin 0, 0 of the coordinate system. While this often is the case, this is not necessarily true. One always has to take the CropBox (which defaults to the MediaBox) into account, i.e. for the OP's code:
Rectangle cropBox = reader.GetCropBox(1);
stamper.GetOverContent(1).AddTemplate(template, cropBox.Left + 150, cropBox.Bottom + 200, true);
the library takes rotation not correctly, but gives 0 degrees.
But that is correct! Your sample PDF is somewhat special as it uses an unrotated rectangle for landscape and a rotated rectangle for portrait.

chart location of pie chart in iText pdf in java

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.

ITextSharp - text field in PdfPCell

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.