Hyperlink Alignment issue with Aspose.Net PDF with DOM Approach - aspose

Hyperlink text not aligned properly in PDF. For example,if textfragment have Both normal text and hyperlink text irrespective of fragment.IsInLineParagraph = true/false;
in detail: have a multiple lines of hyperlink text with normal text, Hyperlink content coming in new line and not coming as inline text (continuous with normal text). Please find the attached screenshot.
TextFragment fragment = new Aspose.Pdf.Text.TextFragment();
TextSegment segment1 = new TextSegment("Before hyperlink ");
segment1.TextState.FontSize = 12;
segment1.TextState.Font= Aspose.Pdf.Text.FontRepository.FindFont("Arial");
fragment.Segments.Add(segment1);
fragment.IsInLineParagraph = true;
floatBox.Paragraphs.Add(fragment);
fragment = new Aspose.Pdf.Text.TextFragment();
fragment.IsInLineParagraph = true;
TextSegment segment = new TextSegment("This is Hyperlink This is HyperlinkThis is HyperlinkThis is HyperlinkThis is HyperlinkThis is HyperlinkThis is HyperlinkThis is HyperlinkThis is HyperlinkThis is Hyperlink ");
segment.TextState.ForegroundColor = Aspose.Pdf.Color.FromArgb(0, 119, 204);
segment.TextState.Underline = true;
segment.TextState.FontSize = 12;
segment.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial");
fragment.Segments.Add(segment);
fragment.Hyperlink = new WebHyperlink("www.aspose.com");
fragment.IsInLineParagraph = true;
floatBox.Paragraphs.Add(fragment);
fragment = new Aspose.Pdf.Text.TextFragment(" after hyperlink");
fragment.IsInLineParagraph = true;
floatBox.Paragraphs.Add(fragment);
Please Let me know if i miss anything in code or provide me solution how to resolve it.
Thanks in Advance!

Related

Read xml file which contain text and image and display it in a QtextEdit

i'm trying to develop at application window that contain a widget and two buttons Open and Save. As an input I have an xml file which contains text plus an image. I want to display both text and image in the same widget, apply modification and save it finally. My xml file look like this:
<?xml version="1.0" encoding="UTF-8"?>
<Stage>
<Item id = "1" Name = "Ensure span is erected">
<image id = "1" src = "im.png"/>
</Item>
</Stage>
What kind of widget can display both text and image
and what to do to display them?
I could display only image in a Qlabel
QPixmap logo;
QByteArray ba;
QFile file("img.txt");
if(file.open(QIODevice::ReadOnly)) {
ba = file.readAll();
}
logo.loadFromData(QByteArray::fromBase64(ba));
ui->label->setPixmap(logo.scaled(ui->label->width(),ui->label->height(),Qt::KeepAspectRatioByExpanding));
Any idea please!
Your help is highly appreciated.
Here is a small example. I ignore ids in my case. Convert your xml to html:
QString MainWindow::HtmlFromXml(const QString &xmlFileName)
{
QFile file(xmlFileName);
if (!file.open(QIODevice::ReadOnly)) {
qDebug() << "Cant open file: " << file.errorString();
return "";
}
QDomDocument htmlDoc;
QDomElement htmlRoot = htmlDoc.createElement("html");
QDomDocument xmlDoc;
xmlDoc.setContent(&file);
QDomElement xmlRoot = xmlDoc.documentElement();
QDomElement xmlItem = xmlRoot.firstChild().toElement();
while(!xmlItem.isNull())
{
//read xml
int itemId = xmlItem.attribute("id", "0").toInt();
QString itemName = xmlItem.attribute("Name", "");
QDomElement xmlImg = xmlItem.firstChild().toElement();
QString imgSrc;
int imgId = 0;
if (!xmlImg.isNull()) {
imgSrc = xmlImg.attribute("src", "");
imgId = xmlImg.attribute("id", "0").toInt();
}
//create html
QDomElement htmlItem = htmlDoc.createElement("p");
QDomElement htmlImg = htmlDoc.createElement("img");
htmlImg.setAttribute("src", imgSrc);
QDomElement htmlText = htmlDoc.createElement("p");
QDomText textName = htmlDoc.createTextNode(itemName);
htmlText.appendChild(textName);
htmlItem.appendChild(htmlImg);
htmlItem.appendChild(htmlText);
htmlRoot.appendChild(htmlItem);
//next
xmlItem = xmlItem.nextSibling().toElement();
}
htmlDoc.appendChild(htmlRoot);
return htmlDoc.toString();
}
Then set html you got to QTextEdit
QString strHtml = HtmlFromXml(fileName);
ui->textEdit->setHtml(strHtml);
Now you can edit in QTextEdit. Before saving you need to get html from QTextEdit
QString strHtml = ui->textEdit->toHtml();
QString strXml = XmlFromHtml(strHtml);
And convert this html into xml
QString MainWindow::XmlFromHtml(const QString &strHtml)
{
QDomDocument xmlDoc;
QDomElement xmlRoot = xmlDoc.createElement("Stage");
QDomDocument htmlDoc;
htmlDoc.setContent(strHtml);
QDomElement htmlRoot = htmlDoc.documentElement();
QDomElement htmlHead = htmlRoot.firstChild().toElement();
QDomElement htmlBody = htmlHead.nextSibling().toElement();
int dummyId = 1;
QDomElement htmlItem = htmlBody.firstChild().toElement();
while (!htmlItem.isNull())
{
//<p><img/></p><p>text</p>
QDomElement htmlImg = htmlItem.firstChild().toElement();
QString imgSrc = htmlImg.attribute("src", "");
htmlItem = htmlItem.nextSibling().toElement(); //move to <p> with text
QDomText itemText = htmlItem.firstChild().toText();
//create xml elements
QDomElement xmlItem = xmlDoc.createElement("Item");
xmlItem.setAttribute("id", dummyId);
xmlItem.setAttribute("Name", itemText.data());
QDomElement xmlImg = xmlDoc.createElement("image");
xmlImg.setAttribute("src", imgSrc);
xmlImg.setAttribute("id", dummyId);
xmlItem.appendChild(xmlImg);
xmlRoot.appendChild(xmlItem);
//next
htmlItem = htmlItem.nextSibling().toElement();
dummyId++;
}
xmlDoc.appendChild(xmlRoot);
return xmlDoc.toString();
}
And then save into text file.
Note: QTextEdit allow you to edit text, but have some troubles with images. You can remove or copy/paste images inside QTextEdit but you can not paste images from outside clipboard. It's because QTextEdit operates with <img/> text tag not with image. To edit your images inside QTextEdit you need to edit text html of your QTextEdit instance.

