OpenXMl Bulleted List with mUltiple Bullet Charcter.? - c#

My Expected out put is
● AA
► BBB
■ CCC
I have tried a lot and but gets the output like this
● AA
● BBB
● CCC
My code is
Document document1 = new Document();
document1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
Body body1 = new Body();
Paragraph paragraph3 = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "ListParagraph" };
Shading shading1 = new Shading(){ Val = ShadingPatternValues.Clear, Color = "auto", Fill = "Blue" };
SpacingBetweenLines spacingBetweenLines1 = new SpacingBetweenLines();
Justification justification1 = new Justification(){ Val = JustificationValues.Left };
NumberingProperties numberingProperties1 = new NumberingProperties();
NumberingLevelReference numberingLevelReference1 = new NumberingLevelReference(){ Val = 0 };
NumberingId numberingId1 = new NumberingId(){ Val = 1 };
numberingProperties1.Append(numberingLevelReference1);
numberingProperties1.Append(numberingId1);
paragraphProperties1.Append(paragraphStyleId1);
paragraphProperties1.Append(shading1);
paragraphProperties1.Append(spacingBetweenLines1);
paragraphProperties1.Append(justification1);
paragraphProperties1.Append(numberingProperties1);
Run run3 = new Run();
Text text2 = new Text();
text2.Text = "AA";
run3.Append(runProperties2);
run3.Append(text2);
paragraph3.Append(paragraphProperties1);
paragraph3.Append(run3);
Paragraph paragraph4 = new Paragraph();
ParagraphProperties paragraphProperties2 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId(){ Val = "ListParagraph" };
Shading shading2 = new Shading(){ Val = ShadingPatternValues.Clear, Color = "auto", Fill = "Blue" };
SpacingBetweenLines spacingBetweenLines2 = new SpacingBetweenLines();
Justification justification2 = new Justification(){ Val = JustificationValues.Left };
NumberingProperties numberingProperties2 = new NumberingProperties();
NumberingLevelReference numberingLevelReference2 = new NumberingLevelReference(){ Val = 0 };
NumberingId numberingId2 = new NumberingId(){ Val = 2 };
numberingProperties2.Append(numberingLevelReference2);
numberingProperties2.Append(numberingId2);
paragraphProperties2.Append(paragraphStyleId2);
paragraphProperties2.Append(shading2);
paragraphProperties2.Append(spacingBetweenLines2);
paragraphProperties2.Append(justification2);
paragraphProperties2.Append(numberingProperties2);
Run run4 = new Run();
Text text3 = new Text();
text3.Text = "BBB";
run4.Append(runProperties3);
run4.Append(text3);
paragraph4.Append(paragraphProperties2);
paragraph4.Append(run4);
Paragraph paragraph5 = new Paragraph();
ParagraphProperties paragraphProperties3 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId3 = new ParagraphStyleId(){ Val = "ListParagraph" };
Shading shading3 = new Shading(){ Val = ShadingPatternValues.Clear, Color = "auto", Fill = "Blue" };
SpacingBetweenLines spacingBetweenLines3 = new SpacingBetweenLines();
Justification justification3 = new Justification(){ Val = JustificationValues.Left };
NumberingProperties numberingProperties3 = new NumberingProperties();
NumberingLevelReference numberingLevelReference3 = new NumberingLevelReference(){ Val = 0 };
NumberingId numberingId3 = new NumberingId(){ Val = 3 };
numberingProperties3.Append(numberingLevelReference3);
numberingProperties3.Append(numberingId3);
paragraphProperties3.Append(paragraphStyleId3);
paragraphProperties3.Append(shading3);
paragraphProperties3.Append(spacingBetweenLines3);
paragraphProperties3.Append(justification3);
paragraphProperties3.Append(numberingProperties3);
Run run5 = new Run();
Text text4 = new Text();
text4.Text = "CCC";
run5.Append(runProperties4);
run5.Append(text4);
paragraph5.Append(paragraphProperties3);
paragraph5.Append(run5);
body1.Append(paragraph1);
body1.Append(paragraph2);
body1.Append(paragraph3);
body1.Append(paragraph4);
body1.Append(paragraph5);
document1.Append(body1);
document1.Append(openXmlUnknownElement2);
mainDocumentPart1.Document = document1;
Numbering numbering1 = new Numbering();
numbering1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
AbstractNum abstractNum1 = new AbstractNum(){ AbstractNumberId = 0 };
MultiLevelType multiLevelType1 = new MultiLevelType(){ Val = MultiLevelValues.HybridMultilevel };
Level level1 = new Level(){ LevelIndex = 0 };
StartNumberingValue startNumberingValue1 = new StartNumberingValue(){ Val = 1 };
NumberingFormat numberingFormat1 = new NumberingFormat(){ Val = NumberFormatValues.Bullet };
LevelText levelText1 = new LevelText(){ Val = "●" };
LevelJustification levelJustification1 = new LevelJustification(){ Val = LevelJustificationValues.Left };
NumberingSymbolRunProperties numberingSymbolRunProperties1 = new NumberingSymbolRunProperties();
RunFonts runFonts6 = new RunFonts(){ Hint = FontTypeHintValues.Default, Ascii = "Arial", HighAnsi = "Arial" };
numberingSymbolRunProperties1.Append(runFonts6);
level1.Append(startNumberingValue1);
level1.Append(numberingFormat1);
level1.Append(levelText1);
level1.Append(levelJustification1);
level1.Append(numberingSymbolRunProperties1);
abstractNum1.Append(multiLevelType1);
abstractNum1.Append(level1);
NumberingInstance numberingInstance1 = new NumberingInstance(){ NumberID = 1 };
AbstractNumId abstractNumId1 = new AbstractNumId(){ Val = 0 };
numberingInstance1.Append(abstractNumId1);
AbstractNum abstractNum2 = new AbstractNum(){ AbstractNumberId = 1 };
MultiLevelType multiLevelType2 = new MultiLevelType(){ Val = MultiLevelValues.HybridMultilevel };
Level level2 = new Level(){ LevelIndex = 0 };
StartNumberingValue startNumberingValue2 = new StartNumberingValue(){ Val = 1 };
NumberingFormat numberingFormat2 = new NumberingFormat(){ Val = NumberFormatValues.Bullet };
LevelText levelText2 = new LevelText(){ Val = "►" };
LevelJustification levelJustification2 = new LevelJustification(){ Val = LevelJustificationValues.Left };
NumberingSymbolRunProperties numberingSymbolRunProperties2 = new NumberingSymbolRunProperties();
RunFonts runFonts7 = new RunFonts(){ Hint = FontTypeHintValues.Default, Ascii = "Arial", HighAnsi = "Arial" };
numberingSymbolRunProperties2.Append(runFonts7);
level2.Append(startNumberingValue2);
level2.Append(numberingFormat2);
level2.Append(levelText2);
level2.Append(levelJustification2);
level2.Append(numberingSymbolRunProperties2);
abstractNum2.Append(multiLevelType2);
abstractNum2.Append(level2);
NumberingInstance numberingInstance2 = new NumberingInstance(){ NumberID = 2 };
AbstractNumId abstractNumId2 = new AbstractNumId(){ Val = 6 };
numberingInstance2.Append(abstractNumId2);
AbstractNum abstractNum3 = new AbstractNum(){ AbstractNumberId = 2 };
MultiLevelType multiLevelType3 = new MultiLevelType(){ Val = MultiLevelValues.HybridMultilevel };
Level level3 = new Level(){ LevelIndex = 0 };
StartNumberingValue startNumberingValue3 = new StartNumberingValue(){ Val = 1 };
NumberingFormat numberingFormat3 = new NumberingFormat(){ Val = NumberFormatValues.Bullet };
LevelText levelText3 = new LevelText(){ Val = "■" };
LevelJustification levelJustification3 = new LevelJustification(){ Val = LevelJustificationValues.Left };
NumberingSymbolRunProperties numberingSymbolRunProperties3 = new NumberingSymbolRunProperties();
RunFonts runFonts8 = new RunFonts(){ Hint = FontTypeHintValues.Default, Ascii = "Arial", HighAnsi = "Arial" };
numberingSymbolRunProperties3.Append(runFonts8);
level3.Append(startNumberingValue3);
level3.Append(numberingFormat3);
level3.Append(levelText3);
level3.Append(levelJustification3);
level3.Append(numberingSymbolRunProperties3);
abstractNum3.Append(multiLevelType3);
abstractNum3.Append(level3);
NumberingInstance numberingInstance3 = new NumberingInstance(){ NumberID = 3 };
AbstractNumId abstractNumId3 = new AbstractNumId(){ Val = 3 };
numberingInstance3.Append(abstractNumId3);
numbering1.Append(abstractNum1);
numbering1.Append(numberingInstance1);
numbering1.Append(abstractNum2);
numbering1.Append(numberingInstance2);
numbering1.Append(abstractNum3);
numbering1.Append(numberingInstance3);
numberingDefinitionsPart1.Numbering = numbering1;

