How to use DrawTable using dbAutoTrack - c#

I am using dbAutoTrack for generate PDF in that I have to add Table but I get the Exception like object references is not set to an instance of object
Below is my code
Table t=new Table();
Row row=new Row(t);
row.Cells.Add("ABC");
t.Rows.Add(row);
_pdfGraphics.DrawTable(100,200, t);
where _pdfGraphics is object of PDFGraphics.
Thanks in Advances

Most probably you have message like: "Not enough columns in this row to have a column span of 1." And you needed just to add them.
Workable sample:
//Initialize a new PDF Document
Document _document = new Document();
_document.Title = "Аpitron Sample";
_document.Author = "StanlyF";
_document.Creator = "АPItron LTD.";
Page page = null;
PDFGraphics graphics = null;
Table _table = new Table();
_table.Columns.Add(30);
Row row = new Row(_table);
row.Height = 25;
row.Cells.Add("ABC");
_table.Rows.Add(row);
while (_table != null)
{
//Initialize new page with default PageSize A4
page = new Page(PageSize.A4);
//Add page to document
_document.Pages.Add(page);
//Get the PDFGraphics object for drawing to the page.
graphics = page.Graphics;
_table = graphics.DrawTable(100,200, _table);
}
using(FileStream _fs = new FileStream("Table_Sample.pdf", System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
//Generate PDF to the stream
_document.Generate(_fs);
Process.Start("Table_Sample.pdf");
}

The accepted answer didn't work for me and I was still getting null reference exceptions. It turns out that the rows need to be added with a TableStyle otherwise the cells, for whatever reason, won't be generated:
StandardFonts standardfont = StandardFonts.Helvetica;
PDFFont fontSmall_reg = new PDFFont(standardfont, FontStyle.Regular);
TableStyle ts = new TableStyle(fontSmall_reg, 12, nullBorder);
Table data = new Table(ts);
data.Columns.Add(100);
data.Columns.Add(100);
Row r = new Row(data, ts);
r.Height = 25;
r.Cells.Add("Row 1 Col1");
data.add(r)
r.Cells.Add("Row 1 Col2");

Related

System.NullReferenceException: 'Object reference not set to an instance of an object.' page was null

I'm traying to test this method that checks for specific tag footer to show.
here is the function:
public void CheckForOrderOfFooterTags(PdfDocument pdfDoc, out TagTreePointer autoP, out TagTreePointer posBackup, out TagTreeInfo p)
{
// the following logic takes care of changing the order of the footer tags
// so that footers appear in the proper order in the tag tree structure
autoP =pdfDoc.GetTagStructureContext().GetAutoTaggingPointer();
posBackup =new TagTreePointer(autoP);
PagesTag.TryGetValue(PageNumber, out p);
if (p?.TagTreePointer != null)
{
// Make sure that content that will be tagged is placed in tag structure specifically where we need it.
int indexInParentKidsList = p.TagTreePointer.GetIndexInParentKidsList();
autoP.MoveToPointer(p.TagTreePointer).MoveToParent();
// setting new index only works on taggable items
if (p.Tagged)
{
autoP.SetNextNewKidIndex(indexInParentKidsList + 1);
}
}
}
and this is the test:
public void Check_For_Specific_Tag_Footer_To_Show_Successfully_Called()
{
var ir = new TestIllustrationReport().Report;
var reportProperties = new TestDocument().ReportProperties;
var sec = new FooterSection(new ProductContent(ir));
sec.Build();
Dictionary<int, TagTreeInfo> dict = new Dictionary<int, TagTreeInfo>();
reportProperties.FooterTag=dict;
FooterEventHandler footerEvent = new FooterEventHandler(sec, reportProperties);
// Must have write permissions to the path folder
var path = System.IO.Path.GetTempPath();
var fileName = System.IO.Path.ChangeExtension("Test", ".pdf");
var com = System.IO.Path.Combine(path, fileName);
// pdf
PdfWriter writer = new PdfWriter(com);
PdfDocument pdf = new PdfDocument(writer);
Event headerEvent= new PdfDocumentEvent("pdf", pdf);
PdfDocumentEvent docEvent = (PdfDocumentEvent)headerEvent;
iText.Layout.Document document = new iText.Layout.Document(pdf);
pdf.GetCatalog().SetLang(new PdfString("en-US"));
// Header and Paragraph
Paragraph header = new Paragraph(TestData.Header)
.SetTextAlignment(TextAlignment.CENTER)
.SetFontSize(20);
document.Add(header);
pdf.SetTagged();
pdf.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
PdfDocumentInfo info = pdf.GetDocumentInfo();
info.SetTitle("Testing tags");
Paragraph p = new Paragraph();
p.Add("The quick brown ");
PdfPage page = docEvent.GetPage();
// Rectangle and canvas
Rectangle rectangle = new Rectangle(
pdf.GetDefaultPageSize().GetX() + document.GetLeftMargin(),
pdf.GetDefaultPageSize().GetTop() - 80,
page.GetPageSize().GetWidth() - document.GetLeftMargin() - document.GetRightMargin(),
50);
Div canvas = new Div().SetFixedPosition(pdf.GetPageNumber(page), rectangle.GetLeft(), rectangle.GetBottom(), rectangle.GetWidth());
Paragraph pFooter = new Paragraph(TestData.Paragraph);
pFooter.GetAccessibilityProperties().SetRole("H");
canvas.Add(pFooter);
document.Add(canvas);
// Don't close document itself! It would close the PdfDocument!
document.GetRenderer().Close();
var pPointer = new TagTreePointer(pdf);
var pInfo = new TagTreeInfo { Tagged = true };
// Act
footerEvent.CheckForOrderOfFooterTags(pdf, out pPointer, out pPointer, out pInfo);
document.Close();
// Assert
Assert.NotNull(pdf);
}
For this test, I needed to create the pdf, set and build the footer section. I added the header to the pdf and the "footer test", I also set the document to be tagged.
I don't know why I always get the page is null if I'm adding a header and a paragraph to it.

How to add a first page footer different from default one in NPOI - Docx?

I'm generating a docx file using NPOI 2.5.2 and I stuck with headers/footers for first page.
I'd like to have a first page custom footer and start numbering pages from the second one.
Here is my code for the first page footer:
// First page
doc.Document.body.sectPr = new CT_SectPr();
var footer = new CT_Ftr();
var footerParagraph = footer.AddNewP();
footerParagraph.AddNewR().AddNewT().Value = $"FIRST PAGE CUSTOM FOOTER";
var footerPar = new XWPFParagraph(footerParagraph, doc);
var parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerPar;
var headerFooterPolicy = doc.GetHeaderFooterPolicy();
if (headerFooterPolicy == null)
headerFooterPolicy = doc.CreateHeaderFooterPolicy();
headerFooterPolicy.CreateFooter(XWPFHeaderFooterPolicy.FIRST, parsFooter);
Here is my code for the default footer with page numbering:
// Other pages
footerParagraph = footer.AddNewP();
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
footerParagraph.AddNewR().AddNewInstrText().Value = " PAGE ";
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
footerParagraph.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;
footerPar = new XWPFParagraph(footerParagraph, doc);
parsFooter = new XWPFParagraph[1];
parsFooter[0] = footerPar;
headerFooterPolicy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
With the code above I could not see the first page custom footer but the page numbering in every page. What am I doing wrong?
I found this similar question but I could not find addNewTitlePg method in NPOI.
And is there any appropriate documentation with examples about NPOI?
Here is my code with the first footer working. (using NPOI 2.5.3)
var doc = new XWPFDocument();
using (var sw = File.Create("fileformat.docx"))
{
XWPFParagraph p1 = doc.CreateParagraph();
XWPFParagraph p2 = doc.CreateParagraph();
XWPFRun r1 = p1.CreateRun();
XWPFRun r2 = p2.CreateRun();
r1.SetText("The quick brown fox");
r1.AddBreak(BreakType.PAGE);
r2.SetText("Next page: The quick brown fox");
doc.Document.body.sectPr = new CT_SectPr();
var policy = doc.CreateHeaderFooterPolicy();
var ctSectPr = doc.Document.body.sectPr;
if (ctSectPr.titlePg == null)
{
ctSectPr.titlePg = new CT_OnOff() { val = true };
}
var firstFooter = policy.CreateFooter(ST_HdrFtr.first);
var paragraph = firstFooter.CreateParagraph();
var run = paragraph.CreateRun();
run.SetText("First page footer...");
var defaultFooter = policy.CreateFooter(ST_HdrFtr.#default);
paragraph = defaultFooter.CreateParagraph();
run = paragraph.CreateRun();
paragraph.Alignment = ParagraphAlignment.RIGHT;
paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin;
paragraph.GetCTP().AddNewR().AddNewInstrText().Value = " PAGE ";
paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate;
paragraph.GetCTP().AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end;
run = paragraph.CreateRun();
doc.Write(sw);
}

How to replace text with Image / Table in OpenXML (C# .Net core 3.1)

I am just using OpenXML for manipulating docx document. I have templates that has notation like
{{userFirstName}}
{{tableOfUsers}}
{{signatureImage}} etc
I need to replace those text with table, image etc. How to do that in Openxml ?
I'm using C# .Net core 3.1 with OpenXML 2.5
My code was like this, but the table still not inserted into the right place:
var fileName = Path.Combine(_hostingEnv.WebRootPath, "test/test.docx");
var fileNameResult = Path.Combine(_hostingEnv.WebRootPath, "test/result.docx");
byte[] byteArray = System.IO.File.ReadAllBytes(fileName);
using (MemoryStream stream = new MemoryStream())
{
stream.Write(byteArray, 0, (int)byteArray.Length);
using (WordprocessingDocument wd = WordprocessingDocument.Open(stream, true))
{
Table table = new Table();
// Create a TableProperties object and specify its border information.
TableProperties tblProp = new TableProperties(
new TableBorders(
new TopBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
},
new BottomBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
},
new LeftBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
},
new RightBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
},
new InsideHorizontalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
},
new InsideVerticalBorder()
{
Val =
new EnumValue<BorderValues>(BorderValues.Dashed),
Size = 24
}
)
);
// Append the TableProperties object to the empty table.
table.AppendChild<TableProperties>(tblProp);
// Create a row.
TableRow tr = new TableRow();
// Create a cell.
TableCell tc1 = new TableCell();
// Specify the width property of the table cell.
tc1.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
// Specify the table cell content.
tc1.Append(new Paragraph(new Run(new Text("some text"))));
// Append the table cell to the table row.
tr.Append(tc1);
// Create a second table cell by copying the OuterXml value of the first table cell.
TableCell tc2 = new TableCell(tc1.OuterXml);
// Append the table cell to the table row.
tr.Append(tc2);
// Append the table row to the table.
table.Append(tr);
var tblStr = table.ToString();
//wd.MainDocumentPart.Document.Body.Append(table);
Text tablePl = wd.MainDocumentPart.Document.Body.Descendants<Text>().Where(x => x.Text == "{{tableOfUsers}}").First();
if(tablePl != null)
{
var parent = tablePl.Parent;
tablePl.Parent.InsertAfter<Table>(table, tablePl);
tablePl.Remove();
}
string docText = null;
using (StreamReader sr = new StreamReader(wd.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regexText = new Regex("{{name}}");
docText = regexText.Replace(docText, "Claire " + DateTime.Now.ToString());
// Regex regextable = new Regex("{{tableOfUsers}}");
// docText = regextable.Replace(docText, tblStr);
using (StreamWriter sw = new StreamWriter(wd.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}
await System.IO.File.WriteAllBytesAsync(fileNameResult, stream.ToArray());
}
_fileRepo.SetPath("test");
string fileName2 = "result.docx";
var data = await _fileRepo.DownloadFile(fileName2);
return File(data, "application/octet-stram", "filename.docx");
}
My docx file looks like
Hello {{name}}
Here is the list of users : {{tableOfUsers}}
Sincerely,
{{signatureImage}}
Use the code below to insert a table:
............
string tablePlaceholder = "{{tableOfUsers}}";
Text tablePl = wd.MainDocumentPart.Document.Body
.Descendants<Text>()
.Where(x => x.Text.Contains(tablePlaceholder))
.First();
if (tablePl != null)
{
//Insert the table after the paragraph.
var parent = tablePl.Parent.Parent.Parent;
parent.InsertAfter(table, tablePl.Parent.Parent);
tablePl.Text = tablePl.Text.Replace(tablePlaceholder, "");
wd.MainDocumentPart.Document.Save();
}
string docText = wd.MainDocumentPart.Document.Body.OuterXml;
Regex regexText = new Regex("{{name}}");
docText = regexText.Replace(docText, "Claire " + DateTime.Now.ToString());
wd.MainDocumentPart.Document.Body = new Body(docText);
wd.MainDocumentPart.Document.Save();
.............
You are looking for {{table}} in your code, but the notation in your template is {{tableOfUsers}}.

change table style text in openxml for powerpoint generatron

I have this code using openxml sdk which generates table in PPT report.
This line is the reason for table style.
tableStyleId.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";
Style is :
I need to change the style and colors but I couldn't find anything for that. Please help.
private D.Table GenerateTable(int projectID, string reportType)
{
// Declare and instantiate table
D.Table table = new D.Table();
// Specify the required table properties for the table
D.TableProperties tableProperties = new D.TableProperties() { FirstRow = true, BandRow = false };
D.TableStyleId tableStyleId = new D.TableStyleId();
tableStyleId.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";
tableProperties.Append(tableStyleId);
D.TableGrid tableGrid1 = new D.TableGrid();
System.Data.DataTable dtData = new System.Data.DataTable();
if (reportType == "projcharter")
{
//tblXBenefit
dtData = GetBenefit(projectID);
// Declare and instantiate tablegrid and colums
//D.TableGrid tableGrid1 = new D.TableGrid();
D.GridColumn gridColumn1 = new D.GridColumn() { Width = 1848000L };
D.GridColumn gridColumn2 = new D.GridColumn() { Width = 648000L };
D.GridColumn gridColumn3 = new D.GridColumn() { Width = 648000L };
D.GridColumn gridColumn4 = new D.GridColumn() { Width = 648000L };
tableGrid1.Append(gridColumn1);
tableGrid1.Append(gridColumn2);
tableGrid1.Append(gridColumn3);
tableGrid1.Append(gridColumn4);
}
table.Append(tableProperties);
table.Append(tableGrid1);
// Instantiate the table header row
D.TableRow tableHRow = new D.TableRow() { Height = 0L };
for (int column = 0; column < dtData.Columns.Count; column++)
{
tableHRow.Append(CreateTextCell(dtData.Columns[column].ToString()));
}
table.Append(tableHRow);
// Instantiate the table data row
for (int row = 0; row < dtData.Rows.Count; row++)
{
// Instantiate the table row
D.TableRow tableRow = new D.TableRow() { Height = 0L };
for (int column = 0; column < dtData.Columns.Count; column++)
{
tableRow.Append(CreateTextCell(dtData.Rows[row][column].ToString()));
}
table.Append(tableRow);
}
return table;
}
Links to previous questions:
create dynamic table in powerpoint using openXML with c# and ASP.net
unable to generate second table in PPT report using openxml
How to ignore/solve "Repair" message from Powerpoint report generated by OpenXML with ASP.net
Solved it.
So what I did was, I created a new ppt > inserted tables with required design manually in it and then saved it as ppt xml (you will see this option in save as).
Open that xml search for tablestyleid and use that value in the code.

Open XML Table Generation Using Generic List

I have written a method to insert a table in a word document using Open XML. The method accepts a generic list and a few parameters to control number of columns, column headings etc.
That all works fine.
However when populating the cells in the table I want to pull out the values for each row and place them in their corresponding columns. Given the names of the properties are going to change depending on the contents of the generic list, I am not sure how to accomplish this.
Anyone that can point me in the right direction it would be appreciated.
void InsertTable<T>(List<T> tableData, int[] tableHeadingCount, string[] columnHeadings, string locationInDocument)
{
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(_newDocument, true))
{
var docPart = myDoc.MainDocumentPart;
var doc = docPart.Document;
var table = new Table();
var tableBorderTop = new TopBorder();
var tableBorderBottom = new BottomBorder();
var tableBorderLeft = new LeftBorder();
var tableBorderRight = new RightBorder();
var tableBorderHorizontal = new InsideHorizontalBorder();
var tableBorderVertical = new InsideVerticalBorder();
var tableProperties = new TableProperties();
var borders = new TableBorders();
// Set Border Styles for Table
tableBorderTop.Val = BorderValues.Single;
tableBorderTop.Size = 6;
tableBorderBottom.Val = BorderValues.Single;
tableBorderBottom.Size = 6;
tableBorderLeft.Val = BorderValues.Single;
tableBorderLeft.Size = 6;
tableBorderRight.Val = BorderValues.Single;
tableBorderRight.Size = 6;
tableBorderHorizontal.Val = BorderValues.Single;
tableBorderHorizontal.Size = 6;
tableBorderVertical.Val = BorderValues.Single;
tableBorderVertical.Size = 6;
// Assign Border Styles to Table Borders
borders.TopBorder = tableBorderTop;
borders.BottomBorder = tableBorderBottom;
borders.LeftBorder = tableBorderLeft;
borders.RightBorder = tableBorderRight;
borders.InsideHorizontalBorder = tableBorderHorizontal;
borders.InsideVerticalBorder = tableBorderVertical;
// Append Border Styles to Table Properties
tableProperties.Append(borders);
// Assign Table Properties to Table
table.Append(tableProperties);
var tableRowHeader = new TableRow();
tableRowHeader.Append(new TableRowHeight() { Val = 2000 });
for (int i = 0; i < tableHeadingCount.Length; i++)
{
var tableCellHeader = new TableCell();
//Assign Font Properties to Run
var runPropHeader = new RunProperties();
runPropHeader.Append(new Bold());
runPropHeader.Append(new Color() { Val = "000000" });
//Create New Run
var runHeader = new Run();
//Assign Font Properties to Run
runHeader.Append(runPropHeader);
var columnHeader = new Text();
//Assign the Pay Rule Name to the Run
columnHeader = new Text(columnHeadings[i]);
runHeader.Append(columnHeader);
//Create Properties for Paragraph
var justificationHeader = new Justification();
justificationHeader.Val = JustificationValues.Left;
var paraPropsHeader = new ParagraphProperties(justificationHeader);
SpacingBetweenLines spacing = new SpacingBetweenLines() { Line = "240", LineRule = LineSpacingRuleValues.Auto, Before = "0", After = "0" };
paraPropsHeader.Append(spacing);
var paragraphHeader = new Paragraph();
paragraphHeader.Append(paraPropsHeader);
paragraphHeader.Append(runHeader);
tableCellHeader.Append(paragraphHeader);
var tableCellPropertiesHeader = new TableCellProperties();
var tableCellWidthHeader = new TableCellWidth();
tableCellPropertiesHeader.Append(new Shading() { Val = ShadingPatternValues.Clear, Color = "auto", Fill = "#C0C0C0" });
var textDirectionHeader = new TextDirection();
textDirectionHeader.Val = TextDirectionValues.BottomToTopLeftToRight;
tableCellPropertiesHeader.Append(textDirectionHeader);
tableCellWidthHeader.Type = TableWidthUnitValues.Dxa;
tableCellWidthHeader.Width = "2000";
tableCellPropertiesHeader.Append(tableCellWidthHeader);
tableCellHeader.Append(tableCellPropertiesHeader);
tableRowHeader.Append(tableCellHeader);
}
tableRowHeader.AppendChild(new TableHeader());
table.Append(tableRowHeader);
//Create New Row in Table for Each Record
foreach (var record in tableData)
{
var tableRow = new TableRow();
for (int i = 0; i < tableHeadingCount.Length; i++)
{
//**** This is where I dynamically want to iterate through selected properties and output the value ****
var propertyText = "Test";
var tableCell = new TableCell();
//Assign Font Properties to Run
var runProp = new RunProperties();
runProp.Append(new Bold());
runProp.Append(new Color() { Val = "000000" });
//Create New Run
var run = new Run();
//Assign Font Properties to Run
run.Append(runProp);
//Assign the text to the Run
var text = new Text(propertyText);
run.Append(text);
//Create Properties for Paragraph
var justification = new Justification();
justification.Val = JustificationValues.Left;
var paraProps = new ParagraphProperties(justification);
var paragraph = new Paragraph();
paragraph.Append(paraProps);
paragraph.Append(run);
tableCell.Append(paragraph);
var tableCellProperties = new TableCellProperties();
var tableCellWidth = new TableCellWidth();
tableCellWidth.Type = TableWidthUnitValues.Dxa;
tableCellWidth.Width = "2000";
tableCellProperties.Append(tableCellWidth);
tableCell.Append(tableCellProperties);
tableRow.Append(tableCell);
}
table.Append(tableRow);
}
var res = from bm in docPart.Document.Body.Descendants<BookmarkStart>()
where bm.Name == locationInDocument
select bm;
var bookmark = res.SingleOrDefault();
var parent = bookmark.Parent; // bookmark's parent element
Paragraph newParagraph = new Paragraph();
parent.InsertAfterSelf(newParagraph);
if (bookmark != null)
{
newParagraph.InsertBeforeSelf(table);
}
}
}
I resolved this issue by tackling the problem another way. Essentially rather than passing a List to the Insert Table Method. I decided to Pass a Multi-Dimensional Array with all the data need for the table including the table headings. This essentially meant that the Insert Table method would be more generic and any customization i.e. Generating Data, Specifying Column Headings etc would be done in the Method calling Insert Table.

Categories

Resources