Esempio n. 1
0
        public DocumentReference AddPage(string fileName)
        {
            DocumentReference newDocRef = new DocumentReference();
            FixedDocument     newFd     = new FixedDocument();

            XpsDocument           xpsDocument = new XpsDocument(fileName, FileAccess.Read);
            FixedDocumentSequence docSeq      = xpsDocument.GetFixedDocumentSequence();

            foreach (DocumentReference docRef in docSeq.References)
            {
                FixedDocument fd = docRef.GetDocument(false);

                foreach (PageContent oldPC in fd.Pages)
                {
                    Uri         uSource        = oldPC.Source;                   //读取源地址
                    Uri         uBase          = (oldPC as IUriContext).BaseUri; //读取目标页面地址
                    PageContent newPageContent = new PageContent();
                    newPageContent.GetPageRoot(false);                           //这个地方应当是把文档解压成一个包放到内存中我们再去读取
                    newPageContent.Source = uSource;
                    (newPageContent as IUriContext).BaseUri = uBase;
                    newFd.Pages.Add(newPageContent);//将新文档追加到新的documentRefences中
                }
            }
            newDocRef.SetDocument(newFd);
            xpsDocument.Close();
            return(newDocRef);
        }
Esempio n. 2
0
        public static DocumentReference AddPage(XpsDocument xpsDocument)
        {
            DocumentReference newDocRef = new DocumentReference();
            FixedDocument     newFd     = new FixedDocument();

            FixedDocumentSequence docSeq = xpsDocument.GetFixedDocumentSequence();

            foreach (DocumentReference docRef in docSeq.References)
            {
                FixedDocument fd = docRef.GetDocument(false);

                foreach (PageContent oldPC in fd.Pages)
                {
                    Uri uSource = oldPC.Source;                   //读取源地址
                    Uri uBase   = (oldPC as IUriContext).BaseUri; //读取目标页面地址

                    PageContent newPageContent = new PageContent();
                    newPageContent.GetPageRoot(false);
                    newPageContent.Source = uSource;
                    (newPageContent as IUriContext).BaseUri = uBase;
                    newFd.Pages.Add(newPageContent);//将新文档追加到新的documentRefences中
                }
            }
            newDocRef.SetDocument(newFd);
            xpsDocument.Close();
            return(newDocRef);
        }
Esempio n. 3
0
        public DocumentReference AddPage(string fileName)
        {
            DocumentReference     documentReference     = new DocumentReference();
            FixedDocument         fixedDocument         = new FixedDocument();
            XpsDocument           xpsDocument           = new XpsDocument(fileName, FileAccess.Read);
            FixedDocumentSequence fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();

            foreach (DocumentReference current in fixedDocumentSequence.References)
            {
                FixedDocument document = current.GetDocument(false);
                foreach (PageContent current2 in document.Pages)
                {
                    Uri         source      = current2.Source;
                    Uri         baseUri     = ((IUriContext)current2).BaseUri;
                    PageContent pageContent = new PageContent();
                    pageContent.GetPageRoot(false);
                    pageContent.Source = source;
                    ((IUriContext)pageContent).BaseUri = baseUri;
                    fixedDocument.Pages.Add(pageContent);
                }
            }
            documentReference.SetDocument(fixedDocument);
            xpsDocument.Close();
            return(documentReference);
        }
