如何在Word中排版出漂亮的编程语言代码样式?(较好用的部分方法汇总)

 

[1] 能输出各种编程语言并保持代码格式和语法高亮显示的网站:

PlanetB | Syntax Highlight Code in Word Documents  http://www.planetb.ca/syntax-highlight-word

Welcome! — Pygments  http://pygments.org/

[2] How do you display code snippets in MS Word preserving format and syntax highlighting?

https://stackoverflow.com/questions/387453/how-do-you-display-code-snippets-in-ms-word-preserving-format-and-syntax-highlig#

[3] 如何在Word中排出漂亮的代码 - CSDN博客  http://blog.csdn.net/code4101/article/details/41802715

本人使用VS 2013作为C语言的编程环境,直接将代码从VS中“保留源格式”复制到Word中,因此在原作者写的宏的基础上加了两条宏命令拿来使用的,即:

Options.DefaultHighlightColorIndex = wdNoHighlight  'NeXT_Vision新增的:清除“以不同颜色突出显示文本”,从而解决从VS中“保留源格式”复制带来的文本高亮问题
Selection.Range.HighlightColorIndex = wdNoHighlight  'NeXT_Vision新增的:清除“以不同颜色突出显示文本”,从而解决从VS中“保留源格式”复制带来的文本高亮问题
Sub Word中代码排版之设置代码表格()
' author: code4101 链接http://blog.csdn.net/code4101/article/details/41802715
' Word中代码排版之设置代码表格 宏
'第一个宏需要选定一个一行两列的表格
'
    ' 背景色为morning的配色方案,RGB为(229,229,229)
    With Selection.Tables(1)
        With .Shading
            .Texture = wdTextureNone
            .ForegroundPatternColor = wdColorAutomatic
            .BackgroundPatternColor = 15066597 '15066597这个数其实就是229*(2^16 + 2^8 + 1)
        End With
        Options.DefaultHighlightColorIndex = wdNoHighlight  'NeXT_Vision新增的:清除“以不同颜色突出显示文本”,从而解决从VS中“保留源格式”复制带来的文本高亮问题
        Selection.Range.HighlightColorIndex = wdNoHighlight  'NeXT_Vision新增的:清除“以不同颜色突出显示文本”,从而解决从VS中“保留源格式”复制带来的文本高亮问题
        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
        .Borders(wdBorderRight).LineStyle = wdLineStyleNone
        .Borders(wdBorderTop).LineStyle = wdLineStyleNone
        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
        .Borders(wdBorderVertical).LineStyle = wdLineStyleNone
        .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
        .Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
        .Borders.Shadow = False
        .AutoFitBehavior (wdAutoFitContent)  '自动调整大小
    End With
    With Options
        .DefaultBorderLineStyle = wdLineStyleSingle
        .DefaultBorderLineWidth = wdLineWidth050pt
        .DefaultBorderColor = wdColorAutomatic
    End With
    
    ' 段落无首行缩进,行间距为固定值12磅
    With Selection.ParagraphFormat
        .LeftIndent = CentimetersToPoints(0)
        .RightIndent = CentimetersToPoints(0)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 0
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceExactly  '行距为“固定值”
        .LineSpacing = 12  '行距为“固定值”的“12磅”
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True  '用连字符号连接
        .FirstLineIndent = CentimetersToPoints(0)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .MirrorIndents = False
        .TextboxTightWrap = wdTightNone
        .AutoAdjustRightIndent = True
        .DisableLineHeightGrid = False
        .FarEastLineBreakControl = True
        .WordWrap = True
        .HangingPunctuation = True
        .HalfWidthPunctuationOnTopOfLine = False
        .AddSpaceBetweenFarEastAndAlpha = True
        .AddSpaceBetweenFarEastAndDigit = True
        .BaseLineAlignment = wdBaselineAlignAuto
    End With
    ' 清除原有的段落底纹
    Selection.ParagraphFormat.Shading.BackgroundPatternColor = wdColorAutomatic
End Sub

Sub Word中代码排版之输入连续数字()
' author: code4101  链接http://blog.csdn.net/code4101/article/details/41802715
' Word中代码排版之设置代码表格 宏
'第二个宏是在表格第一列自动加上行号
'
    行数 = InputBox("请输入代码终止行数", "输入行数", "50")
    For i = 1 To 行数 - 1
        Selection.TypeText Text:=i
        Selection.TypeParagraph
    Next
    Selection.TypeText Text:=行数
End Sub

 

Office VBA 参考Office VBA Reference | MSDN  https://msdn.microsoft.com/vba/office-vba-reference

[4] Word里如何排版出漂亮的编程语言代码样式_百度经验  

https://jingyan.baidu.com/article/9c69d48f99eb1b13c8024e7d.html

[5] word2013种如何插入代码着色的代码块? - 知乎  https://www.zhihu.com/question/31544667

[6] 

  • 10
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在 Android SDK 提供特定排版布局的卡片并生成图片,可以使用以下代码示例: 1. 创建布局文件 `card_layout.xml`,定义卡片的排版布局: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <ImageView android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/card_image" /> <TextView android:id="@+id/titleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Card Title" android:textSize="18sp" android:textColor="@android:color/black" /> <TextView android:id="@+id/contentTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Card Content" android:textSize="14sp" android:textColor="@android:color/darker_gray" /> </LinearLayout> ``` 2. 在代码使用布局文件和数据填充卡片: ```java // 加载布局文件 View cardView = LayoutInflater.from(context).inflate(R.layout.card_layout, null); // 查找视图组件 ImageView imageView = cardView.findViewById(R.id.imageView); TextView titleTextView = cardView.findViewById(R.id.titleTextView); TextView contentTextView = cardView.findViewById(R.id.contentTextView); // 设置数据 imageView.setImageResource(R.drawable.card_image); titleTextView.setText("Card Title"); contentTextView.setText("Card Content"); ``` 3. 测量和布局卡片: ```java // 测量和布局 int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.EXACTLY); int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(500, View.MeasureSpec.UNSPECIFIED); cardView.measure(widthMeasureSpec, heightMeasureSpec); cardView.layout(0, 0, cardView.getMeasuredWidth(), cardView.getMeasuredHeight()); ``` 4. 创建 Bitmap 并绘制卡片: ```java // 创建 Bitmap Bitmap bitmap = Bitmap.createBitmap(cardView.getWidth(), cardView.getHeight(), Bitmap.Config.ARGB_8888); // 创建 Canvas Canvas canvas = new Canvas(bitmap); // 绘制卡片 cardView.draw(canvas); ``` 5. 保存图片到文件: ```java // 保存图片到文件 FileOutputStream outputStream; try { outputStream = new FileOutputStream("/path/to/save/image.jpg"); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } ``` 以上代码示例演示了如何在 Android SDK 提供特定排版布局的卡片并生成图片。您可以根据实际需求进行适当的修改和调整。希望对你有所帮助!如有更多问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值