itext pdf with com.lowagie.text.table - itext

how to avoid space or gap between two tables in iText
using the class com.lowagie.text.table
Table table;
com.lowagie.text.Cell cell;
table = new Table(2);
table.setPadding(2);
table.setWidths(new int[] {100});
table.setWidth(100);
cell = new com.lowagie.text.Cell(new Phrase(
"sample text1", FontFactory
.getFont(FontFactory.HELVETICA, commonfontsize)));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setBorder(0);
table.addCell(cell);
document.add(table);
table = new Table(2);
table.setPadding(2);
table.setWidths(new int[] {50,50});
table.setWidth(100);
cell = new com.lowagie.text.Cell(new Phrase(
"sample text1", FontFactory
.getFont(FontFactory.HELVETICA, commonfontsize)));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setBorder(0);
table.addCell(cell);
cell = new com.lowagie.text.Cell(new Phrase(
"sample text2", FontFactory
.getFont(FontFactory.HELVETICA, 10)));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorder(0);
table.addCell(cell);
document.add(table);
how to remove the space between the first table and second table?

This should give spaces before and after the PdfTable table
table.setSpacingBefore();
table.setSpacingAfter();

Related

Multiple PDF From Original PDF using iTextSharp

I'm using iTextSharp to create an invoice in PDF.
Now I need to create multiple copies of that PDF, adding headers such as "DUPLICATE", "TRIPLICATE", and so on.
Is that possible?
Document pdfDoc = new Document(PageSize.A4, 20, 25, 25, 10);
System.IO.FileStream file = new System.IO.FileStream(Server.MapPath("~/" + fileUploadLoc + "/" + "File.pdf", System.IO.FileMode.OpenOrCreate);
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, file);
fileName = Convert.ToString("Invoice.pdf");
pdfDoc.Open();
pdfWriter.Open();
PdfPTable nested = new PdfPTable(1);
nested.DefaultCell.Border = Rectangle.BOX;
nested.WidthPercentage = 100;
PdfPCell Tableheader = new PdfPCell(new Phrase("Company Name", titleHeaderFont));
Tableheader.HorizontalAlignment = 1;
Tableheader.Border = Rectangle.LEFT_BORDER | Rectangle.RIGHT_BORDER | Rectangle.TOP_BORDER;
Tableheader.Left = 45f;
Tableheader.Right = 45f;
table.AddCell(Tableheader);
PdfPCell Tableheader7 = new PdfPCell(new Phrase("Triplicate for Supplier", fontsize7normaol));
////Above line has to be changed in each new pdf
Tableheader7.HorizontalAlignment = 1;
Tableheader7.Border = Rectangle.LEFT_BORDER |
Rectangle.RIGHT_BORDER | Rectangle.BOTTOM_BORDER |
Rectangle.TOP_BORDER;
Tableheader7.Right = 25f;
nested1.AddCell(Tableheader7);
pdfDoc.Add(table);
pdfWriter.CloseStream = false;
pdfDoc.Close();

how to change code so it works with itext 7

