iText Pdf Merging Checkbox Issue - itext

We are using iText Pdf API version 5.5.3 and we are trying to merge pdf documents which sometimes contain checkboxes , we write pdf document in servlet output stream and displays in iframe.
It works well in IE(tried in 11) but checkbox doesn't appear in Chrome(tried in 52).
Below is the sample code we use to display PDF.
InputStream in = docFile.getImage()
.getBinaryStream();
reader = new PdfReader(in);
pdfReaderList.add(reader);
document = new Document(PageSize.A4);
byteArrayOutputStream = new ByteArrayOutputStream();
PdfCopy copy = new PdfCopy(document, byteArrayOutputStream);
document.setMargins(35, 35, 48, 75);
document.open();
copy.setMargins(35, 35, 48, 75);
// add a document
Iterator<PdfReader> pdfReader = pdfReaderList.iterator();
while (pdfReader.hasNext()){
PdfImportedPage page;
reader = MCPDFFileWriter.unlockPdf(pdfReader.next());
for (int currentPage = 1; currentPage <= reader.getNumberOfPages(); currentPage++) {
page = copy.getImportedPage(reader, currentPage);
pageNum++;
copy.addPage(page);
footerNamesList.add(fileName);
footerMap.put(footerMap.size() + 1, fileName);
}
}
document.close ();
reader.close();
copy.close();
I also gone through this Link and given solution seems to be working when I use below code -
InputStream in = docFile.getImage()
.getBinaryStream();
reader = new PdfReader(in);
pdfReaderList.add(reader);
document = new Document(PageSize.A4);
byteArrayOutputStream = new ByteArrayOutputStream();
PdfCopy copy = new PdfCopy(document, byteArrayOutputStream);
document.setMargins(35, 35, 48, 75);
document.open();
copy.setMargins(35, 35, 48, 75);
**copy.setMergeFields();**
// add a document
**copy.addDocument(reader);**
document.close ();
reader.close();
copy.close();
However I sometimes face OutOfMemoryError and looking for some idea if there is any known issue with copy.setMergeFields();copy.addDocument(reader); and how it can be overcome ?
A bug was also reported in chrome ( in the thought of plugin issue) and sample pdf are attached there

Related

The text position in TextForm is different between iText5 and iText7

