Exemplo n.º 1
0
        //保存到XPS格式文档
        private void SaveToXpsFile()
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.Filter = "XPS文档(*.xps)|*.xps";
            if (dlg.ShowDialog() != true)
            {
                return;
            }
            try
            {
                //创建XPS文件内容
                FixedDocument   fixedDoc     = null;
                List <DrugInfo> curPrintData = new List <DrugInfo>();
                curPrintData.AddRange(printData);
                curPrintData.RemoveAll(data => radioSelected.IsChecked == true && data.tempuse == 0);
                fixedDoc = CreateIdentifyDocument(curPrintData);
                if (fixedDoc == null)
                {
                    return;
                }

                //保存到XPS文件
                DocumentPaginator paginator   = fixedDoc.DocumentPaginator;
                XpsDocument       xpsDocument = new XpsDocument(dlg.FileName, FileAccess.Write);
                System.Windows.Xps.XpsDocumentWriter documentWriter = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
                documentWriter.Write(paginator);
                xpsDocument.Close();

                System.Diagnostics.Process.Start(dlg.FileName);
            }
            catch (System.Exception ex)
            {
                CommonMethod.ErrorMsgBox(ex.Message);
            }
        }
Exemplo n.º 2
0
        private void DoThePrint(System.Windows.Documents.FlowDocument document)
        {
            // Clone the source document's content into a new FlowDocument.
            // This is because the pagination for the printer needs to be
            // done differently than the pagination for the displayed page.
            // We print the copy, rather that the original FlowDocument.
            System.IO.MemoryStream s = new System.IO.MemoryStream();
            TextRange source         = new TextRange(document.ContentStart, document.ContentEnd);

            source.Save(s, DataFormats.Xaml);
            FlowDocument copy = new FlowDocument();
            TextRange    dest = new TextRange(copy.ContentStart, copy.ContentEnd);

            dest.Load(s, DataFormats.Xaml);

            // Create a XpsDocumentWriter object, implicitly opening a Windows common print dialog,
            // and allowing the user to select a printer.

            // get information about the dimensions of the seleted printer+media.
            System.Printing.PrintDocumentImageableArea ia        = null;
            System.Windows.Xps.XpsDocumentWriter       docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(ref ia);

            double pixelsInOneMM = 3.77952755905512;

            if (docWriter != null && ia != null)
            {
                DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator;

                // Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device.
                paginator.PageSize = new Size(ia.MediaSizeWidth, ia.MediaSizeHeight);
                Thickness t = new Thickness(6 * pixelsInOneMM, 16 * pixelsInOneMM, 6 * pixelsInOneMM, 16 * pixelsInOneMM);  // copy.PagePadding;
                //copy.PagePadding = new Thickness(
                //                 Math.Max(ia.OriginWidth, t.Left),
                //                   Math.Max(ia.OriginHeight, t.Top),
                //                   Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), t.Right),
                //                   Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), t.Bottom));

                copy.ColumnWidth = double.PositiveInfinity;
                //copy.PageWidth = 528; // allow the page to be the natural with of the output device

                // Send content to the printer.
                docWriter.Write(paginator);
            }
        }
Exemplo n.º 3
0
        //function to save WPF document as XPS then transform into PDF with PdfSharp dll
        public void SavePDF()
        {
            Microsoft.Win32.SaveFileDialog save = new Microsoft.Win32.SaveFileDialog();
            save.FileName        = "StatisticReport";
            save.DefaultExt      = ".pdf";
            save.Filter          = "PDF Documents (.pdf)|*.pdf";
            save.OverwritePrompt = true;

            Nullable <bool> result = save.ShowDialog();

            if (result == true)
            {
                string fileName = save.FileName;
                try
                {
                    if (System.IO.File.Exists(fileName))
                    {
                        System.IO.File.Delete(fileName);
                    }
                }

                catch (IOException e)
                {
                }
                MemoryStream  ms     = new MemoryStream();
                Package       pkg    = Package.Open(ms, FileMode.Create);
                FixedDocument doc    = (FixedDocument)docViewer.Document;
                XpsDocument   xpsDoc = new XpsDocument(pkg);
                System.Windows.Xps.XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
                xpsWriter.Write(doc);
                xpsDoc.Close();
                pkg.Close();
                var xpsToPdf = PdfSharp.Xps.XpsModel.XpsDocument.Open(ms);
                PdfSharp.Xps.XpsConverter.Convert(xpsToPdf, fileName, 0);
            }
        }
