Create indents and numeration in Table of Content(itextsharp) - c#

I have a question about TOC. How can I create TOC With indents and numeration?
Now I have TOC without it(just list). I create it using Chunk and Paragraph. What should I use for creating TOC with it? Maybe should I use List and add to document or not?
Here I'm creating TOC:
private int CreateTOC(XmlNode xmlNode, Document doc, PdfWriter writer, int number)
{
var toc = ev.GetTOC();
KeyValuePair<string, int> value;
Chunk dottedLine = new Chunk(new iTextSharp.text.pdf.draw.DottedLineSeparator());
for (int i = 0; i < xmlNode.ChildNodes.Count; i++)
{
var text = xmlNode.ChildNodes[i].Attributes["text"].Value;
value = toc[text];
var dest = value.Key;
var page = value.Value;
var c = new Chunk((i+1).ToString()+ ". " + text, font);
c.SetAction(PdfAction.GotoLocalPage(dest, false));
var p = new Paragraph(c);
p.Add(dottedLine);
c = new Chunk(page.ToString(), font);
c.SetAction(PdfAction.GotoLocalPage(dest, false));
p.Add(c);
doc.Add(p);
CreateTOC(xmlNode.ChildNodes[i], doc, writer, i+1);
}
return writer.PageNumber;
}
And I get list with reference to chapters in content.
But I need the following:
1. chapter1-------------------1page
1.1 subchupter1-------------2page
1.2 subchupter2-------------2page
1.3 subchupter3-------------3page
2. chupter2-------------------4page
2.1 subchupter4-------------4page
2.3 subchupter4-------------4page
2.3.1 subsubchupter------5page
...
...
...
How can I fix it?
Thank you!

Introduce a level and multiply that level with an indentation value. Use that value as the value for IndentationLeft:
private int CreateTOC(XmlNode xmlNode, Document doc, PdfWriter writer, int number, int level) {
var toc = ev.GetTOC();
KeyValuePair<string, int> value;
Chunk dottedLine = new Chunk(new iTextSharp.text.pdf.draw.DottedLineSeparator());
for (int i = 0; i < xmlNode.ChildNodes.Count; i++)
{
var text = xmlNode.ChildNodes[i].Attributes["text"].Value;
value = toc[text];
var dest = value.Key;
var page = value.Value;
var c = new Chunk((i+1).ToString()+ ". " + text, font);
c.SetAction(PdfAction.GotoLocalPage(dest, false));
var p = new Paragraph(c);
p.IndentationLeft = 10 * level;
p.Add(dottedLine);
c = new Chunk(page.ToString(), font);
c.SetAction(PdfAction.GotoLocalPage(dest, false));
p.Add(c);
doc.Add(p);
CreateTOC(xmlNode.ChildNodes[i], doc, writer, i+1, level + 1);
}
return writer.PageNumber;
}
Use 0 for the level when you first call CreateToc().

Related

Create borderless table in iText 7

