การทำงานกับรูปแบบและธีม

คลาสStyleCollectionจะใช้ในการจัดการในตัวและใช้การตั้งค่าที่ผู้ใช้กำหนดกับรูปแบบ.

วิธีการแยกเนื้อหาตามรูปแบบ

ในระดับง่ายเรียกเนื้อหาตามลักษณะจากเอกสารคำจะมีประโยชน์ในการระบุรายการแล ตัวอย่างเช่นคุณอาจต้องระบุชนิดของเนื้อหาในเอกสารเช่นตัวอย่างชื่ออ้างอิงคำสำคัญชื่อ.

เพื่อใช้ขั้นตอนต่อไปนี้อีกสองสามขั้นตอนนอกจากนี้ยังสามารถใช้เพื่อใช้ประโยชน์จากโครงสร้างของเอกสารที่กำหนดโดยสไตล์ที่ใช้เพื่อวัตถุประสงค์เอกสารใหม่สำหรับเอาต์พุตอื่นเช่นHTML ในความเป็นจริงวิธีการสร้างเอกสารแอสโพส,วางAspose.Wordsเพื่อทดสอบ. เครื่องมือที่สร้างขึ้นโดยใช้Aspose.Wordsจะใช้เอกสารคำต้นฉบับและแยกออกเป็นหัวข้อในระดับหัวข้อ ไฟล์XMLถูกผลิตโดยใช้Aspose.Wordsซึ่งใช้ในการสร้างต้นไม้นำทางที่คุณสามารถดูได้ทางด้านซ้าย แล้วAspose.Wordsแปลงแต่ละหัวข้อในHTML การแก้ปัญหาสำหรับการดึงข้อความที่จัดรูปแบบด้วยลักษณะเฉพาะในเอกสารคำโดยทั่วไปจะประหยัดและตรงไปตรงมาโดยใช้Aspose.Words.

เพื่อแสดงให้เห็นAspose.Wordsจัดการกับการดึงเนื้อหาตามสไตล์ได้อย่างง่ายดายเพียงใดลองดูตัวอย่าง ในตัวอย่างนี้เรากำลังจะดึงข้อความที่จัดรูปแบบด้วยลักษณะย่อหน้าเฉพาะและลักษณะตัวอัก.

ในระดับสูงนี้จะเกี่ยวข้องกับ:

  1. การเปิดเอกสารคำโดยใช้คลาสDocument.
  2. การรับคอลเล็กชันของย่อหน้าทั้งหมดและทั้งหมดทำงานในเอกสาร.
  3. เลือกเฉพาะย่อหน้าที่ต้องการและรัน.

โดยเฉพาะอย่างยิ่งเราจะดึงข้อความที่จัดรูปแบบด้วยรูปแบบย่อหน้า’Heading 1’และรูปแบบตัวอักษร’เน้

working-with-styles-aspose-words-java-1

ในเอกสารตัวอย่างนี้ข้อความที่จัดรูปแบบด้วยสไตล์'Heading 1’ย่อหน้าคือ’แทรกแท็บ',‘รูปแบบด่วน’และ’ธีม’และข้อความที่จัดรูปแบบด้วยรูปแบบอักขระ’เน้นเข้มข้น’คือข้อความที่เป็นตัวหนาหลายอินสแตนซ์เช่น’แกลเลอรี่'และ’รูปลักษณ์โดยรวม’.

การดำเนินงานของแบบสอบถามตามสไตล์ค่อนข้างง่ายในแบบจำลองวัตถุเอกสารAspose.Wordsเนื่องจ สองวิธีการระดับจะดำเนินการสำหรับการแก้ปัญหานี้:

  1. ParagraphsByStyleName-วิธีนี้จะดึงอาร์เรย์ของย่อหน้าเหล่านั้นในเอกสารที่มีชื่อสไตล์ที่เฉพาะเจาะจง.
  2. RunsByStyleName-เมธอดนี้จะดึงอาร์เรย์ของผู้ที่รันในเอกสารที่มีชื่อสไตล์ที่เฉพาะเจาะจง.

