Adding Left Border line in itextsharp - pdf-generation

I was facing issue to work on rowspan but with some code in C#, I was able to achieve that. Currently I have the data what I needed.
How can I draw left border line for the first column so that it looks correct.
here is what I have now.
I need to add left border line.
here is my code:
{
ReportTableCell cell = null;
// prevent null values from crashing the system (always a good idea)
if (text == null)
{
text = string.Empty;
}
// else already valid
ReportTable.CreateReportTableCell(text, out cell);
cell.RowSpan = row_span;
cell.ColumnSpan = column_span;
cell.Style.WrapText = true;
cell.Style.BorderWidth = border_width;
// cell.Style.BorderColor = CellBorderColor.Gray;
if ( repeated)
{
// cell.Style.Border = BOTTOM_BORDER;
cell.Style.BorderWidth = border_width;
}
else
{
cell.Style.BorderWidth = 0;
repeated = true;
}
cell.Style.Font.FontStyle = font_style;
cell.Style.Font.FontSize = font_size;
cell.Style.BackgroundColor = background_color;
cell.Style.IsColumnHeader = is_column_header;
cell.Style.HorizontalAlignment = horizontal_alignment;
if (is_column_header)
{
cell.Style.VerticalAlignment = VerticalCellAlignment.Middle;
}
else
{
cell.Style.VerticalAlignment = VerticalCellAlignment.Top;
}
repeated = true;
return cell;
}
based on the condition i.e.
if ( repeated){}
I am removing border, which I know it has removed border around 4 corners, how can i append an extra line only for the left side.
UPDATE : 1
after using the code as below :
if ( repeated)
{
cell.Style.BorderWidth = border_width;
}
else
{
cell.Style.BorderWidth = 0;
cell.Style.Border = BOTTOM_BORDER | LEFT_BORDER;
repeated = true;
}
changed nothing!.
Here is the base class of the report.
public class ReportTableCell
{
public static readonly int ALL_BORDER;
public static readonly int BOTTOM_BORDER;
public static readonly int LEFT_BORDER;
public static readonly int NO_BORDER;
public static readonly int RIGHT_BORDER;
public static readonly int TOP_BORDER;
public bool AllowBlanks { get; set; }
public int ColumnSpan { get; set; }
public ReportFont Font { get; set; }
public int RowSpan { get; set; }
public TableCellStyle Style { get; set; }
public ReportTable Table { get; set; }
public object Value { get; set; }
public void AddImage(string FileName);
}
UPDATE : 2
I have changed the code as you suggested but , I just have to add single border line to the last. this is what i achieved till now :

Related

xamarin forms dynamic image tapgesture recognizer control

