Remove left and right side borders in itextshap an

2019-03-04 03:22发布

问题:

Remove the left and right side borders of Approved By and sign and also i need to draw a small rectangular box after calibration certificate no:

PdfPCell CalibrationContent = new PdfPCell(FormatPhrase("", 8, Font.NORMAL, BaseColor.BLACK));
CalibrationContent.Colspan = 1;
CalibrationContent.BackgroundColor = BaseColor.WHITE;
CalibrationContent.NoWrap = true;
CalibrationContent.HorizontalAlignment = Element.ALIGN_CENTER;
PdfMastertable.AddCell(CalibrationContent);

CalibrationContent = new PdfPCell(FormatPhrase("Approved By", 8, Font.NORMAL, BaseColor.BLACK));
CalibrationContent.Colspan = 1;
CalibrationContent.MinimumHeight = 20f;
CalibrationContent.BackgroundColor = BaseColor.WHITE;        
CalibrationContent.NoWrap = true;
CalibrationContent.HorizontalAlignment =0;  
pdfMastertable.AddCell(CalibrationContent);

CalibrationContent = new PdfPCell(FormatPhrase("Sign", 8, Font.NORMAL, BaseColor.BLACK));
CalibrationContent.Colspan =1;
CalibrationContent.MinimumHeight = 20f;
CalibrationContent.BackgroundColor = BaseColor.WHITE;
CalibrationContent.NoWrap = true;
CalibrationContent.HorizontalAlignment = 0;
pdfMastertable.AddCell(CalibrationContent); 

Below code is for that certificate number with Tick box

CAPAContent = new PdfPCell(FormatPhrase("Calibration Certificate No: " + "ddmmyy" + '\n' + "Date Issued : " + "ddmmyy" + '\n' + '\n' + "Monthly instrument type wise " + '\n' + "Test conducted Day wise" + '\n', 11, Font.NORMAL, BaseColor.BLACK));
CAPAContent.Colspan = 1;
CAPAContent.BackgroundColor = BaseColor.WHITE;
CAPAContent.NoWrap = true;
CAPAContent.Border = 0;
CAPAContent.HorizontalAlignment = Element.ALIGN_RIGHT;
pdfAction.AddCell(CAPAContent);
pdfAction.SpacingAfter = 20f;
pdfDoc.Add(pdfAction);

回答1:

This indeed removes the border:

CAPAContent.Border = 0;

But whenever I see a student doing this, I'd subtract a point because using an int makes the code hard to read. It's better to do this:

CAPAContent.Border = Rectangle.NO_BORDER;

This way, you can easily see the 0 means: no border will be drawn.

Using the constants that are available in the Rectangle class, also teaches you that there are other options. For instance: If you want to tweak the borders as shown in your screen shot, you can do this:

PdfPCell CalibrationContent = new PdfPCell(FormatPhrase("", 8, Font.NORMAL, BaseColor.BLACK));
CalibrationContent.Border = Rectangle.TOP | Rectangle.LEFT | Rectangle.BOTTOM;
...
CalibrationContent = new PdfPCell(FormatPhrase("Approved By", 8, Font.NORMAL, BaseColor.BLACK));
CalibrationContent.Border = Rectangle.TOP | Rectangle.BOTTOM;
...
CalibrationContent = new PdfPCell(FormatPhrase("Sign", 8, Font.NORMAL, BaseColor.BLACK));
CalibrationContent.Border = Rectangle.TOP | Rectangle.RIGHT | Rectangle.BOTTOM;

This tweaks the borders exactly the way you want to. See also Chapter 4 of "iText in Action - Second Edition", more specifically the example RotationAndColors.cs

Extra answer:

You probably refuse to accept this answer because I didn't answer your follow-up question in the comments. However, you didn't answer my question either. You want to add a tick box, but you aren't answering the question: Do you want an interactive one? That is: a check box that is an actual form field (AcroForm technology). Or do you want a check box character? That is: a Zapfdingbats character that represents a small empty square.

Let's assume that you merely want a character like this:

That can be easily done with a Zapfdingbats character as shown in the TickboxCharacter example:

Paragraph p = new Paragraph("This is a tick box character: ");
Font zapfdingbats = new Font(Font.FontFamily.ZAPFDINGBATS, 14);
Chunk chunk = new Chunk("o", zapfdingbats);
p.add(chunk);
document.add(p);