ทั้งสองวิธีเหล่านี้จะคล้ายกันมากความแตกต่างเพียงอย่างเดียวคือชนิดโหนดและการแส นี่คือการดำเนินการของParagraphsByStyleNameที่แสดงในตัวอย่างรหัสที่ระบุไว้ด้านล่างเพื่อหาย่อหน้าทั้งหมดที่จัดรูปแบบด้วยสไตล์ที่ระบุ.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
public static ArrayList<Paragraph> paragraphsByStyleName(Document doc, String styleName) throws Exception {
// Create an array to collect paragraphs of the specified style.
ArrayList<Paragraph> paragraphsWithStyle = new ArrayList();
// Get all paragraphs from the document.
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
// Look through all paragraphs to find those with the specified style.
for (Paragraph paragraph : (Iterable<Paragraph>) paragraphs) {
if (paragraph.getParagraphFormat().getStyle().getName().equals(styleName))
paragraphsWithStyle.add(paragraph);
}
return paragraphsWithStyle;
}

การใช้งานนี้ยังใช้วิธีการDocument.getChildNodes()ของคลาสของDocumentซึ่งส่งคืนคอลเล็กชันของโหนดย่อยทันทีทั้งหมด.

นอกจากนี้ยังมีมูลค่าชี้ให้เห็นว่าคอลเลกชันย่อหน้าไม่ได้สร้างค่าใช้จ่ายทันทีเนื่องจากย่อหย่อหน้าที่มีลักษณะที่ระบุไว้ในอาร์เรย์paragraphsWithStyle ชื่อลักษณะParagraphสามารถพบได้ในคุณสมบัติStyle.getName()ของออบเจกต์Paragraph.getParagraphFormat().

การดำเนินงานของRunsByStyleNameเกือบจะเหมือนกันแม้ว่าเราจะเห็นได้ชัดว่าใช้NodeType.Runเพื่อดึงโหนดเรียกใช้ คุณสมบัติFont.getStyle()ของออบเจกต์Runถูกใช้เพื่อเข้าถึงข้อมูลลักษณะในโหนดRun

ตัวอย่างรหัสต่อไปนี้พบการทำงานทั้งหมดที่จัดรูปแบบด้วยลักษณะที่ระบุ.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
public static ArrayList<Run> runsByStyleName(Document doc, String styleName) throws Exception {
// Create an array to collect runs of the specified style.
ArrayList<Run> runsWithStyle = new ArrayList();
// Get all runs from the document.
NodeCollection runs = doc.getChildNodes(NodeType.RUN, true);
// Look through all runs to find those with the specified style.
for (Run run : (Iterable<Run>) runs) {
if (run.getFont().getStyle().getName().equals(styleName))
runsWithStyle.add(run);
}
return runsWithStyle;
}

ตัวอย่างโค้ดต่อไปนี้เรียกใช้แบบสอบถามและแสดงผลลัพธ์.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Open the document.
Document doc = new Document(dataDir + "TestFile.doc");
// Define style names as they are specified in the Word document.
final String PARA_STYLE = "Heading 1";
final String RUN_STYLE = "Intense Emphasis";
// Collect paragraphs with defined styles.
// Show the number of collected paragraphs and display the text of this paragraphs.
ArrayList<Paragraph> paragraphs = paragraphsByStyleName(doc, PARA_STYLE);
System.out.println(java.text.MessageFormat.format("Paragraphs with \"{0}\" styles ({1}):", PARA_STYLE, paragraphs.size()));
for (Paragraph paragraph : paragraphs)
System.out.print(paragraph.toString(SaveFormat.TEXT));
// Collect runs with defined styles.
// Show the number of collected runs and display the text of this runs.
ArrayList<Run> runs = runsByStyleName(doc, RUN_STYLE);
System.out.println(java.text.MessageFormat.format("\nRuns with \"{0}\" styles ({1}):", RUN_STYLE, runs.size()));
for (Run run : runs)
System.out.println(run.getRange().getText());

เมื่อทุกอย่างเสร็จสิ้นการทำงานตัวอย่างจะแสดงผลลัพธ์ต่อไปนี้:

