cannot preserve space between runs - space

i want to generate a word document
as an input i have this string "open packaging conventions" and each word will have a different style
the result should be open packaging conventions
WordprocessingDocument document = WordprocessingDocument.Create(
#"C:\test PFE.docx",
WordprocessingDocumentType.Document
);
MainDocumentPart mainDocumentPart = document.AddMainDocumentPart();
mainDocumentPart.Document = new Document();
mainDocumentPart.Document.AddNamespaceDeclaration("ve", "http://schemas.openxmlformats.org/markup-compatibility/2006");
mainDocumentPart.Document.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
mainDocumentPart.Document.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
mainDocumentPart.Document.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
mainDocumentPart.Document.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
mainDocumentPart.Document.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
mainDocumentPart.Document.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
mainDocumentPart.Document.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
mainDocumentPart.Document.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
Body documentBody = new Body();
mainDocumentPart.Document.Append(documentBody);
StyleDefinitionsPart styleDefinitionsPart =
mainDocumentPart.AddNewPart<StyleDefinitionsPart>();
FileStream stylesTemplate =
new FileStream("styles.xml", FileMode.Open, FileAccess.Read);
styleDefinitionsPart.FeedData(stylesTemplate);
styleDefinitionsPart.Styles.Save();
#region Titre du document
Paragraph titleParagraphe = new Paragraph() { RsidParagraphAddition = "00AF4948", RsidParagraphProperties = "00625634", RsidRunAdditionDefault = "00625634" }; ;
Run run = new Run();
RunProperties rpr = new RunProperties();
RunStyle rstylr = new RunStyle { Val = "style1" };
run.Append(rpr);
Text t = new Text("open");
run.Append(t);
titleParagraphe.Append(run);
run = new Run();
rpr = new RunProperties();
rstylr = new RunStyle { Val = "style2" };
run.Append(rpr);
t = new Text("packaging")
{
Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" }
};
run.Append(t);
titleParagraphe.Append(run);
run = new Run();
rpr = new RunProperties();
rstylr = new RunStyle { Val = "style1" };
run.Append(rpr);
t = new Text("conventions")
{
Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues> { InnerText = "preserve" }
};
run.Append(t);
titleParagraphe.Append(run);
documentBody.Append(titleParagraphe);
document.MainDocumentPart.Document.Save();
document.Dispose();
and the result is open*packaging*conventions without space between words
can some one help me please?!

You're on good way by handling the Space property, but you need to do it like this:
t = new Text()
{
Text = "your text with spaces ",
Space = SpaceProcessingModeValues.Preserve
};

Here is another way to set the attribute Space that can be used to specify SpaceProcessingMode.
t = new Text("This is some text");
t.Space = SpaceProcessingModeValues.Preserve;
The default of the attribute is SpaceProcessingModeValues.Default.
From API Documentation:
<w:r>
<w:t> significant whitespace </w:t>
</w:r>
Although there are three spaces on each side of the text content in the run, that whitespace has not been specifically marked as significant, therefore it is subject to the space preservation rules currently specified in that run's scope. end example]
The possible values for this attribute are defined by ยง2.10 of the XML 1.0 specification.

Related

Display text in new line textarea in MVC razor

i have a textarea in my mvc application when the user save it for example
hi
how are
you
but when user want to edit that textarea it shows
hi how are you
but i want in that way
hi
how are
you
I have tried
#Html.TextAreaFor(model => model.Notes, new { #class = "form-control", placeholder = "Name", required = "required", #rows = 5 })
To replace each whitespace with a new line character
Edit the ActionResult so that when you save, you replace each " " with Environment.NewLine
like so:
NoteManager.Save(note.Replace(" ", Environment.NewLine));
EDIT
To replace 1st and last space with newline:
// set string
string s = "hi how are you";
// make sure that there are no trailing spaces otherwise our code will not work
s = s.TrimEnd(' ');
// remove 1st space
// get index
int index = s.IndexOf(" ");
// remove space
s = s.Remove(index, 1);
// replace with newline
s = s.Insert(index, Environment.NewLine);
// remove last space
// get index
index = s.LastIndexOf(" ");
// remove space
s = s.Remove(index, 1);
// replace with newline
s = s.Insert(index, Environment.NewLine);
Console.Write(s);
Fiddle: https://dotnetfiddle.net/kg7kcf

Can not add a list in document object using iTextSharp

