Why do my PDF fields stay blank in Adobe Reader if I do not flatten the PDF document? - itext

I use iTextSharp to populate fields in a PDF. I would like the PDF to stay editable when the user opens it (i.e. so I can't flatten the PDF).
The problem is that when I stamp the PDF and view the values in Adobe Reader the fields remain blank.
If I stamp the PDF, and flatten it, and then view the values then the fields do in fact have values.
How can I populate fields and keep the PDF editable?
Here's my method as an ASP.NET MVC action:
public ActionResult GetPdfTWOWithSurname()
{
PdfHelpers _pdfHelper = new PdfHelpers(Server, Url, "~/Content/newForm.pdf");
var outStream = new MemoryStream();
var pdfReader = new PdfReader(_pdfHelper.GetPdfBytes());
var pdfStamper = new PdfStamper(pdfReader, outStream, '\0', true); //The 'true' value is important. Otherwise the document loses some dynamic features.
pdfStamper.Writer.CloseStream = false;
pdfStamper.FormFlattening = false; //Allow editing after close.
var fields = pdfStamper.AcroFields;
//Fill in text fields...
fields.SetField("topmostSubform[0].Page2[0].InvestorDetails_Surname[0]", "This is a surname");
pdfStamper.Close();
//Get outStream bytes and return...
outStream.Seek(0, SeekOrigin.Begin);
byte[] outBytes = new byte[outStream.Length];
outStream.Read(outBytes, 0, (int)outStream.Length);
return File(outBytes, "application/pdf", "ThePdfFileTWO.pdf");
}
Note that this PDF is reader enabled, and I assume it stays that way even after the save since Adobe reader shows the "Extended" toolbox when I open the file. And the fields remain editable.
Also note that the PDF remains editable, and the fields have values... in FoxitPDF reader.
Please HELP

I have that same issue and it only happens if the PDF was created with Adobe Acrobat with Extended Features. Only option I've found is to re-create the form so that pop-up box has no reason to pop-up. GL

http://what-when-how.com/itext-5/preserving-the-usage-rights-of-reader-enabled-forms-itext-5/
set FormFalttering to False and set RemoveUsageRights() (depends from the iText version)

Related

Change page numbers in EvoPDF

Within a pdf it is possible to change page numbering, so the first page would be page 5, etc.
(This has nothing to do with headers and footers, i'm speaking strictly about the page numbers as they appear in the pdf toolbar)
Is it possible to control those numbers with EvoPDF?
Yes, apparently with EVOPDF v5 you can set the number to be displayed on the page using the PageNumberingStartIndex property of the PdfHeaderOptions object (same for Footers). I don't know of any examples using this.
It is not possible to change the page numbering displayed by Adobe Reader using an option in the generated PDF document. What you can do is to make the PDF viewer go to a certain page in PDF document when the document is opened. You can check the Go To a Location in a PDF Page When the Document is Opened Demo . The C# code to implement this feature is:
protected void convertToPdfButton_Click(object sender, EventArgs e)
{
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";
Document pdfDocument = null;
try
{
// Convert a HTML page to a PDF document object
pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text);
int goToPageNumber = int.Parse(pageNumberTextBox.Text);
if (goToPageNumber > pdfDocument.Pages.Count)
{
return;
}
// Get destination PDF page
PdfPage goToPage = pdfDocument.Pages[goToPageNumber - 1];
// Get the destination point in PDF page
float goToX = float.Parse(xLocationTextBox.Text);
float goToY = float.Parse(yLocationTextBox.Text);
PointF goToLocation = new PointF(goToX, goToY);
// Get the destination view mode
DestinationViewMode viewMode = SelectedViewMode();
// Create the destination in PDF document
ExplicitDestination goToDestination = new ExplicitDestination(goToPage, goToLocation, viewMode);
// Set the zoom level when the destination is displayed
if (viewMode == DestinationViewMode.XYZ)
goToDestination.ZoomPercentage = int.Parse(zoomLevelTextBox.Text);
// Set the document Go To open action
pdfDocument.OpenAction.Action = new PdfActionGoTo(goToDestination);
// Save the PDF document in a memory buffer
byte[] outPdfBuffer = pdfDocument.Save();
// Send the PDF as response to browser
// Set response content type
Response.AddHeader("Content-Type", "application/pdf");
// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Go_To_Page_Open_Action.pdf; size={0}", outPdfBuffer.Length.ToString()));
// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);
// End the HTTP response and stop the current page processing
Response.End();
}
finally
{
// Close the PDF document
if (pdfDocument != null)
pdfDocument.Close();
}
}

How to move text written in Type 3 Font from one pdf to other pdf?

I have a pdf which include text written in Type 3 Font.
I want to get some text from it and write it into other pdf in exactly same shape.
I am using itext. Please give me a tip.
edit: I attached my code.
DocumentFont f = renderInfo.getFont();
String str = renderInfo.getText();
x = renderInfo.getBaseline().getStartPoint().get(Vector.I1);
In this code, I want to write str into x value position.
In Type 3 Font, is it work?
You can copy parts of one page to a new one using code like this:
InputStream resourceStream = getClass().getResourceAsStream("from.pdf");
PdfReader reader = new PdfReader(new FileOutputStream("from.pdf"));
Rectangle pagesize = reader.getPageSizeWithRotation(1);
Document document = new Document(pagesize);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("areaOfFrom.pdf"));
document.open();
PdfContentByte content = writer.getDirectContent();
PdfImportedPage page = writer.getImportedPage(reader, 1);
content.saveState();
content.rectangle(0, 350, 360, 475);
content.clip();
content.newPath();
content.addTemplate(page, 0, 0);
content.restoreState();
document.close();
reader.close();
This turns your
into
Unfortunately, though, that hidden content is merely... hidden... but it is still there. You can especially mark the lines with that hidden text and try to copy&paste them.
If you want to completely remove that hidden text (or start out by merely copying the desired text), you have to inspect the content of the imported page and filter it. I'm afraid iText does not yet explicitly support something like that. It can be done using the iText lowlevel API but it is quite some work.

