Acrofields created with libre office are not code fillable unless I edit the pdf once - itext

I am creating a form control in Libre office and am exporting the document to pdf.
Trying to set the text of the control (a textbox) using itextsharp (in other words c# program) only empties the box.
However, if I open the pdf using acrobat reader and edits the text in the box, saving the document results in a pdf where it is possible to write to that textbox.
Why do I have to do that?
Error reproduction
Cliking the toolbar icon in libre office.
Dragging out a square in the document.
Double clicking that box, giving it the name currenttime.
Exporting to pdf:
c# code
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (var fs = new FileStream(saveFileDialog1.FileName, FileMode.Create))
{
var reader = new PdfReader(openFileDialog1.FileName);
{
using (var pdfStamper = new PdfStamper(reader, fs))
{
var acroFields = pdfStamper.AcroFields;
acroFields.SetField("currentdate", DateTime.Now.ToString());
pdfStamper.FormFlattening = true;
pdfStamper.FreeTextFlattening = true;
pdfStamper.Writer.CloseStream = false;
}
}
reader.Close();
fs.Close();
}
}
}
edit
Here comes textual dumps of the pdf. I have changed some binary data places with "some binary data". The textbox has been given the default value "123".
pdf after it has been created with libre office is of version 1.4
%PDF-1.4
some binary data
2 0 obj
<</Length 3 0 R/Filter/FlateDecode>>
stream
some binary data
endstream
endobj
3 0 obj
78
endobj
7 0 obj
<</Type/FontDescriptor/FontName/LiberationSans
/Flags 4
/FontBBox[-543 -303 1301 980]/ItalicAngle 0
/Ascent 905
/Descent -211
/CapHeight 979
/StemV 80
>>
endobj
8 0 obj
<</Type/Font/Subtype/TrueType/BaseFont/LiberationSans
/Encoding/WinAnsiEncoding
/FirstChar 32 /LastChar 255
/Widths[277 277 354 556 556 889 666 190 333 333 389 583 277 333 277 277
556 556 556 556 556 556 556 556 556 556 277 277 583 583 583 556
1015 666 666 722 722 666 610 777 722 277 500 666 556 833 722 777
666 777 722 666 610 722 666 943 666 666 610 277 277 277 469 556
333 556 556 500 556 556 277 556 556 222 222 500 222 833 556 556
556 556 333 500 277 556 500 722 500 500 500 333 259 333 583 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
277 333 556 556 556 556 259 556 333 736 370 556 583 333 736 552
399 548 333 333 333 576 537 333 333 333 365 556 833 833 833 610
666 666 666 666 666 666 1000 722 666 666 666 666 277 277 277 277
722 722 777 777 777 777 777 583 777 722 722 722 722 666 666 610
556 556 556 556 556 556 889 500 556 556 556 556 277 277 277 277
556 556 556 556 556 556 556 548 610 556 556 556 556 500 556 500
]
/FontDescriptor 7 0 R>>
endobj
5 0 obj
<</F1 8 0 R
>>
endobj
9 0 obj
<</Font 5 0 R
/ProcSet[/PDF/Text]
>>
endobj
1 0 obj
<</Type/Page/Parent 6 0 R/Resources 9 0 R/MediaBox[0 0 595 842]/Annots[
4 0 R ]
/Group<</S/Transparency/CS/DeviceRGB/I true>>/Contents 2 0 R>>
endobj
6 0 obj
<</Type/Pages
/Resources 9 0 R
/MediaBox[ 0 0 595 842 ]
/Kids[ 1 0 R ]
/Count 1>>
endobj
10 0 obj
<</Type/XObject
/Subtype/Form
/BBox[0 0 82.7 23.1]
/Resources 9 0 R
/Length 18
/Filter/FlateDecode
>>
stream
some binary data
endstream
endobj
4 0 obj
<</Type/Annot/Subtype/Widget/F 4
/Rect[59.6 759.3 142.5 782.2]
/FT/Tx
/P 1 0 R
/T(currenttime)
/Ff 4096
/V <FEFF003100320033>
/DV <FEFF003100320033>
/DR<</Font 5 0 R>>
/DA(0 0 0 rg /F1 12 Tf)
/AP<<
/N 10 0 R
>>
>>
endobj
11 0 obj
<</Type/Catalog/Pages 6 0 R
/OpenAction[1 0 R /XYZ null null 0]
/Lang(sv-SE)
/AcroForm<</Fields[
4 0 R
]/DR 9 0 R/NeedAppearances true>>
>>
endobj
12 0 obj
<</Creator<FEFF005700720069007400650072>
/Producer<FEFF004C0069006200720065004F0066006600690063006500200035002E0033>
/CreationDate(D:20170606104859+02'00')>>
endobj
xref
0 13
0000000000 65535 f
0000001431 00000 n
0000000019 00000 n
0000000168 00000 n
0000001843 00000 n
0000001347 00000 n
0000001590 00000 n
0000000187 00000 n
0000000357 00000 n
0000001378 00000 n
0000001688 00000 n
0000002073 00000 n
0000002231 00000 n
trailer
<</Size 13/Root 11 0 R
/Info 12 0 R
/ID [ <5F5DD24A5E7FF740A8BB6B15F88EF602>
<5F5DD24A5E7FF740A8BB6B15F88EF602> ]
/DocChecksum /BFFAD3050AA9FF87945C97B9608B3C6C
>>
startxref
2406
%%EOF
after it has been edited in acrobat reader (I changed the default value of the textbox from "123" to "12"), it will be saved in version 1.6 and an interesting x:xmpmeta information is inserted. Also a lot of empty lines are inserted in the document. At this point, it is programmatically editable.
%PDF-1.6
%âãÏÓ
7 0 obj
<</Linearized 1/L 6449/O 9/E 2599/N 1/T 6160/H [ 451 149]>>
endobj
13 0 obj
<</DecodeParms<</Columns 4/Predictor 12>>/Filter/FlateDecode/ID[<5F5DD24A5E7FF740A8BB6B15F88EF602><FAE65369E246E7409111A7D5BDED1E6F>]/Index[7 17]/Info 6 0 R/Length 52/Prev 6161/Root 8 0 R/Size 24/Type/XRef/W[1 2 1]>>stream
some binary data
endstream
endobj
startxref
0
%%EOF
23 0 obj
<</Filter/FlateDecode/I 92/Length 65/S 38/V 69>>stream
some binary data
endstream
endobj
8 0 obj
<</AcroForm<</DA(/Helv 0 Tf 0 g )/DR 22 0 R/Fields[14 0 R]>>/Lang(sv-SE)/Metadata 1 0 R/OpenAction[9 0 R/XYZ null null 0]/Pages 5 0 R/Type/Catalog>>
endobj
9 0 obj
<</Annots[14 0 R]/Contents 12 0 R/CropBox[0 0 595 842]/Group<</CS/DeviceRGB/I true/S/Transparency>>/MediaBox[0 0 595 842]/Parent 5 0 R/Resources 22 0 R/Rotate 0/Type/Page>>
endobj
10 0 obj
<</BBox[0.0 0.0 82.9 22.9]/Filter/FlateDecode/Length 68/Resources 15 0 R>>stream
some binary data
endstream
endobj
11 0 obj
<</Filter/FlateDecode/First 66/Length 1226/N 9/Type/ObjStm>>stream
some binary data
endstream
endobj
12 0 obj
<</Filter/FlateDecode/Length 78>>stream
some binary data
endstream
endobj
1 0 obj
<</Length 3146/Subtype/XML/Type/Metadata>>stream
<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.4-c005 78.147326, 2012/08/23-13:03:03 ">
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<rdf:Description rdf:about=""
xmlns:xmp="http://ns.adobe.com/xap/1.0/"
xmlns:pdf="http://ns.adobe.com/pdf/1.3/"
xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/"
xmlns:dc="http://purl.org/dc/elements/1.1/">
<xmp:CreateDate>2017-06-06T10:48:59+02:00</xmp:CreateDate>
<xmp:CreatorTool>Writer</xmp:CreatorTool>
<xmp:ModifyDate>2017-06-06T11:20:41+02:00</xmp:ModifyDate>
<xmp:MetadataDate>2017-06-06T11:20:41+02:00</xmp:MetadataDate>
<pdf:Producer>LibreOffice 5.3</pdf:Producer>
<xmpMM:DocumentID>uuid:fcdf7344-18ca-44b6-934c-8d5ab8fc8ea3</xmpMM:DocumentID>
<xmpMM:InstanceID>uuid:895fdc09-0aaa-4421-86b2-418c75f88d22</xmpMM:InstanceID>
<dc:format>application/pdf</dc:format>
</rdf:Description>
</rdf:RDF>
</x:xmpmeta>
<?xpacket end="w"?>
endstream
endobj
2 0 obj
<</Filter/FlateDecode/First 4/Length 48/N 1/Type/ObjStm>>stream
some binary data
endstream
endobj
3 0 obj
<</Filter/FlateDecode/First 4/Length 106/N 1/Type/ObjStm>>stream
some binary data
endstream
endobj
4 0 obj
<</DecodeParms<</Columns 3/Predictor 12>>/Filter/FlateDecode/ID[<5F5DD24A5E7FF740A8BB6B15F88EF602><FAE65369E246E7409111A7D5BDED1E6F>]/Info 6 0 R/Length 37/Root 8 0 R/Size 7/Type/XRef/W[1 2 0]>>stream
some binary data
endstream
endobj
startxref
116
%%EOF
edit2
I was putting the files on my dropbox.
https://www.dropbox.com/sh/5btzl9qqzua18q1/AACIjCrvNZ5cunuLj9sze-l3a?dl=0