How can I provide control (change) of my dynamic images created in xamarin forms by clicking?
Image ggImage = new Image()
ggImage.Source = otoTip.imagex
ggImage.AutomationId = "seat_" +otoTip.num
ggImage.WidthRequest = imageWidth
ggImage.HeightRequest = 50
ggImage.VerticalOptions = LayoutOptions.Start
ggImage.HorizontalOptions = LayoutOptions.Start
ggImage.GestureRecognizers.Add(tapGestureRecognizer)
For example, I just want to change a resume source.
You have to implement a TapGestureRecognizer and then add it to your image...
Image ggImage = new Image();
ggImage.Source = otoTip.imagex
ggImage.AutomationId = "seat_" +otoTip.num
ggImage.WidthRequest = imageWidth
ggImage.HeightRequest = 50;
ggImage.VerticalOptions = LayoutOptions.Start;
ggImage.HorizontalOptions = LayoutOptions.Start;
// your TapGestureRecognizer implementation, this is just a sample...
var tapGestureRecognizer = new TapGestureRecognizer();
tapGestureRecognizer.Tapped += (s, e) => { ggImage.Source = yourNewSource };
ggImage.GestureRecognizers.Add(tapGestureRecognizer);
In order to change every unique image of your dynamically created images you could extend your OtoTip class and
public class OtoTip : INotifyPropertyChanged
{
public int x { get; set; }
public int y { get; set; }
public int num { get; set; }
public int near { get; set; }
public int status { get; set; }
public int yy { get; set; }
public int xx { get; set; }
// xx
private string _imagex;
public string imagex
{
get { return _imagex; }
set
{
_imagex = value;
OnPropertyChanged(nameof(imagex));
}
}
private string _imageActivex;
public string imageActivex
{
get { return _imageActivex; }
set
{
_imageActivex = value;
OnPropertyChanged(nameof(imageActivex));
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
You have to extend your getVoyagesData() now, to also set the imageActivex like your standard image.
Now you have to adjust the TapGestureRecognizer to use the new property
tapGestureRecognizer.Tapped += (s, e) =>
{
if(ggImage.Source = otoTip.imagex) {
ggImage.Source = otoTip.imageActivex;
}
else
{
ggImage.Source = otoTip.imagex;
}
};
Does this help?

Xamarin.Forms MVVM WebView not Loading

We use the Xamarin.Forms WebView to display content of a certain object, but this content won't be displayed in the WebView until we rotate the Device twice.
The .xmal-File Code:
<WebView x:Name="WebView" HeightRequest="{Binding Height}" WidthRequest="{Binding Width}">
<WebView.Source>
<HtmlWebViewSource Html="{Binding Code}"/>
</WebView.Source>
</WebView>
And the ModelView:
private double _height;
private double _width;
public ICommand ItemTappedCommand { get; private set; }
public string SearchBarLabelText { get; private set; }
public object LastTappedItem { get; set; }
public int ColumnCount { get; private set; } = 2;
public string CodeName { get; private set; }
public string CodeContent { get; private set; }
public string Code { get; private set; }
public double Height
{
get => _height;
private set
{
if (_height == value)
return;
_height = value;
OnPropertyChanged(nameof(Height));
}
}
public double Width
{
get => _width;
private set
{
if (_width == value)
return;
_width = value;
OnPropertyChanged(nameof(Width));
}
}
public ErrorcodeDetailPageViewModel(Errorcode code)
{
Device.Info.PropertyChanged += DevicePropertyChanged;
DevicePropertyChanged(null, null);
Code = code.Content;
CodeName = code.Label;
CodeContent = code.Content;
}
private void DevicePropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (Device.Info.CurrentOrientation)
{
case DeviceOrientation.Portrait:
Height = Device.Info.PixelScreenSize.Height - 150; // 120 is the Height of the ControlTemplate used on this Page + the top row on uwp; only required in portrait mode
Width = Device.Info.PixelScreenSize.Width;
break;
case DeviceOrientation.Landscape:
Height = Device.Info.PixelScreenSize.Height - 50;
Width = Device.Info.PixelScreenSize.Width;
break;
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
So, als already described, if we rotate the Device twice the content will be displayed, otherwise it won't. I think the problem is related to our bindings...
I should mention that we have some kind ob banner/headline on top of the WebView.
Code should also have a backing notification so that the view is aware of when its value changes.
Like
private string code;
public string Code {
get => code;
private set {
if (code == value)
return;
code = value;
OnPropertyChanged(nameof(Code));
}
}
If you walk through the step of why you have to turn twice, you will see that is why you have to do that.
When the value is first set it is blank.
On first turn the value is set by the event handler not since no notification is sent to the view it is not visibly changed.
On second turn the value is already set and when the view redraws, the value is shown in the view.

How to send complex objects in GET to WEB API 2

Let's say that you have the following code
public class MyClass {
public double Latitude {get; set;}
public double Longitude {get; set;}
}
public class Criteria
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public MyClass MyProp {get; set;}
}
[HttpGet]
public Criteria Get([FromUri] Criteria c)
{
return c;
}
I'd like to know if someone is aware of a library that could transform any object into query string that is understood by a WEB API 2 Controller.
Here is an example of what I'd like
SerializeToQueryString(new Criteria{StartDate=DateTime.Today, EndDate = DateTime.Today.AddDays(1), MyProp = new MyProp{Latitude=1, Longitude=3}});
=> "startDate=2015-10-13&endDate=2015-10-14&myProp.latitude=1&myProp.longitude=3"
A full example with httpClient might look like :
new HttpClient("http://localhost").GetAsync("/tmp?"+SerializeToQueryString(new Criteria{StartDate=DateTime.Today, EndDate = DateTime.Today.AddDays(1), MyProp = new MyProp{Latitude=1, Longitude=3}})).Result;
At the moment, I use a version (taken from a question I do not find again, maybe How do I serialize an object into query-string format? ...).
The problem is that it is not working for anything else than simple properties.
For example, calling ToString on a Date will not give something that is parseable by WEB API 2 controller...
private string SerializeToQueryString<T>(T aObject)
{
var query = HttpUtility.ParseQueryString(string.Empty);
var fields = typeof(T).GetProperties();
foreach (var field in fields)
{
string key = field.Name;
var value = field.GetValue(aObject);
if (value != null)
query[key] = value.ToString();
}
return query.ToString();
}
"Transform any object to a query string" seems to imply there's a standard format for this, and there just isn't. So you would need to pick one or roll your own. JSON seems like the obvious choice due to the availability of great libraries.
Since it seems no one has dealt with the problem before, here is the solution I use in my project :
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Web;
namespace App
{
public class QueryStringSerializer
{
public static string SerializeToQueryString(object aObject)
{
return SerializeToQueryString(aObject, "").ToString();
}
private static NameValueCollection SerializeToQueryString(object aObject, string prefix)
{
//!\ doing this to get back a HttpValueCollection which is an internal class
//we want a HttpValueCollection because toString on this class is what we want in the public method
//cf http://stackoverflow.com/a/17096289/1545567
var query = HttpUtility.ParseQueryString(String.Empty);
var fields = aObject.GetType().GetProperties();
foreach (var field in fields)
{
string key = string.IsNullOrEmpty(prefix) ? field.Name : prefix + "." + field.Name;
var value = field.GetValue(aObject);
if (value != null)
{
var propertyType = GetUnderlyingPropertyType(field.PropertyType);
if (IsSupportedType(propertyType))
{
query.Add(key, ToString(value));
}
else if (value is IEnumerable)
{
var enumerableValue = (IEnumerable) value;
foreach (var enumerableValueElement in enumerableValue)
{
if (IsSupportedType(GetUnderlyingPropertyType(enumerableValueElement.GetType())))
{
query.Add(key, ToString(enumerableValueElement));
}
else
{
//it seems that WEB API 2 Controllers are unable to deserialize collections of complex objects...
throw new Exception("can not use IEnumerable<T> where T is a class because it is not understood server side");
}
}
}
else
{
var subquery = SerializeToQueryString(value, key);
query.Add(subquery);
}
}
}
return query;
}
private static Type GetUnderlyingPropertyType(Type propType)
{
var nullablePropertyType = Nullable.GetUnderlyingType(propType);
return nullablePropertyType ?? propType;
}
private static bool IsSupportedType(Type propertyType)
{
return SUPPORTED_TYPES.Contains(propertyType) || propertyType.IsEnum;
}
private static readonly Type[] SUPPORTED_TYPES = new[]
{
typeof(DateTime),
typeof(string),
typeof(int),
typeof(long),
typeof(float),
typeof(double)
};
private static string ToString(object value)
{
if (value is DateTime)
{
var dateValue = (DateTime) value;
if (dateValue.Hour == 0 && dateValue.Minute == 0 && dateValue.Second == 0)
{
return dateValue.ToString("yyyy-MM-dd");
}
else
{
return dateValue.ToString("yyyy-MM-dd HH:mm:ss");
}
}
else if (value is float)
{
return ((float) value).ToString(CultureInfo.InvariantCulture);
}
else if (value is double)
{
return ((double)value).ToString(CultureInfo.InvariantCulture);
}
else /*int, long, string, ENUM*/
{
return value.ToString();
}
}
}
}
Here is the unit test to demonstrate :
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Framework.WebApi.Core.Tests
{
[TestClass]
public class QueryStringSerializerTest
{
public class EasyObject
{
public string MyString { get; set; }
public int? MyInt { get; set; }
public long? MyLong { get; set; }
public float? MyFloat { get; set; }
public double? MyDouble { get; set; }
}
[TestMethod]
public void TestEasyObject()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EasyObject(){MyString = "string", MyInt = 1, MyLong = 1L, MyFloat = 1.5F, MyDouble = 1.4});
Assert.IsTrue(queryString.Contains("MyString=string"));
Assert.IsTrue(queryString.Contains("MyInt=1"));
Assert.IsTrue(queryString.Contains("MyLong=1"));
Assert.IsTrue(queryString.Contains("MyFloat=1.5"));
Assert.IsTrue(queryString.Contains("MyDouble=1.4"));
}
[TestMethod]
public void TestEasyObjectNullable()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EasyObject() { });
Assert.IsTrue(queryString == "");
}
[TestMethod]
public void TestUrlEncoding()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EasyObject() { MyString = "&=/;+" });
Assert.IsTrue(queryString.Contains("MyString=%26%3d%2f%3b%2b"));
}
public class DateObject
{
public DateTime MyDate { get; set; }
}
[TestMethod]
public void TestDate()
{
var d = DateTime.ParseExact("2010-10-13", "yyyy-MM-dd", CultureInfo.InvariantCulture);
var queryString = QueryStringSerializer.SerializeToQueryString(new DateObject() { MyDate = d });
Assert.IsTrue(queryString.Contains("MyDate=2010-10-13"));
}
[TestMethod]
public void TestDateTime()
{
var d = DateTime.ParseExact("2010-10-13 20:00", "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
var queryString = QueryStringSerializer.SerializeToQueryString(new DateObject() { MyDate = d });
Assert.IsTrue(queryString.Contains("MyDate=2010-10-13+20%3a00%3a00"));
}
public class InnerComplexObject
{
public double Lat { get; set; }
public double Lon { get; set; }
}
public class ComplexObject
{
public InnerComplexObject Inner { get; set; }
}
[TestMethod]
public void TestComplexObject()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new ComplexObject() { Inner = new InnerComplexObject() {Lat = 50, Lon = 2} });
Assert.IsTrue(queryString.Contains("Inner.Lat=50"));
Assert.IsTrue(queryString.Contains("Inner.Lon=2"));
}
public class EnumerableObject
{
public IEnumerable<int> InnerInts { get; set; }
}
[TestMethod]
public void TestEnumerableObject()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EnumerableObject() {
InnerInts = new[] { 1,2 }
});
Assert.IsTrue(queryString.Contains("InnerInts=1"));
Assert.IsTrue(queryString.Contains("InnerInts=2"));
}
public class ComplexEnumerableObject
{
public IEnumerable<InnerComplexObject> Inners { get; set; }
}
[TestMethod]
public void TestComplexEnumerableObject()
{
try
{
QueryStringSerializer.SerializeToQueryString(new ComplexEnumerableObject()
{
Inners = new[]
{
new InnerComplexObject() {Lat = 50, Lon = 2},
new InnerComplexObject() {Lat = 51, Lon = 3},
}
});
Assert.Fail("we should refuse something that will not be understand by the server");
}
catch (Exception e)
{
Assert.AreEqual("can not use IEnumerable<T> where T is a class because it is not understood server side", e.Message);
}
}
public enum TheEnum : int
{
One = 1,
Two = 2
}
public class EnumObject
{
public TheEnum? MyEnum { get; set; }
}
[TestMethod]
public void TestEnum()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EnumObject() { MyEnum = TheEnum.Two});
Assert.IsTrue(queryString.Contains("MyEnum=Two"));
}
}
}
I'd like to thank all the participants even if this is not something that you should usually do in a Q&A format :)