I'm trying to update from iText5 to iText7. In that, I'm aware that the text top position in TextField is difference. See the link image, It is hard to see the difference but the text position is not same; the text position in the pdf generated by iText 7 is slightly upper.
The pdf generation code by iText5
Document doc = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("itext5.pdf"));
writer.setPdfVersion(PdfWriter.VERSION_1_7);
writer.setTagged();
doc.open();
doc.addTitle("itext5 sample");
TextField tf = new TextField(writer, new Rectangle(100, 100, 261, 131), "text");
tf.setTextColor(BaseColor.BLACK);
tf.setFontSize(0);
tf.setText("TEST");
writer.addAnnotation(tf.getTextField());
doc.close();
PdfReader reader = new PdfReader("itext5.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("itext5_flatten.pdf"));
stamper.setFormFlattening(true);
stamper.close();
The pdf generation code by iText7
PdfDocument doc = new PdfDocument(new PdfWriter("itext7_flatten.pdf"));
PdfAcroForm form = PdfAcroForm.getAcroForm(doc, true);
PdfTextFormField nameField = PdfTextFormField.createText(doc, new Rectangle(100, 100, 161, 31), "text", "TEST");
nameField.setFontSize(0);
form.addField(nameField);
form.flattenFields();
doc.close();
I searched iText API as much as possible, but any API change text position in TextField seemed to be not provided. Could I adjust text poistion in TextField, in iText7 ?
The source codes and PDf files I used is here.

saving modified data in pdf itextsharp

I have created a pdf using itextsharp which contains some editable fields, when the service is called the pdf is created. But the problem what I am facing is, if i am changing anything in the pdf and downloading it then the changes are not saved. Also i want to open the pdf in a new tab through server side.
Code what I am using is:
public static String[] LANGUAGES_gc = { "English", "Math", "Science" };
[HttpGet]
[ODataRoute("GetPdf")]
public void DownloadPDF()
{
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", "inline;filename=Example.pdf");
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
Document doc = new Document(iTextSharp.text.PageSize.A4, 10f, 10f, 100f, 0f);
string pdfFilePath = HttpContext.Current.Server.MapPath(".") + "/PDFFiles";
PdfWriter wri = PdfWriter.GetInstance(doc, HttpContext.Current.Response.OutputStream);
doc.Open();
doc.AddAuthor("Test author");
doc.AddCreationDate();
PdfContentByte cb = wri.DirectContent;
Font _bf = new Font(Font.FontFamily.HELVETICA, 6);
PdfFormField _radioGroup = PdfFormField.CreateRadioButton(wri, true);
_radioGroup.FieldName = "language_gc";
Rectangle _rect;
RadioCheckField _radioG;
PdfFormField _radioField1;
PdfFormField field;
for (int i = 0; i < LANGUAGES_gc.Length; i++)
{
_rect = new Rectangle(46, 806 - i * 40, 60, 788 - i * 40);
_radioG = new RadioCheckField(wri, _rect, null, LANGUAGES_gc[i]);
_radioG.BackgroundColor = new GrayColor(0.8f);
_radioG.BorderColor = GrayColor.BLACK;
_radioG.CheckType = RadioCheckField.TYPE_CIRCLE;
_radioField1 = _radioG.RadioField;
_radioGroup.AddKid(_radioField1);
ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT, new Phrase(LANGUAGES_gc[i], new Font(Font.FontFamily.HELVETICA, 18)), 70, 790 - i * 40, 0);
}
/* Button */
_rect = new Rectangle(300, 806, 370, 788);
PushbuttonField button = new PushbuttonField(wri, _rect, "Buttons");
button.BackgroundColor = new GrayColor(0.75f);
button.BorderColor = GrayColor.GRAYBLACK;
button.BorderWidth = 1;
button.BorderStyle = PdfBorderDictionary.STYLE_BEVELED;
button.TextColor = GrayColor.GRAYBLACK ;
button.FontSize = 12;
button.Text = "Submit";
//button.Layout = PushbuttonField.LAYOUT_ICON_LEFT_LABEL_RIGHT;
button.ScaleIcon = PushbuttonField.SCALE_ICON_ALWAYS;
button.ProportionalIcon = true;
button.IconHorizontalAdjustment = 0;
field = button.Field;
field.Action = PdfAction.JavaScript("this.showButtonState()", wri);
wri.AddAnnotation(field);
//}
//return ms.ToArray();
/*----------------------------------------------------*/
wri.AddAnnotation(_radioGroup);
wri.AddAnnotation(button.Field);
cb = wri.DirectContent;
doc.Close();
HttpContext.Current.Response.Write(doc);
HttpContext.Current.Response.End();
}
Can someone suggest me the solution??
Let's make an analogy here, with a simpler file-type. Suppose you're showing the user a textfile. They open it in their native .txt app (notepad) and make some changes.
Do you expect their changes to be communicated automatically back to your server?
Or that their changes are magically propagated? Of course not.
And even if it would, this seems like functionality notepad would have to provide, rather than something the creator of the file should do.
Now, as it so happens, there is a particular standard of pdf documents that exists for precisely your use-case. Essentially, the document sets up a connection to a server, and synchronizes. However, this standard is rather obscure, not a lot of viewers support it.
To the best of my knowledge, no pdf library (including iText) supports making documents like that.

iText Text Expansion Not Working in Certain Conditions

The following code will generate a pdf that will not use the specified text expansions when read out loud. It seems that adding the image in a container is causing the problem.
void Main()
{
using(var ms = new MemoryStream())
{
var doc = new Document(PageSize.LETTER, 72, 72, 72, 72);
var writer = PdfWriter.GetInstance(doc, ms);
writer.SetTagged();
doc.Open();
var c1 = new Chunk("ABC");
c1.SetTextExpansion("the alphabet");
var p1 = new Paragraph();
p1.Add(c1);
doc.Add(p1);
// Adding this image to the document as img > chunk > doc causes the text expansion not to work.
// Adding this image to the document as img > doc works
var t = writer.DirectContent.CreateTemplate(6, 6);
t.SetLineWidth(1f);
t.Circle(3f, 3f, 1.5f);
t.SetGrayFill(0);
t.FillStroke();
var i = iTextSharp.text.Image.GetInstance(t);
var c2 = new Chunk(i, 0, 0);
doc.Add(c2);
var c3 = new Chunk("foobar");
c3.SetTextExpansion("foo bar");
var p3 = new Paragraph();
p3.Add(c3);
doc.Add(p3);
doc.Close();
ms.Flush();
File.WriteAllBytes("d:\\expansion.pdf", ms.ToArray());
}
}
Is this just me doing something wrong or a bug?
While this doesn't read out loud correctly in Adobe Reader, it does in JAWS. So it sounds like this is a reader issue and not necessarily an iText issue.

iTextSharp does not render header/footer when using element generated by XmlWorkerHelper

