iText7 PdfFormField.SetValue removes leading blanks - itext7

I am filling a pdf form with iText7. If I set the value of a form field (PdfFormField.SetValue()) with leading blanks (ie " some text"), the blanks are not visible in the final pdf. Is there any way to keep the leading blanks in the final pdf?
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDocument, true);
foreach (var fld in form.GetFormFields())
{
PdfFormField field = form.GetField(fld.Key);
field.SetValue(" some text");
}

Related

How to force with iText7 a new blank page only when necessary in order to have each text exactly on two pages?

I'm using itext 7.2.1 and I've this situation: I have a list of letters with variable content that normally fits in one page, but occasionally can span over two pages.
My goal is to force a new blank page after the "short" letters so they start alway on odd pages. The length of text is not know in advance.
Basically I have the following code:
PdfWriter writer = new PdfWriter("letters.pdf");
Document document = new Document(new PdfDocument(writer));
List<String> letters = . . .code to retrieve letters text;
int nLetter = 0;
for (String text : letters) {
Paragraph p = new Paragraph().add(text);
doc.add(p);
nLetter++;
//now after layout of last paragraph I must ensure that
//the next paragraph starts on page (nLetter*2 + 1): how can I do this?
}
. . .
doc.close();
What is the best way to do so in iText7? I tried with custom DocumentRenderer but I haven't found a clean and working solution.
The best and simpliest way to know if your next paragraph starts on the next page of the document is to get the root renderer of your document and get current area. And it will return the area(rectangle and page) where the end of the text is located.
doc.getRenderer().getCurrentArea();

Using Chinese fonts in TCPDF and FPDI. Encoding problems

