Creating PdfWriter Object? - itext

I want to write a simple code that will generate a pdf file. While following steps given by http://www.c-sharpcorner.com/UploadFile/f2e803/basic-pdf-creation-using-itextsharp-part-i/. I am unable to create an object of PdfWriter class. The following is the error code that occurred while creating PdfWriter object.
'iTextSharp.text.pdf.PdfWriter.PdfWriter(iTextSharp.text.pdf.PdfDocument, System.IO.Stream)' is inaccessible due to its protection level

Related

Novacode LineChart type

I have a code that implements a Novacode.LineChart. And the LineChart type which is shown by default is this one:
But I dont want this type of chart, I want it without points, like this:
This is the code where I create the chart:
LineChart c = new LineChart();
c.AddLegend(ChartLegendPosition.Bottom, false);
c.Grouping = Grouping.Stacked;
Anyone knows how can I hide thoose points and show only the lines? Thanks to everyone!!
Your question is shown up while I was searching for the exact same feature. It's probably a bit late but I hope it would be useful for other people in need of this feature.
My so called answer is not more than a few lines of dirty and unmanageable hack so unless you are not in dire need, I do not recommend to follow this way.
I also do not know if is it an approved approach here but I prefer to write the solution step by step so it may help you to grasp the concept and use better methods.
After I have realized that I was unable to use DocX to create a line chart without markers, using currently provided API, I wanted to know what were the differences between actual and desired output. So I saved a copy of .docx file with line chart after I manually edited the chart to expected result.
Before and after the edit
As you may already know, a .docx is a container format and essentially comprised of a few different folders and files. You can open it up with a .zip archive extractor. I used 7-Zip for this task and found chart file at location of /word/charts/chart1.xml but this may differ depending on the file, but you can easily figure it out.
Compared both of chart1.xml files and the difference was, the file without the markers had and extra XML tag with an additional attribute;
<c:marker>
<c:symbol val="none" />
</c:marker>
I had to somehow add this segment of code to chart. I added these up to example code provided by DocX. You can follow up from: DocX/ChartSample.cs at master
This is where the fun begins. Easy part first.
using System.Xml;
using System.Xml.Linq;
using Xceed.Words.NET;
// Create a line chart.
var line_chart = new LineChart();
// Create the data.
var PlaceholderData = ChartData.GenerateRandomDataForLinechart();
// Create and add series
var Series_1 = new Series("Your random chart with placeholder data");
Series_1.Bind(PlaceholderData, "X-Axis", "Y-Axis");
line_chart.AddSeries(Series_1);
// Create a new XmlDocument object and clone the actual chart XML
XmlDocument XMLWithNewTags = new XmlDocument();
XMLWithNewTags.LoadXml(line_chart.Xml.ToString());
I've used XPath Visualizer Tool to determine the XPath query, which is important to know because you can't just add the marker tag to somewhere and expect it to work. Why do I tell this? Because I appended marker tag on a random line and expected it to work. Naive.
// Set a namespace manager with the proper XPath location and alias
XmlNamespaceManager NSMngr = new XmlNamespaceManager(XMLWithNewTags.NameTable);
string XPathQuery = "/c:chartSpace/c:chart/c:plotArea/c:lineChart/c:ser";
string xmlns = "http://schemas.openxmlformats.org/drawingml/2006/chart";
NSMngr.AddNamespace("c", xmlns);
XmlNode NewNode = XMLWithNewTags.SelectSingleNode(XPathQuery, NSMngr);
Now create necessary tags on newly created XML Document object with specified namespace
XmlElement Symbol = XMLWithNewTags.CreateElement("c", "symbol", xmlns);
Symbol.SetAttribute("val", "none");
XmlElement Marker = XMLWithNewTags.CreateElement("c", "marker", xmlns);
Marker.AppendChild(Symbol);
NewNode.AppendChild(Marker);
And we should copy the contents of latest changes to actual XML object. But oops, understandably it is defined as private so it is a read-only object. This is where I thought like "Okay, I've fiddled enough with this. I better find another library" but then decided to go on because reasons.
Downloaded DocX repo, changed this line to
get; set;
recompiled, copied Xceed.Words.NET.dll to both projectfolder/packages and projectfolder/projectname/bin/Debug folder and finally last a few lines were
// Copy the contents of latest changes to actual XML object
line_chart.Xml = XDocument.Parse(XMLWithNewTags.InnerXml);
// Insert chart into document
document.InsertChart(line_chart);
// Save this document to disk.
document.Save();
Is it worth it? I'm not sure but I have learned a few things while working on it. There're probably lots of bad programming practises in this answer so please tell me if you see one. Sorry for meh English.