Exemplo n.º 4
0
 private static void PrintDocumentPaginator(XpsDocumentWriter xpsDocumentWriter, DocumentPaginator document)
 {
     xpsDocumentWriter.Write(document);
 }
Exemplo n.º 5
0
        private void CreateDocument_STAThread()
        {
            #region Prep
            System.Collections.ArrayList data = objData;
            //Create new document
            FixedDocument doc = new FixedDocument();
            //Set page size
            doc.DocumentPaginator.PageSize = new Size(PAPER_SIZE_WIDTH_96, PAPER_SIZE_HEIGHT_96);

            //Number of records
            double count = (double)data.Count;
            #endregion

            if (count > 0)
            {
                #region Declare Variables
                AveryBarcodeLabel label;

                //Determine number of pages to generate
                double pageCount = Math.Ceiling(count / LABELS_PER_SHEET);

                int dataIndex        = 0;
                int currentColumn    = 0;
                int currentPDFColumn = 0;
                int currentRow       = 0;

                iTextSharp.text.pdf.PdfPTable objPDFTable = null;
                #endregion

                #region Open PDF Document
                iTextSharp.text.Rectangle rectPaperSize = new iTextSharp.text.Rectangle((float)PAPER_SIZE_WIDTH_72, (float)PAPER_SIZE_HEIGHT_72);
                iTextSharp.text.Document  objPDFDoc     = CreatePagePDF(_strFilePathPDF, rectPaperSize, (float)SIDE_MARGIN_72, (float)SIDE_MARGIN_72, (float)(TOP_MARGIN_72), 0);
                //objPDFDoc.
                #endregion

                #region Define PDF Column Widths
                //Define PDF Column Widths
                float[] columnWidth = new float[5];
                columnWidth[0] = (float)LABEL_WIDTH_72;
                columnWidth[1] = (float)HORIZONTAL_GAP_72;
                columnWidth[2] = (float)LABEL_WIDTH_72;
                columnWidth[3] = (float)HORIZONTAL_GAP_72;
                columnWidth[4] = (float)LABEL_WIDTH_72;
                #endregion

                for (int i = 0; i < pageCount; i++)
                {
                    #region Prep XPS Page
                    //Create page
                    PageContent page      = new PageContent();
                    FixedPage   fixedPage = this.CreatePageXPS();
                    #endregion

                    //Create labels
                    for (int j = 0; j < 30; j++)
                    {
                        #region Set currentRow
                        if (j % 10 == 0)
                        {
                            currentRow = 0;
                        }
                        else
                        {
                            currentRow++;
                        }
                        #endregion

                        #region Set curentColumn (Vertically)
                        if (j < 10)
                        {
                            currentColumn = 0;
                        }
                        else if (j > 19)
                        {
                            currentColumn = 2;
                        }
                        else
                        {
                            currentColumn = 1;
                        }
                        #endregion

                        #region Set currentPDFColumn (Horizontally)
                        if (j % 3 == 0)
                        {
                            currentPDFColumn = 0;
                        }
                        else if (j % 3 == 1)
                        {
                            currentPDFColumn = 1;
                        }
                        else if (j % 3 == 2)
                        {
                            currentPDFColumn = 2;
                        }
                        #endregion

                        if (dataIndex < count)
                        {
                            #region Start a New Page When Necessary
                            if (dataIndex % 30 == 0 || dataIndex == 0)
                            {
                                if (objPDFTable != null)
                                {
                                    objPDFDoc.Add(objPDFTable);
                                    objPDFDoc.NewPage();
                                }
                                objPDFTable = new iTextSharp.text.pdf.PdfPTable(5);
                                objPDFTable.SetTotalWidth(columnWidth);
                                objPDFTable.LockedWidth = true;
                                //objPDFTable.SplitLate = false;
                                objPDFTable.SkipLastFooter = true;
                                objPDFTable.KeepTogether   = true;
                                //objPDFTable.ExtendLastRow = true;
                            }
                            #endregion

                            #region Get Data and Fill Lines
                            label = new AveryBarcodeLabel();

                            if (data[dataIndex].GetType() == typeof(AveryBarcodeLabel))
                            {
                                label = (AveryBarcodeLabel)data[dataIndex];
                            }
                            else if (data[dataIndex].GetType() == typeof(AveryLabelDataModel))
                            {
                                label = new AveryBarcodeLabel((AveryLabelDataModel)data[dataIndex]);
                            }
                            else if (data[dataIndex].GetType() == typeof(PostalAddress))
                            {
                                PostalAddress objAddr = (PostalAddress)data[dataIndex];
                                label.Line1 = objAddr.Reference;
                                label.Line2 = objAddr.Address1;
                                if (!StringFunctions.IsNullOrWhiteSpace(objAddr.Address2))
                                {
                                    label.Line2 += " " + objAddr.Address2;
                                }
                                if (!StringFunctions.IsNullOrWhiteSpace(objAddr.Address3))
                                {
                                    label.Line2 += " " + objAddr.Address3;
                                }
                                label.Line3 = objAddr.ToLocationString() + " " + objAddr.PostalCode;
                            }
                            else if (data[dataIndex].GetType() == typeof(AddressBookEntry))
                            {
                                AddressBookEntry objAddr = (AddressBookEntry)data[dataIndex];
                                label.Line1 = objAddr.FullName;
                                label.Line2 = objAddr.Address1;
                                if (!StringFunctions.IsNullOrWhiteSpace(objAddr.Address2))
                                {
                                    label.Line2 += " " + objAddr.Address2;
                                }
                                if (!StringFunctions.IsNullOrWhiteSpace(objAddr.Address3))
                                {
                                    label.Line2 += " " + objAddr.Address3;
                                }
                                label.Line3 = objAddr.ToLocationString() + " " + objAddr.PostalCode;
                                label.Email = objAddr.Email;
                                label.Phone = objAddr.Phone;
                            }

                            /*
                             * line1 = (string)data.Rows[dataIndex]["Name"];
                             * line2 = (string)data.Rows[dataIndex]["Address"];
                             * postalCode = (string)data.Rows[dataIndex]["PostalCode"];
                             * line3 = (string)data.Rows[dataIndex]["City"] + " " + (string)data.Rows[dataIndex]["State"] + " " + postalCode;
                             */
                            #endregion

                            #region Draw Label Cell in PDF
                            //Create individual label
                            iTextSharp.text.pdf.PdfPTable tblCell;

                            if ((label.Phone != null && label.Phone.Valid) || (label.Email != null && label.Email.Valid) || (label.Date != null && label.Date != DateTime.MinValue && label.Date != DateTime.MaxValue))
                            {
                                #region Label With Phone/Email/Date
                                Font objFont = new Font();
                                objFont.Size = Font.DEFAULTSIZE - 4;

                                tblCell = new iTextSharp.text.pdf.PdfPTable(2);

                                if (label.Date != null && label.Date != DateTime.MinValue && label.Date != DateTime.MaxValue)
                                {
                                    iTextSharp.text.pdf.PdfPCell objLine1 = new iTextSharp.text.pdf.PdfPCell(new Phrase(label.Line1, objFont));
                                    objLine1.Border = 0;
                                    tblCell.AddCell(objLine1);

                                    iTextSharp.text.pdf.PdfPCell objDate = new iTextSharp.text.pdf.PdfPCell(new Phrase("wd: " + label.Date.ToShortDateString(), objFont));
                                    objDate.HorizontalAlignment = 2; //Right
                                    objDate.Border = 0;
                                    tblCell.AddCell(objDate);
                                }
                                else
                                {
                                    iTextSharp.text.pdf.PdfPCell objLine1 = new iTextSharp.text.pdf.PdfPCell(new Phrase(label.Line1, objFont));
                                    objLine1.Border  = 0;
                                    objLine1.Colspan = 2;
                                    tblCell.AddCell(objLine1);
                                }

                                iTextSharp.text.pdf.PdfPCell objLine2 = new iTextSharp.text.pdf.PdfPCell(new Phrase(label.Line2, objFont));
                                objLine2.Border  = 0;
                                objLine2.Colspan = 2;
                                tblCell.AddCell(objLine2);

                                if (label.Phone != null && label.Phone.Valid)
                                {
                                    iTextSharp.text.pdf.PdfPCell objLine3 = new iTextSharp.text.pdf.PdfPCell(new Phrase(label.Line3, objFont));
                                    objLine3.Border = 0;
                                    objLine3.NoWrap = true;
                                    tblCell.AddCell(objLine3);

                                    iTextSharp.text.pdf.PdfPCell objPhone = new iTextSharp.text.pdf.PdfPCell(new Phrase(label.Phone.ToString(), objFont));
                                    objPhone.HorizontalAlignment = 2; //Right
                                    objPhone.Border = 0;
                                    tblCell.AddCell(objPhone);
                                }
                                else
                                {
                                    iTextSharp.text.pdf.PdfPCell objLine3 = new iTextSharp.text.pdf.PdfPCell(new Phrase(label.Line3, objFont));
                                    objLine3.Border  = 0;
                                    objLine3.Colspan = 2;
                                    tblCell.AddCell(objLine3);
                                }

                                if (label.Email != null && label.Email.Valid)
                                {
                                    iTextSharp.text.pdf.PdfPCell objEmail = new iTextSharp.text.pdf.PdfPCell(new Phrase(label.Email.ToString(), objFont));
                                    objEmail.HorizontalAlignment = 2; //Right
                                    objEmail.Border  = 0;
                                    objEmail.Colspan = 2;
                                    tblCell.AddCell(objEmail);
                                }

                                #endregion
                            }
                            else
                            {
                                #region Standard Address Label
                                tblCell = new iTextSharp.text.pdf.PdfPTable(1);

                                Font objNameFont = new Font();
                                objNameFont.Size = Font.DEFAULTSIZE - 1;
                                iTextSharp.text.pdf.PdfPCell objLine1 = new iTextSharp.text.pdf.PdfPCell(new Phrase(label.Line1, objNameFont));
                                objLine1.Border = 0;
                                tblCell.AddCell(objLine1);

                                Font objAddrFont = new Font();
                                objAddrFont.Size = Font.DEFAULTSIZE - 3;
                                iTextSharp.text.pdf.PdfPCell objLine2 = new iTextSharp.text.pdf.PdfPCell(new Phrase(label.Line2, objAddrFont));
                                objLine2.Border = 0;
                                tblCell.AddCell(objLine2);

                                if (!StringFunctions.IsNullOrWhiteSpace(label.Line3))
                                {
                                    iTextSharp.text.pdf.PdfPCell objLine3 = new iTextSharp.text.pdf.PdfPCell(new Phrase(label.Line3, objAddrFont));
                                    objLine3.Border = 0;
                                    tblCell.AddCell(objLine3);
                                }

                                #endregion
                            }
                            iTextSharp.text.pdf.PdfPCell pCell = new iTextSharp.text.pdf.PdfPCell(tblCell);
                            pCell.FixedHeight = (float)(LABEL_HEIGHT_72);
                            pCell.Padding     = 5;
                            pCell.Border      = 0;
                            objPDFTable.AddCell(pCell);
                            #endregion

                            #region Add Spacer Cell
                            iTextSharp.text.pdf.PdfPCell objGap = new iTextSharp.text.pdf.PdfPCell();
                            objGap.Border = 0;

                            if (currentPDFColumn == 0)
                            {
                                objPDFTable.AddCell(objGap);
                            }
                            else if (currentPDFColumn == 1)
                            {
                                objPDFTable.AddCell(objGap);
                            }
                            #endregion

                            #region Set XPS Position and Add to Document
                            //Set label location
                            if (currentColumn == 0)
                            {
                                FixedPage.SetLeft(label, SIDE_MARGIN_96);
                            }
                            else if (currentColumn == 1)
                            {
                                FixedPage.SetLeft(label, SIDE_MARGIN_96 + LABEL_WIDTH_96 + HORIZONTAL_GAP_96);
                            }
                            else
                            {
                                FixedPage.SetLeft(label, SIDE_MARGIN_96 + LABEL_WIDTH_96 * 2 + HORIZONTAL_GAP_96 * 2);
                            }
                            FixedPage.SetTop(label, TOP_MARGIN_96 + currentRow * LABEL_HEIGHT_96);

                            //Add label object to page
                            fixedPage.Children.Add(label);
                            #endregion

                            #region Finish Last PDF Row By Adding Blanks
                            if (dataIndex == count - 1) //If I'm on the last cell
                            {
                                if (currentPDFColumn != 3)
                                {
                                    while (currentPDFColumn != 3)
                                    {
                                        currentPDFColumn++;
                                        objPDFTable.AddCell(objGap);
                                    }
                                }
                            }
                            #endregion

                            dataIndex++;
                        }
                    }

                    #region Finalize XPS Page
                    //Invoke Measure(), Arrange() and UpdateLayout() for drawing
                    fixedPage.Measure(new Size(PAPER_SIZE_WIDTH_96, PAPER_SIZE_HEIGHT_96));
                    fixedPage.Arrange(new Rect(new Point(), new Size(PAPER_SIZE_WIDTH_96, PAPER_SIZE_HEIGHT_96)));
                    fixedPage.UpdateLayout();
                    ((IAddChild)page).AddChild(fixedPage);
                    doc.Pages.Add(page);
                    #endregion
                }

                #region Finalize PDF Document
                objPDFDoc.Add(objPDFTable);
                objPDFDoc.Close();
                #endregion
            }

            #region Write XPS Document
            if (!StringFunctions.IsNullOrWhiteSpace(_strFilePathXPS))
            {
                if (File.Exists(_strFilePathXPS))
                {
                    File.Delete(_strFilePathXPS);
                }
                XpsDocument xpsd = new XpsDocument(_strFilePathXPS, FileAccess.ReadWrite);
                System.Windows.Xps.XpsDocumentWriter xw = XpsDocument.CreateXpsDocumentWriter(xpsd);
                xw.Write(doc);
                xpsd.Close();
            }
            #endregion
        }