Esempio n. 4
0
        private async void LoadPageTask(int pageId, TaskInfo taskInfo)
        {
            DispatcherContainer target = (DispatcherContainer)contentStackPanel.Children[pageId];

            // if (target.Child != null && target.Child is FixedPage) return;
            //  if (target.GetIsUpdatingChild()) return;
            // Console.WriteLine("获取页面:" + pageId);
            Console.WriteLine("获取页面:" + pageId + "___ThreadId:" + Thread.CurrentThread.ManagedThreadId);

            //await 回调,执行下一个
            await target.SetChildAsync <FixedPage>(() =>
            {
                FixedPage fp = new FixedPage();
                try
                {
                    XpsDocument xpsDoc = new XpsDocument(wordFullPath, FileAccess.Read);
                    FixedDocumentSequence fixedDocumentSequence = xpsDoc.GetFixedDocumentSequence();
                    foreach (DocumentReference DocRef in fixedDocumentSequence.References)
                    {
                        Console.WriteLine("获取页面await:" + pageId + "___ThreadId:" + Thread.CurrentThread.ManagedThreadId);
                        Console.WriteLine();
                        bool bForceReload       = false;
                        FixedDocument DocFd     = DocRef.GetDocument(bForceReload);
                        PageContent pageContent = DocFd.Pages[pageId];
                        fp = pageContent.GetPageRoot(false);

                        // pageContent.SetValue(ContentPresenter.ContentProperty, null);

                        //for (int i = 0; i < fixedPage.Children.Count; i++)
                        //{
                        //    UIElement uiElement = fp.Children[i];
                        //    fixedPage.Children.Remove(uiElement);
                        //    fp.Children.Add(uiElement);
                        //}
                    }
                    // xpsDoc.Close();//这个地方需要注意关闭,否则的话会出现莫名其妙的错误
                    return(fp);
                }
                catch (Exception)
                {
                    return(fp);
                }
            }
                                                   , () =>
            {
                FixedPage fp = new FixedPage();
                pageQueueList[pageId].list.Remove(taskInfo);
                if (pageQueueList[pageId].list.Count > 0)
                {
                    LoadPageTask(pageId, pageQueueList[pageId].list[0]);
                }
                return(fp);
            });


            //  Console.WriteLine("任务是否完成:"+task.IsCompleted);
        }
Esempio n. 5
0
        /// <summary>
        /// Gets the first fixed page in a fixed document, or null.
        /// </summary>
        /// <param name="doc">The fixed document</param>
        /// <returns>The first fixed document, or null.</returns>
        private static FixedPage GetFirstPageOfFixedDocument(FixedDocument doc)
        {
            FixedPage firstPage = null;

            if (doc != null && doc.Pages.Count != 0)
            {
                PageContent firstCont = doc.Pages[0];
                firstPage = firstCont.GetPageRoot(false);
            }

            return(firstPage);
        }
Esempio n. 6
0
        private void AddPages(DocumentReference documentReference, RollUpFixedDocument document)
        {
            FixedDocument fixedDocument = new FixedDocument();

            documentReference.SetDocument(fixedDocument);
            foreach (RollUpFixedPage page in document.Pages)
            {
                PageContent pageContent = new PageContent();
                if (page.BaseUri == null)
                {
                    (pageContent as IAddChild).AddChild(page.FixedPage);
                }
                else
                {
                    pageContent.Source = page.Source;
                    (pageContent as IUriContext).BaseUri = page.BaseUri;
                }
                pageContent.GetPageRoot(true);
                fixedDocument.Pages.Add(pageContent);
            }
        }
Esempio n. 7
0
        public DocumentReference AddPage(string fileName)
        {
            DocumentReference newDocRef = new DocumentReference();
            FixedDocument     newFd     = new FixedDocument();

            XpsDocument           xpsDocument = new XpsDocument(fileName, FileAccess.Read);
            FixedDocumentSequence docSeq      = xpsDocument.GetFixedDocumentSequence();
            int page = 0;

            foreach (DocumentReference docRef in docSeq.References)
            {
                FixedDocument fd = docRef.GetDocument(false);
                page = fd.Pages.Count;
                foreach (PageContent oldPC in fd.Pages)
                {
                    Uri         uSource        = oldPC.Source;                   //读取源地址
                    Uri         uBase          = (oldPC as IUriContext).BaseUri; //读取目标页面地址
                    PageContent newPageContent = new PageContent();
                    newPageContent.GetPageRoot(false);                           //这个地方应当是把文档解压成一个包放到内存中我们再去读取
                    newPageContent.Source = uSource;
                    (newPageContent as IUriContext).BaseUri = uBase;
                    newFd.Pages.Add(newPageContent);//将新文档追加到新的documentRefences中
                }
            }

            if (page % 2 != 0 && ConfigHelper.GetSetNode("Dprint") == "Y") //双面打印机增加空白页
            {
                FrameworkElement FE = new FrameworkElement();
                FixedPage        FP = new FixedPage();
                FP.Width  = 793.76;
                FP.Height = 1122.56;
                FP.Children.Add(FE);
                PageContent pageContent = new PageContent();
                pageContent.Child = FP;
                newFd.Pages.Add(pageContent);
            }
            newDocRef.SetDocument(newFd);
            xpsDocument.Close();
            return(newDocRef);
        }