You need to append the NumberingInstances AFTER you have appended ALL the AbstractNumbers. They shouldn't be between them.

Related

requests[0]' (oneof), oneof field 'kind' is already set. Cannot set 'updateCell encountered when generating checkbox

I'm trying to generate a checkbox from C#.net using google sheets API but I'm encountering the oneof field kind is already set error. I tried combining extendedValue and DataValidation. Please see code snippet below:
ConditionValue conditionValueTrue = new ConditionValue();
conditionValueTrue.UserEnteredValue = "TRUE";
ConditionValue conditionValueFalse = new ConditionValue();
conditionValueFalse.UserEnteredValue = "FALSE";
ConditionValue[] validValues = { conditionValueTrue, conditionValueFalse };
BooleanCondition bc = new BooleanCondition();
bc.Type = "BOOLEAN";
bc.Values = validValues;
DataValidationRule dataValidationRule = new DataValidationRule();
dataValidationRule.Condition = bc;
dataValidationRule.ShowCustomUi = true;
GridRange validationRange = new GridRange();
validationRange.StartColumnIndex = 7;
validationRange.EndColumnIndex = 7;
validationRange.SheetId = 0;
SetDataValidationRequest setDataValidationRequest = new SetDataValidationRequest();
setDataValidationRequest.Rule = dataValidationRule;
setDataValidationRequest.Range = validationRange;
ExtendedValue extendedValue = new ExtendedValue();
extendedValue.BoolValue = true;
BatchUpdateSpreadsheetRequest busr = new BatchUpdateSpreadsheetRequest();
busr.Requests = new List<Request>();
Request r = new Request();
busr.Requests.Add(r);
r.UpdateCells = new UpdateCellsRequest();
r.SetDataValidation = setDataValidationRequest;
var gc = new GridCoordinate();
gc.ColumnIndex = 7;
gc.RowIndex = row;
gc.SheetId = 0;
r.UpdateCells.Start = gc;
r.UpdateCells.Fields = "*";
r.UpdateCells.Rows = new List<RowData>();
var rd = new RowData();
r.UpdateCells.Rows.Add(rd);
rd.Values = new List<CellData>();
var cd = new CellData();
cd.UserEnteredValue = extendedValue;
rd.Values.Add(cd);
SpreadsheetsResource.BatchUpdateRequest bur = _sheetsService.Spreadsheets.BatchUpdate(busr, SpreadsheetId);
bur.Execute();

