Conversion from images to XPS and PDF

Source: Internet
Author: User
Tags getstream

 

Conversion is actually adding an image to XPS. pdf files are not really a format conversion. In this blog, we have introduced how to convert an image to a PDF file. The conversion from images to XPS is introduced below. New features in. Net framework3.0 are used here. Net 3.0 has classes that support XPS files, and XPS is also in openxml format,
First, add the reference reachframework, windowbase, and,
ConversionCodeThe Code is as follows:

Protected   Bool Convert ( String Sourcepath, String Targetpath)
{
Image img = Image. fromfile (sourcepath );

String Resfile = Sourcepath;
Xpsdocument xpsdoc =   New Xpsdocument (targetpath, fileaccess. readwrite );
Ixpsfixeddocumentsequencewriter FDS = Xpsdoc. addfixeddocumentsequence ();
Ixpsfixeddocumentwriter FD = FDS. addfixeddocument ();
Ixpsfixedpagewriter fp = FD. addfixedpage ();
Xpsresource res =   Null ;
Xpsresource thumb =   Null ;

Res = FP. addimage (xpsimagetype. jpegimagetype );
Thumb = Xpsdoc. addthumbnail (xpsimagetype. jpegimagetype );

Writestream (res. getstream (), resfile );
Writepagecontent (FP. xmlwriter, res, IMG. Width, IMG. Height );
Res. Commit ();

Writestream (thumb. getstream (), resfile );
Thumb. Commit ();

FP. Commit ();
FD. Commit ();
FDS. Commit ();
Xpsdoc. Close ();
Return   True ;
}

Private   Static   Void Writepagecontent (system. xml. xmlwriter, xpsresource res, Int Width, Int Height)
{
Xmlwriter. writestartelement ( " Fixedpage " );
Xmlwriter. writeattributestring ( " Xmlns " , @" Http://schemas.microsoft.com/xps/2005/06 " );
Xmlwriter. writeattributestring ( " Width " , Width. tostring ());
Xmlwriter. writeattributestring ( " Height " , Height. tostring ());
Xmlwriter. writeattributestring ( " XML: Lang " , " En-US " );
Xmlwriter. writestartelement ( " Canvas " );

If (Res Is Xpsimage)
{
Xmlwriter. writestartelement ( " Path " );
Xmlwriter. writeattributestring ( " Data " , " M 20, 20 L 770,770, 20 20,770 Z " );
Xmlwriter. writestartelement ( " Path. Fill " );
Xmlwriter. writestartelement ( " Imagebrush " );
Xmlwriter. writeattributestring ( " Imagesource " , Res. Uri. tostring ());
Xmlwriter. writeattributestring ( " Viewbox " , " 0, 0, 750,750 " );
Xmlwriter. writeattributestring ( " Viewboxunits " , " Absolute " );
Xmlwriter. writeattributestring ( " Viewport " , " 750,750 " );
Xmlwriter. writeattributestring ( " Viewportunits " , " Absolute " );
Xmlwriter. writeendelement ();
Xmlwriter. writeendelement ();
Xmlwriter. writeendelement ();
}
Xmlwriter. writeendelement ();
Xmlwriter. writeendelement ();
}

Private   Static   Void Writestream (Stream stream, String Resfile)
{
Using (Filestream sourcestream =   New Filestream (resfile, filemode. Open, fileaccess. Read ))
{
Byte [] Buf =   New   Byte [ 1024 ];
Int Read =   0 ;
While (Read = Sourcestream. Read (BUF, 0 , Buf. Length )) >   0 )
{
Stream. Write (BUF,0, Read );
}
}
}

In this way, the XPS and PDF of the same image are quite different,

Of course, by adjusting the code, we can also get similar results, but it takes time to think of a compromise here, Office 2007 can convert the document to XPS and PDF through addin, through it, the XPS and PDF should be the same. Therefore, here we first add the image to a Word document, and then get the XPS and PDF through Office 2007. In the previous blog, we add the code to add the image to the Word document:

Worddocument = Wordapplication. Documents. Add ( Ref Parammissing, Ref Parammissing, Ref Parammissing, Ref Parammissing );

Worddocument. inlineshapes. addpicture (sourcepath, Ref Parammissing, Ref Parammissing, Ref Parammissing );

Object A = Path. gettemppath () + Path. gettempfilename () +   " . Docx " ;
Object Format = Word. wdsaveformat. wdformatdocumentdefault;
Worddocument. saveas ( Ref A, Ref Format,
Ref Parammissing, Ref Parammissing, Ref Parammissing,
Ref Parammissing, Ref Parammissing, Ref Parammissing,
Ref Parammissing, Ref Parammissing, Ref Parammissing,
Ref Parammissing, Ref Parammissing, Ref Parammissing,
Ref Parammissing, Ref Parammissing );

After an image is added, the Word documents must be saved to the disk before being converted to XPS and PDF. Otherwise, a word window is opened. In this way, the XPS and PDF files are the same.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.