如何创建具有标准(荷兰语)标题样式的Word文件?

huangapple go评论72阅读模式
英文:

How can I create a Word file with a standard (Dutch) heading style?

问题

如何在新的Word文件中添加标准(荷兰语)标题样式?

第一个标题应该使用(荷兰语)样式“Kop 1”(= Heading1)。

我尝试了一些标准解决方案,但它们不起作用。结果始终是默认的标准样式。

1:创建一个空的Word文件,并将其用作具有样式的模板:

XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx"));
paragraph = document.createParagraph();
paragraph.setStyle("Heading1");

或者

XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx"));
paragraph = document.createParagraph();
paragraph.setStyle("Kop 1");

2:使用模板文件。

XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx")));       
XWPFDocument doc = new XWPFDocument();
XWPFStyles newStyles = doc.createStyles();
newStyles.setStyles(template.getStyle());

XWPFParagraph para = doc.createParagraph();
para.setStyle("Heading1");
// 或者:para.setStyle("Kop 1");

XWPFRun run = para.createRun();
run.setText("Header 1");
// 或者:run.setText("Kop 1");

如何创建具有标准(荷兰语)标题样式的Word文件?

英文:

How can I add a standard (Dutch) heading style to a new Word file?

如何创建具有标准(荷兰语)标题样式的Word文件?

The first title should have style (Dutch) style "Kop 1" (= Heading1).

I tried a number of standard solutions, but they don't work. The result is always the default standard style.

1: Create an empty Word file and use it as a kind of template with styles:

XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx");
paragraph = document.createParagraph();
paragraph.setStyle("Heading1");

Or

XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx");
paragraph = document.createParagraph();
paragraph.setStyle("Kop 1");

2: Use a template file.

XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx")));       
XWPFDocument doc = new XWPFDocument();
XWPFStyles newStyles = doc.createStyles();
newStyles.setStyles(template.getStyle());

XWPFParagraph para = doc.createParagraph();
para.setStyle("Heading1");
// OR: para.setStyle( "Kop 1");

XWPFRun run = para.createRun();
run.setText("Header 1");
// or: run.setText("Kop 1");

答案1

得分: 2

这里有多个问题。

首先,Word模板必须已经包含样式。并非所有Word文档都默认包含所有样式。只有在文档中使用过的样式才会包含在内。所谓“以前使用过”只是指在文档中曾经使用过该样式。因此,要保存在文档中的任何样式在保存模板之前必须暂时使用。

而且,似乎Microsoft的某些人混淆了样式的ID和名称。按照我的理解,ID和名称应该是技术上的内部标识符,而名称应该是可见的标题,也可以本地化。但是在Word样式中,ID是本地化的。例如,在德语的Word中,标题样式的ID是“berschrift1”,“berschrift2”,...这来自于“Überschrift1”,“Überschrift2”,...没有umlauts(重音符号),因为ID不允许使用umlauts。相反,这些样式的名称是“heading 1”,“heading 2”,...即使对于德语Word文档也是如此。我认为应该反过来。

我猜你的荷兰语Word中样式的ID是“Kop1”,“Kop2”,...没有空格。但更好的方法是通过名称获取样式,因为名称似乎对所有Word区域设置都是英文的。

完整示例:

WordTemplate.docx:

如何创建具有标准(荷兰语)标题样式的Word文件?

代码:

import java.io.FileOutputStream;
import java.io.FileInputStream;

import org.apache.poi.xwpf.usermodel.*;

public class WordUseStylesByName {

 private static XWPFStyle getStyleByName(XWPFStyles styles, String styleName) { 
  if (styles == null || styleName == null) return null;
  XWPFStyle style = styles.getStyleWithName(styleName); 
  if (style == null) {
   System.out.println("No style with name " + styleName + " present.");
  }
  return style;
 }    