I am trying to add header/footer to a PDF whose content is otherwise generated by XMLWorkerHelper. Not sure if it's a placement issue but I can't see the header/footer.
This is in an ASP.NET MVC app using iTextSharp and XmlWorker packages ver 5.4.4 from Nuget.
Code to generate PDF is as follows:
private byte[] ParseXmlToPdf(string html)
{
XhtmlToListHelper xhtmlHelper = new XhtmlToListHelper();
Document document = new Document(PageSize.A4, 30, 30, 90, 90);
MemoryStream msOutput = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, msOutput);
writer.PageEvent = new TextSharpPageEventHelper();
document.Open();
var htmlContext = new HtmlPipelineContext(null);
htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());
var cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);
cssResolver.AddCssFile(HttpContext.Server.MapPath("~/Content/themes/mytheme.css"), true);
var pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer)));
var worker = new XMLWorker(pipeline, true);
var parser = new XMLParser();
parser.AddListener(worker);
using (TextReader sr = new StringReader(html))
{
parser.Parse(sr);
}
//string text = "Some Random Text";
//for (int k = 0; k < 8; ++k)
//{
// text += " " + text;
// Paragraph p = new Paragraph(text);
// p.SpacingBefore = 8f;
// document.Add(p);
//}
worker.Close();
document.Close();
return msOutput.ToArray();
}
Now instead of using these three lines
using (TextReader sr = new StringReader(html))
{
parser.Parse(sr);
}
if I comment them out and uncomment the code to add a random Paragraph of text (commented in above sample), I see the header/footer along with the random text.
What am I doing wrong?
The EventHandler is as follows:
public class TextSharpPageEventHelper : PdfPageEventHelper
{
public Image ImageHeader { get; set; }
public override void OnEndPage(PdfWriter writer, Document document)
{
float cellHeight = document.TopMargin;
Rectangle page = document.PageSize;
PdfPTable head = new PdfPTable(2);
head.TotalWidth = page.Width;
PdfPCell c = new PdfPCell(ImageHeader, true);
c.HorizontalAlignment = Element.ALIGN_RIGHT;
c.FixedHeight = cellHeight;
c.Border = PdfPCell.NO_BORDER;
head.AddCell(c);
c = new PdfPCell(new Phrase(
DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + " GMT",
new Font(Font.FontFamily.COURIER, 8)
));
c.Border = PdfPCell.TOP_BORDER | PdfPCell.RIGHT_BORDER | PdfPCell.BOTTOM_BORDER | PdfPCell.LEFT_BORDER;
c.VerticalAlignment = Element.ALIGN_BOTTOM;
c.FixedHeight = cellHeight;
head.AddCell(c);
head.WriteSelectedRows(
0, -1, // first/last row; -1 flags all write all rows
0, // left offset
// ** bottom** yPos of the table
page.Height - cellHeight + head.TotalHeight,
writer.DirectContent
);
}
}
Feeling angry and stupid for having lost more than two hours of my life to Windows 8.1! Apparently the built in PDF Reader app isn't competent enough to show 'Headers'/'Footers'. Installed Foxit Reader and opened the output and it shows the Headers allright!
I guess I should try with Adobe Reader too!!!
UPDATES:
I tried opening the generated PDF in Adobe Acrobat reader and finally saw some errors. This made me look at the HTML that I was sending to the XMLWorkerHelper more closely. The structure of the HTML had <div>s inside <table>s and that was tripping up the generated PDF. I cleaned up the HTML to ensure <table> inside <table> and thereafter the PDF came out clean in all readers.
Long story short, the code above is fine, but if you are testing for PDF's correctness, have Adobe Acrobat Reader handy at a minimum. Other readers behave unpredictably as in either not reporting errors or incorrectly rendering the PDF.

How can I remove the default top margin on a pdf document with itextsharp?

I´m trying to get my pdf document to start at (0,0) however it seems that the document object has a default top margin which I cannot set to 0.
Is there a way to do this?
My code looks like the following
using (MemoryStream memoria = new MemoryStream())
{
Document pdf = new Document(new Rectangle(288, 144));
try
{
PdfWriter writer = PdfWriter.GetInstance(pdf, memoria);
pdf.Open();
pdf.SetMargins(0, 0, 0, 0);
PdfPTable tPrincipal = new PdfPTable(2);
tPrincipal .WidthPercentage = 100;
tPrincipal .DefaultCell.Border = 0;
tPrincipal .TotalWidth = 288f;
tPrincipal .LockedWidth = true;
....
I just can´t get to set the top margin to 0. It just doesnt care about my setting to (0,0,0,0) and leaves a top margin (around 50f).
You'll need to set your margins in your Document constructor, like this:
Document pdf = new Document(new Rectangle(288f, 144f), 0, 0, 0, 0);
You won't need to use the Document.SetMargins() method. I believe you'd use SetMargins()after you create a new page by calling Document.NewPage().
Option 1:
Document doc = new Document();
doc.setMargins(0 , 0 , 0 , 0);
Option 2:
Document pdf = new Document(new Rectangle(595 , 842 ), 0, 0, 0, 0);
Where, 595x842 is A4 size paper.
I did like below where “…/XYZ.pdf” is the path to generated pdf file download path.
PdfDocument pdfDoc = new PdfDocument(new com.itextpdf.kernel.pdf.PdfWriter(“…/XYZ.pdf”));
Document doc = new Document(pdfDoc);
doc.setMargins(0f , 0f , 0f , 0f);