locked
How do i create my pdf renderer in C++?

    Question

  • follow this sample:

    http://code.msdn.microsoft.com/windowsapps/PDF-viewer-showcase-sample-39ced1e8

    I interest in C#,C++ Sample.

    I want to create my c++ pdf render using RenderPageToSurface API and then parse VirtualSurfaceImageSource from c# project.

    This sample ViewModel class is in c++ project  but I want ViewModel in c# and use c++ for pdf render only!

    How should I do , thanks.

    P.S. I try to create follow below code, but it's don't work when surface image is moved by scroll or zoom.

    C#

                StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(PDF_FILE_NAME);
                document = await PdfDocument.LoadFromFileAsync(file);
                PdfPage page = document.GetPage(0);
                 var w = (int)(page.Size.Width * 1);
                var h = (int)(page.Size.Height * 1);
                SurfaceImageSource image = new SurfaceImageSource(w,h,false);
                pdfRenderer.RenderPage(page, image);
                ImageSource = image;

    C++

    #include "pch.h"
    #include "PDFDocumentRenderer.h"
    #include "DirectXSample.h"
    
    using namespace PDFRendererComponent;
    using namespace Platform;
    using namespace Microsoft::WRL;
    using namespace Windows::Data::Pdf;
    using namespace Concurrency;
    using namespace Windows::Storage;
    using namespace Windows::UI::Xaml::Media::Imaging;
    
    PDFDocumentRenderer::PDFDocumentRenderer()
    {
    	CreateDeviceResources();
    }
    
    // Initialize hardware-dependent resources.
    void PDFDocumentRenderer::CreateDeviceResources()
    {
    	// This flag adds support for surfaces with a different color channel ordering
    	// than the API default. It is required for compatibility with Direct2D.
    	UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
    
    	// This array defines the set of DirectX hardware feature levels this app will support.
    	// Note the ordering should be preserved.
    	// Don't forget to declare your application's minimum required feature level in its
    	// description.  All applications are assumed to support 9.1 unless otherwise stated.
    	const D3D_FEATURE_LEVEL featureLevels [] =
    	{
    		D3D_FEATURE_LEVEL_11_1,
    		D3D_FEATURE_LEVEL_11_0,
    		D3D_FEATURE_LEVEL_10_1,
    		D3D_FEATURE_LEVEL_10_0,
    		D3D_FEATURE_LEVEL_9_3,
    		D3D_FEATURE_LEVEL_9_2,
    		D3D_FEATURE_LEVEL_9_1,
    	};
    
    	// Create the Direct3D 11 API device object.
    	DX::ThrowIfFailed(
    		D3D11CreateDevice(
    		nullptr,                        // Specify nullptr to use the default adapter.
    		D3D_DRIVER_TYPE_HARDWARE,
    		nullptr,
    		creationFlags,                  // Set debug and Direct2D compatibility flags.
    		featureLevels,                  // List of feature levels this app can support.
    		ARRAYSIZE(featureLevels),
    		D3D11_SDK_VERSION,              // Always set this to D3D11_SDK_VERSION for Metro style apps.
    		&m_d3dDevice,                   // Returns the Direct3D device created.
    		nullptr,
    		nullptr
    		)
    		);
    
    
    	m_d3dDevice.As(&dxgiDevice);
    	HRESULT pdfCreateHR = PdfCreateRenderer(dxgiDevice.Get(), &m_PdfRenderer);
    }
    
    void PDFDocumentRenderer::RenderPage(PdfPage^ page, SurfaceImageSource^ surface){
    
    	IInspectable* sisInspectable = (IInspectable*) reinterpret_cast<IInspectable*>(surface);
    	sisInspectable->QueryInterface(__uuidof(ISurfaceImageSourceNative), (void **)&m_sisNative);
    	m_sisNative->SetDevice(dxgiDevice.Get());
    	POINT offset = { 0, 0 };
    	RECT rectangle = { 0,
    		0,
    		static_cast<LONG>(page->Size.Width),
    		static_cast<LONG>(page->Size.Height)
    	};
    	ComPtr<IDXGISurface> dxgiSurface;
    	HRESULT beginDrawHR = m_sisNative->BeginDraw(rectangle, &dxgiSurface, &offset);
    
    	create_task([this, page, offset, dxgiSurface, rectangle]()
    	{
    		PDF_RENDER_PARAMS params;
    		params.DestinationHeight = page->Size.Height;
    		params.DestinationWidth = page->Size.Width;
    		params.BackgroundColor = DX::ConvertToColorF(Windows::UI::Colors::White);
    		params.SourceRect.left = 0;
    		params.SourceRect.top = 0;
    		params.SourceRect.bottom = page->Size.Height;
    		params.SourceRect.right = page->Size.Width;
    		HRESULT pdfRenderHR = m_PdfRenderer->RenderPageToSurface((IUnknown*)page, dxgiSurface.Get(), offset, &params);
    	}, task_continuation_context::use_arbitrary()).then([this]()
    	{
    		HRESULT endDrawHR = m_sisNative->EndDraw();
    	}, task_continuation_context::use_current());
    }

    Thursday, June 26, 2014 10:14 AM

All replies

  • Why do you want to do this in C#?

    The SurfaceImageSource and VirtualSurfaceImageSource implementations require calling COM interfaces and need to be done in C++. They cannot be done in C# without using 3rd party libraries to wrap the native calls.

    --Rob

    Thursday, June 26, 2014 11:17 PM
    Owner