I am porting an application from iTextSharp 5 to iText 7. In the original application I added page numbers using a stamper that added a table to the top of the page. I have almost succeeded but I am unable to create a table with only a bottom border (internal, left, right and top is borderless. I tried the example from the iText building blocks book and have looked at itext 7 table borderless
But I still get top, internal and side borders. Here is my code:
private Table makepagetable(string doctitle, int curpage, int totalpage)
{
PdfFont font = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.TIMES_ROMAN);
Text title = new Text(doctitle).SetFontSize(11f).SetFont(font);
Text pages = new Text("page " + curpage.ToString() + " of " + totalpage.ToString()).SetFont(font);
Paragraph p1 = new Paragraph(title).SetTextAlignment(TextAlignment.LEFT);
Paragraph p2 = new Paragraph(pages).SetTextAlignment(TextAlignment.RIGHT);
Table mytable = new Table(new float[] { 3, 1 })
.SetBorderTop(Border.NO_BORDER)
.SetBorderRight(Border.NO_BORDER)
.SetBorderLeft(Border.NO_BORDER)
.SetBorderBottom(new SolidBorder(2));
float pval = 500;
mytable.SetWidth(pval);
mytable.AddCell(new Cell().Add(p1)).SetBorder(Border.NO_BORDER);
mytable.AddCell(new Cell().Add(p2)).SetBorder(Border.NO_BORDER);
// SetBorderTop(Border.NO_BORDER).SetBorderLeft(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER);
return mytable;
}
I have also tried mytable.AddCell(new Cell().Add(p2)).SetBorderTop(Border.NO_BORDER).SetBorderLeft(Border.NO_BORDER).SetBorderRight(Border.NO_BORDER); and various similar combinations on the table object, but I still get the borders.
For completion here is the routine that calls this function
public Byte[] AddPageNumbers(Byte[] firstpass, string doctitle) {
//firstpass = array of the original pdfdocument
// doctitle = text on left of table
int i;
float x = 30;
float y = 810;
MemoryStream ms2 = new MemoryStream(firstpass);
PdfReader reader = new PdfReader(ms2);
MemoryStream ms3 = new MemoryStream();
PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(ms3));
Document document = new Document(pdfDoc, PageSize.A4, false);
int n = pdfDoc.GetNumberOfPages();
for (i = 3; i <= n; i++)
{
Paragraph header;
//PdfPage page = pdfDoc.GetPage(i);
//header = new Paragraph(String.Format(doctitle + " Page {0} of {1}", i, n));
Table table;
PdfPage page = pdfDoc.GetPage(i);
table = makepagetable(doctitle, i, n);
header = new Paragraph().Add(table);
document.ShowTextAligned(header,x,y, i, TextAlignment.JUSTIFIED, VerticalAlignment.MIDDLE, 0);
}
document.Close();
pdfDoc.Close();
return ms3.ToArray();
}
Borderless Table or Cell, for c# and iText7, you can use:
//Table with no border
table.SetBorder(Border.NO_BORDER);
//Cell with no border
cell.SetBorder(Border.NO_BORDER);
I review your code:
//your source code
mytable.AddCell(new Cell().Add(p1)).SetBorder(Border.NO_BORDER);
//changed code
mytable.AddCell(new Cell().Add(p1).SetBorder(Border.NO_BORDER));
Hope this is helpful.

Splitting word document into separate pages using c#

An hour ago I been searching for a code that split word document into separate pages I found this question
Using the code in the thread
static class PagesExtension {
public static IEnumerable<Range> Pages(this Document doc) {
int pageCount = doc.Range().Information[WdInformation.wdNumberOfPagesInDocument];
int pageStart = 0;
for (int currentPageIndex = 1; currentPageIndex <= pageCount; currentPageIndex++) {
var page = doc.Range(
pageStart
);
if (currentPageIndex < pageCount) {
//page.GoTo returns a new Range object, leaving the page object unaffected
page.End = page.GoTo(
What: WdGoToItem.wdGoToPage,
Which: WdGoToDirection.wdGoToAbsolute,
Count: currentPageIndex+1
).Start-1;
} else {
page.End = doc.Range().End;
}
pageStart = page.End + 1;
yield return page;
}
yield break;
}
}
I call the code above using this code
var app = new Microsoft.Office.Interop.Word.Application();
object missObj = System.Reflection.Missing.Value;
app.Visible = false;
var doc = app.Documents.Open(fileLocation);
int pageNumber = 1;
foreach (var page in doc.Pages())
{
Microsoft.Office.Interop.Word.Document newDoc = app.Documents.Add(ref missObj, ref missObj, ref missObj, ref missObj);
page.Copy();
var doc2 = app.Documents.Add();
doc2.Range().Paste();
object newDocName = pageNumber.ToString() + ".docx";
Console.WriteLine(newDocName);
doc2.SaveAs2(newDocName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLDocument,
CompatibilityMode: Microsoft.Office.Interop.Word.WdCompatibilityMode.wdWord2010);
pageNumber++;
}
app.ActiveDocument.Close();
app.Quit();
But I'm getting an error in a specific document and here is the error
This method or property is not available because no text is selected.
What is the reason for it? i checked the document and found out that the document contains lots of spaces before the next page. How can I solve this?
And using the code above it didn't copy the header and footer. Thank you
Update: Error
This method or property is not available because no text is selected.
at Microsoft.Office.Interop.Word.Range.Copy()
at retrieveObjects(String location) in Document.cs:line 31
and this is the line
page.Copy();

Troubles with reading text file after Build

