I tried to add 3 footer contents to PDF using MigraDoc. Here the contents are not aligning in the same line.
MigraDoc.DocumentObjectModel.Paragraph paragraph = section.Footers.Primary.AddParagraph();
section.PageSetup.DifferentFirstPageHeaderFooter = true;
paragraph.AddText(Environment.NewLine + title);
paragraph.Format.Font.Size = 7;
paragraph.Format.Alignment = ParagraphAlignment.Left;
MigraDoc.DocumentObjectModel.Paragraph middle = section.Footers.Primary.AddParagraph();
middle.AddText(operatorName + " "+ methodToRun);
middle.Format.Font.Size = 7;
middle.Format.Alignment = ParagraphAlignment.Center;
MigraDoc.DocumentObjectModel.Paragraph created = section.Footers.Primary.AddParagraph();
created.AddText("Created: " + DateTime.Now.ToString("MMMM dd, yyyy HH:mm zzz"));
created.AddTab();
created.Format.Font.Size = 7;
created.AddText(" Page ");
created.AddPageField();
created.Format.Alignment = ParagraphAlignment.Right;
Using above code footer contents are not aligned in same line. Can anybody give a suggestion to align the footer contents in same line?
If you want to have three elements in a single-line footer, you can achieve this by using tap stops.
Just remove all default tap stops, add a centre-aligned tab stop in the middle, a right-aligned tab stop at the right edge. Then add the contents to a single paragraph, separated by tab stops.
Sample code, assuming A4 page with default margins:
// Create the primary footer.
var footer = section.Footers.Primary;
// Add content to footer.
var paragraph = footer.AddParagraph();
paragraph.Format.TabStops.ClearAll();
paragraph.Format.AddTabStop("8cm", TabAlignment.Center);
paragraph.Format.AddTabStop("16cm", TabAlignment.Right);
paragraph.Format.Alignment = ParagraphAlignment.Left;
paragraph.AddText("Some Info (left)");
paragraph.AddTab();
paragraph.Add(new DateField() { Format = "yyyy/MM/dd HH:mm:ss" });
paragraph.AddTab();
paragraph.AddText("More Info ()right");
Related
I am trying to align to center a paragraph but is not having any affection on the paragraph.I am using OpenXml. Below is the code:
//paragraph properties
ParagraphProperties User_heading_pPr = new ParagraphProperties();
//trying to align center a paragraph
Justification justification1 = new Justification() { Val = JustificationValues.Center };
// build paragraph piece by piece
Text text = new Text(DateTime.Now.ToString() + " , ");
Text text1 = new Text(gjenerimi + " , ");
Text text2 = new Text(merreshifren());
var run = new Run();
run.Append(text,text1,text2);
Paragraph newParagraph = new Paragraph(run);
User_heading_pPr.Append(justification1);
newParagraph.Append(User_heading_pPr);
Here is how I am trying to align center the paragraph.
Reverse the order in which you assign text and paragraph properties:
User_heading_pPr.Append(justification1);
Paragraph newParagraph = new Paragraph(User_heading_pPr);
newParagraph.Append(run);
In valid and well-formed Word Open XML the paragraph properties must precede the run. So you have to build the Open XML document the same way.
This is a bit different than object models, the way we're typically used to dealing with them - the order does matter!
I have a code in C# with EPPLUS for Excel that fills a cell green if the value of the cell is over 100. It works:
ExcelAddress _formatRangeAddress = new ExcelAddress("G4:G" + (c.Count + 4));
string _statement = "IF(G4>100,1,0)";
var _cond2 = hoja.ConditionalFormatting.AddExpression(_formatRangeAddress);
_cond2.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond2.Style.Fill.BackgroundColor.Color = System.Drawing.Color.LimeGreen;
_cond2.Formula = _statement;
But what I really need is to fill the entire row. If I change the range:
ExcelAddress _formatRangeAddress = new ExcelAddress("A4:G" + (c.Count + 4));
Applies only for the cells in the A column, not to the entire row.
What I am doing wrong?
Make the reference to G4 absolute otherwise the formula will be apply relative to that cell. So change this line:
string _statement = "IF(G4>100,1,0)";
to apply to the entire row based ONLY on G4
string _statement = "IF($G$4>100,1,0)";
(Response to Comments)
to apply to entire rows relative to row number but keep the column fixed to G:
string _statement = "IF($G4>100,1,0)";
I'm trying to add a TextField (acrofield) in the middle of a Paragraph sentence using iTextSharp. An example would be "The Effective Date is [Day] day of [Month], [Year] that this will begin."
Things I have tried:
Paragraph para1 = new Paragraph();
para1.Add(New Phrase("The Effective Date is",fontBold));
//The next line is where it breaks, "Insertion of illegal Element: 30"
para1.Add(CreateTextField("Day",1,0)); //this function returns a PdfPCell.
PdfPCell tempCell = new PdfPCell();
tempCell.AddElement(new Phrase("The Effective Date is",fontBold));
//the next line breaks as well, "Element not allowed."
tempCell.AddElement(CreateTextField("Day",1,0));
Paragraph para1 = new Paragraph();
para1.Add(New Phrase("The Effective Date is",fontBold));
para1.AddSpecial(CreateTextField("Day",1,0));
//This doesn't generate an error, but the TextField is not displayed on PDF
Paragraph para1 = new Paragraph();
PdfPTable tempTable = new PdfPTable(1);
para1.Add(New Phrase("Hello",fontBold));
tempTable.AddCell(CreateTextField("Day",1,0));
para1.Add(tempTable);
para1.Add(New Phrase("World",fontBold));
//This doesn't generate an error, but the TextField is not displayed on PDF
I know the CreateTextField(...) works because I am using it in several other places on the page.
How can I add a TextField inline with other text without using tables and tediously trying to manipulate cell size to accommodate what I need?
Thanks for the help!
Your question is wrong. You don't want to add a PdfPCell to a Paragraph. You want to create inline form fields. That's a totally different question.
Take a look at the GenericFields example. In this example, we create the Paragraph you need like this:
Paragraph p = new Paragraph();
p.add("The Effective Date is ");
Chunk day = new Chunk(" ");
day.setGenericTag("day");
p.add(day);
p.add(" day of ");
Chunk month = new Chunk(" ");
month.setGenericTag("month");
p.add(month);
p.add(", ");
Chunk year = new Chunk(" ");
year.setGenericTag("year");
p.add(year);
p.add(" that this will begin.");
Do you see how we add empty Chunks where you want to add a PdfPCell? We use the setGenericTag() method on these Chunk object to add a form field where ever the Chunks are rendered.
For this to work, we need to declare a page event:
writer.setPageEvent(new FieldChunk());
The FieldChunk class looks like this:
public class FieldChunk extends PdfPageEventHelper {
#Override
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
TextField field = new TextField(writer, rect, text);
try {
writer.addAnnotation(field.getTextField());
} catch (IOException ex) {
throw new ExceptionConverter(ex);
} catch (DocumentException ex) {
throw new ExceptionConverter(ex);
}
}
}
Every time a "generic chunk" is rendered, the onGenericTag() method will be called passing the parameter we used in the setGenericTag() method as the text parameter. We use the writer, rect and text parameters to create and add a TextField. The result looks like this:
Feel free to adapt rect if you want to create a bigger text field.
Important: my example is written in Java. If you want to port the example to C#, just change the first letter of each method to upper case (e.g. change add() into Add()). If that doesn't work, try setting the parameter as a member variable (e.g. change writer.setPageEvent(event) into writer.PageEvent = event).
Update: If you want to make the field bigger, you should create a new Rectangle. For instance:
Rectangle rect2 = new Rectangle(rect.Left, rect.Bottom - 5, rect.Right, rect.Top + 2);
TextField field = new TextField(writer, rect2, text);
im need to implement a way to one-click add a footer to a word document consisting of one line.
The first part needs to be the absolute Path to the document and it has to be left-bound. In addition to this, there has to be the actual page number aligned to the right.
This wasn't a problem on Excel; there I could use LeftFooter, CenterFooter, RightFooter.
On Word however there are no such properties to access.
edit: I found a semi-working solution which has some bugs in it and isn't properly designed because I could not find a proper way yet.
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
foreach (Word.Section wordSection in doc.Sections)
{
Word.Range PageNumberRange = wordSection.Range;
PageNumberRange.Fields.Add(PageNumberRange, Word.WdFieldType.wdFieldEmpty ,"PAGE Arabic ", true);
Word.Range footer = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footer.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
footer.Tables.Add(footer, 1, 3);
Word.Table tbl = footer.Tables[1];
tbl.Cell(1, 1).Range.Text = doc.FullName;
tbl.Cell(1, 3).Range.Text = PageNumberRange.Text;
/**/
footer.Font.ColorIndex = Word.WdColorIndex.wdBlack;
footer.Font.Size = 6;
PageNumberRange.Text = "";
The problems with this one are: It never overwrites the exisiting footer. If it writes "document1 ... 1" and you click on it again, because you saved your document, it doenst change the footer. Furthermore: If you have multiple pages, every page except page 1 gets deleted.
I never imagined it could be so hard, to implement such an easy task.
An alternative approach using styles
Document doc = this.application.ActiveDocument;
Section wordSection = doc.Sections[1];
Range footer = wordSection.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
footer.Fields.Add(footer, WdFieldType.wdFieldEmpty, #"PAGE \* ARABIC", true);
footer.Collapse(WdCollapseDirection.wdCollapseStart);
footer.InsertBefore("\t \t");
footer.InsertBefore(doc.FullName);
footer.Font.Name = "Arial";
So, I want to export my data from database to Ms Word using openXML. I formatting my paragraph as below
para = new Paragraph();
run = new Run(new Text(row["name"].ToString()));
paraProp = new ParagraphProperties();
spacing = new SpacingBetweenLines() { Before = "60", After = "60" };
paraProp.Append(spacing);
para.Append(paraProp);
para.Append(run);
The problem is some data is empty, and this make my paragraph formatting not working.
I try to add empty space like this
run = new Run(new Text(row["name"].ToString() + " "));
But it also not working.
So how to apply paragraph formatting even the data is empty?
I am guessing the empty paragraphs are not spacing properly and causing your formatting issues.
Try changing your spacingbetweenlines properties to:
{ After = "60", Before = "60" Line = "240", LineRule = LineSpacingRuleValues.Exact};
The Line value is the height of the line and the and the LineRule sets how the before and after are applied to the paragraph.