Using ValueInjecter, is there a way to only inject a given property one time

Given 3 classes,
A, and B which each have an ID property, and then various other properties
and C, which has an ID, and the combined properties of A and B,
I want to
C.InjectFrom(A);
C.InjectFrom(B);
such that the ID from A is preserved and not overwritten by B.
Obviously in this simple case, I could just reverse the order of the two calls, but in my real world example, it is slightly more complicated where I cannot just solve the problem with ordering.
Esentially I want the second injection to ignore anything that the first injection has already handled, and this may be continued down a chain of several injections. Some of these injections may be from the same objects too
C.InjectFrom(A);
C.InjectFrom<SomeInjector>(A);
C.InjectFrom<SomeInjector2>(A);
C.InjectFrom<SomeInjector3>(A);
etc.
here you go:
using System;
using System.Collections.Generic;
using Omu.ValueInjecter;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var a = new { Id = 1, P1 = "p1" };
var b = new { Id = 2, P2 = "p2" };
var c = new C();
var propList = new List<string>();
c.InjectFrom(new HandlePropOnce(propList), a);
c.InjectFrom(new HandlePropOnce(propList), b);
Console.WriteLine("Id = {0} P1 = {1} P2 = {2}", c.Id, c.P1, c.P2);
}
}
public class C
{
public int Id { get; set; }
public string P1 { get; set; }
public string P2 { get; set; }
}
public class HandlePropOnce : ConventionInjection
{
private readonly IList<string> handledProps;
public HandlePropOnce(IList<string> handledProps)
{
this.handledProps = handledProps;
}
protected override bool Match(ConventionInfo c)
{
if (handledProps.Contains(c.SourceProp.Name)) return false;
var isMatch = c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Type == c.TargetProp.Type;
if (isMatch) handledProps.Add(c.SourceProp.Name);
return isMatch;
}
}
}