working-with-styles-aspose-words-java-2

ที่คุณสามารถดูนี้เป็นตัวอย่างที่ง่ายมากแสดงให้เห็นจำนวนและข้อความของย่อหน้าที่รวบร.

แทรกตัวคั่นสไตล์ที่จะนำรูปแบบที่แตกต่างกันย่อหน้า

สามารถเพิ่มตัวคั่นลักษณะไปยังส่วนท้ายของย่อหน้าได้โดยใช้แป้นพิมพ์ลัดMSคำ คุณลักษณะนี้ช่วยให้ลักษณะย่อหน้าต่างๆสองใช้ในย่อหน้าตรรกะหนึ่งพิมพ์ หากคุณต้องการให้ข้อความจากจุดเริ่มต้นของหัวข้อเฉพาะปรากฏในสารบัญแต่ไม่ต้องการ

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกตัวคั่นลักษณะที่จะใส่ลักษณะย่อหน้าที่แตกต่างกัน

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Style paraStyle = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, "MyParaStyle");
paraStyle.getFont().setBold(false);
paraStyle.getFont().setSize(8);
paraStyle.getFont().setName("Arial");
// Append text with "Heading 1" style.
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.write("Heading 1");
builder.insertStyleSeparator();
// Append text with another style.
builder.getParagraphFormat().setStyleName(paraStyle.getName());
builder.write("This is text with some other formatting ");
dataDir = dataDir + "InsertStyleSeparator_out.doc";
doc.save(dataDir);

คัดลอกสไตล์ทั้งหมดจากแม่แบบ

มีกรณีเมื่อคุณต้องการคัดลอกลักษณะทั้งหมดจากเอกสารหนึ่งไปยังอีก คุณสามารถใช้วิธีการDocument.CopyStylesFromTemplateเพื่อคัดลอกลักษณะจากเทมเพลตที่ระบุไปยังเอกสาร เมื่อสไตล์ถูกคัดลอกจากเทมเพลทไปยังเอกสารลักษณะที่เหมือนชื่อในเอกสารจะถูกนิยาม รูปแบบที่ไม่ซ้ำกันจากแม่แบบจะถูกคัดลอกไปยังเอกสาร รูปแบบที่ไม่ซ้ำกันในเอกสารยังคงเหมือนเดิม

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการคัดลอกลักษณะจากเอกสารหนึ่งไปยังอีก.

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
Document doc = new Document(dataDir + "template.docx");
Document target = new Document(dataDir + "TestFile.doc");
target.copyStylesFromTemplate(doc);
dataDir = dataDir + "CopyStyles_out.docx";
doc.save(dataDir);

วิธีการจัดการคุณสมบัติของชุดรูปแบบ

เราได้เพิ่มขั้นพื้นฐานAPIในAspose.Wordsในการเข้าถึงคุณสมบัติของชุดรูปแบบเอกสาร สำหรับตอนนี้APIนี้มีวัตถุสาธารณะต่อไปนี้:

  • ธีม
  • ThemeFonts
  • ThemeColors

นี่คือวิธีที่คุณจะได้รับคุณสมบัติของชุดรูปแบบ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(GetThemeProperties.class);
Document doc = new Document(dataDir + "Document.doc");
Theme theme = doc.getTheme();
// Major (Headings) font for Latin characters.
System.out.println(theme.getMajorFonts().getLatin());
// Minor (Body) font for EastAsian characters.
System.out.println(theme.getMinorFonts().getEastAsian());
// Color for theme color Accent 1.
System.out.println(theme.getColors().getAccent1());

และนี่คือวิธีที่คุณสามารถตั้งค่าคุณสมบัติของชุดรูปแบบ:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(SetThemeProperties.class);
Document doc = new Document(dataDir + "Document.doc");
Theme theme = doc.getTheme();
// Set Times New Roman font as Body theme font for Latin Character.
theme.getMinorFonts().setLatin("Algerian");
// Set Color.Gold for theme color Hyperlink.
theme.getColors().setHyperlink(java.awt.Color.DARK_GRAY);
doc.save(dataDir + "output.doc");