The following code works nicely with iText 5:
LEVEL = PdfAConformanceLevel.PDF_A_3A
writer = PdfAWriter.getInstance(document, baos, LEVEL)
What is the correct syntax for iText 7?
Where can I find examples and documentation about iText 7?
why so negative? Seems like you want to put extra pressure on me to write documentation. That's kind of frustrating. I think that encouragement works better. Nevertheless, your claim that there's no documentation is wrong.
See Chapter 7: Creating PDF/UA and PDF/A documents of the iText 7: Jump-Start Tutorial
You overlooked the UnitedStates_PDFA_3a example in which we have a CSV file that we convert to a PDF/A-3 document adding that CSV file as attachment.
public void createPdf(String dest) throws IOException, XMPException {
PdfADocument pdf = new PdfADocument(new PdfWriter(dest),
PdfAConformanceLevel.PDF_A_3A,
new PdfOutputIntent("Custom", "", "http://www.color.org",
"sRGB IEC61966-2.1", new FileInputStream(INTENT)));
Document document = new Document(pdf, PageSize.A4.rotate());
document.setMargins(20, 20, 20, 20);
//Setting some required parameters
pdf.setTagged();
pdf.getCatalog().setLang(new PdfString("en-US"));
pdf.getCatalog().setViewerPreferences(
new PdfViewerPreferences().setDisplayDocTitle(true));
PdfDocumentInfo info = pdf.getDocumentInfo();
info.setTitle("iText7 PDF/A-3 example");
//Add attachment
PdfDictionary parameters = new PdfDictionary();
parameters.put(PdfName.ModDate, new PdfDate().getPdfObject());
PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec(
pdf, Files.readAllBytes(Paths.get(DATA)), "united_states.csv",
"united_states.csv", new PdfName("text/csv"), parameters,
PdfName.Data, false);
fileSpec.put(new PdfName("AFRelationship"), new PdfName("Data"));
pdf.addFileAttachment("united_states.csv", fileSpec);
PdfArray array = new PdfArray();
array.add(fileSpec.getPdfObject().getIndirectReference());
pdf.getCatalog().put(new PdfName("AF"), array);
//Embed fonts
PdfFont font = PdfFontFactory.createFont(FONT, true);
PdfFont bold = PdfFontFactory.createFont(BOLD_FONT, true);
// Create content
Table table = new Table(new float[]{4, 1, 3, 4, 3, 3, 3, 3, 1});
table.setWidthPercent(100);
BufferedReader br = new BufferedReader(new FileReader(DATA));
String line = br.readLine();
process(table, line, bold, true);
while ((line = br.readLine()) != null) {
process(table, line, font, false);
}
br.close();
document.add(table);
//Close document
document.close();
}
public void process(Table table, String line, PdfFont font, boolean isHeader) {
StringTokenizer tokenizer = new StringTokenizer(line, ";");
while (tokenizer.hasMoreTokens()) {
if (isHeader) {
table.addHeaderCell(new Cell().setHorizontalAlignment(HorizontalAlignment.CENTER).add(new Paragraph(tokenizer.nextToken()).setHorizontalAlignment(HorizontalAlignment.CENTER).setFont(font)));
} else {
table.addCell(new Cell().setHorizontalAlignment(HorizontalAlignment.CENTER).add(new Paragraph(tokenizer.nextToken()).setHorizontalAlignment(HorizontalAlignment.CENTER).setFont(font)));
}
}
}

Adding page numbers to pdf using iTextSharp on second pass is not working

So as the title says I am trying to add page numbers to an existing pdf document, I have researched this problem and come up with a few tutorials/examples.
Here is the simplest example i can find
My code Compiles and runs successfully however the changes are not reflected in the pdf
my code
byte[] bytes = File.ReadAllBytes(filePath + ".pdf");
PdfReader pdfReader = new PdfReader(bytes);
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(pdfReader, ms,'\0',true))
{
int n = pdfReader.NumberOfPages;
for (int i = 1; i <= n; i++)
{
creatPageCountFooter(i + 1, n).WriteSelectedRows(0, -1, 34, 803, stamper.GetOverContent(i));
//ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
}
ms.ToArray();
}
pdfReader.Close();
}
File.WriteAllBytes(filePath + ".pdf", bytes);
the function "creatPageCountFooter"
/**
* Create a header table with page X of Y
* #param count the page number
* #param Total the total number of pages
* #return a table that can be used as header
*/
protected PdfPTable creatPageCountFooter(int count,int Total)
{
PdfPTable pageCount = new PdfPTable(3);
pageCount.TotalWidth=250;
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 6);
PdfPCell Cell = new PdfPCell(new Phrase(DateTime.Now.ToString("dd MMM yyyy"), times));
Cell.HorizontalAlignment = Element.ALIGN_RIGHT;
Cell.Border = 0;
pageCount.AddCell(Cell);
Cell = new PdfPCell(new Phrase(count.ToString() +" / "+ Total.ToString(), times));
Cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
Cell.Border = 0;
pageCount.AddCell(Cell);
Cell = new PdfPCell(new Phrase("Company name " + DateTime.Now.ToString("yyyy"), times));
Cell.HorizontalAlignment = Element.ALIGN_MIDDLE;
Cell.Border = 0;
pageCount.AddCell(Cell);
return pageCount;
}
As a further note I have checked that this code actually runs and I have tried writing the file over the existing document or creating a new document and both times the changes don't reflect.
I will provide further details if required.
Your code writes the byte[] bytes to a file:
File.WriteAllBytes(filePath + ".pdf", bytes);
But the only code in which that variable is set, is the initial
byte[] bytes = File.ReadAllBytes(filePath + ".pdf");
Thus, it is not surprising that the changes are not reflected in the pdf result file because you simply write the original bytes unchanged.
I assume you meant to set bytes to the contents of the memory stream in this line
ms.ToArray();
but forgot the bytes =. Unfortunately, though, that array retrieval call happens too early, it is still inside the using PdfStamper block:
using (PdfStamper stamper = new PdfStamper(pdfReader, ms,'\0',true))
{
...
ms.ToArray();
}
Just like in the sample you refer to, the array has to be retrieved outside that using block, so that it is retrieved after the PdfStamper implicitly gets closed during disposal. Thus:
using (PdfStamper stamper = new PdfStamper(pdfReader, ms,'\0',true))
{
...
}
bytes = ms.ToArray();