Esempio n. 8
0
        private FixedDocumentSequence CreateManagedXpsDocumentFromPages(IEnumerable <PageContent> pages)
        {
            var newSequence     = new FixedDocumentSequence();
            var newDocReference = new DocumentReference();
            var newDoc          = new FixedDocument();

            newDocReference.SetDocument(newDoc);

            foreach (PageContent page in pages)
            {
                var newPage = new PageContent
                {
                    Source = page.Source
                };
                (newPage as IUriContext).BaseUri = (page as IUriContext)?.BaseUri;
                newPage.GetPageRoot(true);
                newDoc.Pages.Add(newPage);
            }

            newSequence.References.Add(newDocReference);
            return(newSequence);
        }
Esempio n. 9
0
        public DocumentReference AddPage(string fileName)
        {
            DocumentReference     documentReference     = new DocumentReference();
            FixedDocument         fixedDocument         = new FixedDocument();
            XpsDocument           xpsDocument           = new XpsDocument(fileName, FileAccess.Read);
            FixedDocumentSequence fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
            int page = 0;

            foreach (DocumentReference current in fixedDocumentSequence.References)
            {
                FixedDocument document = current.GetDocument(false);
                page = document.Pages.Count;
                foreach (PageContent current2 in document.Pages)
                {
                    Uri         source      = current2.Source;
                    Uri         baseUri     = ((IUriContext)current2).BaseUri;
                    PageContent pageContent = new PageContent();
                    pageContent.GetPageRoot(false);
                    pageContent.Source = source;
                    ((IUriContext)pageContent).BaseUri = baseUri;
                    fixedDocument.Pages.Add(pageContent);
                }
            }
            if (page % 2 != 0 && ConfigHelper.GetSetNode("Dprint") == "Y") //双面打印机增加空白页
            {
                FrameworkElement FE = new FrameworkElement();
                FixedPage        FP = new FixedPage();
                FP.Width  = 793.76;
                FP.Height = 1122.56;
                FP.Children.Add(FE);
                PageContent pageContent = new PageContent();
                pageContent.Child = FP;
                fixedDocument.Pages.Add(pageContent);
            }
            documentReference.SetDocument(fixedDocument);
            xpsDocument.Close();
            return(documentReference);
        }
Esempio n. 10
0
 public PageItem(PageContent pageContent)
 {
     _pageContent = pageContent;
     _fixedPage   = _pageContent.GetPageRoot(false);
     _pageBrush   = new VisualBrush(_fixedPage);
 }
Esempio n. 11
0
 public PageItem(PageContent pageContent)
 {
     PageContent = pageContent;
     FixedPage   = PageContent.GetPageRoot(false);
     PageBrush   = new VisualBrush(FixedPage);
 }