Need dynamic variable names in C# for ebay feeds

I need these varaible names to be dynamic.
To give you an example of what I'm looking at > https://ebaydts.com/eBayKBDetails?KBid=1742
Now in the above link, it has some code for variaitons.
//Specify Variations
VariationTypeCollection VarCol = new VariationTypeCollection();
//Variation 1 - Black S
VariationType var1 = new VariationType();
var1.SKU = "VAR1";
var1.Quantity = 10;
var1.StartPrice = new AmountType();
var1.StartPrice.currencyID = CurrencyCodeType.AUD;
var1.StartPrice.Value = 35;
var1.VariationSpecifics = new NameValueListTypeCollection();
NameValueListType Var1Spec1 = new NameValueListType();
StringCollection Var1Spec1Valuecoll = new StringCollection();
Var1Spec1.Name = "Colour";
Var1Spec1Valuecoll.Add("Black");
Var1Spec1.Value = Var1Spec1Valuecoll;
var1.VariationSpecifics.Add(Var1Spec1);
NameValueListType Var1Spec2 = new NameValueListType();
StringCollection Var1Spec2Valuecoll = new StringCollection();
Var1Spec2.Name = "Size";
Var1Spec2Valuecoll.Add("S");
Var1Spec2.Value = Var1Spec2Valuecoll;
var1.VariationSpecifics.Add(Var1Spec2);
VarCol.Add(var1);
//Variation 2 - Black L
VariationType var2 = new VariationType();
var2.SKU = "VAR2";
var2.Quantity = 10;
var2.StartPrice = new AmountType();
var2.StartPrice.currencyID = CurrencyCodeType.AUD;
var2.StartPrice.Value = 45;
var2.VariationSpecifics = new NameValueListTypeCollection();
NameValueListType Var2Spec1 = new NameValueListType();
StringCollection Var2Spec1Valuecoll = new StringCollection();
Var2Spec1.Name = "Colour";
Var2Spec1Valuecoll.Add("Black");
Var2Spec1.Value = Var2Spec1Valuecoll;
var2.VariationSpecifics.Add(Var2Spec1);
NameValueListType Var2Spec2 = new NameValueListType();
StringCollection Var2Spec2Valuecoll = new StringCollection();
Var2Spec2.Name = "Size";
Var2Spec2Valuecoll.Add("L");
Var2Spec2.Value = Var2Spec2Valuecoll;
var2.VariationSpecifics.Add(Var2Spec2);
VarCol.Add(var2);
So the code I'm working with I would Like dynamically create the varaible names, because as you can imagine there is no way for me to know how many variations each product has.
Sure i could count the amount of variations, and set up an if else structure
if (count == 1) {
//some code
}
else if (count == 2) {
//some code
}
But that doesn't seem like a good solution.
At the moment variables are required to be output inside a for each loop
Here is the code I'm working with:
string UPC = "";
string Brand = "";
string MPN = "";
if (!String.IsNullOrEmpty(product.Gtin))
UPC = product.Gtin;
if (!String.IsNullOrEmpty(ebayProduct.EbayProductBrandName))
Brand = ebayProduct.EbayProductBrandName;
if (!String.IsNullOrEmpty(product.ManufacturerPartNumber))
MPN = product.ManufacturerPartNumber;
//create the call object
AddFixedPriceItemCall AddFPItemCall = new AddFixedPriceItemCall(context);
AddFPItemCall.AutoSetItemUUID = true;
//create an item object and set the properties
ItemType item = new ItemType();
//set the item condition depending on the value from GetCategoryFeatures
item.ConditionID = 1000; //new
//Basic properties of a listing
item.Country = CountryCodeType.AU;
item.Currency = CurrencyCodeType.AUD;
//Track item by SKU
item.InventoryTrackingMethod = InventoryTrackingMethodCodeType.SKU;
item.SKU = ebayProduct.EbayProductSKU;
if (!String.IsNullOrEmpty(ebayProduct.EbayProductDescription))
item.Description = ebayProduct.EbayProductDescription;
else
item.Description = "This product is brand new.";
if (!String.IsNullOrEmpty(ebayProduct.EbayProductTitle))
item.Title = ebayProduct.EbayProductTitle;
item.SubTitle = ebayProduct.EbayProductItemSubtitle;
item.ListingDuration = "GTC";
item.ItemSpecifics = new NameValueListTypeCollection();
NameValueListTypeCollection ItemSpecs = new NameValueListTypeCollection();
var productSpecAttributes = product.ProductSpecificationAttributes.ToList();
foreach (var val in productSpecAttributes)
{
StringCollection valueCol1 = new StringCollection();
NameValueListType nv1 = new NameValueListType();
nv1.Name = val.SpecificationAttributeOption.SpecificationAttribute.Name.Replace(":", "");
valueCol1.Add(val.CustomValue);
nv1.Value = valueCol1;
ItemSpecs.Add(nv1);
}
item.ItemSpecifics = ItemSpecs;
item.PaymentMethods = new BuyerPaymentMethodCodeTypeCollection();
item.PaymentMethods.Add(BuyerPaymentMethodCodeType.PayPal);
item.PayPalEmailAddress = "test#test.com";
item.PostalCode = "5000";
//Specify Shipping Services
item.DispatchTimeMax = 3;
item.ShippingDetails = new ShippingDetailsType();
item.ShippingDetails.ShippingServiceOptions = new ShippingServiceOptionsTypeCollection();
ShippingServiceOptionsType shipservice1 = new ShippingServiceOptionsType();
shipservice1.ShippingService = "AU_Regular";
shipservice1.ShippingServicePriority = 1;
shipservice1.ShippingServiceCost = new AmountType();
shipservice1.ShippingServiceCost.currencyID = CurrencyCodeType.AUD;
shipservice1.ShippingServiceCost.Value = 1.0;
shipservice1.ShippingServiceAdditionalCost = new AmountType();
shipservice1.ShippingServiceAdditionalCost.currencyID = CurrencyCodeType.AUD;
shipservice1.ShippingServiceAdditionalCost.Value = 1.0;
item.ShippingDetails.ShippingServiceOptions.Add(shipservice1);
ShippingServiceOptionsType shipservice2 = new ShippingServiceOptionsType();
shipservice2.ShippingService = "AU_Express";
shipservice2.ShippingServicePriority = 2;
shipservice2.ShippingServiceCost = new AmountType();
shipservice2.ShippingServiceCost.currencyID = CurrencyCodeType.AUD;
shipservice2.ShippingServiceCost.Value = 4.0;
shipservice2.ShippingServiceAdditionalCost = new AmountType();
shipservice2.ShippingServiceAdditionalCost.currencyID = CurrencyCodeType.AUD;
shipservice2.ShippingServiceAdditionalCost.Value = 1.0;
item.ShippingDetails.ShippingServiceOptions.Add(shipservice2);
//Specify Return Policy
item.ReturnPolicy = new ReturnPolicyType();
item.ReturnPolicy.ReturnsAcceptedOption = "ReturnsAccepted";
item.StartPrice = new AmountType();
item.StartPrice.currencyID = CurrencyCodeType.AUD;
item.StartPrice.Value = Double.Parse(ebayProduct.EbayProductPrice.ToString());
item.PrimaryCategory = new CategoryType();
item.PrimaryCategory.CategoryID = ebayProduct.EbayCategoryID;
item.ProductListingDetails = new ProductListingDetailsType();
//Specifying UPC as the product identifier. Other applicable product identifiers
//include ISBN, EAN, Brand-MPN.
item.ProductListingDetails.UPC = UPC;
//If multiple product identifiers are specified, eBay uses the first one that
//matches a product in eBay's catalog system.
item.ProductListingDetails.BrandMPN = new BrandMPNType();
item.ProductListingDetails.BrandMPN.Brand = ebayProduct.EbayProductBrandName;
item.ProductListingDetails.BrandMPN.MPN = product.ManufacturerPartNumber;
//If multiple prod matches found, list the item with the 1st product's information
item.ProductListingDetails.UseFirstProduct = true;
//Add pictures
item.PictureDetails = new PictureDetailsType();
item.PictureDetails.PictureURL = new StringCollection();
//Specify GalleryType
item.PictureDetails.GalleryType = GalleryTypeCodeType.None;
item.PictureDetails.GalleryTypeSpecified = true;
/*
* Handle Variations
*/
var childProducts = _productService.GetAssociatedProducts(product.Id);
if (childProducts != null)
{
//Specify VariationsSpecificsSet
item.Variations = new VariationsType();
item.Variations.VariationSpecificsSet = new NameValueListTypeCollection();
var colourSpec = productSpecAttributes.FirstOrDefault(
x => x.SpecificationAttributeOption.SpecificationAttribute.Name == "Colour");
var sizeSpec = productSpecAttributes.FirstOrDefault(
x => x.SpecificationAttributeOption.SpecificationAttribute.Name == "Size");
if (colourSpec != null && sizeSpec == null)
{
List<string> colourSpecsList = new List<string>();
foreach (var cp in childProducts)
{
var colourSpecs = cp.ProductSpecificationAttributes.FirstOrDefault(
x => x.SpecificationAttributeOption.SpecificationAttribute.Name == "Colour");
colourSpecsList.Add(colourSpecs.SpecificationAttributeOption.Name);
}
//sizes
NameValueListType NVListVS2 = new NameValueListType();
NVListVS2.Name = "Colour";
StringCollection VSvaluecollection2 = new StringCollection();
String[] Colour = colourSpecsList.ToArray();
VSvaluecollection2.AddRange(Colour);
NVListVS2.Value = VSvaluecollection2;
item.Variations.VariationSpecificsSet.Add(NVListVS2);
}
//Add Variation Specific Pictures
item.Variations.Pictures = new PicturesTypeCollection();
foreach (var cp in childProducts)
{
//Specify Variations
VariationTypeCollection VarCol = new VariationTypeCollection();
PicturesType pic = new PicturesType();
var specs = cp.ProductSpecificationAttributes.FirstOrDefault(x => x.SpecificationAttributeOption.SpecificationAttribute.Name == "Colour");
var childEbayProduct = _ebayProductService.GetEbayProductById(cp.Id);
if (!String.IsNullOrEmpty(specs.SpecificationAttributeOption.Name))
{
VariationType var1 = new VariationType();
var1.SKU = cp.RexProductId.ToString();
Debug.WriteLine(cp.RexProductId.ToString());
var1.Quantity = cp.StockQuantity;
var1.StartPrice = new AmountType();
var1.StartPrice.currencyID = CurrencyCodeType.AUD;
var1.StartPrice.Value = Double.Parse(cp.Price.ToString());
var1.VariationSpecifics = new NameValueListTypeCollection();
NameValueListType Var1Spec1 = new NameValueListType();
StringCollection Var1Spec1Valuecoll = new StringCollection();
Var1Spec1.Name = "Colour";
Var1Spec1Valuecoll.Add(specs.SpecificationAttributeOption.Name);
Var1Spec1.Value = Var1Spec1Valuecoll;
var1.VariationSpecifics.Add(Var1Spec1);
//pics
pic.VariationSpecificName = "Colour";
pic.VariationSpecificPictureSet = new VariationSpecificPictureSetTypeCollection();
VariationSpecificPictureSetType VarPicSet1 = new VariationSpecificPictureSetType();
VarPicSet1.VariationSpecificValue = specs.SpecificationAttributeOption.Name;
StringCollection PicURLVarPicSet1 = new StringCollection();
PicURLVarPicSet1.Add(childEbayProduct.EbayProductMainImgUrl);
VarPicSet1.PictureURL = PicURLVarPicSet1;
pic.VariationSpecificPictureSet.Add(VarPicSet1);
item.Variations.Pictures.Add(pic);
VarCol.Add(var1);
item.Variations.Variation = VarCol;
}
}
}
Anyone know what I might be able to do here.
Cheers
The closest thing to "dynamic variable names" is a Dictionary.
Dictionary<string, string> variables = new Dictionary<string, string>();
variables.Add("var1", "test1");
variables.Add("var2", "test2");
MessageBox.Show(variables["var1"]); // will display test1
If you have a set of needed data for each "variable", make a class or struct (maybe call it "TheData")
Dictionary<string, TheData> variables = new Dictionary<string, TheData>();
variables.Add("var1", new TheData() { price = 25.28, upc = "01121...", manufacturer = "ACME" });