Add itextsharp barcode image to PdfPCell

I'm trying generate barcodes from a text string and then PDF the results using iTextSharp. Right now, I have the code below:
// Create a Document object
var pdfToCreate = new Document(PageSize.A4, 0, 0, 0, 0);
// Create a new PdfWrite object, writing the output to a MemoryStream
var outputStream = new MemoryStream();
var pdfWriter = PdfWriter.GetInstance(pdfToCreate, outputStream);
PdfContentByte cb = new PdfContentByte(pdfWriter);
// Open the Document for writing
pdfToCreate.Open();
PdfPTable BarCodeTable = new PdfPTable(3);
// Create barcode
Barcode128 code128 = new Barcode128();
code128.CodeType = Barcode.CODE128_UCC;
code128.Code = "00100370006756555316";
// Generate barcode image
iTextSharp.text.Image image128 = code128.CreateImageWithBarcode(cb, null, null);
// Add image to table cell
BarCodeTable.AddCell(image128);
// Add table to document
pdfToCreate.Add(BarCodeTable);
pdfToCreate.Close();
When I run this and try to open the PDF programmatically, I receive an error saying "The document has no pages."
However when I use:
pdfToCreate.Add(image128);
(instead of adding to a table, I add it to a cell), the barcode shows up (though it's floating off the document).
I'd like to send the barcodes to a table so that I can format the document easier. The final product will be 20-60 barcodes read from a database. Any idea on where I'm going wrong?
You are telling the PdfPTable constructor to create a three column table but only providing one of the columns:
PdfPTable BarCodeTable = new PdfPTable(3);
iTextSharp by default ignores incomplete rows. You can either change the constructor to match your column count, add the extra cells or call BarCodeTable.CompleteRow() which will fill in the blanks using the default cell template.

creating a pdf from a template in itextsharp and outputting as content disposition.

I would like to open an existing pdf, add some text and then output as content disposition using itext sharp. I have the following code. Where it falls down it is that i want to output as memory stream but need to filestream to open the original file.
Here's what i have. Obviously defining PdfWriter twice won't work.
public static void Create(string path)
{
var Response = HttpContext.Current.Response;
Response.Clear();
Response.ContentType = "application/pdf";
System.IO.MemoryStream m = new System.IO.MemoryStream();
Document document = new Document();
PdfWriter wri = PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
PdfWriter.GetInstance(document, m);
document.Open();
document.Add(new Paragraph(DateTime.Now.ToString()));
document.NewPage();
document.Add(new Paragraph("Hello World"));
document.Close();
Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
Response.OutputStream.Flush();
Response.OutputStream.Close();
Response.End();
}
You've got a couple of problems that I'll try to walk you through.
First, the Document object is only for working with new PDFs, not modifying existing ones. Basically the Document object is a bunch of wrapper classes that abstract away the underlying parts of the PDF spec and allow you to work with higher level things like paragraphs and reflowable content. These abstractions turn what you think of "paragraphs" into raw commands that write the paragraph one line at a time with no relationship between lines. When working with an existing document there's no safe way to say how to reflow text so these abstractions aren't used.
Instead you want to use the PdfStamper object. When working with this object you have two choices for how to work with potentially overlapping content, either your new text gets written on top of existing content or your text gets written below it. The two methods GetOverContent() or GetUnderContent() of an instantiated PdfStamper object will return a PdfContentByte object that you can then write text with.
There's two main ways to write text, either manually or through a ColumnText object. If you've done HTML you can think of the ColumnText object as using a big fixed-position single row, single column <TABLE>. The advantage of the ColumnText is that you can use the higher level abstractions such as Paragraph.
Below is a full working C# 2010 WinForms app targeting iTextSharp 5.1.2.0 that show off the above. See the code comments for any questions. It should be pretty easy to convert this to ASP.Net.
using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
string existingFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file1.pdf");
string newFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file2.pdf");
using (FileStream fs = new FileStream(existingFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (Document doc = new Document(PageSize.LETTER)) {
using (PdfWriter writer = PdfWriter.GetInstance(doc, fs)) {
doc.Open();
doc.Add(new Paragraph("This is a test"));
doc.Close();
}
}
}
//Bind a PdfReader to our first document
PdfReader reader = new PdfReader(existingFile);
//Create a new stream for our output file (this could be a MemoryStream, too)
using (FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
//Use a PdfStamper to bind our source file with our output file
using (PdfStamper stamper = new PdfStamper(reader, fs)) {
//In case of conflict we want our new text to be written "on top" of any existing content
//Get the "Over" state for page 1
PdfContentByte cb = stamper.GetOverContent(1);
//Begin text command
cb.BeginText();
//Set the font information
cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, false), 16f);
//Position the cursor for drawing
cb.MoveText(50, 50);
//Write some text
cb.ShowText("This was added manually");
//End text command
cb.EndText();
//Create a new ColumnText object to write to
ColumnText ct = new ColumnText(cb);
//Create a single column who's lower left corner is at 100x100 and upper right is at 500x200
ct.SetSimpleColumn(100,100,500,200);
//Add a higher level object
ct.AddElement(new Paragraph("This was added using ColumnText"));
//Flush the text buffer
ct.Go();
}
}
this.Close();
}
}
}
As to your second problem about the FileStream vs MemoryStream, if you look at the method signature for almost every (actually all as far as I know) method within iTextSharp you'll see that they all take a Stream object and not just a FileStream object. Any time you see this, even outside of iTextSharp, this means that you can pass in any subclass of Stream which includes the MemoryStream object, everything else stays the same.
The code below is a slightly modified version of the one above. I've removed most of the comments to make it shorter. The main change is that we're using a MemoryStream instead of a FileStream. Also, when we're done with the PDF when need to close the PdfStamper object before accessing the raw binary data. (The using statment will do this for us automatically later but it also closes the stream so we need to manually do it here.)
One other thing, never, ever use the GetBuffer() method of the MemoryStream. It sounds like what you want (and I have mistakenly used it, too) but instead you want to use ToArray(). GetBuffer() includes uninitialized bytes which usually produces corrupt PDFs. Also, instead of writing to the HTTP Response stream I'm saving the bytes to array first. From a debugging perspective this allows me to finish all of my iTextSharp and System.IO code and make sure that it is correct, then do whatever I want with the raw byte array. In my case I don't have a web server handy so I'm writing them to disk but you could just as easily call Response.BinaryWrite(bytes)
string existingFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file1.pdf");
string newFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file2.pdf");
PdfReader reader = new PdfReader(existingFile);
byte[] bytes;
using(MemoryStream ms = new MemoryStream()){
using (PdfStamper stamper = new PdfStamper(reader, ms)) {
PdfContentByte cb = stamper.GetOverContent(1);
ColumnText ct = new ColumnText(cb);
ct.SetSimpleColumn(100,100,500,200);
ct.AddElement(new Paragraph("This was added using ColumnText"));
ct.Go();
//Flush the PdfStamper's buffer
stamper.Close();
//Get the raw bytes of the PDF
bytes = ms.ToArray();
}
}
//Do whatever you want with the bytes
//Below I'm writing them to disk but you could also write them to the output buffer, too
using (FileStream fs = new FileStream(newFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
fs.Write(bytes, 0, bytes.Length);
}
The second part of your question title says:
"outputting as content disposition"
If that's what you really want you can do this:
Response.AddHeader("Content-Disposition", "attachment; filename=DESIRED-FILENAME.pdf");
Using a MemoryStream is unnecessary, since Response.OutputStream is available. Your example code is calling NewPage() and not trying to add the text to an existing page of your PDF, so here's one way to do what you asked:
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=itextTest.pdf");
PdfReader reader = new PdfReader(readerPath);
// store the extra text on the last (new) page
ColumnText ct = new ColumnText(null);
ct.AddElement(new Paragraph("Text on a new page"));
int numberOfPages = reader.NumberOfPages;
int newPage = numberOfPages + 1;
// get all pages from PDF "template" so we can copy them below
reader.SelectPages(string.Format("1-{0}", numberOfPages));
float marginOffset = 36f;
/*
* we use the selected pages above with a PdfStamper to copy the original.
* and no we don't need a MemoryStream...
*/
using (PdfStamper stamper = new PdfStamper(reader, Response.OutputStream)) {
// use the same page size as the __last__ template page
Rectangle rectangle = reader.GetPageSize(numberOfPages);
// add a new __blank__ page
stamper.InsertPage(newPage, rectangle);
// allows us to write content to the (new/added) page
ct.Canvas = stamper.GetOverContent(newPage);
// add text at an __absolute__ position
ct.SetSimpleColumn(
marginOffset, marginOffset,
rectangle.Right - marginOffset, rectangle.Top - marginOffset
);
ct.Go();
}
I think you've already figured out that the Document / PdfWriter combination doesn't work in this situation :) That's the standard method for creating a new PDF document.