As already surmised in a comment, the problem is caused by Libre Office creating the PDF with NeedAppearances set to true in the AcroForm dictionary. Furthermore the wrong field name is used.
Field name
In your code you set the field "currentdate" while in your sample PDFs the field is called "currenttime". Obviously you have to use the correct field name.
NeedAppearances flag
This flag tells a PDF viewer that it shall construct appearance streams and appearance dictionaries for all widget annotations in the document. iText, therefore, when filling in the form field
acroFields.SetField("currentdate", DateTime.Now.ToString());
does not create an appearance for that field - any viewer is required to construct new appearances anyways.
Unfortunately form flattening
pdfStamper.FormFlattening = true;
is implemented by using the existing appearances and only them. As no appearance has been created when setting the field, its flattened form turns out to be empty.
(Strictly speaking this implementation of form flattening is wrong: In this case iText is the PDF processor that wants to make use of the appearances; thus, it should create all appearances here, even ignoring existing ones.)
You can fix this by telling iText to create appearances during form fill-ins in spite of the NeedAppearances flag:
using (var pdfStamper = new PdfStamper(reader, fs))
{
var acroFields = pdfStamper.AcroFields;
acroFields.GenerateAppearances = true;// <<<<<<<<<<<<<<<<<<
acroFields.SetField("currenttime", DateTime.Now.ToString());
pdfStamper.FormFlattening = true;
pdfStamper.FreeTextFlattening = true;
pdfStamper.Writer.CloseStream = false;
}
After adding the marked line above, the output of the code includes the changed newly set value.
Additionally Libre Office does not embed the LiberationSans font. As I have not installed it on my system, I only see dots. I would propose you make LibreOffice embed such fonts or else use standard 14 fonts. Otherwise your PDFs won't display as desired on a number of computers.