What process is Streamreader using?

I made a simple program with
StreamReader sr = new StreamReader(File.OpenRead(dlg.FileName));
txtBox1.Text = sr.ReadToEnd();
and forgot to put
sr.Dispose();
and now when I try to run the program and open a file I get the IOException was unhandled error message that says "The process cannot access the file because it is being used by another process." So my question is, does anyone know what process is it using? I would like to be able to find it in the taskmanager and end it instead of writing a bunch of exception handling code since this is just a program that I'm using for practice.
The file I tried to open is a txt file in MyDocuments.
The process that is using the file is the process that opened it which is very likely .vshost.exe if you are running this from from Visual Studio.
To avoid 'forgetting' to dispose of objects in the future using the using statementfor anything implementing IDisposable (which StreamReader does). Your code would look like
using(StreamReader sr = new StreamReader(File.OpenRead(dlg.FileName)))
{
txtBox1.Text = sr.ReadToEnd();
}
I got the same error when I was using a streamreader to find out the end of the text in the file so I could then add the new text after the text in the file with the writeline method. My Documents has accessibility issues when it come to development anyway. Try putting the .txt into some other location maybe /temp folder?

Creating Sample Data in Blend Expressions

I been watching TechDays 2010 Understanding MVVM and at one point he talks about blend and creating sample data but instead of generating it in blend he makes the data in C# code.
I am wondering if you create sample data(from sample class,new sample data and etc) does it save it somewhere in the project(ie I give my project to someone else will they see the same data in blend when they load up the project)? Can you switch from sample data and live data easily?
Sample data is just an xaml (not just xml) file defining your object graph that is marked with the build types DesignData or DesignDataWithDesignTimeCreatableTypes. The docs are sparse on MSDN, but this document about its use in the Silverlight designer is essentially the same in any xaml designer in 2012.
There is no "live data" when using these types of samples. All the values are set in the xaml file. You can't change the data for, say, a particular text box within the designer. Nor can you easily switch between different samples.
There are two ways to create the sample data--you can build it by hand (if you know your types and if you are comfortable writing xaml), or you can spin up a simple console application, build your object graph, then use the XamlServices class to serialize your graph to a string (or just rewrite to drop it to a stream instead). Here's some C# pseudocode that may or may not work as written:
public string Serialize(object toSerialize)
{
var sb = new StringBuilder();
var writer = XmlWriter.Create(sb);
XamlServices.Save(writer, toSerialize);
writer.Flush();
writer.Close();
return sb.ToString();
}
You just create a new file, give it an .xaml extension, drop the result in that file, save it to your solution, and set its Build Action to DesignData (the designer mocks the structure of your types) or DesignTimeDataWithDesignTimeCreatableTypes (the latter if your graph can be deserialized with XamlServices, doesn't throw any exceptions when used in the designer, etc).

Automatically coming slashes with FileStream in Monodroid

I have been working on a project using Monodroid. I need to use FileStream to access a file in my project. I write the path as a first argument in FileStream like FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read) but two slashes come automatically from FileStream, therefore the path is not valid. For example, if the path is "res/raw/aFile.txt", after FileStream method it becomes "//res/raw/aFile.txt" and an exception occurs. Because of the exception, I cannot split and get the valid path. How can I get rid of the two slashes that automatically coming from the FileStream method?
Regards.
If you're trying to open a raw resource file, I would suggest using the OpenRawResource() method instead:
using (var stream = Resources.OpenRawResource(Resource.Raw.File))
{
}

java dom xml parser- how to check if the document is empty (or not)

I am trying to parse through a document using a DOM Parser in java.
For the following line of code--
Element docEle = dom.getDocumentElement();
I am getting the following error message--
2011-10-06 05:03:19,944 ERROR AWT-EventQueue-0 XMLDOMParser.class - Full error Stack Trace=java.lang.NullPointerException
at com.arvind.googlepatentsdata.XMLDOMParser.parseDocument(XMLDOMParser.java:290)
As I understand, this error occurs whenever the document itself is empty... Is there some way to check (before the dom element is obtained) if the document is empty or not... So that if it is empty then I will not do any further processing and move on to the next file?
I don't see, that there is a need for a separate check. In getDocumentElement() this check is integrated and it throws an exception if there is no root element.
So all you have to do is to catch that exception and go to the next file.