Пример #1
0
 /// <summary>
 /// Class constructor
 /// </summary>
 /// <param name="document">Document to perform operations on</param>
 /// <param name="fullName">Full path name of document</param>
 public PTWordprocessingDocument(OpenXmlSDK.WordprocessingDocument document, string fullName)
     : base()
 {
     Document         = document;
     FullName         = fullName;
     InnerContent     = new WordprocessingDocumentManager(this);
     Comments         = new CommentAccessor(this);
     Changes          = new ChangeAccessor(this);
     Headers          = new HeaderAccessor(this);
     Footer           = new FooterAccessor(this);
     Setting          = new SettingAccessor(this);
     CustomXml        = new CustomXmlAccessor(this);
     Background       = new BackgroundAccessor(this);
     Style            = new StyleAccessor(this);
     Format           = new ContentFormatAccessor(this);
     Picture          = new PictureAccessor(this);
     Watermark        = new WatermarkAccesor(this);
     Theme            = new ThemeAccessor(this);
     TOC              = new TOCAccessor(this);
     TOF              = new TOFAccessor(this);
     TOA              = new TOAAccessor(this);
     Index            = new IndexAccessor(this);
     CoreProperties   = new CorePropertiesAccesor(this);
     CustomProperties = new CustomPropertiesAccesor(this);
 }
Пример #2
0
    private static void full_block(
        DocumentFormat.OpenXml.Packaging.WordprocessingDocument document,
        Dictionary <String, String> marks)
    {
        int table_count = 0;

        foreach (var table in document.MainDocumentPart.Document.Body.Descendants <Table>())
        {
            Console.WriteLine("table " + ++table_count);

            full_table(document, table, marks);
        }
    }
Пример #3
0
        public void  WritingInWordDocument()
        {
            using (DocumentFormat.OpenXml.Packaging.WordprocessingDocument wordDocument =
                       WordprocessingDocument.Create(@"C:\\Users\\Public\\Music\\info.doc", WordprocessingDocumentType.Document))
            {
                MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();

                // Create the document structure and add some text.
                mainPart.Document = new Document();
                Body      body = mainPart.Document.AppendChild(new Body());
                Paragraph para = body.AppendChild(new Paragraph());
                Run       run  = para.AppendChild(new Run());
                run.AppendChild(new Text("Hello, my name is Sumit."));
            }
        }
Пример #4
0
    private static void full_table(
        DocumentFormat.OpenXml.Packaging.WordprocessingDocument document,
        DocumentFormat.OpenXml.Wordprocessing.Table table,
        Dictionary <String, String> marks)
    {
        //int cell_count = 0;

        foreach (var cell in table.Descendants <TableCell>())
        {
            //Console.WriteLine(" " + ++cell_count + " cell value: " + cell);

            foreach (var item in cell)
            {
                if (item.GetType() == typeof(DocumentFormat.OpenXml.Wordprocessing.Table))
                {
                    full_table(document, (DocumentFormat.OpenXml.Wordprocessing.Table)item, marks);
                }
                else if (item.GetType() == typeof(DocumentFormat.OpenXml.Wordprocessing.Paragraph))
                {
                    foreach (var mrk in marks)
                    {
                        if (cell.InnerText.Contains(labelText))
                        {
                            ImagePart imagePart = document.MainDocumentPart.AddImagePart(ImagePartType.Jpeg);
                            using (FileStream stream = new FileStream(imageFile, FileMode.Open))
                            {
                                imagePart.FeedData(stream);
                            }
                            cell.RemoveAllChildren();
                            addImageToCell(cell, document.MainDocumentPart.GetIdOfPart(imagePart));
                        }
                        else if (cell.InnerText.Contains(mrk.Key))
                        {
                            String str = cell.InnerText;
                            ((DocumentFormat.OpenXml.Wordprocessing.Paragraph)item).RemoveAllChildren();
                            ((DocumentFormat.OpenXml.Wordprocessing.Paragraph)item).Append(new Run(new Text(str.Replace(mrk.Key, mrk.Value))));
                        }
                    }
                }
            }
        }
    }