Related

Unrecognized Quartz MS font

I tried to generate the image with the Quartz MS font as follows.
Then use jTessBoxEditorFX to generate the box file, as follows.
0 26 23 97 125 0
1 169 26 189 122 0
2 209 23 279 124 0
3 305 23 370 124 0
4 391 25 461 121 0
5 481 23 551 124 0
6 571 23 641 124 0
7 665 27 731 124 0
8 753 24 822 124 0
9 842 24 912 125 0
The traineddata cannot be recognized normally. Is there any problem with my practice?
Whether there are similar experiences of seniors can guide me, thank you.

Triangle Sum through down with NON-PRIME numbers with Swift 4

I know this question is asked before for different programming languages, and I tried to implement this with Swift 4 but once I submit my answer, I've been told that my answer was wrong so here is the task;
You will have a TRIANGLE input from a file and you need to find the maximum sum of the numbers according to given rules below;
You will start from the top and move downwards to an adjacent number as in below.
You are only allowed to walk downwards and diagonally.
You can only walk over NON PRIME NUMBERS.
According to above rules the maximum sum of the numbers from top to bottom in below example is 24.
var sampleString = """
1
8 4
2 6 9
8 5 9 3
As you can see this has several paths that fits the rule of NOT PRIME NUMBERS; 1>8>6>9, 1>4>6>9, 1>4>9>9
1 + 8 + 6 + 9 = 24. As you see 1, 8, 6, 9 are all NOT PRIME NUMBERS and walking over these yields the maximum sum.
assignment string:
var assignmentString = """
215
193 124
117 237 442
218 935 347 235
320 804 522 417 345
229 601 723 835 133 124
248 202 277 433 207 263 257
359 464 504 528 516 716 871 182
461 441 426 656 863 560 380 171 923
381 348 573 533 447 632 387 176 975 449
223 711 445 645 245 543 931 532 937 541 444
330 131 333 928 377 733 017 778 839 168 197 197
131 171 522 137 217 224 291 413 528 520 227 229 928
223 626 034 683 839 53 627 310 713 999 629 817 410 121
924 622 911 233 325 139 721 218 253 223 107 233 230 124 233"""
my code:
func maxSumForTriangle(triangleString: String) {
var temporaryIndex = 0
var earlierIndex = 0
var greatSum = 0
var temporaryMaxInLine = 0
let values = triangleString.components(separatedBy: .newlines).map {
$0.components(separatedBy: .whitespaces).compactMap(Int.init)
}
print(values)
print(values.count)
for line in values {
if line.count == 1 {
greatSum += line[0]
earlierIndex = line.count - 1
} else {
for number in line.enumerated() {
if number.offset == earlierIndex || number.offset == earlierIndex + 1 {
//Check the number if its prime or not with the isPrime function we defined
if !isPrime(number.element) {
if number.element > temporaryMaxInLine {
temporaryMaxInLine = number.element
temporaryIndex = number.offset
}
}
}
}
earlierIndex = temporaryIndex
greatSum += temporaryMaxInLine
temporaryMaxInLine = 0
}
}
print(greatSum)
}
Which results in 7619 but then I realized where my problem is; I don't check for every possible path, I just check for the highest non prime number at each line and continue summing with it.
So I need to find a different approach for this problem so that my function can check every possible scenarios and return with the highest sum
I could not figure it out yet, should I implement a different function where it calls itself again so that it can check for all the possible paths?
Sorry for long question but I also wanted to show my old implementation.
This is a typical problem which can be solved with “dynamic programming”: The idea
is to compute the maximal possible sum for every starting
point in the pyramid.
And that becomes simple if we start at the bottom row and work upwards:
We only have to add to each entry the larger of its two lower neighbors.
At the end, the top entry is the desired maximal sum.
Taking the additional condition about non-primes into account, this
can be implemented as
var values = triangleString.components(separatedBy: .newlines).map {
$0.components(separatedBy: .whitespaces).compactMap(Int.init)
}
for row in values.indices.reversed() {
for col in values[row].indices {
if isPrime(values[row][col]) {
values[row][col] = Int.min
} else if row + 1 < values.endIndex {
values[row][col] += max(values[row+1][col], values[row+1][col+1])
}
}
}
print(values[0][0])

Make histogram of pixel intensities without imhist

I have used the unique command to get the unique pixel intensities from my image. Then I tried to make a histogram using them, but it doesn't use all of the intensity values
I = imread('pout.tif');
[rows, columns] = size(I);
UniquePixels=unique(I);
hist=histogram(UniquePixels)
An alternative approach would be to use accumarray combined with unique. I would specifically use the third output of unique to transform your data into a consecutive sequence of 1 up to N where N is the total number of unique intensities, then leverage the first output of unique that will give you the list of unique intensities. Therefore, if the first output of unique is A and the output of accumarray is B, the effect is that at location B(i), this gives the total number of intensities of A(i).
Therefore:
[UniquePixels, ~, id] = unique(I);
histo = accumarray(id, 1);
UniquePixels gives you all unique pixels while histo gives you the counts of each unique pixel corresponding to each element in UniquePixels.
Here's a quick example:
>> I = randi(255, 10, 10)
I =
42 115 28 111 218 107 199 60 140 237
203 22 246 233 159 13 100 91 76 198
80 59 2 47 90 231 62 210 190 125
135 233 198 68 131 241 103 4 49 112
43 39 209 38 103 126 25 11 176 114
154 211 222 35 20 125 34 44 47 79
68 138 22 222 62 87 241 166 94 130
167 255 102 148 32 230 244 187 160 131
176 20 67 141 47 95 147 166 199 209
191 113 205 37 62 29 16 115 21 203
>> [UniquePixels, ~, id] = unique(I);
>> histo = accumarray(id, 1);
>> [UniquePixels histo]
ans =
2 1
4 1
11 1
13 1
16 1
20 2
21 1
22 2
25 1
28 1
29 1
32 1
34 1
35 1
37 1
38 1
39 1
42 1
43 1
44 1
47 3
49 1
59 1
60 1
62 3
67 1
68 2
76 1
79 1
80 1
87 1
90 1
91 1
94 1
95 1
100 1
102 1
103 2
107 1
111 1
112 1
113 1
114 1
115 2
125 2
126 1
130 1
131 2
135 1
138 1
140 1
141 1
147 1
148 1
154 1
159 1
160 1
166 2
167 1
176 2
187 1
190 1
191 1
198 2
199 2
203 2
205 1
209 2
210 1
211 1
218 1
222 2
230 1
231 1
233 2
237 1
241 2
244 1
246 1
255 1
If you double check the input example and the final output, you will see that only the unique pixels are shown combined with their counts. Any bins that were zero in count are not shown.

tesseract OCR output words bounds

How to output words bounds using tesseract command line with config file?
So far I been able to output chars using
tesseract image.png myBox makebox
This created a myBox.box file that looks like this:
N 51 1844 75 1874 0
o 80 1843 100 1867 0
S 113 1843 136 1875 0
I 140 1844 145 1874 0
M 151 1844 181 1874 0
c 197 1843 216 1867 0
a 219 1843 238 1867 0
r 243 1844 254 1867 0
d 256 1843 275 1876 0
How ever those only chars and I need words, so I been able to combine it with standard output
tesseract image.png myBox
This creates a file like this:
no simcard
Combining those two outputs I can get words bounds. How ever I prefer to find a method that does not require examining the same image twice. Please help

subtracting two matrices in matlab, the negative values in result are substituted by zero

I have two matrices in matlab,
> IRwindow =
>
> **183** 171 150 125 137
138 167 184 173 152
105 114 141 167 185
148 113 105 115 141
186 183 147 112 105
>
> ILwindow =
>
> **201** 170 165 177 203
181 174 167 169 189
154 150 156 168 181
187 175 158 131 144
173 186 183 167 141
I want to subtract these two matrices element-wise and get the result; for example for first element (183 - 201= -18 ) BUT the output for this element gives zero. the outcome result will be as below:
> IRwindow - ILwindow
ans =
**0** 1 0 0 0
0 0 17 4 0
0 0 0 0 4
0 0 0 0 0
13 0 0 0 0
how could I keep the real results? without getting zero for negatives in my result-matrix
Run the following example code:
%# Create random matrices
X = randi(100, 5, 5);
Y = randi(100, 5, 5);
%# Convert to strictly non-negative format
X = uint8(X);
Y = uint8(Y);
%# Perform subtractions
A = X - Y;
%# Convert to double format
X = double(X);
Y = double(Y);
%# Perform subtraction
B = X - Y;
For a given sample run:
A =
0 15 36 0 0
0 0 0 0 3
0 0 0 25 0
13 0 15 0 0
0 49 0 0 14
while:
B =
-8 15 36 -4 -65
0 -47 -45 -11 3
-18 -17 -11 25 -52
13 -53 15 -15 -1
-35 49 -47 -8 14
You will notice that all the negative numbers in A have been replaced by 0, while the negative numbers in B are displayed correctly.
Stated simply: if you use a numerical format that is not able to store negative numbers, then Matlab truncates at 0. The solution is to convert to a format that is able to accomodate "real" numbers (or a close approximation thereof) such as double, or perhaps in your case one of the int formats may be more appropriate, such as int8, int16, int32 or int64.
Another option is to use single or double on the subtraction in one line as follows:
ans=double(IRwindow-ILwindow)
I dont get the same problem as you: I have this code:
IRwindow = [
183 171 150 125 137
138 167 184 173 152
105 114 141 167 185
148 113 105 115 141
186 183 147 112 105]
ILwindow = [
201 170 165 177 203
181 174 167 169 189
154 150 156 168 181
187 175 158 131 144
173 186 183 167 141]
IRwindow - ILwindow
and i get this output:
IRwindow =
183 171 150 125 137
138 167 184 173 152
105 114 141 167 185
148 113 105 115 141
186 183 147 112 105
ILwindow =
201 170 165 177 203
181 174 167 169 189
154 150 156 168 181
187 175 158 131 144
173 186 183 167 141
ans =
-18 1 -15 -52 -66
-43 -7 17 4 -37
-49 -36 -15 -1 4
-39 -62 -53 -16 -3
13 -3 -36 -55 -36
Check that you are creating your matrices are being created properly (as doubles and not as unsigned integers).