public ActionResult PartTimeFacultyCourseLoadReport()
{
var teacherStatistics = (from t in db.Teachers
join c in db.Courses
on t.Id equals c.TeacherId into cGroup
where t.Status == "Part Time"
orderby t.Designation descending
select new
{
TeacherInfo = t,
CourseInfo = from cg in cGroup
orderby cg.Code ascending
select cg
}).ToList();
List<TeacherStatistics> teacherStatisticses = new List<TeacherStatistics>();
int count = 0;
foreach (var teacherStatistic in teacherStatistics)
{
TeacherStatistics aTeacherStatistics = new TeacherStatistics();
aTeacherStatistics.Name = teacherStatistic.TeacherInfo.Name;
aTeacherStatistics.Designation = teacherStatistic.TeacherInfo.Designation;
aTeacherStatistics.NumberOfCourse = teacherStatistic.TeacherInfo.NumberOfCourse;
count = 0;
foreach (var courseInfo in teacherStatistic.CourseInfo)
{
if (count != 0)
{
aTeacherStatistics.Courses += ", ";
}
aTeacherStatistics.Courses += courseInfo.Code;
aTeacherStatistics.Courses += "(";
aTeacherStatistics.Courses += courseInfo.Section;
aTeacherStatistics.Courses += ")";
count++;
}
teacherStatisticses.Add(aTeacherStatistics);
}
var document = new Document(PageSize.A4, 50, 50, 25, 25);
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
document.Open();
var data = teacherStatisticses.ToList();
document.Add(data);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=PartTimeFaculty.pdf");
Response.BinaryWrite(output.ToArray());
document.Close();
return View(teacherStatisticses);
}
I want to pass a list named 'teacherStatisticses' through document object for making a PDF. My code doesn't work. It showed me following Error -
Argument 1: cannot convert from 'System.Collections.Generic.List' to 'iTextSharp.text.IElement'
I assume that you are using some version of itextpdf for the PDF generation.
The error is in the line:
document.Add(data);
There is no way to add plain .NET objects into the PDF document. I can not predict the structure of PDF which you`d like to achieve, but the code mentioned above could be written as:
foreach(var teacher in teacherStatistics)
{
var paragraph = new Paragraph(teacher.ToString()); // instead of teacher.ToString() should be some code which translates teacherStatistics projection to the string representation
document.Add(paragraph);
}
//not tested
A lot of useful samples could be found at
http://developers.itextpdf.com/examples
Basic tutorial for the itextpdf:
http://developers.itextpdf.com/content/itext-7-jump-start-tutorial/chapter-1-introducing-basic-building-blocks

Set new placement in Group Criterion via API

How can I add new placement (website, for example www.example.com) in AdGroup using AdWords API?
I have found
AdGroupCriterionService->mutate,
but I need to know ID of website
$adGroupCriterion->criterion = new Criterion($criterionId);
So my question:
How I can found ID for any website or exist another way to add new Placement in AdGroup?
I found an answer:
$adGroupService = $user->GetService('AdGroupCriterionService', 'v201509');
$AdGroupCriterionOperation = new AdGroupCriterionOperation();
$AdGroupCriterionOperation->operator = 'ADD';
$AdGroupCriterion = new BiddableAdGroupCriterion();
$AdGroupCriterion->adGroupId = $adGroupId;
$AdGroupCriterion->criterionUse = 'BIDDABLE';
$AdGroupCriterion->AdGroupCriterionType = 'PLACEMENT';
$Placement = new Placement();
$Placement->id = null;
$Placement->CriterionType = 'PLACEMENT';
$Placement->type = 'PLACEMENT';
$Placement->url = $url;
$AdGroupCriterion->criterion = $Placement;
$AdGroupCriterionOperation->operand = $AdGroupCriterion;
$operations = array($AdGroupCriterionOperation);
// Make the mutate request.
$result = $adGroupService->mutate($operations);

FedEx Webservice for printing Multiple shipping labels

I am trying to connect to the FedEx shipping webservice v8.
Everything works fine when I only have one RequestedPackageLineItems set. When I add two items I get the following error.
"Invalid package count or invalid package sequence number."
My Code is as follows
ProcessShipmentRequest request = CreatePendingShipmentRequest();
ShipService service = new ShipService();
ProcessShipmentReply reply = service.processShipment(request);
...
private static ProcessShipmentRequest CreatePendingShipmentRequest()
{
ProcessShipmentRequest request = new ProcessShipmentRequest();
request.WebAuthenticationDetail = new WebAuthenticationDetail();
request.WebAuthenticationDetail.UserCredential = new WebAuthenticationCredential();
request.WebAuthenticationDetail.UserCredential.Key = "XXX";
request.WebAuthenticationDetail.UserCredential.Password = "XXX";
request.ClientDetail = new ClientDetail();
request.ClientDetail.AccountNumber = "XXX";
request.ClientDetail.MeterNumber = "XXX";
request.TransactionDetail = new TransactionDetail();
request.TransactionDetail.CustomerTransactionId = "*** Ground Domestic Shipping Request v8 using C# ***";
request.Version = new VersionId();
//Inside this method I set request.RequestedShipment.PackageCount = "2";
SetShipmentDetails(request);
SetPackageLineItems(request);
return request;
}
private static void SetPackageLineItems(ProcessShipmentRequest request)
{
request.RequestedShipment.RequestedPackageLineItems = new RequestedPackageLineItem[2];
request.RequestedShipment.RequestedPackageLineItems[0] = new RequestedPackageLineItem();
request.RequestedShipment.RequestedPackageLineItems[0].SequenceNumber = "1";
request.RequestedShipment.RequestedPackageLineItems[0].Weight = new Weight();
request.RequestedShipment.RequestedPackageLineItems[0].Weight.Value = 50.0M;
request.RequestedShipment.RequestedPackageLineItems[0].Weight.Units = WeightUnits.LB;
request.RequestedShipment.RequestedPackageLineItems[0].ItemDescription = "Item";
request.RequestedShipment.RequestedPackageLineItems[0].Dimensions = new Dimensions();
request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Length = "108";
request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Width = "5";
request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Height = "5";
request.RequestedShipment.RequestedPackageLineItems[0].Dimensions.Units = LinearUnits.IN;
request.RequestedShipment.RequestedPackageLineItems[0].CustomerReferences = new CustomerReference[3];
request.RequestedShipment.RequestedPackageLineItems[0].CustomerReferences[0] = new CustomerReference();
request.RequestedShipment.RequestedPackageLineItems[0].CustomerReferences[0].CustomerReferenceType = CustomerReferenceType.CUSTOMER_REFERENCE;
request.RequestedShipment.RequestedPackageLineItems[0].CustomerReferences[0].Value = "[LOT NUMBER]";
request.RequestedShipment.RequestedPackageLineItems[0].CustomerReferences[1] = new CustomerReference();
request.RequestedShipment.RequestedPackageLineItems[0].CustomerReferences[1].CustomerReferenceType = CustomerReferenceType.INVOICE_NUMBER;
request.RequestedShipment.RequestedPackageLineItems[0].CustomerReferences[1].Value = "45646";
request.RequestedShipment.RequestedPackageLineItems[0].CustomerReferences[2] = new CustomerReference();
request.RequestedShipment.RequestedPackageLineItems[0].CustomerReferences[2].CustomerReferenceType = CustomerReferenceType.P_O_NUMBER;
request.RequestedShipment.RequestedPackageLineItems[0].CustomerReferences[2].Value = "456446";
request.RequestedShipment.RequestedPackageLineItems[1] = new RequestedPackageLineItem();
request.RequestedShipment.RequestedPackageLineItems[1].SequenceNumber = "2";
request.RequestedShipment.RequestedPackageLineItems[1].Weight = new Weight();
request.RequestedShipment.RequestedPackageLineItems[1].Weight.Value = 50.0M;
request.RequestedShipment.RequestedPackageLineItems[1].Weight.Units = WeightUnits.LB;
request.RequestedShipment.RequestedPackageLineItems[1].ItemDescription = "Item";
....
}
Found out how to do this.
In order to get multiple shipping labels into one pdf the process is as follows.
Create a request
Fill in the shipping info
Post and get a reply.
Save that replay and the byte array and MasterShippingID
Create a new request and assign the master shipping id to it.
Added shipping weight and dimensions
Post and get reply
Save byte array with the other
Continue until all shipments are generated (max 200 per master shipping id)
Merge all the pdfs returned from FedEx into one pdf.
Do a happy dance.
A simpler approach, (though there is a cost) is the Shiprush SDK. It lets you build a single XML block and let ShipRush do all the funny stuff with fedex (or whoever).
They also support their tool.

asp.net Chart Controls on a user control in MVC

I am new to the MVC Framework. Im working on a dashboard project in the MVC framework. The project consists of a bunch of charting control in a user controls contained in a master page. I did a test on a charting control on a aspx page..and it works...but when I moved the code to a ascx (usercontrol) the chart doesnt render. Any ideas?!?!?!...I'm stuck. Thanks in advance
Jeff
Code that is in in the .aspx
<%
System.Web.UI.DataVisualization.Charting.Chart Chart1 = new System.Web.UI.DataVisualization.Charting.Chart();
Chart1.Width = 450;
Chart1.Height = 296;
Chart1.RenderType = RenderType.ImageTag;
Chart1.ImageLocation = "..\\..\\TempImages\\ChartPic_#SEQ(200,30)";
Chart1.Palette = ChartColorPalette.BrightPastel;
Title t = new Title("Program Pipeline", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
Chart1.Titles.Add(t);
Chart1.ChartAreas.Add("Prog 1");
// create a couple of series
Chart1.Series.Add("Backlog");
Chart1.Series.Add("Constructed");
Chart1.Series.Add("Billed");
Chart1.Series.Add("BudgetUsed");
Chart1.Series.Add("Total");
Chart1.Series["Backlog"].ChartType = SeriesChartType.StackedBar100;
Chart1.Series["Constructed"].ChartType = SeriesChartType.StackedBar100;
Chart1.Series["Billed"].ChartType = SeriesChartType.StackedBar100;
Chart1.Series["Total"].ChartType = SeriesChartType.StackedBar100;
Chart1.Series["BudgetUsed"].ChartType = SeriesChartType.StackedBar100;
Chart1.Series["Backlog"]["DrawingStyle"] = "Cylinder";
Chart1.Series["Constructed"]["DrawingStyle"] = "Cylinder";
Chart1.Series["Billed"]["DrawingStyle"] = "Cylinder";
Chart1.Series["BudgetUsed"]["DrawingStyle"] = "Cylinder";
Chart1.Series["Total"]["DrawingStyle"] = "Cylinder";
// Bar Size
Chart1.Series["Backlog"]["PointWidth"] = "0.6";
Chart1.Series["Constructed"]["PointWidth"] = "0.6";
Chart1.Series["Billed"]["PointWidth"] = "0.6";
Chart1.Series["BudgetUsed"]["PointWidth"] = "0.6";
Chart1.Series["Total"]["PointWidth"] = "0.6";
int _total = 0;
int _newTotalAmt = 100 - _total;
foreach (MvcApplication1.Models.Amount obj in Model.GetTotalAmt("plm1"))
{
_total += obj.TotalAmount;
Chart1.Series[obj.PLMType].Points.AddY(obj.TotalAmount);
}
Chart1.Series["BudgetUsed"].Points.AddY(0);
Chart1.Series["Total"].Points.AddY(_newTotalAmt);
_total = 0;
_newTotalAmt = 100 - _total;
foreach (MvcApplication1.Models.Amount obj in Model.GetTotalAmtForPLM2("plm2"))
{
_total += obj.TotalAmount;
Chart1.Series[obj.PLMType].Points.AddY(obj.TotalAmount);
}
Chart1.Series["BudgetUsed"].Points.AddY(0);
Chart1.Series["Total"].Points.AddY(_newTotalAmt);
_total = 0;
_newTotalAmt = 100 - _total;
foreach (MvcApplication1.Models.Amount obj in Model.GetTotalAmt("plm3"))
{
_total += obj.TotalAmount;
Chart1.Series[obj.PLMType].Points.AddY(obj.TotalAmount);
}
Chart1.Series["BudgetUsed"].Points.AddY(0);
Chart1.Series["Total"].Points.AddY(_newTotalAmt);
// MvcApplication1.Models.TotalPOAmount oTotal = Model.GetOverAllBudget();
// add points to series 3
Chart1.Series["Billed"].Points.AddY(0);
Chart1.Series["Constructed"].Points.AddY(0);
Chart1.Series["Backlog"].Points.AddY(0);
Chart1.Series["BudgetUsed"].Points.AddY(39);
Chart1.Series["Total"].Points.AddY(100);
Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
Chart1.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
Chart1.BorderlineDashStyle = ChartDashStyle.Solid;
Chart1.BorderWidth = 2;
Chart1.Legends.Add("Legend");
// show legend based on check box value
// Chart1.Legends["Legend1"].Enabled = ShowLegend.Checked;
// Render chart control
Chart1.Page = this;
HtmlTextWriter writer = new HtmlTextWriter(Page.Response.Output);
Chart1.RenderControl(writer);
//IList<SelectListItem> list = new List<SelectListItem>();
//SelectListItem sli = new SelectListItem();
//sli.Text = "test1";
//sli.Value = "1";
//list.Add(sli);
//ViewData["Test"] = list;
%>
I've had exactly the same issue. My problem was to do with the paths to the image file. The chart control was getting it wrong when placed on a usercontrol. If I changed the chart to use Imagestoragemode of HttpHandler then it worked as intended.
unfortunatly this stopped me being able to unit test my views. In the end I put the chart control on an aspx page & then used jQuery to load it when needed. (Luckily my dashboard page used javascript to load the contents of the portlets)
I've just been trying to get round what seems to be the same problem. When I moved the code (similar to yours above) to a UserControl the System.Web.UI.DataVisualization namespace wasn't recognised and I received the error:
The type or namespace name
'DataVisualization' does not exist in
the namespace 'System.Web.UI' (are you
missing an assembly reference?)
The namespace only seemed to be recognised when the Chart code lay within an asp control (in the aspx page it was within an <asp:Content> control). So I put the Chart code within an <asp:Panel> control and it worked.

Resources