I'm having trouble reading a text file in Unity3D.
I've created a method which returns a type float[][] and takes a streamreader as argument:
public float[][] CreateWeights(StreamReader reader){
int n = 0;
float[][] Weights = new float[50][];
while((!reader.EndOfStream)){
string text = reader.ReadLine();
if (text == null)
break;
string[] strFloats = text.Split (new char[0]);
float[] floats = new float[strFloats.Length];
for(int i = 0; i<strFloats.Length; i++){
floats[i] = float.Parse(strFloats[i]);
}
Weights[n] = floats;
n++;
}
return Weights;
}
I make use of this method in void Start() to create "weights":
float[][] WeightsIH;
float[][] WeightsHO;
void Start(){
FileInfo theSourceFile = new FileInfo(Application.dataPath + "/Resources/WeightsIH.txt");
StreamReader reader = theSourceFile.OpenText();
FileInfo theSourceFile2 = new FileInfo(Application.dataPath + "/Resources/WeightsHO.txt");
StreamReader reader2 = theSourceFile2.OpenText();
WeightsIH = CreateWeights(reader);
WeightsHO = CreateWeights(reader2);
Yhidden = new float[50][];
HiddenOutput = new float[50][];
Xoutput = new float[1];
}
And this will work fine in Unity's play mode. However, after creating an executable, the files won't be found, which I do understand. So to make it work, I understood that I need to use Resources.Load and I have:
void Start(){
TextAsset text1 = Resources.Load("WeightsIH") as TextAsset;
TextAsset text2 = Resources.Load("WeightsHO") as TextAsset;
WeightsIH = CreateWeights(text1);
WeightsHO = CreateWeights(text2);
Yhidden = new float[50][];
HiddenOutput = new float[50][];
Xoutput = new float[1];
}
Of course the argument type can't be a streamReader anymore, and I changed it to take TextAsset as argument. Here's how it changed:
public float[][] CreateWeights(TextAsset textAsset){
float[][] Weights = new float[50][];
string[] linesFromFile = textAsset.text.Split("\n"[0]);
for(int i = 0; i<linesFromFile.Length; i++){
string[] strFloats = linesFromFile[i].Split (new char[0]);
float[] floats = new float[strFloats.Length];
for(int j = 0; j<strFloats.Length; j++){
floats[j] = float.Parse(strFloats[j]);
}
Weights[i] = floats;
}
return Weights;
}
Now this won't work at all, not even in play mode. The run-time error I would get is as follows:
FormatException: Invalid format.
System.Double.Parse (System.String s, NumberStyles style,
IFormatProvider provider) ( at
/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Double.cs:209)
System.Single.Parse (System.String s) ( at
/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Single.cs:183)
FollowShortestPath.CreateWeights (UnityEngine.TextAsset textAsset)
( at Assets/Scripts/Pathfinding/FollowShortestPath.cs:203)
FollowShortestPath.Start () ( at
Assets/Scripts/Pathfinding/FollowShortestPath.cs:54)
line 54 refers to:
WeightsIH = CreateWeights(text1);
and line 203 refers to:
floats[j] = float.Parse(strFloats[j]);
What am I doing wrong? How can I get the text files to be read successfully in in the executable?
The problem you have is with text file format you are loading.
Couse you have many white spaces
string[] strFloats = text.Split (new char[0]);
will result in that some strings are empty.
To fix this, remove extra withe spaces from text files or use:
for(int j = 0; j<strFloats.Length; j++){
if (string.IsNullOrEmpty (strFloats [j]))
continue;
floats[j] = float.Parse(strFloats[j]);
}

How to Merge items within a List<> collection C#