I am writing a script that generates Chinese character worksheets (so students can generate and practice writing)
The script is passed a 15 character string from a form in index.php.
The string is then exploded into an array of 15 elements (each a Chinese character).
The problem arises when I want to use the Write() function to populate the file with these characters, I've used the input to pick appropiate images without any problems but now it's the encoding of the fonts that gives me a hard time.
PS. I need to use a cursive/handwritten font as default 'print' fonts are not suitable for handwriting practice.
Ideally I would like to use HDZB_36.TTF or Sharp Regular Script Font
See the code below as well as images of errors I get with some different fonts.
<?php
header('Content-Type: text/html; charset=utf-8');
// linking TCPDF and FPDI libraries
require_once('tcpdf/tcpdf.php');
require_once('fpdi/fpdi.php');
// First retrieve a 15 chinese charcters long string from POST form in index.php
$hanzi = $_POST["hanzi"];
// Explode the hanzi into a 15 items array
function mb_str_split($hanzi){
return preg_split('/(?<!^)(?!$)/u', $hanzi);
}
$charlist = mb_str_split($hanzi);
// Define starting y positions of each line of the grid
$yPos1 = 10.71;
$yPos2 = 17.94;
// Creating new page with PDF as a background
$pdf = new FPDI();
$background = $pdf->setSourceFile('images/worksheet_template1.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->AddPage();
$pdf->useTemplate($tplIdx, 0, 0, 210, 285, false);
/*
This is where the problem starts, I can manage to display latin characters using helvetica
but when I use any of the chinese fonts (usually encoded as GB2312 or BIG5) it fails.
With some larger (ex. stsong) fonts I get a browser error saying: No data received ERR_EMPTY_RESPONSE (Image 1)
With font 'htst3' the characters appeared upside down and were full of artifacts (Image 2).
With font HDZB_36 the characters were not rendered at all.
Other fonts will result in all of the chars displayed as '?' (Image 3)
*/
$fontname = TCPDF_FONTS::addTTFfont('ukai.ttf', 'TrueTypeUnicode', '', 64);
$pdf->SetFont('ukai','', 20);
for ($i = 0; $i <= 14; $i++){
// Generating path of the stroke order image (that works fine)
$sImgPath = "images/x-s.png";
$sImgPath = str_ireplace('x', $charlist[$i], $sImgPath);
// Stroke order image
$pdf->Image($sImgPath, '14', $yPos1, '','5');
// Here we will populate grid of the worksheet with chinese characters as TEXT
$pdf->SetXY(12.4,$yPos2);
$pdf->SetTextColor(0, 0, 0);
$pdf->Write(0, $charlist[$i], '', false);
$pdf->SetXY(24.2,$yPos2);
$pdf->SetTextColor(192,192,192);
$pdf->Write(0, $charlist[$i], '', false);
// Increase the y pos values so the next run of for() will draw in another line
$yPos1 = $yPos1+17.83;
$yPos2 = $yPos2+17.78;
}
ob_clean();
$pdf->Output('worksheet.pdf', 'I');
?>
Just a suggestion:
The file you generate worksheet.pdfshould perhaps have the same encoding as your letters.
The PDF should have the appropriate encoding, see: https://stackoverflow.com/a/10656899/1933185

Parse PDF with ABCPDF

I want to parse a PDF document I download with ABCPDF, but I cant find any elements in the document or how to reach them and iterate them. I want to parse out some text.
var webClient = new WebClient();
var bytes = webClient.DownloadData("http://test.com/test.pdf");
var doc = new Doc();
doc.Read(bytes);
Use the Doc.GetText method to extract content from the current page, specifying the format in which content is to be returned.
doc.PageNumber = 1;
string pageContent = doc.GetText("Text");
The example above will return plain text in layout order. Specifying "SVG" or "SVG+" returns additional information along with the text, such as style and position.

Vaadin : My label ignores the carriage return character

I have an incoming text string that contains a line break ("\r").
When I output it with : System.out.println(myString), the carriage return is interpreted.
However, when I set the string as the Label's content, it ignores the carriage return.
How can I force the Label to interpret the carriage/line return (without the XHTML mode) ?
This is how you can put this text into your label:
#Override
public void init() {
Window window = new Window();
Label label = new Label("<pre>First line\rSecond line\nThird line</pre>", Label.CONTENT_XHTML);
window.addComponent(label);
setMainWindow(window);
}
The key is using Label.CONTENT_XHTML content mode and enclosing the text inside a <pre> tag.
In Vaadin 7.0 you can use ContentMode.PREFORMATTED e.g.:
String resultText = "First line\rSecond line\nThird line";
Label dateLabel = new Label( resultText, ContentMode.PREFORMATTED );
and if you want text to look sexy you can use some themes, something like:
dateLabel.setStyleName( Runo.LABEL_SMALL );
That should work and is elegant as well.
After reading The book of Vaadin, and few tests, I don't think \r can be interpreted by the Label.
Replacing \r with \n gives you two options :
Label.setContentMode(Label.CONTENT_XHTML). //But you don't want to do this
Label.setContentMode(Label.CONTENT_PREFORMATTED) //But I think it's not the display you want
Regards.
Éric

Appending ="0xxxx" to excel cell while exporting using NPOI to maintain leading zero, or how to set cell as text

I am trying to maintain the leading zeros while exporting a column holding phone numbers to excel using NPOI in an asp.net mvc 3 application. I have read here; http://creativyst.com/Doc/Articles/CSV/CSV01.htm#CSVAndExcel that I can append ="[some number beginning with 0]" to have excel maintain the zeros. I have also read that if I set the cell as text it will maintain the zero. I have been unsuccessful in my attempts to do this. Here is my code;
public ActionResult Export(int page, string orderBy, string filter)
{
//Get the data representing the current grid state - page, sort and filter
GridModel model = Model().ToGridModel(page, 10, orderBy, string.Empty, filter);
var orders = model.Data.Cast<Advertiser>();
//Create new Excel workbook
var workbook = new HSSFWorkbook();
//Create new Excel sheet
var sheet = workbook.CreateSheet();
//(Optional) set the width of the columns
sheet.SetColumnWidth(0, 10 * 256);
sheet.SetColumnWidth(1, 50 * 256);
sheet.SetColumnWidth(2, 50 * 256);
sheet.SetColumnWidth(3, 50 * 256);
//Create a header row
var headerRow = sheet.CreateRow(0);
//Set the column names in the header row
headerRow.CreateCell(0).SetCellValue("Name");
headerRow.CreateCell(1).SetCellValue("Phone");
headerRow.CreateCell(5).SetCellValue("Company Name");
headerRow.CreateCell(7).SetCellValue("Address 1");
headerRow.CreateCell(8).SetCellValue("Address 2");
headerRow.CreateCell(9).SetCellValue("Address 3");
headerRow.CreateCell(10).SetCellValue("Address 4");
headerRow.CreateCell(11).SetCellValue("Post Code");
headerRow.CreateCell(14).SetCellValue("Email");
headerRow.CreateCell(16).SetCellValue("Website");
headerRow.CreateCell(19).SetCellValue("Listing Type");
//(Optional) freeze the header row so it is not scrolled
sheet.CreateFreezePane(0, 1, 0, 1);
int rowNumber = 1;
//Populate the sheet with values from the grid data
foreach (Advertiser order in orders)
{
//Create a new row
var row = sheet.CreateRow(rowNumber++);
//Set values for the cells
row.CreateCell(0).SetCellValue(order.AdvertiserName);
row.CreateCell(1).SetCellValue(order.Phone);
row.CreateCell(3).SetCellValue(order.CompanyName);
row.CreateCell(5).SetCellValue(order.Address1);
row.CreateCell(6).SetCellValue(order.Address2);
row.CreateCell(7).SetCellValue(order.Address3);
row.CreateCell(8).SetCellValue(order.Address4);
row.CreateCell(9).SetCellValue(order.Postcode);
row.CreateCell(10).SetCellValue(order.AdvertiserEmail);
row.CreateCell(11).SetCellValue(order.Website);
row.CreateCell(12).SetCellValue(order.listing.type);
}
//Write the workbook to a memory stream
MemoryStream output = new MemoryStream();
workbook.Write(output);
//Return the result to the end user
return File(output.ToArray(), //The binary data of the XLS file
"application/vnd.ms-excel", //MIME type of Excel files
"Advertisers.xls"); //Suggested file name in the "Save as" dialog which will be displayed to the end user
}
}
I have tried the setCellTyoe method in various places with no luck.
I don't mind how it's done, I just want to maintain the leading zeros when the sheet is exported.
I can not test this but did you try setting the type in the createCell call? If all else fails you could resort to the tried and true hack of putting a single quote before the leading zero:
'00185
This will force the cell to text and excel should only display it on edit.
Changed the data type from string to int and NPOI set the cell as text, keeping the leading zero.

Resources