How to add a form field to an existing pdf with itextsharp?

How to add a form field to an existing pdf with itextsharp?
I have an existing pdf document, I'd like to add form fields to it without creating a copy and writing out a new document.
After further review, the ruling on the field is overturned. Turns out if you form flatten the stamper the fields do not show on the resulting document (because they lack 'appearance' settings). BTW, form flattening prevents further edits of a form field. Now we can add appearance to the form, however, an easier way is to use the TextField class and not worry about explicitly setting up 'appearance' objects.
public void ABetterWayToAddFormFieldToExistingPDF( )
{
PdfReader reader = new PdfReader(#"c:\existing.pdf");
FileStream out = new FileStream(#"C:\existingPlusFields.pdf", FileMode.Create, FileAccess.Write);
PdfStamper stamp = new PdfStamper(reader, out);
TextField field = new TextField(stamp.Writer, new iTextSharp.text.Rectangle(40, 500, 360, 530), "some_text");
// add the field here, the second param is the page you want it on
stamp.AddAnnotation(field.GetTextField(), 1);
stamp.FormFlattening = true; // lock fields and prevent further edits.
stamp.Close();
}
I struggled with this for awhile so figured I'd post the Question & Answer
Using the PdfStamper itext class is the key. (I guess this does make a copy but it's much cleaner than using the itext PdfCopy classes).
public void AddFormFieldToExistingPDF( )
{
PdfReader reader = new PdfReader(#"c:\existing.pdf");
FileStream out = new FileStream(#"C:\existingPlusFields.pdf", FileMode.Create, FileAccess.Write);
PdfStamper stamp = new PdfStamper(reader, out);
PdfFormField field = PdfFormField.CreateTextField(stamp.Writer, false, false, 50);
// set a field w/some position and size
field.SetWidget(new iTextSharp.text.Rectangle(40, 500, 360, 530),
PdfAnnotation.HIGHLIGHT_INVERT);
field.SetFieldFlags(PdfAnnotation.FLAGS_PRINT);
field.FieldName = "some_field";
// add the field here, the second param is the page you want it on
stamp.AddAnnotation(field, 1);
stamp.Close();
}
Using pdfStamper you can complete it.
PdfStamper Stamper= new PdfStamper(new PdfReader(sourcefile), File.Create(NewOutputFile));
TextField moreText = new TextField(Stamper.Writer,
new iTextSharp.text.Rectangle(20, 20, 590, 780), "moreText");
moreText.Visibility = TextField.VISIBLE_BUT_DOES_NOT_PRINT;
moreText.Text = "Use this space for any additional information";
moreText.Options = (TextField.MULTILINE);
PdfFormField Fieldtxt = moreText.GetTextField();
Stamper.AddAnnotation(Fieldtxt, n);