data mode : read write with c# local database in wp7

I created a local db with helper app project. and deployed it from isolate storage to installation folder,i added to project directory with content build action by add existing item. my problem is that i want to insert data, but i don't know how to move the db file to isolate storage to insert and data must add to my .sdf file that is locate in my project directory also.
Souphia,
While learning to use WP, I wrote a simple application that tracked tasks.
One version of that app stored all task data in Sql on the phone.
You can read the post and download all the code for the app here:
http://www.ritzcovan.com/2012/02/building-a-simple-windows-phone-app-part-3/
But, here is some of the code from that project:
First we have the model class decorated with the appropriate attributes:
[Table]
public class Task : INotifyPropertyChanged, INotifyPropertyChanging
{
[Column(IsDbGenerated = false, IsPrimaryKey = true, CanBeNull = false)]
public string Id
{
get { return _id; }
set
{
NotifyPropertyChanging("Id");
_id = value;
NotifyPropertyChanging("Id");
}
}
[Column]
public string Name
{
get { return _name; }
set
{
NotifyPropertyChanging("Name");
_name = value;
NotifyPropertyChanged("Name");
}
}
[Column]
public string Category
{
get { return _category; }
set
{
NotifyPropertyChanging("Category");
_category = value;
NotifyPropertyChanged("Category");
}
}
[Column]
public DateTime? DueDate
{
get { return _dueDate; }
set
{
NotifyPropertyChanging("DueDate");
_dueDate = value;
NotifyPropertyChanged("DueDate");
}
}
[Column]
public DateTime? CreateDate
{
get { return _createDate; }
set
{
NotifyPropertyChanging("CreateDate");
_createDate = value;
NotifyPropertyChanged("CreateDate");
}
}
[Column]
public bool IsComplete
{
get { return _isComplete; }
set
{
NotifyPropertyChanging("IsComplete");
_isComplete = value;
NotifyPropertyChanged("IsComplete");
}
}
[Column(IsVersion = true)] private Binary _version;
private string _id;
private bool _isComplete;
private DateTime? _createDate;
private DateTime? _dueDate;
private string _name;
private string _category;
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
public void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
public void NotifyPropertyChanging(string property)
{
if (PropertyChanging != null)
PropertyChanging(this, new PropertyChangingEventArgs(property));
}
}
In the constructor in app.xaml.cs, I have the following:
TaskMasterDataContext = new TaskMasterDataContext();
if (!TaskMasterDataContext.DatabaseExists())
{
TaskMasterDataContext.CreateDatabase();
DatabaseHelper.SetupDatabase(TaskMasterDataContext);
}
and here is the TaskMasterDataContext.cs code
public class TaskMasterDataContext : DataContext
{
public TaskMasterDataContext() : base("Data Source=isostore:/TaskMasterData.sdf")
{
}
public Table<Task> Tasks;
}
public static class DatabaseHelper
{
public static void SetupDatabase(TaskMasterDataContext dataContext)
{
string category = string.Empty;
var tasks = new List<Task>();
for (int i = 0; i < 20; i++)
{
tasks.Add(new Task()
{
Id = System.Guid.NewGuid().ToString(),
Category = GetCategoryString(i),
CreateDate = DateTime.Now,
DueDate = DateTime.Now.AddDays(new Random().Next(1, 30)),
IsComplete = false,
Name = String.Format("{0} Task # {1}", GetCategoryString(i), i)
});
}
dataContext.Tasks.InsertAllOnSubmit(tasks);
dataContext.SubmitChanges();
}
private static string GetCategoryString(int i)
{
if (i%2 == 0)
return "home";
if (i%3 == 0)
return "personal";
return "work";
}
}
The DatabaseHelper class is just there to populate the DB with some test data after its created.
I hope this helps.

Resources