ドキュメントのコンテンツと書式設定をプログラムで読み取り、操作、変更するには、ドキュメントを Aspose.Words Document Object Model (DOM) に変換する必要があります。
Word 文書とは対照的に、Markdown は Aspose.Words Document Object Model (DOM) 記事で説明されている DOM に準拠していません。ただし、Aspose.Words は、Markdown ドキュメントを DOM に変換したり逆に変換したりするための独自のメカニズムを提供しているため、テキストの書式設定、テーブル、ヘッダーなどの要素を正常に操作できます。
この記事では、さまざまな markdown 機能を Aspose.Words DOM に変換し、Markdown 形式に戻す方法について説明します。
翻訳の複雑さ Markdown – DOM – Markdown
このメカニズムの主な困難は、Markdown を DOM に変換することだけでなく、逆変換を行うこと、つまり損失を最小限に抑えてドキュメントを Markdown 形式に保存し直すことです。複数レベルの引用符など、逆変換が簡単ではない要素があります。
# Use a document builder to add content to the document.builder=aw.DocumentBuilder()# Make the text Bold.builder.font.bold=Truebuilder.writeln("This text will be Bold")builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.bold_text_example.md")
Italic *italic text*
Font.italic = True
# Use a document builder to add content to the document.builder=aw.DocumentBuilder()# Make the text Italic.builder.font.italic=Truebuilder.writeln("This text will be Italic")builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.italic_text_example.md")
Strikethrough ~Strikethrough text~
Font.strike_through = True
# Use a document builder to add content to the document.builder=aw.DocumentBuilder()# Make the text Strikethrough.builder.font.strike_through=Truebuilder.writeln("This text will be Strikethrough")builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.strikethrough_text_example.md")
# Use a document builder to add content to the document.builder=aw.DocumentBuilder()# Number of backticks is missed, one backtick will be used by default.inlineCode1BackTicks=builder.document.styles.add(aw.StyleType.CHARACTER,"InlineCode")builder.font.style=inlineCode1BackTicksbuilder.writeln("Text with InlineCode style with 1 backtick")# There will be 3 backticks.inlineCode3BackTicks=builder.document.styles.add(aw.StyleType.CHARACTER,"InlineCode.3")builder.font.style=inlineCode3BackTicksbuilder.writeln("Text with InlineCode style with 3 backtick")builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.inline_code_example.md")
# Use a document builder to add content to the document.builder=aw.DocumentBuilder()# Insert hyperlink.builder.insert_hyperlink("https://www.aspose.com","https://www.aspose.com",False);builder.insert_hyperlink("email@aspose.com","mailto:email@aspose.com",False);builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.autolink_example.md")
# Use a document builder to add content to the document.builder=aw.DocumentBuilder()# Insert hyperlink.builder.insert_hyperlink("Aspose","https://www.aspose.com",False)builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.link_example.md")
# Use a document builder to add content to the document.builder=aw.DocumentBuilder()# Insert image.shape=aw.drawing.Shape(builder.document,aw.drawing.ShapeType.IMAGE)shape.wrap_type=aw.drawing.WrapType.INLINEshape.image_data.source_full_name="/attachment/1456/pic001.png"shape.image_data.title="title"builder.insert_node(shape)builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.image_example.md")
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ParagraphFormat.style_name = "Heading N"、(1≤ N ≤ 9)。 これは組み込みスタイルに変換され、指定されたパターンと正確に一致する必要があります (接尾辞や接頭辞は許可されません)。 それ以外の場合は、対応するスタイルを持つ単なる通常の段落になります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use a document builder to add content to the document.doc=aw.Document()builder=aw.DocumentBuilder(doc)builder.paragraph_format.style_name="Heading 1"builder.writeln("This is an H1 tag")# Reset styles from the previous paragraph to not combine styles between paragraphs.builder.font.bold=Falsebuilder.font.italic=FalsesetexHeading1=doc.styles.add(aw.StyleType.PARAGRAPH,"SetexHeading1")builder.paragraph_format.style=setexHeading1doc.styles.get_by_name("SetexHeading1").base_style_name="Heading 1"builder.writeln("Setex Heading level 1")builder.paragraph_format.style=doc.styles.get_by_name("Heading 3")builder.writeln("This is an H3 tag")# Reset styles from the previous paragraph to not combine styles between paragraphs.builder.font.bold=Falsebuilder.font.italic=FalsesetexHeading2=doc.styles.add(aw.StyleType.PARAGRAPH,"SetexHeading2")builder.paragraph_format.style=setexHeading2doc.styles.get_by_name("SetexHeading2").base_style_name="Heading 3"# Setex heading level will be reset to 2 if the base paragraph has a Heading level greater than 2.builder.writeln("Setex Heading level 2")builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.setext_heading_example.md")
# Use a document builder to add content to the document.builder=aw.DocumentBuilder()indentedCode=builder.document.styles.add(aw.StyleType.PARAGRAPH,"IndentedCode")builder.paragraph_format.style=indentedCodebuilder.writeln("This is an indented code")builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.indented_code_example.md")
# Use a document builder to add content to the document.builder=aw.DocumentBuilder()fencedCode=builder.document.styles.add(aw.StyleType.PARAGRAPH,"FencedCode")builder.paragraph_format.style=fencedCodebuilder.writeln("This is an fenced code")fencedCodeWithInfo=builder.document.styles.add(aw.StyleType.PARAGRAPH,"FencedCode.C#")builder.paragraph_format.style=fencedCodeWithInfobuilder.writeln("This is a fenced code with info string")builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.fenced_code_example.md")
# Use a document builder to add content to the document.doc=aw.Document()builder=aw.DocumentBuilder(doc)# By default a document stores blockquote style for the first level.builder.paragraph_format.style_name="Quote"builder.writeln("Blockquote")# Create styles for nested levels through style inheritance.quoteLevel2=doc.styles.add(aw.StyleType.PARAGRAPH,"Quote1")builder.paragraph_format.style=quoteLevel2doc.styles.get_by_name("Quote1").base_style_name="Quote"builder.writeln("1. Nested blockquote")builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.quote_example.md")
# Use a document builder to add content to the document.builder=aw.DocumentBuilder()builder.list_format.apply_bullet_default()builder.list_format.list.list_levels[0].number_format="-"builder.writeln("Item 1")builder.writeln("Item 2")builder.list_format.list_indent()builder.writeln("Item 2a")builder.writeln("Item 2b")builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.bulleted_list_example.md")
# Use a document builder to add content to the document.builder=aw.DocumentBuilder()# Add the first row.builder.insert_cell()builder.writeln("a")builder.insert_cell()builder.writeln("b")builder.end_row()# Add the second row.builder.insert_cell()builder.writeln("c")builder.insert_cell()builder.writeln("d")builder.end_table()builder.document.save(docs_base.artifacts_dir+"WorkingWithMarkdown.ordered_list_table.md")