Add FootNotes to Word Document Programatically using OpenXML SDK C#

I need to create a Word Document using Open XML SDK. I have the document text and the Footnotes . I am using the below Snippets to Create the Word Document.
I am able to create the document with the text ,But I am not able to add Footnotes to it.
Can you please let us know how to add FootNotes programmatically using Open Xml
public void CreateWordDocument()
{
using (MemoryStream DocxMemory = new MemoryStream())
{
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(DocxMemory, WordprocessingDocumentType.Document, true))
{
// Add a main document part.
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
// Create the document structure and add some text.
this.AddSettingsToMainDocumentPart(mainPart);
StyleDefinitionsPart part = mainPart.StyleDefinitionsPart;
// If the Styles part does not exist, add it.
if (part == null)
{
this.AddStylesPartToPackage(mainPart);
}
mainPart.Document = new Document();
Body body = mainPart.Document.AppendChild(new Body());
Paragraph Para = body.AppendChild(new Paragraph());
Run run = Para.AppendChild(new Run());
run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text("This is main text of the document"));
var footnotesPart = mainPart.AddNewPart<FootnotesPart>("rId1");
this.GenerateFooterPartContent(mainPart.FootnotesPart);
mainPart.Document.Save();
wordDocument.Close();
MemoryStream Result = new MemoryStream();
DocxMemory.Seek(0, SeekOrigin.Begin);
DocxMemory.CopyTo(Result);
Result.Position = 0;
////Citation processing
byte[] BufferFileData = new byte[Result.Length];
Result.Read(BufferFileData, 0, (int)Result.Length);
File.WriteAllBytes("Output1.docx", BufferFileData);
}
}
}
private void AddSettingsToMainDocumentPart(MainDocumentPart part)
{
DocumentSettingsPart settingsPart = part.DocumentSettingsPart;
if (settingsPart == null)
settingsPart = part.AddNewPart<DocumentSettingsPart>();
settingsPart.Settings = new Settings(
new Compatibility(
new CompatibilitySetting()
{
Name = new EnumValue<CompatSettingNameValues>
(CompatSettingNameValues.CompatibilityMode),
Val = new StringValue("14"),
Uri = new StringValue
("http://schemas.microsoft.com/office/word")
}
)
);
settingsPart.Settings.Save();
}
private StyleDefinitionsPart AddStylesPartToPackage(MainDocumentPart mainPart)
{
StyleDefinitionsPart part;
part = mainPart.AddNewPart<StyleDefinitionsPart>();
Styles root = new Styles();
root.Save(part);
return part;
}
// Generates content of part.
private void GenerateFooterPartContent(FootnotesPart part)
{
Footnotes footnotes1 = new Footnotes() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
footnotes1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
footnotes1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
footnotes1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
footnotes1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
footnotes1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
footnotes1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
footnotes1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
footnotes1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
footnotes1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
footnotes1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
footnotes1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
footnotes1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
footnotes1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
footnotes1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
footnotes1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Footnote footnote1 = new Footnote() { Type = FootnoteEndnoteValues.Separator, Id = -1 };
Paragraph paragraph1 = new Paragraph() { RsidParagraphAddition = "003F1A60", RsidParagraphProperties = "00626544", RsidRunAdditionDefault = "003F1A60" };
Run run1 = new Run();
SeparatorMark separatorMark1 = new SeparatorMark();
run1.Append(separatorMark1);
paragraph1.Append(run1);
footnote1.Append(paragraph1);
Footnote footnote2 = new Footnote() { Type = FootnoteEndnoteValues.ContinuationSeparator, Id = 0 };
Paragraph paragraph2 = new Paragraph() { RsidParagraphAddition = "003F1A60", RsidParagraphProperties = "00626544", RsidRunAdditionDefault = "003F1A60" };
Run run2 = new Run();
ContinuationSeparatorMark continuationSeparatorMark1 = new ContinuationSeparatorMark();
run2.Append(continuationSeparatorMark1);
paragraph2.Append(run2);
footnote2.Append(paragraph2);
Footnote footnote3 = new Footnote() { Id = 1 };
Paragraph paragraph3 = new Paragraph() { RsidParagraphMarkRevision = "009774CC", RsidParagraphAddition = "00626544", RsidParagraphProperties = "00626544", RsidRunAdditionDefault = "00626544" };
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
RunFonts runFonts1 = new RunFonts() { AsciiTheme = ThemeFontValues.MinorHighAnsi, HighAnsiTheme = ThemeFontValues.MinorHighAnsi, ComplexScriptTheme = ThemeFontValues.MinorHighAnsi };
FontSize fontSize1 = new FontSize() { Val = "24" };
FontSizeComplexScript fontSizeComplexScript1 = new FontSizeComplexScript() { Val = "24" };
paragraphMarkRunProperties1.Append(runFonts1);
paragraphMarkRunProperties1.Append(fontSize1);
paragraphMarkRunProperties1.Append(fontSizeComplexScript1);
paragraphProperties1.Append(paragraphMarkRunProperties1);
Run run3 = new Run() { RsidRunProperties = "009774CC" };
RunProperties runProperties1 = new RunProperties();
RunFonts runFonts2 = new RunFonts() { AsciiTheme = ThemeFontValues.MinorHighAnsi, HighAnsiTheme = ThemeFontValues.MinorHighAnsi, ComplexScriptTheme = ThemeFontValues.MinorHighAnsi };
FontSize fontSize2 = new FontSize() { Val = "24" };
FontSizeComplexScript fontSizeComplexScript2 = new FontSizeComplexScript() { Val = "24" };
VerticalTextAlignment verticalTextAlignment1 = new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript };
runProperties1.Append(runFonts2);
runProperties1.Append(fontSize2);
runProperties1.Append(fontSizeComplexScript2);
runProperties1.Append(verticalTextAlignment1);
Text text1 = new Text();
text1.Text = "1";
run3.Append(runProperties1);
run3.Append(text1);
Run run4 = new Run() { RsidRunProperties = "009774CC" };
RunProperties runProperties2 = new RunProperties();
RunFonts runFonts3 = new RunFonts() { AsciiTheme = ThemeFontValues.MinorHighAnsi, HighAnsiTheme = ThemeFontValues.MinorHighAnsi, ComplexScriptTheme = ThemeFontValues.MinorHighAnsi };
runProperties2.Append(runFonts3);
Text text2 = new Text() { Space = SpaceProcessingModeValues.Preserve };
text2.Text = " ";
run4.Append(runProperties2);
run4.Append(text2);
Run run5 = new Run() { RsidRunProperties = "009774CC", RsidRunAddition = "009774CC" };
RunProperties runProperties3 = new RunProperties();
RunFonts runFonts4 = new RunFonts() { AsciiTheme = ThemeFontValues.MinorHighAnsi, HighAnsiTheme = ThemeFontValues.MinorHighAnsi, ComplexScriptTheme = ThemeFontValues.MinorHighAnsi };
FontSize fontSize3 = new FontSize() { Val = "21" };
FontSizeComplexScript fontSizeComplexScript3 = new FontSizeComplexScript() { Val = "21" };
runProperties3.Append(runFonts4);
runProperties3.Append(fontSize3);
runProperties3.Append(fontSizeComplexScript3);
Text text3 = new Text();
text3.Text = "This is the foot note text";
run5.Append(runProperties3);
run5.Append(text3);
paragraph3.Append(paragraphProperties1);
paragraph3.Append(run3);
paragraph3.Append(run4);
paragraph3.Append(run5);
footnote3.Append(paragraph3);
footnotes1.Append(footnote1);
footnotes1.Append(footnote2);
footnotes1.Append(footnote3);
part.Footnotes = footnotes1;
}
I managed to add footnotes using the following code.
I hope this helps.
var ms = new MemoryStream();
using (WordprocessingDocument myDoc = WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
var footPart = mainPart.AddNewPart<FootnotesPart>();
footPart.Footnotes = new Footnotes();
mainPart.Document = new Document();
Body body = new Body();
var p = new Paragraph();
var r = new Run();
var t = new Text("123");
r.Append(t);
p.Append(r);
// ADD THE FOOTNOTE
var footnote = new Footnote();
footnote.Id = 1;
var p2 = new Paragraph();
var r2 = new Run();
var t2 = new Text();
t2.Text = "My FootNote Content";
r2.Append(t2);
p2.Append(r2);
footnote.Append(p2);
footPart.Footnotes.Append(footnote);
// ADD THE FOOTNOTE REFERENCE
var fref = new FootnoteReference();
fref.Id = 1;
var r3 = new Run();
r3.RunProperties = new RunProperties();
var s3 = new VerticalTextAlignment();
s3.Val = VerticalPositionValues.Superscript;
r3.RunProperties.Append(s3);
r3.Append(fref);
p.Append(r3);
body.Append(p);
mainPart.Document.Append(body);
mainPart.Document.Save();
ms.Flush();
}
return ms.ToArray();
I'm not an expert on OpenXML but I think you're missing adding an actual reference to the footnote in the body text, I tweaked your code like this and I now get the footnote in the document. Hope it helps or at least gets you started.
Run run = Para.AppendChild(new Run());
run.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Text("This is main text of the document"));
var footnoteref = new FootnoteReference() { Id = 1 };
run.Append(footnoteref);

