-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
230 lines (210 loc) · 9.09 KB
/
Copy pathmain.cpp
File metadata and controls
230 lines (210 loc) · 9.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <QTextDocument>
#include <QTextCursor>
#include <QTextImageFormat>
#include <QTextDocumentWriter>
#include <QBuffer>
#include <QTextTableCell>
//直接插入文件到doc
void test()
{
QTextDocument document;
QTextCursor cursor(&document);
// 插入图片
QImage image("D:/Desktop/3.png");
document.addResource(QTextDocument::ImageResource, QUrl("1.png"), QVariant(image));
cursor.insertImage("1.png");
QTextImageFormat imageFormat;
imageFormat.setName("1.png");
imageFormat.setWidth(550);
imageFormat.setHeight(900);
cursor.insertImage(imageFormat);
// 保存为.doc文件
QTextDocumentWriter writer("D:/Desktop/document.doc");
writer.setFormat("odf"); // 将格式设置为纯文本,以便保存为Word文档(.doc)
writer.write(&document);
}
int main()
{
// 创建文档
QTextDocument document;
// 创建光标
QTextCursor cursor(&document);
{
QImage image("D:/Desktop/1.png");
// 缩小文件
//image = image.scaled(QSize(692, 1113), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
qDebug() << image.size();
// 将图像转换为Base64编码
QByteArray imageData;
QBuffer buffer(&imageData);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
QString base64Image = QString::fromLatin1(imageData.toBase64().data());
// 在文档中插入图像
cursor.insertHtml("<img src='data:image/png;base64," + base64Image + "' width='550' height='900'>");
// 在文档中插入换行
cursor.insertBlock();
// 在文档中插入题注
cursor.insertText("图 1 ");
cursor.insertHtml("<a id=\"anchor1\">This is 1 centered text</a>");
// 设置段落格式为居中对齐
QTextBlockFormat blockFormat;
blockFormat.setAlignment(Qt::AlignCenter);
cursor.mergeBlockFormat(blockFormat);
// 设置文本格式
QTextCharFormat charFormat;
cursor.mergeCharFormat(charFormat);
// 在文档中插入换行
cursor.insertBlock();
}
{
QImage image("D:/Desktop/2.png");
image = image.scaled(QSize(692, 1113), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
qDebug() << image.size();
// 将图像转换为Base64编码
QByteArray imageData;
QBuffer buffer(&imageData);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
QString base64Image = QString::fromLatin1(imageData.toBase64().data());
// 在文档中插入图像
cursor.insertHtml("<img src='data:image/png;base64," + base64Image + "'>");
// 在文档中插入换行
cursor.insertBlock();
// 在文档中插入题注
cursor.insertText("图 2 ");
cursor.insertHtml("<a id=\"anchor2\">This is 2 centered text</a>");
// 设置段落格式为居中对齐
QTextBlockFormat blockFormat;
blockFormat.setAlignment(Qt::AlignCenter);
cursor.mergeBlockFormat(blockFormat);
// 设置文本格式
QTextCharFormat charFormat;
cursor.mergeCharFormat(charFormat);
// 在文档中插入换行
cursor.insertBlock();
}
{
QImage image("D:/Desktop/3.png");
image = image.scaled(QSize(692, 1113), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
qDebug() << image.size();
// 将图像转换为Base64编码
QByteArray imageData;
QBuffer buffer(&imageData);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
QString base64Image = QString::fromLatin1(imageData.toBase64().data());
// 在文档中插入图像
cursor.insertHtml("<img src='data:image/png;base64," + base64Image + "'>");
// 在文档中插入换行
cursor.insertBlock();
// 在文档中插入题注
cursor.insertText("图 3 ");
cursor.insertHtml("<a id=\"anchor3\">This is 3 centered text</a>");
// 设置段落格式为居中对齐
QTextBlockFormat blockFormat;
blockFormat.setAlignment(Qt::AlignCenter);
cursor.mergeBlockFormat(blockFormat);
// 设置文本格式
QTextCharFormat charFormat;
cursor.mergeCharFormat(charFormat);
// 在文档中插入换行
cursor.insertBlock();
}
{
QImage image("D:/Desktop/4.png");
image = image.scaled(QSize(692, 1113), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
qDebug() << image.size();
// 将图像转换为Base64编码
QByteArray imageData;
QBuffer buffer(&imageData);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
QString base64Image = QString::fromLatin1(imageData.toBase64().data());
// 在文档中插入图像
cursor.insertHtml("<img src='data:image/png;base64," + base64Image + "'>");
// 在文档中插入换行
cursor.insertBlock();
// 在文档中插入题注
cursor.insertText("图 4 ");
cursor.insertHtml("<a id=\"anchor4\">This is 4 centered text</a>");
// 设置段落格式为居中对齐
QTextBlockFormat blockFormat;
blockFormat.setAlignment(Qt::AlignCenter);
cursor.mergeBlockFormat(blockFormat);
// 设置文本格式
QTextCharFormat charFormat;
cursor.mergeCharFormat(charFormat);
// 在文档中插入换行
cursor.insertBlock();
}
{
QImage image("D:/Desktop/5.png");
image = image.scaled(QSize(692, 1113), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
qDebug() << image.size();
// 将图像转换为Base64编码
QByteArray imageData;
QBuffer buffer(&imageData);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
QString base64Image = QString::fromLatin1(imageData.toBase64().data());
// 在文档中插入图像
cursor.insertHtml("<img src='data:image/png;base64," + base64Image + "'>");
// 在文档中插入换行
cursor.insertBlock();
// 在文档中插入题注
cursor.insertText("图 5 ");
cursor.insertHtml("<a id=\"anchor5\">This is 5 centered text</a>");
// 设置段落格式为居中对齐
QTextBlockFormat blockFormat;
blockFormat.setAlignment(Qt::AlignCenter);
cursor.mergeBlockFormat(blockFormat);
// 设置文本格式
QTextCharFormat charFormat;
cursor.mergeCharFormat(charFormat);
// 在文档中插入换行
cursor.insertBlock();
}
{
// 创建一个 QTextTable 对象,定义行数和列数
int numRows = 6;
int numColumns = 8;
QTextTableFormat tableFormat;
tableFormat.setWidth(QTextLength(QTextLength::PercentageLength, 100));
//tableFormat.setAlignment(Qt::AlignHCenter);
tableFormat.setBorderCollapse(true);
QTextTable* table = cursor.insertTable(numRows, numColumns, tableFormat);
table->cellAt(0, 0).firstCursorPosition().insertText("日期");
table->cellAt(0, 1).firstCursorPosition().insertText("时间");
table->cellAt(0, 2).firstCursorPosition().insertText("VIB60");
table->cellAt(0, 3).firstCursorPosition().insertText("TFuel");
table->cellAt(0, 4).firstCursorPosition().insertText("MB");
table->cellAt(0, 5).firstCursorPosition().insertText("VtB");
table->cellAt(0, 6).firstCursorPosition().insertText("VeB");
table->cellAt(0, 7).firstCursorPosition().insertText("引用");
// 为表格设置内容
for (int row = 1; row < numRows; ++row) {
//for (int col = 0; col < numColumns; ++col) {
// QTextTableCell cell = table->cellAt(row, col);
// QTextCursor cellCursor = cell.firstCursorPosition();
// cellCursor.insertText("Row " + QString::number(row) + ", Column " + QString::number(col));
//}
table->cellAt(row, 0).firstCursorPosition().insertText("20220717");
table->cellAt(row, 1).firstCursorPosition().insertText("08:33:14.733");
table->cellAt(row, 2).firstCursorPosition().insertText("-0.0");
table->cellAt(row, 3).firstCursorPosition().insertText("center");
table->cellAt(row, 4).firstCursorPosition().insertText("0.0");
table->cellAt(row, 5).firstCursorPosition().insertText("0.0");
table->cellAt(row, 6).firstCursorPosition().insertText("0.0");
table->cellAt(row, 7).firstCursorPosition().insertHtml(QString("<a href='#anchor%1'>图 %2</a>").arg(row).arg(row));
}
}
// 保存文档为doc文件
QFile file("D:/Desktop/1.doc");
if (file.open(QIODevice::WriteOnly)) {
QString str = document.toHtml(QByteArray("UTF-8")).toUtf8();
file.write(document.toHtml(QByteArray("UTF-8")).toUtf8());
file.close();
}
return 0;
}