Exemplo n.º 1
0
        /// <summary>
        /// Implements the PDF file to XPS file conversion.
        /// </summary>
        public static void Convert(XpsDocument xpsDocument, string pdfFilename, int docIndex)
        {
            if (xpsDocument == null)
            {
                throw new ArgumentNullException("xpsDocument");
            }

            if (String.IsNullOrEmpty(pdfFilename))
            {
                throw new ArgumentNullException("pdfFilename");
            }

            FixedDocument fixedDocument = xpsDocument.GetDocument();
            PdfDocument   pdfDocument   = new PdfDocument();
            PdfRenderer   renderer      = new PdfRenderer();

            int pageIndex = 0;

            foreach (FixedPage page in fixedDocument.Pages)
            {
                if (page == null)
                {
                    continue;
                }
                Debug.WriteLine(String.Format("  doc={0}, page={1}", docIndex, pageIndex));
                PdfPage pdfPage = renderer.CreatePage(pdfDocument, page);
                renderer.RenderPage(pdfPage, page);
                pageIndex++;
            }
            pdfDocument.Save(pdfFilename);
        }
Exemplo n.º 2
0
        public static void ConvertToPdfInMemory(XpsDocument xpsDocument, MemoryStream pdfMemoryStream, int docIndex)
        {
            if (xpsDocument == null)
            {
                throw new ArgumentNullException("xpsDocument");
            }
            if (pdfMemoryStream == null)
            {
                throw new ArgumentNullException("pdfMemoryStream");
            }

            FixedDocument fixedDocument = xpsDocument.GetDocument();
            PdfDocument   pdfDocument   = new PdfDocument();
            PdfRenderer   renderer      = new PdfRenderer();

            int pageIndex = 0;

            foreach (FixedPage page in fixedDocument.Pages)
            {
                if (page == null)
                {
                    continue;
                }
                PdfPage pdfPage = renderer.CreatePage(pdfDocument, page);
                renderer.RenderPage(pdfPage, page);
                pageIndex++;
            }
            pdfDocument.Save(pdfMemoryStream, false);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Implements the PDF file to XPS file conversion.
        /// </summary>
        public static void Convert(Stream xpsInStream, Stream pdfOutStream, bool closePdfStream)
        {
            if (xpsInStream == null)
            {
                throw new ArgumentNullException("xpsInStream");
            }

            if (pdfOutStream == null)
            {
                throw new ArgumentNullException("pdfOutStream");
            }

            XpsDocument xpsDocument = null;

            try
            {
                xpsDocument = XpsDocument.Open(xpsInStream);
                FixedDocument fixedDocument = xpsDocument.GetDocument();

                using (PdfDocument pdf = Convert(fixedDocument.Pages))
                {
                    pdf.Save(pdfOutStream, closePdfStream);
                }

                xpsDocument.Close();
                xpsDocument = null;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                if (xpsDocument != null)
                {
                    xpsDocument.Close();
                }
                throw;
            }
            finally
            {
                if (xpsDocument != null)
                {
                    xpsDocument.Close();
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Implements the PDF file to XPS file conversion.
        /// </summary>
        public static void Convert(XpsDocument xpsDocument, string pdfFilename, int docIndex)
        {
            if (xpsDocument == null)
            {
                throw new ArgumentNullException("xpsDocument");
            }

            if (String.IsNullOrEmpty(pdfFilename))
            {
                throw new ArgumentNullException("pdfFilename");
            }

            FixedDocument fixedDocument = xpsDocument.GetDocument();
            PdfDocument   pdfDocument   = new PdfDocument();
            PdfRenderer   renderer      = new PdfRenderer();

            int pageIndex = 0;

            foreach (FixedPage page in fixedDocument.Pages)
            {
                if (page == null)
                {
                    continue;
                }
                Debug.WriteLine(String.Format("  doc={0}, page={1}", docIndex, pageIndex));
                PdfPage pdfPage = renderer.CreatePage(pdfDocument, page);
                renderer.RenderPage(pdfPage, page);
                pageIndex++;

/* DEBUG constant must be active in order to avoid problem with partial pages.
 * See: http://www.nathanpjones.com/wp/2013/03/output-to-pdf-in-wpf-for-free/
 * So, the next limitation is no longer possible...
 #if DEBUG
 *          // stop at page...
 *          if (pageIndex == 50)
 *              break;
 #endif */
            }
            pdfDocument.Save(pdfFilename);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Implements the PDF file to XPS file conversion.
        /// </summary>
        public static void Convert(string xpsFilename, string pdfFilename, int docIndex, bool createComparisonDocument)
        {
            if (String.IsNullOrEmpty(xpsFilename))
            {
                throw new ArgumentNullException("xpsFilename");
            }

            if (String.IsNullOrEmpty(pdfFilename))
            {
                pdfFilename = xpsFilename;
                if (IOPath.HasExtension(pdfFilename))
                {
                    pdfFilename = pdfFilename.Substring(0, pdfFilename.LastIndexOf('.'));
                }
                pdfFilename += ".pdf";
            }

            XpsDocument xpsDocument = null;

            try
            {
                xpsDocument = XpsDocument.Open(xpsFilename);
                FixedDocument fixedDocument = xpsDocument.GetDocument();
                PdfDocument   pdfDocument   = new PdfDocument();
                PdfRenderer   renderer      = new PdfRenderer();

                int pageIndex = 0;
                foreach (FixedPage page in fixedDocument.Pages)
                {
                    if (page == null)
                    {
                        continue;
                    }
                    Debug.WriteLine(String.Format("  doc={0}, page={1}", docIndex, pageIndex));
                    PdfPage pdfPage = renderer.CreatePage(pdfDocument, page);
                    renderer.RenderPage(pdfPage, page);
                    pageIndex++;

#if DEBUG
                    // stop at page...
                    if (pageIndex == 50)
                    {
                        break;
                    }
#endif
                }
                pdfDocument.Save(pdfFilename);
                xpsDocument.Close();
                xpsDocument = null;

                if (createComparisonDocument)
                {
                    System.Windows.Xps.Packaging.XpsDocument       xpsDoc = new System.Windows.Xps.Packaging.XpsDocument(xpsFilename, FileAccess.Read);
                    System.Windows.Documents.FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
                    if (docSeq == null)
                    {
                        throw new InvalidOperationException("docSeq");
                    }

                    XPdfForm    form = XPdfForm.FromFile(pdfFilename);
                    PdfDocument pdfComparisonDocument = new PdfDocument();


                    pageIndex = 0;
                    foreach (PdfPage page in pdfDocument.Pages)
                    {
                        if (page == null)
                        {
                            continue;
                        }
                        Debug.WriteLine(String.Format("  doc={0}, page={1}", docIndex, pageIndex));

                        PdfPage pdfPage = /*renderer.CreatePage(pdfComparisonDocument, page);*/ pdfComparisonDocument.AddPage();
                        double  width   = page.Width;
                        double  height  = page.Height;
                        pdfPage.Width  = page.Width * 2;
                        pdfPage.Height = page.Height;


                        DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageIndex);
                        //byte[] png = PngFromPage(docPage, 96);

                        BitmapSource bmsource = BitmapSourceFromPage(docPage, 96 * 2);
                        XImage       image    = XImage.FromBitmapSource(bmsource);

                        XGraphics gfx = XGraphics.FromPdfPage(pdfPage);
                        form.PageIndex = pageIndex;
                        gfx.DrawImage(form, 0, 0, width, height);
                        gfx.DrawImage(image, width, 0, width, height);

                        //renderer.RenderPage(pdfPage, page);
                        pageIndex++;

#if DEBUG
                        // stop at page...
                        if (pageIndex == 50)
                        {
                            break;
                        }
#endif
                    }

                    string pdfComparisonFilename = pdfFilename;
                    if (IOPath.HasExtension(pdfComparisonFilename))
                    {
                        pdfComparisonFilename = pdfComparisonFilename.Substring(0, pdfComparisonFilename.LastIndexOf('.'));
                    }
                    pdfComparisonFilename += "-comparison.pdf";

                    pdfComparisonDocument.ViewerPreferences.FitWindow = true;
                    //pdfComparisonDocument.PageMode = PdfPageMode.
                    pdfComparisonDocument.PageLayout = PdfPageLayout.SinglePage;
                    pdfComparisonDocument.Save(pdfComparisonFilename);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                if (xpsDocument != null)
                {
                    xpsDocument.Close();
                }
                throw;
            }
            finally
            {
                if (xpsDocument != null)
                {
                    xpsDocument.Close();
                }
            }
        }
Exemplo n.º 6
0
 internal FixedPageCollection(FixedDocument fdoc)
 {
   this.fdoc = fdoc;
 }
    /// <summary>
    /// Parses a FixedDocument element.
    /// </summary>
    FixedDocument ParseFixedDocument()
    {
      Debug.Assert(this.reader.Name == "FixedDocument");
      bool isEmptyElement = this.reader.IsEmptyElement;
      FixedDocument fdoc = new FixedDocument();
      while (MoveToNextAttribute())
      {
        switch (this.reader.Name)
        {
          default:
            //UnexpectedAttribute();
            break;
        }
      }
      if (!isEmptyElement)
      {
        MoveToNextElement();
        while (this.reader.IsStartElement())
        {
          switch (this.reader.Name)
          {
            case "PageContent":
              {
                isEmptyElement = this.reader.IsEmptyElement;
                while (MoveToNextAttribute())
                {
                  switch (this.reader.Name)
                  {
                    case "Source":
                      fdoc.PageContentUriStrings.Add(this.reader.Value);
                      break;

                    case "Width":
                      // TODO: preview width
                      break;

                    case "Height":
                      // TODO: preview height
                      break;

                    default:
                      UnexpectedAttribute(this.reader.Name);
                      break;
                  }
                }
                if (!isEmptyElement)
                {
                  MoveToNextElement();
                  // Move beyond PageContent.LinkTargets
                  if (this.reader.IsStartElement())
                    MoveBeyondThisElement();
                }
              }
              MoveToNextElement();
              break;

            case "LinkTarget":
              Debug.Assert(false);
              MoveBeyondThisElement();
              break;

            default:
              Debugger.Break();
              break;
          }
        }
      }
      MoveToNextElement();
      return fdoc;
    }