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.
Related
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");
I have a method that splits paragraphs into two paragraphs at the same location on the word file.
the logic is working fine but I'm losing the document format and styles.
foreach (Paragraph p in body.Descendants<Paragraph>())
{
//splitting the paragraph
var part2 = p.InnerText.Substring(startIndex);
var part1 = p.InnerText.Substring(0, startIndex);
p.InnerText.Replace(p.InnerText, part1);
p.InnerText.Replace(p.InnerText, part2);
pargs.Add(part1);
pargs.Add(part2);
}
// clean the documetn
body.RemoveAllChildren<Paragraph>();
//re-creat the paragraphs
for (int i = 0; i < pargs.Count; i++)
{
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(pargs[i]));
}
return paras;
above is simplified code
I know that my approach is the cause for this problem since I'm taking the inner text of each paragraph and creating a new paragraph without taking the styles. my question is. is there another approach
If you want to keep the original style, you can just copy run properties and set them in newly created Run.
Something like that (haven't tested though):
var properties = paragraph
.Descendants<Run>()
.First()
.RunProperties
.Clone();
...
var run = newParagraph.AppendChild(new Run()
{
RunProperties = (RunProperties)properties
});
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!
how to add character spacing for range of characters
like i want to give character spacing In word toggling for 2 characters
gg of spacing="Expanded" with By value of 4pt
Open XML has not only SDK, but also a tool for converting any document to C# code.
Best way to find out how to use some word feature is to make 2 short documnets - one with this feature used and the other - without. Then convert both documnets into C# code and compare generated code (you can use WinMerge, for example).
As you wish to apply a style to the middle of a paragraph you will need to have (at least) 3 separate Run elements; the first for the start of the unstyled text, the second for the text that has the spacing and the third for the rest of the unstyled text.
To add the character spacing you need to add a Spacing element to the Run. The Spacing element has a Value property that sets the spacing you want in twentieths of a point (so to get 4pt you need to set the Value to 80).
The following code will create a document with spacing on the gg in the word toggling
public static void CreateDoc(string fileName)
{
// Create a Wordprocessing document.
using (WordprocessingDocument package =
WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
{
// Add a new main document part.
package.AddMainDocumentPart();
//create a body and a paragraph
Body body = new Body();
Paragraph paragraph = new Paragraph();
//add the first part of the text to the paragraph in a Run
paragraph.AppendChild(new Run(new Text("This sentence has spacing between the gg in to")));
//create another run to hold the text with spacing
Run secondRun = new Run();
//create a RunProperties with a Spacing child.
RunProperties runProps = new RunProperties();
runProps.AppendChild(new Spacing() { Val = 80 });
//append the run properties to the Run we wish to assign spacing to
secondRun.AppendChild(runProps);
//add the text to the Run
secondRun.AppendChild(new Text("gg"));
//add the spaced Run to the paragraph
paragraph.AppendChild(secondRun);
//add the final text as a third Run
paragraph.AppendChild(new Run(new Text("ling")));
//add the paragraph to the body
body.AppendChild(paragraph);
package.MainDocumentPart.Document = new Document(body);
// Save changes to the main document part.
package.MainDocumentPart.Document.Save();
}
}
The above produces the following:
Note that you can set the Value of the Spacing to a negative number and the text will be condensed rather than expanded.
Please refer my code where i am writing values on pdf. But i want to align numeric fields to the right.
Is there any property/method for that. I am using this property PdfFormField.MK_CAPTION_RIGHT but it is not working.
var pdfReader = new PdfReader(outputPath);
string fontsfolder1 = #"D:\ItextSharp\Fonts\acmesab.TTF";
var pdfStamper1 = new PdfStamper(pdfReader, new FileStream(outputPath3, FileMode.Create));
BaseFont customfont1 = BaseFont.CreateFont(fontsfolder, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
AcroFields af = pdfStamper1.AcroFields;
List<BaseFont> list1 = new List<BaseFont>();
list.Add(customfont1);
iTextSharp.text.Font bold1 = new iTextSharp.text.Font(customfont1, 6, 0, BaseColor.BLACK);
af.SubstitutionFonts = list;
foreach (var field1 in af.Fields)
{
af.SetFieldProperty(field1.Key, "textalignment", PdfFormField.MK_CAPTION_RIGHT, null);
af.SetField(field1.Key, "123");
}
pdfStamper1.FormFlattening = false;
pdfStamper1.Close();
I don't know how you can do this directly using ItextSharp Property.But one way is to add the spaces to the left of the content,to do this you can get the Field Length and based on that length and length of the content, you can calculate the spaces to be added to the left of the content.it will automatically put your content to the right side of the PDF field.hope this way can help you solve your problem...
You can align the text by using the ShowTextAligned method which takes an alignment parameter, like this:
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT,"Text is left aligned",200,800,0);
cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT,"Text is right aligned",200,788,0);
where cb is PdfContentByte
For Detail