c++ builder Excel Ole Create new worksheet after the last sheet

I want to add a new worksheet not at the beginning but at the end of the worksheet tabs. But i can only manage to get it working at the front.
workSheet = workSheets.OleFunction("Add");
How can i change this line of code to make it add at the end ?
The Worksheets.Add() method has optional Before and After parameters:
Before
An object that specifies the sheet before which the new sheet is added.
After
An object that specifies the sheet after which the new sheet is added.
...
If Before and After are both omitted, the new sheet is inserted before the active sheet.
You need to set After param
Excel::_WorksheetPtr newSheet;
if (const auto sheetsCount = m_book->Worksheets->GetCount(); sheetsCount == 0)
newSheet = m_app->Worksheets->Add();
else
{
IDispatchPtr dispatch = m_app->Worksheets->GetItem(sheetsCount); // Number of sheet, not index
newSheet = m_app->Worksheets->Add(vtMissing, dispatch.GetInterfacePtr(), 1);
}
return newSheet;

Why does this code not set the background color for a cell (Aspose Cells)?

I need a cell to have a light blue background color; I have code that works in another sheet, but the same exact code (except for the names of the vars and column index val):
Style styleShipVariance = null;
CellsFactory cfShipVariance = new CellsFactory();
Cell ShipVarianceCell = customerWorksheet.Cells[0, SHIPVARIANCE_COL];
ShipVarianceCell.PutValue("Ship Variance");
styleShipVariance = cfShipVariance.CreateStyle();
styleShipVariance.HorizontalAlignment = TextAlignmentType.Left;
styleShipVariance.Font.Name = fontForSheets;
styleShipVariance.Font.IsBold = true;
styleShipVariance.Font.Size = 11;
styleShipVariance.ForegroundColor = Color.LightBlue;
styleShipVariance.Pattern = BackgroundType.Solid;
ShipVarianceCell.SetStyle(styleShipVariance);
...does not work on the other sheet. The value ("Ship Variance") is displayed in that cell, but the color is not being applied.
Why would this not work? What is missing?
Note: I also tried adding this:
var flag = new StyleFlag
{
CellShading = true,
FontName = true,
FontSize = true,
FontColor = true,
FontBold = true,
NumberFormat = true
};
...and changing the last line above to:
ShipVarianceCell.SetStyle(styleShipVariance, flag);
...but it made no difference.
UPDATE
I am saving as xlsx:
var filename = String.Format(#"{0}\{1} - Fill Rate - {2}.xlsx", sharedFolder, _unit, fromAsYYYYMMDD);
if (File.Exists(filename))
{
File.Delete(filename);
}
workBook.Save(filename, SaveFormat.Xlsx);
UPDATE 2
I just noticed that the vals are also not being bolded or left-aligned. The text values themselves are being assigned, but none of the formatting is...
UPDATE 3
Why it was necessary, I don't know, but by adding the colorizing code AFTER the data rows were added to the sheet, it works now.
Well, normally it should work fine. But if you are saving to an XLS file (instead of XLSX file), the light blue background color might not be applied so, you got to add to MS Excel color palette first via the line of code:
e.g
Sample code:
.........
workbook.ChangePalette(Color.LightBlue, 55);
Anyways, we need your template Excel file, so we could evaluate your issue precisely and test to apply the color to the cell on our side and guide you appropriately. I think you may follow up your thread in the Aspose.Cells forums.
I am working as Support developer/ Evangelist at Aspose.

Showing images along with text in QTableView model field

I am writing a Qt GUI C++ program where I am planning to populate a tabular view, for example a 3X2 table view. Where the texts in the field will be from a .csv file. And along with the text there will also be a small icon/image.
To give an idea of the UI it might be looking somewhat like;
Now adding text to QTable model view I have done using;
QStandardItemModel *model = new QStandardItemModel;
QFile file("/home/aj/beta_test.csv");
if (file.open(QIODevice::ReadOnly))
{
int lineindex = 0; // file line counter
QTextStream in(&file); // read to text stream
while (!in.atEnd()) {
QStringList lineToken;
QString fileLine = in.readLine(); // read one line (separated by "\n")
lineToken = fileLine.split(",", QString::SkipEmptyParts); // parse a line into separate pieces with "," as the delimiter
for (int j = 0; j < lineToken.size(); j++) // load parsed data to model accordingly
{
QString value = lineToken.at(j);
QStandardItem *item = new QStandardItem(value);
model->setItem(lineindex, j, item);
ui->tableView->setModel(model);
}
lineindex++;
}
file.close();
}
Now how to perform adding the image part???
You can use standard method:
item->setIcon(QIcon("path"));
or do this with index (use setData() and Qt::DecorationRole)
After adding you can call resizeRowsToContents() to show full images in your cells.
Also I noticed that you set your model in every iteration. It is not wrong but it is very inefficient (especially when you populate large data), so set your model one time after the loop.

How to drive excel window from Qt application

In my Qt application, I have a window with a table which gets dynamically updated with data from a backend server. I need my window to be able to open an excel instance, insert all the data in the table to excel window and update the excel cells when data in my table gets updated.
Is this something that is possible to achieve? If so, how I can get this done? I do not mind a platform dependent solution which can only run on Windows.
This is the code I used finally.
Opening Excel:
QAxObject* excel = new QAxObject("Excel.Application", 0);
excel->dynamicCall("SetVisible(bool)", true);
Reading cell values:
QAxObject* workbooks = excel->querySubObject("Workbooks");
QAxObject* workbook = workbooks->querySubObject("Open(const QString&)", "D:\\a.xlsx");
QAxObject* worksheet = workbook->querySubObject("Worksheets(int)", 1);
QAxObject* usedrange = worksheet->querySubObject("UsedRange");
QAxObject* rows = usedrange->querySubObject("Rows");
QAxObject* columns = usedrange->querySubObject("Columns");
int intRowStart = usedrange->property("Row").toInt();
int intColStart = usedrange->property("Column").toInt();
int intCols = columns->property("Count").toInt();
int intRows = rows->property("Count").toInt();
QAxObject* cell;
for (int i = intRowStart; i < intRowStart + intRows; i++)
{
for (int j = intColStart; j < intColStart + intCols; j++)
{
cell = excel->querySubObject("Cells(Int, Int)", i, j);
QVariant cellValue = cell->dynamicCall("value");
qDebug() << cellValue.toString();
}
}
Writing cell values
cell->setProperty("Value", "somevalue");
Note: Remember to create QApplication before doing any of these. I spent a considerable amount of time figuring out what is wrong by not doing this.