How can I show labels on sharpmap using PostgreSQL in c#?

VectorLayer v = new VectorLayer("Point", new PostGIS("server=localhost;port=5433;user=postgres;pwd=9839757437;database=OSM3", "GhanaRegions", "Geometry", "id"));
VectorLayer v1 = new VectorLayer("Point", new PostGIS("server=localhost;port=5433;user=postgres;pwd=9839757437;database=OSM3", "ghanalocation", "geometry", "id"));
v.Style.EnableOutline = true;
v.Style.Outline = Pens.Black;
v.Style.Fill = Brushes.Red;
v1.Style.PointColor = Brushes.White;
SharpMap.Layers.LabelLayer layLabel = new SharpMap.Layers.LabelLayer("Name");
layLabel.DataSource = new PostGIS("server=localhost;port=5433;user=postgres;pwd=9839757437;database=OSM3", "citie33", "geometry", "id");
layLabel.Enabled = true;
layLabel.LabelColumn = "Name";
layLabel.MaxVisible = 2;
layLabel.MaxVisible = 190;
layLabel.MinVisible = 130;
layLabel.MultipartGeometryBehaviour = SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest;
layLabel.LabelFilter = SharpMap.Rendering.LabelCollisionDetection.ThoroughCollisionDetection;
layLabel.PriorityColumn = "id";
layLabel.Style.ForeColor = Color.Beige;
layLabel.Style.Font = new Font(FontFamily.GenericSerif, 12);
layLabel.Style.BackColor = new System.Drawing.SolidBrush(Color.FromArgb(128, 255, 0, 0));
layLabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Center;
// map.Layers.Add(v1);
map.Layers.Add(v);
layLabel.Style.CollisionDetection = true;
map.ZoomToExtents();
pictureBox1.Image = map.GetMap();