Esempio n. 12
0
        /// <summary>
        /// 用于实现的功能,在xps中添加指定的图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1    = new OpenFileDialog();
            string         originalDocument   = "";                                                   //读取模板
            string         detinationDocument = AppDomain.CurrentDomain.BaseDirectory + "newXps.xps"; //生成的新文件

            openFileDialog1.Filter = "图片文件(*.xps)|*.xps";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    originalDocument = openFileDialog1.FileName;

                    // int png_x = Convert.ToInt32(txtX.Text.Trim());
                    //int png_y = Convert.ToInt32(txtY.Text.Trim());

                    int png_x = 0;
                    int png_y = 0;

                    if (File.Exists(detinationDocument))
                    {
                        File.Delete(detinationDocument);
                    }
                    using (Package package = Package.Open(originalDocument, FileMode.Open, FileAccess.Read))
                    {
                        using (Package packageDest = Package.Open(detinationDocument))
                        {
                            string inMemoryPackageName = "memorystream://miXps.xps";
                            Uri    packageUri          = new Uri(inMemoryPackageName);
                            PackageStore.RemovePackage(packageUri);
                            PackageStore.AddPackage(packageUri, package);

                            XpsDocument       xpsDocument           = new XpsDocument(package, CompressionOption.Maximum, inMemoryPackageName);
                            XpsDocument       xpsDocumentDest       = new XpsDocument(packageDest, CompressionOption.Normal, detinationDocument);
                            var               fixedDocumentSequence = xpsDocument.GetFixedDocumentSequence();
                            DocumentReference docReference          = xpsDocument.GetFixedDocumentSequence().References.First();
                            FixedDocument     doc     = docReference.GetDocument(false);
                            PageContent       content = doc.Pages[0];

                            FixedPage fixedPage = content.GetPageRoot(false);
                            //大容器
                            Canvas containCanvas = new Canvas();
                            containCanvas.Width  = fixedPage.Width;
                            containCanvas.Height = fixedPage.Height;


                            containCanvas.Background = Brushes.Transparent;

                            xps_search_point p = new xps_search_point(0, 0, 11);
                            //xps缩放比例
                            double RenderTrans = 1;
                            //获得页面宽度和高度
                            string searchText = "姓名";

                            //循环xps字元素,查找指定内容
                            for (int i = 0; i < fixedPage.Children.Count; i++)
                            {
                                UIElement DocFpUiElem = fixedPage.Children[i];
                                if (DocFpUiElem is Glyphs)
                                {
                                    Glyphs gps     = (Glyphs)DocFpUiElem;
                                    string strMark = gps.UnicodeString;
                                    //找到位置
                                    if (strMark.Trim() == searchText)
                                    {
                                        p = new xps_search_point(gps.OriginX, gps.OriginY, gps.FontRenderingEmSize);
                                        break;
                                    }
                                    textBox1.Text += strMark;
                                }
                                else if (DocFpUiElem is Canvas)
                                {
                                    Canvas cv1 = (Canvas)DocFpUiElem;
                                    //xps缩放比例
                                    RenderTrans = cv1.RenderTransform.Value.M11;
                                    p           = GetGlyphs((Canvas)DocFpUiElem, searchText);
                                }
                            }



                            Image  img = new Image();
                            string fn  = @"d:\1011.png";
                            img.Source = new BitmapImage(new Uri(fn));
                            Canvas cvs = new Canvas();
                            Canvas.SetLeft(cvs, Convert.ToDouble(png_x));
                            //Y坐标*缩放比例-图像本身高度
                            Canvas.SetTop(cvs, Convert.ToDouble(png_y));
                            img.Width  = Convert.ToDouble(62);
                            img.Height = Convert.ToDouble(27);
                            //填入图片
                            cvs.Children.Add(img);
                            //将图片cvs 添加到大容器中
                            containCanvas.Children.Add(cvs);
                            //将大容器填充到 xps Page页面当中
                            fixedPage.Children.Add(containCanvas);
                            ((IAddChild)content).AddChild(fixedPage);
                            //创建写入流,输出保存文件
                            var writter = XpsDocument.CreateXpsDocumentWriter(xpsDocumentDest);
                            writter.Write(fixedPage);

                            //释放
                            xpsDocumentDest.Close();
                            xpsDocument.Close();
                            System.Windows.Forms.MessageBox.Show("操作完成");
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
        }