Turn .txt file into .pdf file on the fly? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I'm we're trying to figure out if there would be a way to convert a .txt file to a .pdf file. Here's the catch. This needs to be done behind the scenes, and on the fly. Meaning, with a radio control selected, OnOK would create a .txt file. Behind the scenes, at run time, we would like for the .txt file to be converted to a .pdf file. Ideally we would like this to be done by running an executable in the background. The executable would take input "File.txt" and output "File.pdf". We're using C++ and Visual Studio 6.
Does anyone have any experience on this? Is this possible?

libHaru may do what you want. Demo.

This a2pdf tool will probably do the trick with minimal effort. Just be sure to turn off perl syntax highlighting.
http://perl.jonallen.info/projects/a2pdf

I recommend using this open source library.
Once you have the base for generating PDF documents programmatically, you would still need a method for converting the text to the PDF elements, while keeping the text flow and word wrapping. This article may help. Please pay attention to the DoText(StreamReader sr) function. It takes text and purge it into separate lines within the PDF document, keeping the rendered within the margins.

On of the simpler methods that has worked for 3 decades e.g. more than one quarter of a century is place a postscript header before the text then use ghostscript ps2pdf it is the same method as used by some commercial apps such as acrobat
at its most basic
Copy heading.ps file.txt printfile.ps
GS -sDEVICE=pdfwrite printfile.ps printfile.pdf
Master Example can be seen here
How to modify this plaintext-to-PDF-converting PostScript from 1992 to actually specify a page size?

Related