how to add footer with images in word document in .net

I am trying top create a word file with the header and footer and footer must have a Image can any one help me to do that.
I am using Open XML to create word file .
You can try this. You need to create a DocumentResource (Item->Add new) and associate an image.
private static Footer BuildFooter(FooterPart hp, string title)
{
ImagePart ip = hp.AddImagePart(ImagePartType.Jpeg);
string imageRelationshipID = hp.GetIdOfPart(ip);
using (Stream imgStream = ip.GetStream())
{
System.Drawing.Bitmap logo = DocumentResources._default;
logo.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
Footer h = new Footer();
DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph();
Run r = new Run();
Drawing drawing = BuildImage(imageRelationshipID, "_default.jpg", 200, 30);
r.Append(drawing);
p.Append(r);
r = new Run();
RunProperties rPr = new RunProperties();
TabChar tab = new TabChar();
Bold b = new Bold();
Color color = new Color { Val = "000000" };
DocumentFormat.OpenXml.Wordprocessing.FontSize sz = new DocumentFormat.OpenXml.Wordprocessing.FontSize {Val = Convert.ToString("40")};
Text t = new Text { Text = title };
rPr.Append(b);
rPr.Append(color);
rPr.Append(sz);
r.Append(rPr);
r.Append(tab);
r.Append(t);
p.Append(r);
h.Append(p);
return h;
}
and to build image
private static Drawing BuildImage(string imageRelationshipID, string imageName, int pixelWidth, int pixelHeight)
{
int emuWidth = (int)(pixelWidth * EMU_PER_PIXEL);
int emuHeight = (int)(pixelHeight * EMU_PER_PIXEL);
Drawing drawing = new Drawing();
d.Wordprocessing.Inline inline = new d.Wordprocessing.Inline { DistanceFromTop = 0, DistanceFromBottom = 0, DistanceFromLeft = 0, DistanceFromRight = 0 };
d.Wordprocessing.Anchor anchor = new d.Wordprocessing.Anchor();
d.Wordprocessing.SimplePosition simplePos = new d.Wordprocessing.SimplePosition { X = 0, Y = 0 };
d.Wordprocessing.Extent extent = new d.Wordprocessing.Extent { Cx = emuWidth, Cy = emuHeight };
d.Wordprocessing.DocProperties docPr = new d.Wordprocessing.DocProperties { Id = 1, Name = imageName };
d.Graphic graphic = new d.Graphic();
d.GraphicData graphicData = new d.GraphicData { Uri = GRAPHIC_DATA_URI };
d.Pictures.Picture pic = new d.Pictures.Picture();
d.Pictures.NonVisualPictureProperties nvPicPr = new d.Pictures.NonVisualPictureProperties();
d.Pictures.NonVisualDrawingProperties cNvPr = new d.Pictures.NonVisualDrawingProperties { Id = 2, Name = imageName };
d.Pictures.NonVisualPictureDrawingProperties cNvPicPr = new d.Pictures.NonVisualPictureDrawingProperties();
d.Pictures.BlipFill blipFill = new d.Pictures.BlipFill();
d.Blip blip = new d.Blip { Embed = imageRelationshipID };
d.Stretch stretch = new d.Stretch();
d.FillRectangle fillRect = new d.FillRectangle();
d.Pictures.ShapeProperties spPr = new d.Pictures.ShapeProperties();
d.Transform2D xfrm = new d.Transform2D();
d.Offset off = new d.Offset { X = 0, Y = 0 };
d.Extents ext = new d.Extents { Cx = emuWidth, Cy = emuHeight };
d.PresetGeometry prstGeom = new d.PresetGeometry { Preset = d.ShapeTypeValues.Rectangle };
d.AdjustValueList avLst = new d.AdjustValueList();
xfrm.Append(off);
xfrm.Append(ext);
prstGeom.Append(avLst);
stretch.Append(fillRect);
spPr.Append(xfrm);
spPr.Append(prstGeom);
blipFill.Append(blip);
blipFill.Append(stretch);
nvPicPr.Append(cNvPr);
nvPicPr.Append(cNvPicPr);
pic.Append(nvPicPr);
pic.Append(blipFill);
pic.Append(spPr);
graphicData.Append(pic);
graphic.Append(graphicData);
inline.Append(extent);
inline.Append(docPr);
inline.Append(graphic);
drawing.Append(inline);
return drawing;
}
To call those functions, in your function that build the document
FooterPart hp = mp.AddNewPart<FooterPart>();
string headerRelationshipID = mp.GetIdOfPart(hp);
SectionProperties sectPr = new SectionProperties();
FooterReference footerReference = new FooterReference();
FooterReference.Id = footerRelationshipID;
FooterReference.Type = HeaderFooterValues.Default;
sectPr.Append(footerReference);
b.Append(sectPr);
d.Append(b);
hp.Footer = BuildFooter(hp, "My Test");

Categories