I have a implememtation where i need to loop through a collection of documents and based on certain condition merge the documents .
The merge condition is very simple, if present document's doctype is same as later document's doctype, then copy all the pages from the later doctype and append it to the pages of present document's and remove the later document from the collection.
Note : Both response.documents and response.documents[].pages are List<> collections.
I was trying this but was getting following exception Once I remove the document.
collection was modified enumeration may not execute
Here is the code:
int docindex = 0;
foreach( var document in response.documents)
{
string presentDoctype = string.Empty;
string laterDoctype = string.Empty;
presentDoctype = response.documents[docindex].doctype;
laterDoctype = response.documents[docindex + 1].doctype;
if (laterDoctype == presentDoctype)
{
response.documents[docindex].pages.AddRange(response.documents[docindex + 1].pages);
response.documents.RemoveAt(docindex + 1);
}
docindex = docindex + 1;
}
Ex:
reponse.documents[0].doctype = "BankStatement" //page count = 1
reponse.documents[1].doctype = "BankStatement" //page count = 2
reponse.documents[2].doctype = "BankStatement" //page count = 2
reponse.documents[3].doctype = "BankStatement" //page count = 1
reponse.documents[4].doctype = "BankStatement" //page count = 4
Expected result:
response.documents[0].doctype = "BankStatement" //page count = 10
Please suggest.Appreciate your help.
I would recommend you to look at LINQ GroupBy and Distinct to process your response.documents
Example (as I cannot use your class, I give example using my own defined class):
Suppose you have DummyClass
public class DummyClass {
public int DummyInt;
public string DummyString;
public double DummyDouble;
public DummyClass() {
}
public DummyClass(int dummyInt, string dummyString, double dummyDouble) {
DummyInt = dummyInt;
DummyString = dummyString;
DummyDouble = dummyDouble;
}
}
Then doing GroupBy as shown,
DummyClass dc1 = new DummyClass(1, "This dummy", 2.0);
DummyClass dc2 = new DummyClass(2, "That dummy", 2.0);
DummyClass dc3 = new DummyClass(1, "These dummies", 2.0);
DummyClass dc4 = new DummyClass(2, "Those dummies", 2.0);
DummyClass dc5 = new DummyClass(3, "The dummies", 2.0);
List<DummyClass> dummyList = new List<DummyClass>() { dc1, dc2, dc3, dc4, dc5 };
var groupedDummy = dummyList.GroupBy(x => x.DummyInt).ToList();
Will create three groups, marked by DummyInt
Then to process the group you could do
for (int i = 0; i < groupedDummy.Count; ++i){
foreach (DummyClass dummy in groupedDummy[i]) { //this will process the (i-1)-th group
//do something on this group
//groupedDummy[0] will consists of "this" and "these", [1] "that" and "those", while [2] "the"
//Try it out!
}
}
In your case, you should create group based on doctype.
Once you create groups based on your doctype, everything else would be pretty "natural" for you to continue.
Another LINQ method which you might be interested in would be Distinct. But I think for this case, GroupBy would be the primary method you would like to use.
Use only "for loop" instead of "foreach".
foreach will hold the collection and cannot be modified while looping thru it.
Here is an example using groupBy, hope this help.
//mock a collection
ICollection<string> collection1 = new List<string>();
for (int i = 0; i < 10; i++)
{
collection1.Add("BankStatement");
}
for (int i = 0; i < 5; i++)
{
collection1.Add("BankStatement2");
}
for (int i = 0; i < 4; i++)
{
collection1.Add("BankStatement3");
}
//merge and get count
var result = collection1.GroupBy(c => c).Select(c => new { name = c.First(), count = c.Count().ToString() }).ToList();
foreach (var item in result)
{
Console.WriteLine(item.name + ": " + item.count);
}
Just use AddRange()
response.documents[0].pages.AddRange(response.documents[1].pages);
it will merge all pages of document[1] with the document[0] into document[0]

ABCpdf, render an HTML within a "template": How to add margin?

I'm trying to render an HTML within a predefined PDF-template (e.g. within a frame.) The template/frame should reach the edges. But the HTML shouldn't do that. So I need some kind of margin for the HTML only. Here is my code so far:
var doc = new Doc();
doc.MediaBox.String = "A4";
doc.Rect.String = doc.MediaBox.String;
var id = doc.AddImageUrl(url.ToString());
doc.AddImageDoc("template.pdf", 1, doc.MediaBox);
while (doc.Chainable(id))
{
doc.Page = doc.AddPage();
id = doc.AddImageToChain(id);
doc.AddImageDoc("template.pdf", 1, doc.MediaBox);
}
for (var i = 1; i <= doc.PageCount; i++)
{
doc.PageNumber = i;
doc.Flatten();
}
I see, that there is a possibility to pass a Rect to #AddImageDoc. But I don't have this option for #AddImageUrl.
Here is how I could solve the problem:
First, I set the position and margins of the doc.Rect:
doc.Rect.Position(15, 15);
doc.Rect.Width = pageWidth - 2*15;
doc.Rect.Height = pageHeight - 2*15;
Then I filled the doc with the images from the parsed URL:
var id = doc.AddImageUrl(url.ToString());
while (doc.Chainable(id))
{
doc.Page = doc.AddPage();
id = doc.AddImageToChain(id);
}
After this, I reset the doc.Rect to the size of the actual paper (in ma case: A4):
doc.Rect.String = "A4";
Now I can loop over all pages and add the template to them:
for (var i = 1; i <= doc.PageCount; i++)
{
doc.PageNumber = i;
doc.AddImageDoc(template, 1, doc.Rect);
doc.Flatten();
}

Categories

Resources