Пример #5
0
    private void generatedocxfile(StringBuilder strHTMLContent, string FederationName)
    {
        /*t create and init a new docx file and
         * a WordprocessingDocument object to represent it t*/

        string docPath = getPPRPath();

        //string docPath=GetSavePath();

        DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Create(docPath + FederationName + ".docx", WordprocessingDocumentType.Document);
        MainDocumentPart mainDocPart = doc.AddMainDocumentPart();

        mainDocPart.Document = new Document();
        Body body = new Body();

        mainDocPart.Document.Append(body);

        // Add an aFChunk part to the package
        string altChunkId = "AltChunkId1";

        AlternativeFormatImportPart chunk = mainDocPart
                                            .AddAlternativeFormatImportPart(
            AlternativeFormatImportPartType.Xhtml, altChunkId);

        string html = strHTMLContent.ToString();

        using (MemoryStream ms =
                   new MemoryStream(Encoding.UTF8.GetBytes(html)))
        {
            chunk.FeedData(ms);
        }

        // Add the aFChunk to the document
        AltChunk altChunk = new AltChunk();

        altChunk.Id = altChunkId;
        mainDocPart.Document.Body.Append(altChunk);

        /*t to save the changes t*/
        doc.MainDocumentPart.Document.Save();
        doc.Dispose();
    }
Пример #6
0
        private static void AddImageToBody(DocumentFormat.OpenXml.Packaging.WordprocessingDocument wordDoc, string relationshipId)
        {
            //Define the reference of the image.
            var element =
                new Drawing(
                    new DW.Inline(
                        new DW.Extent()
            {
                Cx = 990000L, Cy = 792000L
            },
                        new DW.EffectExtent()
            {
                LeftEdge   = 0L,
                TopEdge    = 0L,
                RightEdge  = 0L,
                BottomEdge = 0L
            },
                        new DW.DocProperties()
            {
                Id   = (UInt32Value)1U,
                Name = "Picture 1"
            },
                        new DW.NonVisualGraphicFrameDrawingProperties(
                            new A.GraphicFrameLocks()
            {
                NoChangeAspect = true
            }),
                        new A.Graphic(
                            new A.GraphicData(
                                new PIC.Picture(
                                    new PIC.NonVisualPictureProperties(
                                        new PIC.NonVisualDrawingProperties()
            {
                Id   = (UInt32Value)0U,
                Name = "New Bitmap Image.jpg"
            },
                                        new PIC.NonVisualPictureDrawingProperties()),
                                    new PIC.BlipFill(
                                        new A.Blip(
                                            new A.BlipExtensionList(
                                                new A.BlipExtension()
            {
                Uri =
                    "{28A0092B-C50C-407E-A947-70E740481C1C}"
            })
                                            )
            {
                Embed            = relationshipId,
                CompressionState =
                    A.BlipCompressionValues.Print
            },
                                        new A.Stretch(
                                            new A.FillRectangle())),
                                    new PIC.ShapeProperties(
                                        new A.Transform2D(
                                            new A.Offset()
            {
                X = 0L, Y = 0L
            },
                                            new A.Extents()
            {
                Cx = 990000L, Cy = 792000L
            }),
                                        new A.PresetGeometry(
                                            new A.AdjustValueList()
                                            )
            {
                Preset = A.ShapeTypeValues.Rectangle
            }))
                                )
            {
                Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"
            })
                        )
            {
                DistanceFromTop    = (UInt32Value)0U,
                DistanceFromBottom = (UInt32Value)0U,
                DistanceFromLeft   = (UInt32Value)0U,
                DistanceFromRight  = (UInt32Value)0U,
                EditId             = "50D07946"
            });

            //Append the reference to body, the element should be in a Run.
            wordDoc.MainDocumentPart.Document.Body.AppendChild(new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(element)));
        }