ITextSharp errror "Off is not a valid name for a checkbox appearance (should be Off or Yes)" - itext

Running the following code snippet under VS 2010 using iTextSharp 5.5.6:
PdfReader reader = new PdfReader("Test.pdf");
FileStream fs = new FileStream("New.pdf", FileMode.Create);
PdfStamper p = new PdfStamper(reader, fs);
AcroFields form = p.AcroFields;
...
RadioCheckField newField = new RadioCheckField(p.Writer, new iTextSharp.text.Rectangle(20, 20), "MyCheckBox", "Off");
PdfFormField RadioField = newField.CheckField;
p.AddAnnotation(RadioField, 1);
I receive the error "ITextSharp errror "Off is not a valid name for a checkbox appearance (should be Off or Yes)" at the line declaring the RadioField object. Is this a bug? How do I work around it?

A check box can have two values:
one you define yourself in your method. That will be the value of the check box when selected.
one that is defined in ISO-32000-1: Off. That's the value of the check box when it's not selected.
There is a bug in your code because you are trying to create a check box of which the value is always Off whether it's selected or not.
Choose another value. Off is reserved for the off state. You need yo define a value for the on state. ISO-32000-1 recommends Yes.
This is how you make sure the check box is selected:
newField.Checked = true;
Unchecked is done like this:
newField.Checked = false;

Related

Creating a OOXML document with Track Changes enabled

How do I create a .docx with track changes enabled? I was told inside word/settings.xml I should modify w:proofState but all info in online docs of OOXML I find about that property is related to grammar and spell checking, but nothing about enabling tracking changes.
It is in word/settings.xml but the element you are looking for is w:trackRevisions.
To do this in C# using the OpenXML SDK you can use the TrackRevisions class. The following code will create a document with one paragraph and with change tracking turned on:
using (WordprocessingDocument package = WordprocessingDocument.Create(filename, WordprocessingDocumentType.Document))
{
// Add a new main document part.
package.AddMainDocumentPart();
//create a body and a paragraph
Body body = new Body();
Paragraph paragraph = new Paragraph();
paragraph.AppendChild(new Run(new Text("This document should have change tracking switched on")));
body.AppendChild(paragraph);
package.MainDocumentPart.Document = new Document(body);
//change tracking code
DocumentSettingsPart documentSettingsPart = package.MainDocumentPart.AddNewPart<DocumentSettingsPart>();
Settings settings = new Settings();
TrackRevisions trackRevisions = new TrackRevisions();
settings.Append(trackRevisions);
documentSettingsPart.Settings = settings;
// Save changes to the main document part.
package.MainDocumentPart.Document.Save();
}

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.

What would be a good way of filtering a GWT CellList using multiple CheckBoxes?

Working in Google Web Toolkit (GWT) I am using a CellList to render the details of a list of Tariffs (using a CompositeCell to show a CheckBoxCell next to a custom cell of my own).
I want to filter the list by tariff length (12, 18, 24, 36 months etc). I would like to render a checkbox for each tariff length at the top of the list, and update the dataProvider as necessary when users uncheck and recheck a box.
I do not know in advance the set of tariff lengths, they will be extracted from the result set when the page is rendered. There could just be two (requiring two checkboxes), but possibly there could be 10 (requiring 10 checkboxes) - I only want to render a checkbox for each as needed.
So somehow I need to associate an int value with each checkbox, and then pass that int to a function that updates the list by removing all tariffs that match. I'm just not sure how to add the handler for the checkboxes and how to get the value for that particular box.
This is what I'm thinking:
// panel to hold boxes
private Panel contractLengthPanel = new HorizontalPanel();
textPanel2.add(contractLengthPanel);
// create a set of the terms, by looping the result set
Set<String> contractTerms = new HashSet<String>();
for(ElecTariff tariff : tariffs)
{
contractTerms.add(Integer.toString(tariff.getContractLength()));
}
// loop that set, creating a CheckBox for each value
for(String term : contractTerms)
{
CheckBox box = new CheckBox(term + " Months");
// set all boxes with the same name, and a unique id
box.getElement().setAttribute("name", "termBoxes");
box.getElement().setAttribute("id", "termBox" + term);
contractLengthPanel.add(box);
}
Now I'm not sure if I'm along the right lines here, but now I have each box as part of the same group (they have the same name) I would like to use that to add a handler that is called when a box is checked or unchecked, passing the box id (which contains the tariff length) to that function.
I hope this wasn't too confusingly written. Help appreciated.
There really is nothing like a "group of checkboxes" in HTML, and neither there is in GWT. There are kind of "groups of radiobuttons" though, but it's only about having their checked state mutually exclusive, it doesn't change anything to the way you work with them from code.
You have to listen to changes on each and every checkbox.
What you can do though is to use the same event handler for all your checkboxes; something like:
ValueChangeHandler<Boolean> handler = new ValueChangeHandler<Boolean>() {
#Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
CheckBox box = (CheckBox) event.getSource();
String id = box.getFormValue();
boolean checked = box.getValue();
…
}
};
(note: I used getFormValue() rather than getElement().getId(); I believe it's a better choice: it's specifically made to associate a value with the checkbox)

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

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)

Hiding cursor in Text component in Eclipse RCP application

In my eclipse RCP application there are a few buttons & few input boxes & this below Text component. My problem is as soon as I press one of the buttons a cursor starts blinking in the below test component. Can you please let me know how to solve this.
I tried:
setting focus to false for Text.
SWT.READ_ONLY for Text .
Code:
Cursor cursor = new Cursor(parent.getDisplay(), SWT.CURSOR_NO);
protocolFilterDescription.setCursor(cursor);
Nothing seems to get rid of this unnecessary cursor.
protocolFilterDescription = new Text(parent, SWT.NONE | SWT.READ_ONLY );
FormData protocolFilterDescriptionLData = new FormData();
protocolFilterDescriptionLData.left = new FormAttachment(0, 1000, 650);
protocolFilterDescriptionLData.top = new FormAttachment(0, 1000, 290);
protocolFilterDescriptionLData.width = 450;
protocolFilterDescriptionLData.height = 12;
protocolFilterDescription.setLayoutData(protocolFilterDescriptionLData);
protocolFilterDescription.setForeground(new Color(parent.getDisplay(),
204, 153, 0));
protocolFilterDescription.setBackground(Display.getCurrent()
.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
protocolFilterDescription.setFont(new Font(parent.getDisplay(),"Verdana",
6, 1));
protocolFilterDescription
.setText("captured");
You have to set the focus of some other SWT component to true to remove the focus from the Text component.
You'll probably have to do this in an ActionListener.
If you want to completely remove the cursor from the Text control (which implies inability to perform a selection there, etc), try calling setEnabled(false) on it.
Also, such requirement suggests that you maybe don't need Text component at all, and could use Label instead.