Insert merge field in word using open xml

I want to insert a merge field to an existing word doc. Am able to create a xml element of merge field but am not sure on how to append that to the document. Below is my code
Microsoft.Office.Interop.Word.Document wrdDoc = Globals.ThisAddIn.Application.ActiveDocument;
From above I will get the active document
string instructionText = String.Format(" MERGEFIELD {0} \\* MERGEFORMAT", cmbType.Text + "__" + cmbField.Text);
SimpleField simpleField1 = new SimpleField() { Instruction = instructionText };
DocumentFormat.OpenXml.Wordprocessing.Run run1 = new DocumentFormat.OpenXml.Wordprocessing.Run();
RunProperties runProperties1 = new RunProperties();
NoProof noProof1 = new NoProof();
runProperties1.Append(noProof1);
Text text1 = new Text();
text1.Text = String.Format("«{0}»", cmbType.Text + "__" + cmbField.Text);
run1.Append(runProperties1);
run1.Append(text1);
simpleField1.Append(run1);
DocumentFormat.OpenXml.Wordprocessing.Paragraph paragraph = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
paragraph.Append(new OpenXmlElement[] { simpleField1 });
Here am creating a paragraph. Now how can i append this paragraph element to the wrdDoc
Word.MailMerge wrdMailMerge;
Word.Selection wrdSelection;
Word.MailMergeFields wrdMergeFields;
Microsoft.Office.Interop.Word.Document wrdDoc = Globals.ThisAddIn.Application.ActiveDocument;
Microsoft.Office.Interop.Word.Application wrdApp = Globals.ThisAddIn.Application;
wrdSelection = wrdApp.Selection;
wrdMailMerge = wrdDoc.MailMerge;
wrdMergeFields = wrdMailMerge.Fields;
wrdSelection.ParagraphFormat.Alignment =
Word.WdParagraphAlignment.wdAlignParagraphJustify;
wrdMergeFields.Add(wrdSelection.Range, fieldname);

GXT3/GWT - How to remove cells(HasCell) from compositeCell dynamically?

I have a list of cells which i'll send it as an argument to create a composite cell. In the below code i'm creating the cells and adding it to the list.
List<HasCell<SensorTreeModel, ?>> cells= new ArrayList<HasCell<SensorTreeModel,?>>();
com.sencha.project.client.ImageCell editSwitchIcon = new com.sencha.project.client.ImageCell(ButtonType.EDITSWITCH);
com.sencha.project.client.ImageCell editIcon = new com.sencha.project.client.ImageCell(ButtonType.EDIT);
com.sencha.project.client.ImageCell switchRoleIcon = new com.sencha.project.client.ImageCell(ButtonType.SWITCH);
com.sencha.project.client.ImageCell breakIcon = new com.sencha.project.client.ImageCell(ButtonType.BREAK);
com.sencha.project.client.ImageCell deleteIcon = new com.sencha.project.client.ImageCell(ButtonType.DELETE);
cells.add(editSwitchIcon);
cells.add(editIcon);
cells.add(switchRoleIcon);
cells.add(breakIcon);
cells.add(deleteIcon);
In the below code. I'm adding the list to the composite cell.(ActionCell)
actionsCol.setCell(new ActionCell(cells));
Now what if I want to delete the cells dynamically in the columnConfig(actionCell) depending on the object which is going into the column?