 public static void main(String[] args) throws Exception {
  try (
        XWPFDocument document = new XWPFDocument(new FileInputStream("./WordTemplate.docx"));
        FileOutputStream out = new FileOutputStream("./WordResult.docx");
       ) {

   XWPFStyles styles;
   XWPFStyle style;
   String styleId;
   XWPFParagraph paragraph;
   XWPFRun run;
   
   // 获取标题1样式
   styles = document.getStyles();
   style = getStyleByName(styles, "heading 1");
   styleId = (style != null)?style.getStyleId():"";
   System.out.println(styleId);
  
   paragraph = document.createParagraph();
   paragraph.setStyle(styleId);
   run = paragraph.createRun();
   run.setText("Text formatted as heading 1");
   
   // 获取标题2样式
   style = getStyleByName(styles, "heading 2");
   styleId = (style != null)?style.getStyleId():"";
   System.out.println(styleId);

   paragraph = document.createParagraph();
   paragraph.setStyle(styleId);
   run = paragraph.createRun();
   run.setText("Text formatted as heading 2");

   document.write(out);
  }
 }
}

WordResult.docx:

如何创建具有标准(荷兰语)标题样式的Word文件?


<details>
<summary>英文:</summary>
Multiple problems here.
At first the Word template must contain the style already. Not all Word documents contain all styles per default. Only those styles are contained which got used in the document before. The term &quot;got used before&quot; only means the style sometime was used in the document. There needs not to be current text formatted using that style. Therefore, any styles that are to be saved in the document must be used temporarily before saving the template.
And seems someone at Microsoft has mixed up ID and Name of styles. As I would understand ID vs. Name, the ID should be a technically internal identifier while the name should be the visible title, which also could be localized. But with Word styles, the ID is localized. In German Word for example, the heading style IDs are &quot;berschrift1&quot;, &quot;berschrift2&quot;, ... That comes from &quot;&#220;berschrift1&quot;, &quot;&#220;berschrift2&quot;, ... without the umlauts, which are not allowed for IDs. In opposite the names of that styles are &quot;heading 1&quot;, &quot;heading 2&quot;, ... even for German Word documents. That should be vice versa in my opinion.
I guess in your Dutch Word the style IDs are &quot;Kop1&quot;, &quot;Kop2&quot;, ... without the spaces. But the better way is to get the styles by name as the names seems to be English for all Word locales.
Complete example:
WordTemplate.docx:
[![enter image description here][1]][1]
Code:
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.xwpf.usermodel.*;
public class WordUseStylesByName {
private static XWPFStyle getStyleByName(XWPFStyles styles, String styleName) { 
if (styles == null || styleName == null) return null;
XWPFStyle style = styles.getStyleWithName(styleName); 
if (style == null) {
System.out.println(&quot;No style with name &quot; + styleName + &quot; present.&quot;);
}
return style;
}    
public static void main(String[] args) throws Exception {
try (
XWPFDocument document = new XWPFDocument(new FileInputStream(&quot;./WordTemplate.docx&quot;));
FileOutputStream out = new FileOutputStream(&quot;./WordResult.docx&quot;);
) {
XWPFStyles styles;
XWPFStyle style;
String styleId;
XWPFParagraph paragraph;
XWPFRun run;
// get heading 1 style
styles = document.getStyles();
style = getStyleByName(styles, &quot;heading 1&quot;);
styleId = (style != null)?style.getStyleId():&quot;&quot;;
System.out.println(styleId);
paragraph = document.createParagraph();
paragraph.setStyle(styleId);
run = paragraph.createRun();
run.setText(&quot;Text formatted as heading 1&quot;);
// get heading 2 style
style = getStyleByName(styles, &quot;heading 2&quot;);
styleId = (style != null)?style.getStyleId():&quot;&quot;;
System.out.println(styleId);
paragraph = document.createParagraph();
paragraph.setStyle(styleId);
run = paragraph.createRun();
run.setText(&quot;Text formatted as heading 2&quot;);
document.write(out);
}
}
}
WordResult.docx:
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/nDFOy.png
[2]: https://i.stack.imgur.com/Tk41n.png
</details>

huangapple
  • 本文由 发表于 2023年6月1日 03:17:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定