Exemple #1
0
        public void MemoryOptimization()
        {
            //ExStart
            //ExFor:SaveOptions.MemoryOptimization
            //ExSummary:Shows an option to optimize memory consumption when you work with large documents.
            Document doc = new Document(MyDir + "SaveOptions.MemoryOptimization.doc");
            // When set to true it will improve document memory footprint but will add extra time to processing.
            // This optimization is only applied during save operation.
            SaveOptions saveOptions = SaveOptions.CreateSaveOptions(SaveFormat.Pdf);

            saveOptions.MemoryOptimization = true;

            doc.Save(MyDir + @"\Artifacts\SaveOptions.MemoryOptimization.pdf", saveOptions);
            //ExEnd
        }
        public override string Create <T>(string path, List <T> data)
        {
            Document doc = new Document(path);

            System.Data.DataSet ds = new System.Data.DataSet();
            if (typeof(T) == typeof(System.Data.DataTable))
            {
                foreach (var item in data)
                {
                    var obj = (object)item;
                    var db  = (System.Data.DataTable)obj;
                    db.TableName = "DBTable";
                    ds.Tables.Add(db);
                }
            }
            else
            {
                var table = data.ToTable();
                table.TableName = "DBTable";
                ds.Tables.Add(table);
            }


            //doc.MailMerge.TrimWhitespaces = false;
            //doc.MailMerge.Execute(names, values);
            doc.MailMerge.UseNonMergeFields = true;
            //设置父子表的关系,主表Dt的Id关联从表XM的UserId
            //ds.Relations.Add(dt.Columns["Id"], xmDt.Columns["UserId"]);
            doc.MailMerge.ExecuteWithRegions(ds);
            string ext      = Path.GetExtension(path);
            string filename = null;

            if (string.IsNullOrEmpty(filename))
            {
                filename = Guid.NewGuid().ToString("N") + ext;
            }
            else
            {
                if (!filename.Contains('.'))
                {
                    filename = filename + ext;
                }
            }
            string fileSavePath = Path.GetDirectoryName(path) + "\\" + filename;

            //if (!string.IsNullOrEmpty(newFileName)) {

            //    if (File.Exists(fileSavePath)) {
            //        File.Delete(fileSavePath);
            //    }
            //}
            ws.SaveOptions saveOptions = ws.SaveOptions.CreateSaveOptions(path);
            saveOptions.PrettyFormat = true;
            SectionCollection sc = doc.Sections;

            foreach (Section section in sc)
            {
                section.PageSetup.PaperSize         = PaperSize.A4;
                section.PageSetup.Orientation       = Orientation.Portrait;
                section.PageSetup.VerticalAlignment = PageVerticalAlignment.Top;
                section.PageSetup.PageNumberStyle   = NumberStyle.VietCardinalText;
            }
            doc.Save(fileSavePath, saveOptions);
            return(filename);
        }