image 图片生产XPS文件

由图片转为XPS文件,需要调用windows 版本为6.0后的API创建XPS,然后用图片刷将图片刷上去即可。

并且如需加入print ticket可以用同样的方法加载资源,并载入,以下为我昨天参考MSDN上面的例子写好的源码,共享之。

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <windows.h>  
  3. #include <comdef.h>  
  4. #include <fstream>  
  5. #include <stdio.h>  
  6. #include <windows.h>  
  7. #include <comdef.h>  
  8. #include <XpsObjectModel.h>  
  9.   
  10. #include <StrSafe.h>  
  11. #include <shlobj.h>  
  12.   
  13. using namespace std;  
  14.   
  15. HRESULT   
  16. CreateRectanglePath(  
  17.     __in  IXpsOMObjectFactory   *xpsFactory,  
  18.     __in  const XPS_RECT        *rect,  
  19.     __out IXpsOMPath            **rectPath  
  20. )  
  21. {  
  22.      
  23.     HRESULT hr = S_OK;  
  24.   
  25.     IXpsOMGeometryFigure           *rectFigure;  
  26.     IXpsOMGeometry                 *imageRectGeometry;  
  27.     IXpsOMGeometryFigureCollection *geomFigureCollection;  
  28.   
  29.     // Define start point and three of the four sides of the rectangle.  
  30.     //  The fourth side is implied by setting the path type to CLOSED.  
  31.     XPS_POINT            startPoint = {rect->x, rect->y};  
  32.     XPS_SEGMENT_TYPE     segmentTypes[3] = {  
  33.         XPS_SEGMENT_TYPE_LINE,   
  34.         XPS_SEGMENT_TYPE_LINE,   
  35.         XPS_SEGMENT_TYPE_LINE  
  36.     };  
  37.     FLOAT segmentData[6] = {  
  38.         rect->x,              rect->y+rect->height,   
  39.         rect->x+rect->width,  rect->y+rect->height,   
  40.         rect->x+rect->width,  rect->y   
  41.     };  
  42.     BOOL segmentStrokes[3] = {  
  43.         TRUE, TRUE, TRUE  
  44.     };  
  45.   
  46.     // Create a closed geometry figure using the three   
  47.     //  segments defined above.  
  48.   
  49.     hr = xpsFactory->CreateGeometryFigure( &startPoint, &rectFigure );  
  50.     hr = rectFigure->SetIsClosed( TRUE );  
  51.     hr = rectFigure->SetIsFilled( TRUE );  
  52.     hr = rectFigure->SetSegments( 3, 6,   
  53.             segmentTypes, segmentData, segmentStrokes );  
  54.   
  55.     // Create a geometry that consists of the figure created above.  
  56.     hr = xpsFactory->CreateGeometry( &imageRectGeometry );  
  57.     hr = imageRectGeometry->GetFigures( &geomFigureCollection );  
  58.     hr = geomFigureCollection->Append( rectFigure );  
  59.   
  60.     // Create the path that consists of the geometry created above  
  61.     //  and return the pointer in the parameter passed in to the function.  
  62.     hr = xpsFactory->CreatePath( reinterpret_cast<IXpsOMPath**>(rectPath) );  
  63.     hr = (*rectPath)->SetGeometryLocal( imageRectGeometry );  
  64.   
  65.     // The calling method will release these interfaces when   
  66.     //  it is done with them.  
  67.   
  68.     return hr;  
  69. }  
  70.   
  71.   
  72.   
  73. int main( void ){  
  74.     //XPS size (A4)  
  75.     float width = 793.76f;  
  76.     float height = 1122.56f;  
  77.   
  78.     //color/mono  
  79.   
  80.     //portrait  
  81.   
  82.     //image source  
  83.     LPCWSTR imageFileName = L"1.jpg";  
  84.     float image_width = 768.0f;  
  85.     float image_height = 1024.0f;  
  86.   
  87.   
  88.     /* full fill page 
  89.         XPS_RECT    rect = {0.0f, 0.0f, width, height}; // set to image size 
  90.         XPS_RECT    viewPort = {0.0,0.0, width,height}; 
  91.         XPS_RECT    viewBox = {0.0,0.0, image_width, image_height}; 
  92.     */  
  93.   
  94.     /* adapt page 
  95.  
  96.     */  
  97.     XPS_RECT    rect        = {0.0f, 0.0f, width, height};//page size  
  98.     XPS_RECT    viewPort    = {0.0f, 0.0f, 0.0f, 0.0f};  
  99.     XPS_RECT    viewBox     = {0.0f, 0.0f, image_width, image_height}; //view all image  
  100.   
  101.     if( (image_width > image_height) && (image_width > width ) ){  
  102.         //adapt width  
  103.         float resize_height = image_height * ( width/image_width);  
  104.         float padding_top = (height - resize_height)/2 ;  
  105.   
  106.         viewPort.y      = padding_top;  
  107.         viewPort.width  = width;  
  108.         viewPort.height = resize_height;      
  109.     } else if( (image_height > image_width) && ( image_height >  height) ){  
  110.         float resize_width = image_width * (height/image_height);  
  111.         float padding_left = (width - resize_width) / 2;  
  112.   
  113.         viewPort.x      = padding_left;  
  114.         viewPort.width  = resize_width;  
  115.         viewPort.height = height;  
  116.     } else {  
  117.         //position center  
  118.         float padding_left = (width - image_width)/2;  
  119.         float padding_top  = (height - image_height)/2;  
  120.   
  121.         viewPort.x      = padding_left;  
  122.         viewPort.y      = padding_top;  
  123.         viewPort.width  = image_width;  
  124.         viewPort.height = image_height;  
  125.     }  
  126.   
  127.   
  128.     LPCWSTR XPSFileName = L"a.xps";  
  129.   
  130.     IXpsOMObjectFactory *xpsFactory;  
  131.   
  132.     HRESULT hr = S_OK;  
  133.     // Init COM for this thread if it hasn't   
  134.     //  been initialized, yet.  
  135.     hr = CoInitializeEx(0, COINIT_MULTITHREADED);  
  136.   
  137.     hr = CoCreateInstance(  
  138.         __uuidof(XpsOMObjectFactory),  
  139.         NULL,   
  140.         CLSCTX_INPROC_SERVER,  
  141.         __uuidof(IXpsOMObjectFactory),  
  142.         reinterpret_cast<LPVOID*>(&xpsFactory));  
  143.   
  144.     if (SUCCEEDED(hr))  
  145.     {  
  146.   
  147.         IOpcPartUri                   *opcPartUri = NULL;  
  148.         IXpsOMPackage                 *xpsPackage = NULL;  
  149.         IXpsOMDocumentSequence        *xpsFDS = NULL;  
  150.         IXpsOMDocumentCollection      *fixedDocuments = NULL;  
  151.         IXpsOMDocument                *xpsFD = NULL;  
  152.         IXpsOMPage                    *xpsPage = NULL;  
  153.         IXpsOMPageReferenceCollection *pageRefs = NULL;  
  154.         IXpsOMPageReference           *xpsPageRef = NULL;  
  155.   
  156.         XPS_SIZE pageSize = {width, height};   
  157.       
  158.         // Create the package.  
  159.         hr = xpsFactory->CreatePackage( &xpsPackage );  
  160.   
  161.         // Create the URI for the fixed document sequence part and then    
  162.         //  create the fixed document sequence  
  163.         hr = xpsFactory->CreatePartUri(   
  164.             L"/FixedDocumentSequence.fdseq", &opcPartUri );  
  165.         hr = xpsFactory->CreateDocumentSequence( opcPartUri, &xpsFDS );  
  166.         // Release this URI to reuse the interface pointer.  
  167.         if (NULL != opcPartUri) {opcPartUri->Release(); opcPartUri = NULL;}  
  168.   
  169.   
  170.         // Create the URI for the document part and then create the document.  
  171.         hr = xpsFactory->CreatePartUri(   
  172.             L"/Documents/1/FixedDocument.fdoc", &opcPartUri );  
  173.         hr = xpsFactory->CreateDocument( opcPartUri, &xpsFD );  
  174.         // Release this URI to reuse the interface pointer.  
  175.         if (NULL != opcPartUri) {opcPartUri->Release(); opcPartUri = NULL;}  
  176.   
  177.   
  178.         // Create a blank page.  
  179.         hr = xpsFactory->CreatePartUri(   
  180.             L"/Documents/1/Pages/1.fpage", &opcPartUri );  
  181.         hr = xpsFactory->CreatePage(  
  182.             &pageSize,                  // Page size  
  183.             L"en-US",                   // Page language  
  184.             opcPartUri,                 // Page part name  
  185.             &xpsPage);                  
  186.         // Release this URI to reuse the interface pointer.  
  187.         if (NULL != opcPartUri) {opcPartUri->Release(); opcPartUri = NULL;}  
  188.   
  189.     //add image  
  190.         IStreamPtr             imageStream; // the resulting image stream  
  191.         IOpcPartUri            *imagePartUri;  
  192.         IXpsOMImageResource    *imageResource;  
  193.   
  194.         hr = xpsFactory->CreateReadOnlyStreamOnFile (   
  195.                 imageFileName, &imageStream );  
  196.   
  197.         hr = xpsFactory->CreatePartUri( imageFileName, &imagePartUri );  
  198.   
  199.         /*    imageType; // set to type of image being read in 
  200.     XPS_IMAGE_TYPE_JPEG 
  201.     A JPEG (Joint Photographic Experts Group) image. 
  202.  
  203.     XPS_IMAGE_TYPE_PNG 
  204.     A PNG (Portable Network Graphics) image. 
  205.  
  206.     XPS_IMAGE_TYPE_TIFF 
  207.     A TIFF (Tagged Image File Format) image. 
  208.  
  209.     XPS_IMAGE_TYPE_WDP 
  210.     An HD Photo (formerly Windows Media Photo) image. 
  211.         */  
  212.         hr = xpsFactory->CreateImageResource (   
  213.             imageStream,  
  214.             XPS_IMAGE_TYPE_JPEG,  
  215.             imagePartUri,  
  216.             &imageResource);  
  217.   
  218.         imagePartUri->Release();  
  219.         imageStream->Release();  
  220.         // imageResource can now be used by other parts in the XPS OM.  
  221.         // These variables are initialized outside of this code example,  
  222.         //  for example, as the parameters of a method or from some   
  223.         //  preceding program code.  
  224.   
  225.         // dimensions of the image in pixels  
  226.         XPS_SIZE    bmpDim = {0,0} ;   
  227.   
  228.         // DPI resolution values obtained from image  
  229.         FLOAT        dpiX = 96.0f;  
  230.         FLOAT        dpiY = 96.0f;  
  231.   
  232.         // These are part of this code example.  
  233.         IXpsOMPath *imageRectPath;  
  234.         IXpsOMImageBrush *imageBrush;  
  235.         IXpsOMVisualCollection *pageVisuals;  
  236.   
  237.         // Describe image source dimensions and set viewbox to be the   
  238.         // entire image DIP width of image.   
  239.         //  Example:   
  240.         //    600 image pixels, 300 dpi -> 2 inches -> 2 * 96 = 192 DIP width  
  241.         //viewBox.width = FLOAT((double)bmpDim.width * 96.0 / dpiX);   
  242.         //viewBox.height = FLOAT((double)bmpDim.height * 96.0 / dpiY);  
  243.   
  244.         // Create the image brush.  
  245.         hr = xpsFactory->CreateImageBrush(imageResource, &viewBox, &viewPort,   
  246.             reinterpret_cast<IXpsOMImageBrush**>(&imageBrush));  
  247.   
  248.         // Create the path that describes the outline of the image on the page.  
  249.         //  This step uses the function described in the next code example.  
  250.         hr = CreateRectanglePath(xpsFactory, &rect,  
  251.             reinterpret_cast<IXpsOMPath**>(&imageRectPath));  
  252.   
  253.         // Set the accessibility description for the path object as required.  
  254.         LPCWSTR shortDescText = L"short description text.";  
  255.         hr = imageRectPath->SetAccessibilityShortDescription( shortDescText );  
  256.   
  257.         // Set the image brush to be the fill brush for this path.  
  258.         hr = imageRectPath->SetFillBrushLocal( imageBrush );  
  259.   
  260.         // Get the list of visuals for this page...  
  261.         hr = xpsPage->GetVisuals( &pageVisuals );  
  262.   
  263.         // ...and add the completed path to the list.  
  264.         hr = pageVisuals->Append( imageRectPath );  
  265.   
  266.         // Release locally created interfaces.  
  267.         if (NULL != pageVisuals) pageVisuals->Release();  
  268.         if (NULL != imageRectPath) imageRectPath->Release();  
  269.         if (NULL != imageBrush) imageBrush->Release();  
  270.   
  271.         //create pt  
  272.         /* 
  273.         color:  psk:Monochrome/psk:Color 
  274.         */  
  275.         ofstream ofpt("Job_PT.xml");  
  276.         ofpt << "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \  
  277.             <psf:PrintTicket xmlns:psf=\"http://schemas.microsoft.com/windows/2003/08/printing/printschemaframework\" xmlns:psk=\"http://schemas.microsoft.com/windows/2003/08/printing/printschemakeywords\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"1\">\  
  278.               <psf:Feature name=\"psk:PageOutputColor\">\  
  279.             <psf:Option name=\"psk:Monochrome\">\  
  280.             <psf:ScoredProperty name=\"psk:DeviceBitsPerPixel\">\  
  281.             <psf:Value xsi:type=\"xsd:integer\">24</psf:Value>\  
  282.             </psf:ScoredProperty>\  
  283.             <psf:ScoredProperty name=\"psk:DriverBitsPerPixel\">\  
  284.             <psf:Value xsi:type=\"xsd:integer\">24</psf:Value>\  
  285.             </psf:ScoredProperty>\  
  286.             </psf:Option>\  
  287.             </psf:Feature>\  
  288.             </psf:PrintTicket>";  
  289.         ofpt.close();  
  290.         IStream                     *ptStream = NULL;  
  291.   
  292.         LPCWSTR printTicketFileName = L"Job_PT.xml";  
  293.         hr = xpsFactory->CreateReadOnlyStreamOnFile (  
  294.             printTicketFileName,  
  295.             &ptStream);  
  296.         LPCWSTR printTicketPartName = L"Metadata/Job_PT.xml";  
  297.         hr = xpsFactory->CreatePartUri(   
  298.                 printTicketPartName, &opcPartUri );  
  299.   
  300.         IXpsOMPrintTicketResource* printTicketResource;  
  301.   
  302.         xpsFactory->CreatePrintTicketResource( ptStream, opcPartUri, &printTicketResource);  
  303.         opcPartUri->Release();  
  304.   
  305.         if (NULL != ptStream) ptStream->Release();  
  306.   
  307.   
  308.         // Create a page reference for the page.  
  309.         hr = xpsFactory->CreatePageReference( &pageSize, &xpsPageRef );  
  310.   
  311.         // Add the fixed document sequence to the package.  
  312.         hr = xpsPackage->SetDocumentSequence( xpsFDS );  
  313.   
  314.         xpsFDS->SetPrintTicketResource( printTicketResource);  
  315.   
  316.         // Get the document collection of the fixed document sequence  
  317.         //  and then add the document to the collection.  
  318.         hr = xpsFDS->GetDocuments( &fixedDocuments );  
  319.         hr = fixedDocuments->Append( xpsFD );  
  320.   
  321.         // Get the page reference collection from the document  
  322.         //  and add the page reference and blank page.  
  323.         hr = xpsFD->GetPageReferences( &pageRefs );  
  324.         hr = pageRefs->Append( xpsPageRef );  
  325.         hr = xpsPageRef->SetPage( xpsPage );  
  326.   
  327.         xpsPackage->WriteToFile( XPSFileName, NULL, FILE_ATTRIBUTE_NORMAL, FALSE);  
  328.         // Release interface pointer  
  329.         if (NULL != xpsPage) xpsPage->Release();  
  330.         if (NULL != pageRefs) pageRefs->Release();  
  331.         if (NULL != fixedDocuments) fixedDocuments->Release();  
  332.         if (NULL != xpsPageRef) xpsPageRef->Release();  
  333.         if (NULL != xpsFD) xpsFD->Release();  
  334.         if (NULL != xpsFDS) xpsFDS->Release();  
  335.         if (NULL != xpsPackage) xpsPackage->Release();  
  336.             // ... and release when done  
  337.             xpsFactory->Release();  
  338.     }  
  339.   
  340.     // Uninitialize COM when finished  
  341.     CoUninitialize();  
  342.     return 0;  
  343.   
  344. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值