mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
XML export tool finished
This commit is contained in:
@ -152,7 +152,9 @@ void PDFToolAbstractApplication::initializeCommandLineParser(QCommandLineParser*
|
|||||||
if (optionFlags.testFlag(XmlExport))
|
if (optionFlags.testFlag(XmlExport))
|
||||||
{
|
{
|
||||||
parser->addOption(QCommandLineOption("xml-export-streams", "Export streams as hexadecimally encoded data. By default, stream data are not exported."));
|
parser->addOption(QCommandLineOption("xml-export-streams", "Export streams as hexadecimally encoded data. By default, stream data are not exported."));
|
||||||
parser->addOption(QCommandLineOption("xml-export-streams-as-text", "Export streams as text, if possible. This flag enforces exporting stream data (possibly as hexadecimal strings)."));
|
parser->addOption(QCommandLineOption("xml-export-streams-as-text", "Export streams as text, if possible."));
|
||||||
|
parser->addOption(QCommandLineOption("xml-use-indent", "Use automatic indent when writing output xml file."));
|
||||||
|
parser->addOption(QCommandLineOption("xml-always-binary", "Do not try to attempt transform strings to text."));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,6 +229,14 @@ PDFToolOptions PDFToolAbstractApplication::getOptions(QCommandLineParser* parser
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (optionFlags.testFlag(XmlExport))
|
||||||
|
{
|
||||||
|
options.xmlExportStreams = parser->isSet("xml-export-streams");
|
||||||
|
options.xmlExportStreamsAsText = parser->isSet("xml-export-streams-as-text");
|
||||||
|
options.xmlUseIndent = parser->isSet("xml-use-indent");
|
||||||
|
options.xmlAlwaysBinaryStrings = parser->isSet("xml-always-binary");
|
||||||
|
}
|
||||||
|
|
||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,10 +57,10 @@ struct PDFToolOptions
|
|||||||
Qt::DateFormat verificationDateFormat = Qt::DefaultLocaleShortDate;
|
Qt::DateFormat verificationDateFormat = Qt::DefaultLocaleShortDate;
|
||||||
|
|
||||||
// For option 'XMLExport'
|
// For option 'XMLExport'
|
||||||
bool xmlExportStreams = false; dodelat optiony
|
bool xmlExportStreams = false;
|
||||||
bool xmlExportStreamsAsText = false;
|
bool xmlExportStreamsAsText = false;
|
||||||
bool xmlUseIndent = true; dodelat
|
bool xmlUseIndent = false;
|
||||||
bool xmlAlwaysBinary = false; dodelat
|
bool xmlAlwaysBinaryStrings = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Base class for all applications
|
/// Base class for all applications
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
#include "pdftoolxml.h"
|
#include "pdftoolxml.h"
|
||||||
#include "pdfvisitor.h"
|
#include "pdfvisitor.h"
|
||||||
#include "pdfencoding.h"
|
#include "pdfencoding.h"
|
||||||
|
#include "pdfexception.h"
|
||||||
|
|
||||||
#include <QXmlStreamWriter>
|
#include <QXmlStreamWriter>
|
||||||
|
|
||||||
@ -29,8 +30,10 @@ static PDFToolXmlApplication s_xmlApplication;
|
|||||||
class PDFXmlExportVisitor : public pdf::PDFAbstractVisitor
|
class PDFXmlExportVisitor : public pdf::PDFAbstractVisitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PDFXmlExportVisitor(QXmlStreamWriter* writer) :
|
PDFXmlExportVisitor(QXmlStreamWriter* writer, const pdf::PDFDocument* document, const PDFToolOptions* options) :
|
||||||
m_writer(writer)
|
m_writer(writer),
|
||||||
|
m_options(options),
|
||||||
|
m_document(document)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -50,6 +53,8 @@ private:
|
|||||||
void writeTextOrBinary(const QByteArray& stream, QString name);
|
void writeTextOrBinary(const QByteArray& stream, QString name);
|
||||||
|
|
||||||
QXmlStreamWriter* m_writer;
|
QXmlStreamWriter* m_writer;
|
||||||
|
const PDFToolOptions* m_options;
|
||||||
|
const pdf::PDFDocument* m_document;
|
||||||
};
|
};
|
||||||
|
|
||||||
void PDFXmlExportVisitor::visitNull()
|
void PDFXmlExportVisitor::visitNull()
|
||||||
@ -109,7 +114,32 @@ void PDFXmlExportVisitor::visitStream(const pdf::PDFStream* stream)
|
|||||||
{
|
{
|
||||||
m_writer->writeStartElement("stream");
|
m_writer->writeStartElement("stream");
|
||||||
visitDictionary(stream->getDictionary());
|
visitDictionary(stream->getDictionary());
|
||||||
dodelat export dat
|
|
||||||
|
if (m_options->xmlExportStreams)
|
||||||
|
{
|
||||||
|
m_writer->writeTextElement("data", QString::fromLatin1(stream->getContent()->toHex()).toUpper());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_options->xmlExportStreamsAsText)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Attempt to decode the stream. Exception can be thrown.
|
||||||
|
QByteArray decodedData = m_document->getDecodedStream(stream);
|
||||||
|
|
||||||
|
bool isBinary = true;
|
||||||
|
QString text = pdf::PDFEncoding::convertSmartFromByteStringToUnicode(decodedData, &isBinary);
|
||||||
|
if (!isBinary)
|
||||||
|
{
|
||||||
|
m_writer->writeTextElement("text", text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (pdf::PDFException)
|
||||||
|
{
|
||||||
|
// Do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m_writer->writeEndElement();
|
m_writer->writeEndElement();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,9 +154,19 @@ void PDFXmlExportVisitor::visitReference(const pdf::PDFObjectReference reference
|
|||||||
|
|
||||||
void PDFXmlExportVisitor::writeTextOrBinary(const QByteArray& stream, QString name)
|
void PDFXmlExportVisitor::writeTextOrBinary(const QByteArray& stream, QString name)
|
||||||
{
|
{
|
||||||
bool isBinary = false;
|
bool isBinary = true;
|
||||||
m_writer->writeStartElement(name);
|
m_writer->writeStartElement(name);
|
||||||
QString text = pdf::PDFEncoding::convertSmartFromByteStringToUnicode(stream, &isBinary);
|
|
||||||
|
QString text;
|
||||||
|
if (!m_options->xmlAlwaysBinaryStrings)
|
||||||
|
{
|
||||||
|
text = pdf::PDFEncoding::convertSmartFromByteStringToUnicode(stream, &isBinary);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
text = QString::fromLatin1(stream.toHex()).toUpper();
|
||||||
|
}
|
||||||
|
|
||||||
m_writer->writeAttribute("form", isBinary ? "binary" : "text");
|
m_writer->writeAttribute("form", isBinary ? "binary" : "text");
|
||||||
m_writer->writeCharacters(text);
|
m_writer->writeCharacters(text);
|
||||||
m_writer->writeEndElement();
|
m_writer->writeEndElement();
|
||||||
@ -175,7 +215,7 @@ int PDFToolXmlApplication::execute(const PDFToolOptions& options)
|
|||||||
writer.writeComment(comment);
|
writer.writeComment(comment);
|
||||||
writer.writeStartElement("document");
|
writer.writeStartElement("document");
|
||||||
|
|
||||||
PDFXmlExportVisitor visitor(&writer);
|
PDFXmlExportVisitor visitor(&writer, &document, &options);
|
||||||
writer.writeStartElement("trailer");
|
writer.writeStartElement("trailer");
|
||||||
document.getStorage().getTrailerDictionary().accept(&visitor);
|
document.getStorage().getTrailerDictionary().accept(&visitor);
|
||||||
writer.writeEndElement();
|
writer.writeEndElement();
|
||||||
|
Reference in New Issue
Block a user