Exemplo n.º 6
0
        /// <summary>
        /// Write the specified <see cref="IPlotModel" /> to the specified <see cref="XpsDocumentWriter" />.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="writer">The document writer.</param>
        private void Write(IPlotModel model, XpsDocumentWriter writer)
        {
            var canvas = new Canvas { Width = this.Width, Height = this.Height, Background = this.Background.ToBrush() };
            canvas.Measure(new Size(this.Width, this.Height));
            canvas.Arrange(new Rect(0, 0, this.Width, this.Height));

            var rc = new CanvasRenderContext(canvas);
            rc.TextFormattingMode = this.TextFormattingMode;

            model.Update(true);
            model.Render(rc, this.Width, this.Height);

            canvas.UpdateLayout();

            writer.Write(canvas);
        }
Exemplo n.º 7
0
 // ------------------------- PrintVisualAsync -------------------------
 /// <summary>
 ///   Synchronously prints a given visual
 ///   to a specified document writer.</summary>
 /// <param name="xpsdw">
 ///   The document writer to output to.</param>
 /// <param name="v">
 ///   The visual to print.</param>
 private void PrintVisual(XpsDocumentWriter xpsdw, Visual v)
 {
     xpsdw.Write(v);     // Write visual to single page
 }
Exemplo n.º 8
0
 // ------------------ PrintSingleFlowContentDocument ------------------
 /// <summary>
 ///   Synchronously prints a given paginated flow
 ///   document to a specified document writer.</summary>
 /// <param name="xpsdw">
 ///   The document writer to output to.</param>
 /// <param name="idp">
 ///   The paginated flow document to print.</param>
 private void PrintSingleFlowContentDocument(
     XpsDocumentWriter xpsdw, DocumentPaginator idp)
 {
     xpsdw.Write(idp);   // Write the IDP as a document
 }
Exemplo n.º 9
0
 // ----------------- PrintSingleFixedContentDocument ------------------
 /// <summary>
 ///   Synchronously prints of a given fixed
 ///   document to a specified document writer.</summary>
 /// <param name="xpsdw">
 ///   The document writer to output to.</param>
 /// <param name="fd">
 ///   The fixed document to print.</param>
 private void PrintSingleFixedContentDocument(
     XpsDocumentWriter xpsdw, FixedDocument fd)
 {
     xpsdw.Write(fd);    // Write the FixedDocument as a document.
 }
Exemplo n.º 10
0
 // ---------------- PrintMultipleFixedContentDocuments ----------------
 /// <summary>
 ///   Synchronously prints multiple fixed documents from a given
 ///   FixedDocumentSequence to a specified DocumentWriter.</summary>
 /// <param name="xpsdw">
 ///   The document writer to output to.</param>
 /// <param name="fds">
 ///   The fixed document sequence to print.</param>
 private void PrintMultipleFixedContentDocuments(
     XpsDocumentWriter xpsdw, FixedDocumentSequence fds)
 {
     xpsdw.Write(fds);   // Write as a collection of documents.
 }