Create an invisible text file in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Put simply, I have a browser widget I'm using in FLTK Gui toolkit which can be loaded using a file like so.
browser::load("textfile.txt");
The problem is I don't want to create a physical text file, just an invisible one so I can use it as an argument for browser::load above. I plan to use this invisible text file by loading it with the values I'm going to place in my browser....then use it like this.
browser::load("invisible_textfile.txt");
Is it possible to do this in C++?
I have already tried using ifstream::rdbuf() which probably has nothing to do with this. I'm not even sure what to call this so I'm just calling it an invisible textfile for now.
I'm using windows 7 64 bit. MinGW compiler.
Let's say what you want to add is equivalent of text file like this:
One
Two
Three
But you don't want to have the text file. So one piece of code which would do the same thing is this:
const char *lines[] = { "One", "Two", "Three", 0 };
for(int i = 0 ; lines[i] != 0 ; ++i)
browser.add(lines[i]);
Documentation link for that overload of add
Please refine your question, or perhaps ask a new question, if you want more help on how to get lines with your data.
It depends a lot on what browser::load() actually does internally, but let's assume that it will look for your filename and load it.
You probably already know how to read / write a standard file (e.g. http://www.cplusplus.com/doc/tutorial/files/). Now, if you just want to hide the file from the user, you can set OS specific hidden flags (e.g. windows). I'm assuming that's not what you actually want to accomplish here. (You could obviously also create a temporary file that you delete again, but it's not an elegant solution either.)
What you might want to use is a named pipe. Under Linux you can create these with mkfifo and then stream content through those file objects.
The general point is, though, unless the browser API allows you to pass it a complete string holding the text file or a stringstream, you will need a file object.
If your only target system is NTFS, there is a good answer on creating virtual files over here:
How to create a virtual file?
But in the end, you probably want to create an actual file (in your case probably a temporary one, though). I would recommend placing that file into the systems temporary path.

How do you create headers for c++ within the sublime text 2 interface? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm using Sublime text 2 to create a c++ program, but i do not know how to create and implement a new header in the interface.
Could someone help me out with this?
Well, for starters, it is important to realize that Sublime Text 2 is a text editor, not an integrated development environment. Sublime Text has syntax highlighting features, essentially making it a "smart" text editor. Atop of this, there are several plugins which enable the user to get common features of typical IDEs, such as text completion.
So, to answer your question, create a new file with an extension of .h and add appropriate header gaurds to it, and include it in your build system, whether it be c-make or make or whatever.
A header file is really no different to any other C++ file. By convention, they end in .h (or .hpp) and are only #included by other C++ files. So all you need to do is simply create a new file (File > New File or Ctrl+N) and then save it (File > Save or Ctrl+S) with a file name ending in .h.

C++: Text File's Extensions? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
When i use text files for input and output using fstream filestream, the file extension used is .txt
I have seen people use instead of .txt:
They use .DAT and still they open it in a text editor as if it were a text file.
So is DAT a text file extension and are there what are all the extensions i can use with text files.?
The short answer: text files can have any extension you want, including NO extension. You can take somefile.txt and rename it to somefile.XYZPDQ if you feel like it. It will still be a text file.
That's from a pure C++ language perspective. At the operating system level, a file extension may be associated with a certain program type (you might have .mp4 videos open in a video player, for example). But you can still call any text file anything you want. Nothing stops you from doing this.
An extension is just part of file name. There is no difference what extension you use DAT or TXT. I mean, extensions help to people to recognize the file type, only to people.
You can use any extension, but :
using .txt, you usually give a hint that the file can be opened in a text editor (like vim or notepad) and will be readable by humans
using .dat, you usually give a hint that the file is binary and cannot be opened with a text editor. One should use a special program (maybe yours ;) or an hexadecimal binary editor, and its content will not be easily readable or modifiable.
another common extension you may use is .csv for comma-separated-values files (even when not using comma but tabs or anything), than can be opened either in a text editor or in a spreadsheet app like openoffice of excel.
windows users often use .ini extension to hint that the file is a text file (viewable in a text editor) containing some key/value paramters like ConfirmBeforeExit=true. By extension, it is used for any text file containing parameters.
another one is .log, hinting that the file is a text file, containing the log of execution of something. A linux user will then immediately do a tail -f foo.log while the app is running to look for problems.
By the way, using upper case extensions like TXT or DAT is a reminder of old DOS time and is now considered bad style. Just use lower case.
You can use any extension, because it does not matter. The .dat extension is usually used for binary data, so it may not be obvious for users of your program that it is in fact an editable text file.
The extension has no effect on what type of data you can put in the file. For example you can use TXT, DAT, and even(not recommended) EXE. It's best to stick with one extension. If something is meant to be read by a human, I would use TXT, but DAT or the like to indicate otherwise.

How to extract plain text from MS word document file in pure C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Is there any pure C++ library to extract plain text from a .doc file?
I'm developing a C++ program to read .doc and .pdf files. I have to extract plain text from the file and write it into a .txt file.
You could have a look at the open source C library used by Abiword, wv.
You can also call out to a batch convert tool
Open source batch converter, based on OpenOffice: http://dag.wieers.com/home-made/unoconv/
The open source for unix: http://www.wagner.pp.ru/~vitus/software/catdoc/
Proprietary for windows: http://doc2txt.com/. Note I havn't tried this one.
If you want to manipulate/read .doc files, you can just take the time and learn the format and manipulate the .doc file manually. You can get it at the MSDN page linking to the format-specification (PDF file).
I admit, it's quite a bit of reading to do, but if you're looking to create software to manipulate/read files, you should have the relevant underlying knowledge to back it all up.
Same goes for the pdf format (which is an open format, and as such specifications should be easy to find).
For doc - Use the Word object model to get to the the document and extract the text. This example uses OLE Automation and C . Another link for DOCX that might help you.
For PDF - Use Haru .
You could always use OIVT (OutsideIn Viewer Technology, I think) now owned by oracle.
I'll be honest, it's not a cheap solution, and while this product is to allow you view, print, etc... I think if i remember correctly, they do offer an option to extract the content to text or they another product that does that. it can do this from pretty much any document type including doc, docx, pdf (just to name a few) without having to use the "original" application installed as they have their own set of filters.
Here's a link to get you started
Outside In Viewer Technolog
Good luck

Should the text in a C++ text based game be in the code or in external files? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am creating a text based game using C++ for a school project, the game works by allowing the user to pick a choice from a list of options in each scene; similar to how the games hosted by Choice of Games work. As a result of this I have a large amount of text that must be displayed in my game, however I am unsure as to the proper conventions when working with large amounts text in a program. Should I simply make use of std::cout and write the text directly into the code, or should I write into text files an used std::ifstream in order to read the text.
My only major concern regarding the use of files to hold the text is that each choice the user makes results in a different paragraph being displayed and as a result I believe that I would need to create a text file for each paragraph, which seems like it will lead to more issues (such as using the wrong file name or mistyping my code leading to the game reading from the wrong file) than writing the text straight into the code could. If there is a way to read particular sections of a text file then this would be useful to know, however I am currently unaware of any such method. However I am new to C++ and I am certain that there is plenty that I have yet to learn so I would not be surprised if such a method did exist.
Any help is greatly appreciated, be it anything from simply telling me if I should enter text into my code or into files, to telling me if there is a way to read text from specific sections of a text file. And once again, I am very grateful for any help you can provide.
Please don't put displayed text into code. That's an antipattern. You have to recompile your game for every minor text change like fixing typos, and for major changes like translating into other languages.
Convention for most programming languages is to put all the displayed text into (a few) resource files or properties files as key-value pairs, where the code only references the key of the paragraph to be displayed and the value will be loaded from that external file. (Usually once during startup.) No need to use one file per paragraph, but the kv pairs have to be parsed. There'll be utilities for you to reuse.
I recommend using external files. It makes changing the content much easier and doesn't require recompiling the entire program for a simple typo.
You can use one file and just separate each paragraph with a blank line. Grabbing "all text between blank lines" at that point is trivial.
If the choices cause the paragraph choices to jump around the file you can give them IDs and load them on-the-fly by searching linearly through the file for a given ID.
--EDIT--
As per the request here is an algorithm or two:
Algorithm 1:
Give each paragraph an ID, usually a simple number on the line immediately above the paragraph.
Separate each number-paragraph pair by blank lines.
Parse the file line-by-line looking for a "line" that contains only a number.
From that point you found the paragraph you are looking for, all lines until the next blank is the content of that paragraph.
Display to user.
Algorithm 2 (recommended):
Use XML to store your paragraphs and their IDs.
Use TinyXML2 to parse the file: http://www.grinninglizard.com/tinyxml2/index.html
If you do not plan to translate you game to other languages, you are on your own, both approaches have their pros and cons:
text in source: easy to write, text is near the place where it is used.
text in resource files: easier to remove duplicate strings, forces a better structure of text data.
If you simply imagine that your application could be translated, then you should put all text in ressource files. You can even find framework that will assist your for translations as Gnu gettext, but you can find others, for example qt has its own translation tools.
Storing text in the program files is not a good coding practice. This would result in unnecessary code bloat (it's not even code) and the need to recompile if you need to change the text.
A simple solution would be to create a text file with careful formatting like line numbers or whitespace that would allow you to pull out the desired text.
A more elegant solution would be to put the necessary text in xml or json files, and read them into your program when necessary. This would be a great choice.