我使用以下函数在java中打印文档:
#include <set>
#include <map>
#include <utility>
#include <type_traits>
// (commented code are some other *failed* attempt).
template <typename Iterator, typename Lambda>
auto makeSet(Iterator first, Iterator last, Lambda &&f) {
typedef typename std::remove_reference<decltype(first->first)>::type const_first_t;
typedef typename std::remove_const<const_first_t>::type first_t;
typedef typename std::remove_reference<decltype(first->second)>::type second_t;
typedef std::pair<first_t, second_t> R;
std::set<R> res;
for (; first != last; ++first) res.insert(f(*first));
return res;
}
void foo()
{
std::map<int, int> m;
std::set<std::pair<int, int>> s =
makeSet(m.begin(), m.end(),
[](const auto &x)
{
return std::make_pair(x.second, x.first);
});
}
但问题是,一旦通过难以阅读就达到了行尾,就会打破长词。 例如,对于下面的字符串:
public static String printDocument(String textToPrint,PrintService mPrinter, PrintService defaultPrintService) {
String returnText = "";
try{
DocPrintJob job = defaultPrintService.createPrintJob();
InputStream is = new ByteArrayInputStream(textToPrint.getBytes());
Doc doc = new SimpleDoc(is, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
PrintJobWatcher pjDone = new PrintJobWatcher(job);
job.print(doc, null);
pjDone.waitForDone();
is.close();
returnText = "Printed successfully!";
}catch (Exception e){
returnText="Failed to print because of: "+e.getMessage();
}
return returnText;
}
它打印出来像这样:
textToPrint="Peter Parker Picked A Pack Of Pepper"
如果它溢出页面宽度,我希望它只是将单词打印到下一行:
Peter Parker Picked A Pa
ck Of Pepper
我该怎么做?