Click here to Skip to main content
15,881,248 members
Home / Discussions / C#
   

C#

 
QuestionHow can i make a DVR Server ? i want to connect to more than one DVR from the Internet using ASP.NET Website. Pin
Aimanzaki1-Aug-14 5:36
Aimanzaki1-Aug-14 5:36 
AnswerRe: How can i make a DVR Server ? i want to connect to more than one DVR from the Internet using ASP.NET Website. Pin
Richard Andrew x641-Aug-14 6:25
professionalRichard Andrew x641-Aug-14 6:25 
QuestionGet smart card certificates on a Windows Mobile 6 application and Compact Framework Pin
Wakonda30-Jul-14 20:47
Wakonda30-Jul-14 20:47 
AnswerRe: Get smart card certificates on a Windows Mobile 6 application and Compact Framework Pin
Wakonda8-Sep-14 2:48
Wakonda8-Sep-14 2:48 
QuestionHow to compare the next/previous items in List in C# using linq Pin
Member 1098134230-Jul-14 6:02
Member 1098134230-Jul-14 6:02 
AnswerRe: How to compare the next/previous items in List in C# using linq Pin
Richard Deeming30-Jul-14 7:11
mveRichard Deeming30-Jul-14 7:11 
AnswerRe: How to compare the next/previous items in List in C# using linq Pin
Member 109862521-Aug-14 8:26
Member 109862521-Aug-14 8:26 
QuestionExtract text from PDF in C# using Mupdf library Pin
tmiklos6730-Jul-14 1:19
tmiklos6730-Jul-14 1:19 
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?

C#
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;
    }
}

AnswerRe: Extract text from PDF in C# using Mupdf library Pin
Ravi Bhavnani30-Jul-14 4:31
professionalRavi Bhavnani30-Jul-14 4:31 
GeneralRe: Extract text from PDF in C# using Mupdf library Pin
tmiklos6730-Jul-14 22:32
tmiklos6730-Jul-14 22:32 
QuestionRe: Extract text from PDF in C# using Mupdf library Pin
Richard MacCutchan30-Jul-14 5:14
mveRichard MacCutchan30-Jul-14 5:14 
AnswerRe: Extract text from PDF in C# using Mupdf library Pin
tmiklos6730-Jul-14 22:25
tmiklos6730-Jul-14 22:25 
Questionwant to export to word with text color and logo image in a single doc file through c# windows application Pin
VIVEK KUMAR PAL30-Jul-14 1:14
VIVEK KUMAR PAL30-Jul-14 1:14 
QuestionRe: want to export to word with text color and logo image in a single doc file through c# windows application Pin
ZurdoDev30-Jul-14 2:01
professionalZurdoDev30-Jul-14 2:01 
AnswerRe: want to export to word with text color and logo image in a single doc file through c# windows application Pin
Dave Kreskowiak30-Jul-14 2:55
mveDave Kreskowiak30-Jul-14 2:55 
QuestionMouse event Handling [SOLVED] Pin
V.29-Jul-14 23:18
professionalV.29-Jul-14 23:18 
AnswerRe: Mouse event Handling Pin
OriginalGriff29-Jul-14 23:35
mveOriginalGriff29-Jul-14 23:35 
GeneralRe: Mouse event Handling Pin
V.29-Jul-14 23:39
professionalV.29-Jul-14 23:39 
AnswerRe: Mouse event Handling [SOLVED] Pin
Mycroft Holmes30-Jul-14 0:20
professionalMycroft Holmes30-Jul-14 0:20 
GeneralRe: Mouse event Handling [SOLVED] Pin
V.30-Jul-14 0:24
professionalV.30-Jul-14 0:24 
QuestionGoogle Drive files and folders listing in asp.net web application using c# and Google Apis Pin
NIYAS PK29-Jul-14 20:04
NIYAS PK29-Jul-14 20:04 
AnswerRe: Google Drive files and folders listing in asp.net web application using c# and Google Apis Pin
Pete O'Hanlon29-Jul-14 20:49
mvePete O'Hanlon29-Jul-14 20:49 
QuestionWhat options do I have for reading data from an excel spreadsheet, and what are the limitations of each? Pin
agent15429-Jul-14 15:05
agent15429-Jul-14 15:05 
AnswerRe: What options do I have for reading data from an excel spreadsheet, and what are the limitations of each? Pin
PIEBALDconsult29-Jul-14 15:14
mvePIEBALDconsult29-Jul-14 15:14 
AnswerRe: What options do I have for reading data from an excel spreadsheet, and what are the limitations of each? Pin
SledgeHammer0129-Jul-14 18:09
SledgeHammer0129-Jul-14 18:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.