Example #1
0
        /// <summary>
        /// Create .docx File
        /// </summary>
        /// <param name="fileName">File name of .docx document</param>
        /// <param name="PageSize">New Page Size</param>
        /// <exception cref="System.ArgumentNullException">The exception that is thrown when a null reference is passed to a method that does not accept it as a valid argument.</exception>
        /// <exception cref="DocumentFormat.OpenXml.Packaging.OpenXmlPackageException">OpenXml exception class</exception>
        public bool Create(string fileName, TPageSettings PageSize)
        {
            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                File.Delete(fileName);
            }
            catch (Exception) { }

            if (doc != null)
            {
                doc.Close();
            }
            isOpen = _create(fileName, PageSize);
            return(isOpen);
        }
Example #2
0
        /// <summary>
        /// Internal open file function
        /// </summary>
        /// <param name="fileName">File name</param>
        /// <returns>Return true of open ok</returns>
        private bool _create(string fileName, TPageSettings PageSize)
        {
            this.filename = fileName;
            try
            {
                const float SizeConst = 16838.0f / 297.0f;
                //const float SizeConst = 16838/ 297;

                /*byte[] byteArray = File.ReadAllBytes(fileName);
                 * stream = new MemoryStream();
                 * stream.Write(byteArray, 0, (int)byteArray.Length);*/
                doc = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document);
                doc.AddMainDocumentPart();
                doc.MainDocumentPart.Document      = new Document();
                doc.MainDocumentPart.Document.Body = new Body();

                SectionProperties SecPro = new SectionProperties();
                PageSize          PSize  = new PageSize();
                // w:w="11906" w:h="16838"/>
                PSize.Width  = (UInt32Value)(SizeConst * PageSize.Width);
                PSize.Height = (UInt32Value)(SizeConst * PageSize.Height);
                SecPro.Append(PSize);

                PageMargin pageMargins1 = new PageMargin();
                pageMargins1.Left   = (UInt32Value)(10 * 50.8); // 10mm
                pageMargins1.Right  = (UInt32Value)(10 * 50.8);
                pageMargins1.Top    = (Int32Value)(10 * 50.8);
                pageMargins1.Bottom = (Int32Value)(10 * 50.8);

                SecPro.Append(pageMargins1);

                doc.MainDocumentPart.Document.Body.Append(SecPro);

                ParseParagraphs();
                return(true);
            }
            catch (Exception err)
            {
                if (doc != null)
                {
                    doc.Close();
                }
                return(false);
            }
        }