|
So you only want to combine two adjacent items, even if the third item starts at the same time as the second item ends?
Something like this should do the trick:
public static IEnumerable<Appointment> CombineAdjacentAppointments(
this IEnumerable<Appointment> appointments,
Func<Appointment, Appointment, Appointment> combineAppointments)
{
Appointment previous = null;
foreach (Appointment current in appointments)
{
if (previous == null)
{
previous = current;
}
else if (previous.EndDate != current.StartDate)
{
yield return previous;
previous = current;
}
else
{
yield return combineAppointments(previous, current);
previous = null;
}
}
if (previous != null)
{
yield return previous;
}
}
...
var combinedAppointments = ListAllAppointments().CombineAdjacentAppointments((first, second) => new Appointment
{
StartTime = first.StartTime,
EndTime = second.EndTime,
});
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
As I interpret the question, you want to get adjacent appointments that are in a list of appointments. The following LINQ query produces an IEnumerable collection that contains List<Appointment> objects:
var appointments = new List<Appointment>();
appointments.Add(new Appointment{ index = 1, startTime = new DateTime(...), endTime = new DateTime(...) });
appointments.Add(new Appointment{ index = 2, startTime = new DateTime(...), endTime = new DateTime(...) });
appointments.Add(new Appointment{ index = 3, startTime = new DateTime(...), endTime = new DateTime(...) });
...
var adjacentAppointments = from a in appointments
from b in appointments
where a.endTime == b.startTime
select new List<Appointment> { a, b };
The adjacentAppointments will have type of IEnumerable<List<Appointment>>.
modified 4-Aug-14 8:10am.
|
|
|
|
|
I have a simplified C code witch can extract text from pdf, and write into a file in XML format. The code is based on MuPDF library.
/*
* mudraw -- command line tool simplified
*/
#include "mupdf/fitz.h"
#include "mupdf/pdf.h"
int main(int argc, char **argv)
{
fz_document *doc = NULL;
fz_context *ctx;
fz_page *page;
fz_device *dev = NULL;
fz_text_sheet *sheet = NULL;
fz_text_page *text = NULL;
fz_cookie cookie = { 0 };
fz_output *out = NULL;
char buf[512];
FILE *file;
ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
doc = fz_open_document(ctx, "c:\test\test.pdf");
page = fz_load_page(doc, 0);
sheet = fz_new_text_sheet(ctx);
text = fz_new_text_page(ctx);
dev = fz_new_text_device(ctx, sheet, text);
fz_run_page(doc, page, dev, &fz_identity, &cookie);
fz_free_device(dev);
file = fopen(buf, "c:\test\test.xml");
out = fz_new_output_with_file(ctx, file);
fz_print_text_page_xml(ctx, out, text);
fz_free_text_page(ctx, text);
fz_free_text_sheet(ctx, sheet);
fz_free_page(doc, page);
fz_close_document(doc);
fz_close_output(out);
fz_free_context(ctx);
fclose(file);
}
My question is how can I implement this in C# with P/Invoke?
I tried the following C# code, but it does'n work. Can anybody help me?
using System.IO;
using System;
using System.Runtime.InteropServices;
using Test.Helpers;
namespace Test
{
public static class Test
{
const uint FZ_STORE_DEFAULT = 256 << 20;
public static void CopyTextPage()
{
const float zoomFactor = 1.0f;
var ctm = new Matrix();
var currentDpi = DpiHelpers.GetCurrentDpi();
var zoomX = zoomFactor * (currentDpi.HorizontalDpi / DpiHelpers.DEFAULT_DPI);
var zoomY = zoomFactor * (currentDpi.VerticalDpi / DpiHelpers.DEFAULT_DPI);
ctm.A = zoomX;
ctm.D = zoomY;
var context = NativeMethods.NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT);
var stream = NativeMethods.OpenFile(context, @"c:\test\test.pdf");
var document = NativeMethods.OpenDocumentStream(context, ".pdf", stream);
var page = NativeMethods.LoadPage(document, 0);
using (var filestream = File.Create(@"C:\test\test.xml"))
{
var text = NativeMethods.NewTextPage(context);
var sheet = NativeMethods.NewTextSheet(context);
var dev = NativeMethods.NewTextDevice(context, sheet, text);
if (filestream.SafeFileHandle != null)
{
var header = filestream.SafeFileHandle.DangerousGetHandle();
var output = NativeMethods.NewOutputFile(context, header);
NativeMethods.RunPage(document, page, dev, ctm, IntPtr.Zero);
NativeMethods.PrintTextPage(context, output, text);
NativeMethods.FreeOutputFile(output);
}
NativeMethods.FreeDevice(dev);
NativeMethods.FreeTextSheet(context, sheet);
NativeMethods.FreeTextPage(context, text);
}
NativeMethods.FreePage(document, page);
NativeMethods.CloseDocument(document);
NativeMethods.CloseStream(stream);
NativeMethods.FreeContext(context);
}
private static class NativeMethods
{
const string DLL = "libmupdf.dll";
[DllImport(DLL, EntryPoint = "fz_new_context", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewContext(IntPtr alloc, IntPtr locks, uint max_store);
[DllImport(DLL, EntryPoint = "fz_free_context", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr FreeContext(IntPtr ctx);
[DllImport(DLL, EntryPoint = "fz_open_file_w", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OpenFile(IntPtr ctx, string fileName);
[DllImport(DLL, EntryPoint = "fz_open_document_with_stream", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr OpenDocumentStream(IntPtr ctx, string magic, IntPtr stm);
[DllImport(DLL, EntryPoint = "fz_close", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseStream(IntPtr stm);
[DllImport(DLL, EntryPoint = "fz_close_document", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr CloseDocument(IntPtr doc);
[DllImport(DLL, EntryPoint = "fz_free_device", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreeDevice(IntPtr dev);
[DllImport(DLL, EntryPoint = "fz_free_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreePage(IntPtr doc, IntPtr page);
[DllImport(DLL, EntryPoint = "fz_load_page", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr LoadPage(IntPtr doc, int pageNumber);
[DllImport(DLL, EntryPoint = "fz_run_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void RunPage(IntPtr doc, IntPtr page, IntPtr dev, Matrix transform, IntPtr cookie);
[DllImport(DLL, EntryPoint = "fz_new_text_sheet", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewTextSheet(IntPtr ctx);
[DllImport(DLL, EntryPoint = "fz_free_text_sheet", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreeTextSheet(IntPtr ctx, IntPtr sheet);
[DllImport(DLL, EntryPoint = "fz_new_text_page", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewTextPage(IntPtr ctx);
[DllImport(DLL, EntryPoint = "fz_free_text_page", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreeTextPage(IntPtr ctx, IntPtr page);
[DllImport(DLL, EntryPoint = "fz_new_text_device", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewTextDevice(IntPtr ctx, IntPtr sheet, IntPtr page);
[DllImport(DLL, EntryPoint = "fz_print_text_page_xml", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr PrintTextPage(IntPtr ctx, IntPtr file, IntPtr page);
[DllImport(DLL, EntryPoint = "fz_new_output_with_file", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr NewOutputFile(IntPtr ctx, IntPtr file);
[DllImport(DLL, EntryPoint = "fz_close_output", CallingConvention = CallingConvention.Cdecl)]
public static extern void FreeOutputFile(IntPtr file);
}
}
internal struct Matrix
{
public float A, B, C, D, E, F;
}
}
|
|
|
|
|
tmiklos67 wrote: but it does'n work. Can anybody help me? Maybe. But you'll need to explain what you mean by "it doesn't work".
/ravi
|
|
|
|
|
The code product no output (file). I'm not sure, but I think the problem is how the file handler passing as pointer...
|
|
|
|
|
In your C code you have:
ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
doc = fz_open_document(ctx, "c:\test\test.pdf");
In your C# you have:
var context = NativeMethods.NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT);
var stream = NativeMethods.OpenFile(context, @"c:\test\test.pdf");
var document = NativeMethods.OpenDocumentStream(context, ".pdf", stream);
var context = fz_new_context(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT);
var stream = fz_open_file_w(context, @"c:\test\test.pdf");
var document = fz_open_document_with_stream(context, ".pdf", stream);
Why the difference?
|
|
|
|
|
Very good observation. The C code is working and the C# don't.
Maybe this is the solution and I don't need to use the OpenDocumentStream.
Thanks, I will try your tipp.
|
|
|
|
|
My email is:- vivek.1370@gmail.com
And second iemail is:- vivekpalmailbox@gmail.com
private void ExportToWord()
{
int s1;
for(s1=0;s1<3;s1++)
{
string strForPrint = "";
//first student detail
strForPrint += " S.J.S. PUBLIC SCHOOL" + "\n";
strForPrint += "An English Medium Co-educational School Based on C.B.S.E. Curriculum" + "\n\n\n\n\n";
pic_logo_sjs.Image.Save("E:\\myimage\\myimage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
Word.Application wordApp = new Word.Application();
wordApp.Visible = false;
Word.Document doc = wordApp.Documents.Add();
doc.InlineShapes.AddPicture("E:\\myimage\\myimage.doc");
// strForPrint += richTextBox1.Text + "\r\n";
strForPrint += " REPORT BOOK" + "\n";
strForPrint += " Session 2014-2015" + "\n\n";
strForPrint += "Student Name : " + txt_stu_name.Text + "\r\t\r\t\r\t";
strForPrint += "Roll No : " + txt_roll.Text + "\n\n";
strForPrint += "Class : " + txt_class.Text + "\t";
strForPrint += "Section : " + txt_section.Text + "\t";
strForPrint += "Scholar No : " + txt_scholar.Text + "\r\n\r\n\r\n";
strForPrint += "Date Of Birth : " + txt_dob.Text + "\r\n\r\n\r\n";
strForPrint += "Mother's Name : " + txt_mother.Text + "\r\n\r\n\r\n";
strForPrint += "Father's Name : " + txt_father.Text + "\r\n\r\n\r\n";
strForPrint += "Address : " + txt_address.Text + "\r\n\r\n\r\n\n\n\n\n";
strForPrint += "Tear here line-----------------------------------------------------------" + "\r\n";
//second student detail
strForPrint += " S.J.S. PUBLIC SCHOOL" + "\n";
strForPrint += "An English Medium Co-educational School Based on C.B.S.E. Curriculum" + "\n\n\n\n\n";
strForPrint += " REPORT BOOK" + "\n";
strForPrint += " Session 2014-2015" + "\n\n";
strForPrint += "Student Name : " + txt_name1.Text + "\t\t\t";
strForPrint += "Roll No : " + txt_roll1.Text + "\r\n\r\n";
strForPrint += "Class : " + txt_class1.Text + "\r\t";
strForPrint += "Section : " + txt_section1.Text + "\r\t";
strForPrint += "Scholar No : " + txt_scholar1.Text + "\r\n\r\n\r\n";
strForPrint += "Date Of Birth : " + txt_dob1.Text + "\r\n\r\n\r\n";
strForPrint += "Mother's Name : " + txt_mother1.Text + "\r\n\r\n\r\n";
strForPrint += "Father's Name : " + txt_father1.Text + "\r\n\r\n\r\n";
strForPrint += "Address : " + txt_address1.Text + "\r\n\n";
//First student marks
strForPrint += Exam_I_Term.Text + "\n\n";
strForPrint += "\r\t\r\t" + label1.Text + "\n\n";
strForPrint += lbl_sub.Text + "\r\t" + lbl_english.Text + "\r\t" + lbl_evs.Text + "\r\t" + lbl_hindi.Text + "\r\t" + lbl_math.Text + "\r\t" + lbl_total_marks.Text + "\r\t\n\n";
strForPrint += lbl_marks.Text + "\r\t\r" + lbl_fa_1_english.Text + "\r\t\r\t" + lbl_fa_1_evs.Text + "\r\t\r\t" + lbl_fa_1_hindi.Text + "\r\t\r\t" + lbl_fa_1_math.Text + "\r\t\r\t" + lbl_fa_1_total.Text + "\r\t\r\t\n\n";
strForPrint += lbl_grade.Text + "\r\t\r\t\r\t\r\t\r\t\r\t\r\t\r\t\r\t\r\t" + lbl_fa_1_grade.Text + "\r\n\n";
strForPrint += lbl_attendance.Text + "\r\t\r\t\r\t\r\t\r\t\r\t\r\t\r\t\r\t" + lbl_fa_1_attendance.Text + "\r\n\n\n\n";
strForPrint += lbl_dash_teacher.Text + "\r\t\r\t\r\t\r\t\r\t\r\t" + lbl_dash_principal.Text + "\r\n";
strForPrint += lbl_teacher_signature.Text + "\r\t\r\t\r\t\r\t\r\t\r\t" + lbl_principal_signature.Text + "\r\n\n\n";
strForPrint += "\r\t\r\t\r\t\r\t" + lbl_grading_norm_index.Text + "\r\n\n";
strForPrint += lbl_A_outstainding.Text + "" + lbl_B_Excellent.Text + "" + lbl_C_very_good.Text+ "\r\n\r\n";
strForPrint += lbl_D_good.Text + "\r\t\r\t\r\t" + label3.Text + "\r\n\n\n\n\n\n\n";
//second student marks
strForPrint += Exam_I_Term1.Text + "\n\n";
strForPrint += "\r\t\r\t" + label1_1.Text + "\n\n";
strForPrint += lbl_sub1.Text + "\r\t" + lbl_english1.Text + "\r\t" + lbl_evs1.Text + "\r\t" + lbl_hindi1.Text + "\r\t" + lbl_math1.Text + "\r\t" + lbl_total_marks1.Text + "\r\t\n\n";
strForPrint += lbl_marks1.Text + "\r\t\r" + lbl_fa_1_english1.Text + "\r\t\r\t" + lbl_fa_1_evs1.Text + "\r\t\r\t" + lbl_fa_1_hindi1.Text + "\r\t\r\t" + lbl_fa_1_math1.Text + "\r\t\r\t" + lbl_fa_1_total1.Text + "\r\t\r\t\n\n";
strForPrint += lbl_grade1.Text + "\r\t\r\t\r\t\r\t\r\t\r\t\r\t\r\t\r\t\r\t" + lbl_fa_1_grade1.Text + "\r\n\n";
strForPrint += lbl_attendance1.Text + "\r\t\r\t\r\t\r\t\r\t\r\t\r\t\r\t\r\t" + lbl_fa_1_attendance1.Text + "\r\n\n\n\n";
strForPrint += lbl_dash_teacher1.Text + "\r\t\r\t\r\t\r\t\r\t\r\t" + lbl_dash_principal1.Text + "\r\n";
strForPrint += lbl_teacher_signature1.Text + "\r\t\r\t\r\t\r\t\r\t\r\t" + lbl_principal_signature1.Text + "\r\n\n\n";
strForPrint += "\r\t\r\t\r\t\r\t" + lbl_grading_norm_index1.Text+ "\r\n\n";
strForPrint += lbl_A_outstainding1.Text + "\r\t" + lbl_B_Excellent1.Text + "\r\t" + lbl_C_very_good1.Text + "\r\n\r\n";
strForPrint += lbl_D_good1.Text + "\r\t\r\t\r\t" + label3.Text + "\r\n\n";
label2.Text = "hello.......";
label2.ForeColor = Color.Green;
Encoding utf16 = Encoding.GetEncoding(1254);
byte[] output = utf16.GetBytes(strForPrint);
FileStream fs = new FileStream(sfd.FileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(output, 0, output.Length); //write data into file
bw.Flush();
bw.Close();
fs.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
sfd.Filter = "Word Documents (*.doc)|*.doc";
if (sfd.ShowDialog() == DialogResult.OK)
{
ExportToWord();
}
}
|
|
|
|
|
Where are you stuck?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
The only people who are interested in your email addresses in a public forum are spammers. Nobody is going to email you directly except them.
|
|
|
|
|
I have three mouse events hooked up to an image control. (MouseLeftButtonDown, MouseMove, MouseLeftButtonUp).
I want to draw a rectangle by click - drag - release.
The Down and move events always do the trick, but only sometimes does the MouseLeftButtonUp event fire.
I don't know why.
What could be the problem?
[EDIT]Not solved yet, but if I release very quickly after a move it works, when I'm too slow, it doesn't.[/EDIT]
[SOLUTION]
This is somewhat weird. The workflow is that on MouseDown I create a Rectangle and ad it to the Canvas control.
In Mousemove I update this rectangle to follow my cursor. When I release the button, the cursor is on the Rectangle, not on the image. The solution is to add an event handler to the created rectangle and bubble it further to the image handler .
[/SOLUTION]
modified 30-Jul-14 5:41am.
|
|
|
|
|
Your mouse is f**ked?
Hardware problems aside, it's not at all obvious from that little exactly what is causing the problem - but you might be better off asking in the WPF forum[^] (for that I assume it to be) - it is not something I've seen in a WinForms app, which doesn't have those events anyway!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
As a matter of fact, I just solved the issue the moment you replied.
I'll update the question
|
|
|
|
|
I've got to bookmark that one it is sure to come back and bite me on the arse sometime. It is so OBVIOUS when explained.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
tell me about it
|
|
|
|
|
Thanks in advance
I have an asp.net web application which integrates with Google API and oauth2 authentication. I have completed authentication part, now what I want really is , I need to display user's google drive files and folders in my application as it is listed in Google Drive and also I need to download files.
Need your help.
|
|
|
|
|
The documentation[^] seems to be a good place to start. The Children section seems like a reasonable bet.
|
|
|
|
|
I'm aware of at least two possibilities for reading directly from xls files:
- Microsoft.Office.Interop.Excel
- Microsoft.ACE.OLEDB.12.0 OleDB provider
From what little information I've been able to find on the subject, it seems that the Microsoft.Office.Interop.Excel method requires that Excel be installed on the system where this will be run. Is this true?
Basically, I want a reliable (read: can be run on any system Windows 7 or higher with or without office installed) to read data from an XLS or XLSX spreadsheet. I don't need to do any writing to the format, I just need to read from it and dump the data into a SQLite or SQL Server Compact database table.
Are there any methods other than those two I mentioned, and which would be the highest recommended method of doing what I need?
Thanks.
|
|
|
|
|
agent154 wrote: Is this true?
Yes. So I avoid it.
agent154 wrote: Microsoft.ACE.OLEDB.12.0 OleDB provider
That's my preference.
agent154 wrote: read from it and dump the data into a SQLite or SQL Server Compact database table.
That's the sort of thing that SSIS does.
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
Just an FYI since the other guy didn't mention it at all...
* Your first choice, of course requires that Excel be installed, but is a lot less work and is more platform independent.
* Your second choice does not... HOWEVER, and this is a BIG HOWEVER because if you don't know about this, you'll spend **weeks** trying to figure out what's going on... Microsoft ACE is the 64-bit driver. That's for a 64-bit OS and a 64-bit process **ONLY**. Won't run on a 32-bit OS or if your process is 32-bit. No big deal you're thinking, right? Not quite... if the machine happens to have 32-bit Office installed, you can't even install the 64-bit Ace driver, regardless of your OS because you aren't allowed to mix bitage. You have to install a different 32-bit driver and that limits your process to running as 32-bit because you can't use the 32-bit driver from a 64-bit process or vice versa.
This causes a lot of issues... you'd have to have a 32-bit build that uses the 32-bit driver if the machine has 32-bit Office installed and a 64-bit build that uses the 64-bit driver if the machine has 64-bit Office installed.
Most 64-bit machines I've seen have 32-bit Office installed... just sayin'... so you're limiting yourself to a 32-bit process.
|
|
|
|
|
AAAHHhhhh and they wonder why I dump on Office from a great height every opportunity I get. I hate having to deal with Office version incompatibilities, now you tell me the processor type may be an issue.
Ok so now I can see a benefit to the 32 bit OS policy I have been muttering about for some time.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
Both EPPlus[^] and ClosedXml[^] will allow you to read and write Excel 2007+ files (*.xlsx ).
If you need support for the older format files (*.xls ) as well, then NPOI[^] is pretty good.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
agent154 wrote: requires that Excel be installed on the system where this will be run.
What is the Business case where that matters?
If this is a client machine and the spread sheet is just a read only data store that comes with the application why not use a different format?
If a client machine where a user can modify the file then they would need excel anyways.
If a server machine then the situation is similar to the above but there should be less concern about cost.
|
|
|
|
|
Could someone elaborate on how the runtime determines where to allocate a variable (stack/heap)?
class MyClass()
{
int x;
}
private void AssignmentCheck()
{
MyClass mc = new MyClass();
mc.x = 10;
int MyInt = 5;
}
space will be allocated on the heap for the new instance of MyClass and the reference to the heap location will be saved to the variable mc.
mc.x is on the heap and MyInt will be on the stack.
My question is how does the runtime know to allocate space on the heap for MyClass and space on the stack for myInt ?
Does the runtime do some kind of type checking prior to assignment ?
i.e.
ok, MyClass is a ref type, I'd better put it on the heap
MyInt derives from System.ValueType and it's a local variable, I'd better put it on the stack.
Obviously an implementation detail but curious to know..
thx in advance
|
|
|
|
|