GetStream() public method

public GetStream ( FileMode mode, FileAccess access ) : Stream
mode FileMode
access FileAccess
return Stream
Beispiel #1
0
        /// <summary>
        /// Loads a document 'Input.docx' and writes the text 'Hello World' into the first imbedded Image.
        /// This code creates the file 'Output.docx'.
        /// </summary>
        static void ProgrammaticallyManipulateImbeddedImage()
        {
            Console.WriteLine("\tProgrammaticallyManipulateImbeddedImage()");
            const string str = "Hello World";

            // Open the document Input.docx.
            using (DocX document = DocX.Load(@"Input.docx"))
            {
                // Make sure this document has at least one Image.
                if (document.Images.Count() > 0)
                {
                    Novacode.Image img = document.Images[0];

                    // Write "Hello World" into this Image.
                    Bitmap b = new Bitmap(img.GetStream(FileMode.Open, FileAccess.ReadWrite));

                    /*
                     * Get the Graphics object for this Bitmap.
                     * The Graphics object provides functions for drawing.
                     */
                    Graphics g = Graphics.FromImage(b);

                    // Draw the string "Hello World".
                    g.DrawString
                    (
                        str,
                        new Font("Tahoma", 20),
                        Brushes.Blue,
                        new PointF(0, 0)
                    );

                    // Save this Bitmap back into the document using a Create\Write stream.
                    b.Save(img.GetStream(FileMode.Create, FileAccess.Write), ImageFormat.Png);
                }
                else
                {
                    Console.WriteLine("The provided document contains no Images.");
                }

                // Save this document as Output.docx.
                document.SaveAs(@"docs\Output.docx");
                Console.WriteLine("\tCreated: docs\\Output.docx\n");
            }
        }
Beispiel #2
0
        private Stream CreateLabel(int id)
        {
            var    model        = new ProfileBoxModel();
            var    profileBox   = _profileBoxService.GetById(id);
            string label        = profileBox.Name.ToUpper();
            string profileCount = "";

            if (!profileBox.ProfileType.Scanned)
            {
                profileCount = profileBox.ProfileCount.ToString();
            }
            else
            {
                var profiles = _profileService.SearchProfilesByStatus(id, 0, 1, int.MaxValue, null, null);
                profileCount = profiles.Count(x => x.StatusId != (short)ProfileStatus.Deleted).ToString();
            }
            string filePath     = Server.MapPath(Url.Content("~/Template/Label.docx"));
            DocX   document     = DocX.Load(filePath);
            var    outputStream = new MemoryStream();

            document.ReplaceText("{Label}", label);
            document.ReplaceText("{count}", profileCount);
            // get placeholder image
            Novacode.Image img = document.Images[0];
            // create barcode encoder
            var barcodeWriter = new BarcodeWriter
            {
                Format  = BarcodeFormat.CODE_128,
                Options = new EncodingOptions
                {
                    PureBarcode = false,
                    Height      = 100,
                    Width       = 300,
                    Margin      = 10
                }
            };
            // create barcode image
            var bitmap = barcodeWriter.Write(label);

            // replace place holder image in document with barcode image
            bitmap.Save(img.GetStream(FileMode.Create, FileAccess.Write), ImageFormat.Png);

            document.SaveAs(outputStream);
            outputStream.Position = 0;
            document.Dispose();
            return(outputStream);
        }