//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2014 by PDFTron Systems Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------
import pdftron.Common.PDFNetException;
import pdftron.PDF.*;
import pdftron.SDF.SDFDoc;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ConvertTest {
public static int indexOfAny(String str, String[] searchStrs, int startIdx) {
if ((str == null) || (searchStrs == null)) {
return -1;
}
int sz = searchStrs.length;
// String's can't have a MAX_VALUEth index.
int ret = Integer.MAX_VALUE;
int tmp = 0;
for (int i = 0; i < sz; i++) {
String search = searchStrs[i];
if (search == null) {
continue;
}
tmp = str.indexOf(search, startIdx);
if (tmp == -1) {
continue;
}
if (tmp < ret) {
ret = tmp;
}
}
return (ret == Integer.MAX_VALUE) ? -1 : ret;
}
public static PDFDoc textToPDF(String text_file_path)
{
PDFDoc doc = null;
try
{
doc = new PDFDoc();
ElementBuilder eb = new ElementBuilder();
ElementWriter writer = new ElementWriter();
Element element;
// Start a new page ------------------------------------
// Position an image stream on several places on the page
Page page = doc.pageCreate();
writer.begin(page); // begin writing to this page
// Begin writing a block of text
element = eb.createTextBegin(Font.create(doc, Font.e_courier), 8);
// Position the text on the page...
double col_start_x = 60;
double col_start_y = page.getPageHeight() - 20;
element.setTextMatrix(1, 0, 0, 1, col_start_x, col_start_y);
element.getGState().setLeading(15); // Set the spacing between lines
writer.writeElement(element);
String para = new String(Files.readAllBytes(Paths.get(text_file_path)), Charset.defaultCharset());
int para_end = para.length();
int text_run = 0;
int text_run_end;
// Set text column width
double para_width = page.getPageWidth() - col_start_x - 20;
// Draw the text on the page...
double cur_width = 0;
String[] line_break = {" ", "\r", "\n"};
while (text_run < para_end)
{
text_run_end = indexOfAny(para, line_break, text_run);
if (text_run_end < 0) text_run_end = para_end - 1;
boolean new_line = false;
if (para.charAt(text_run_end) == '\r' || para.charAt(text_run_end) == '\n')
{ // If new line character ...
new_line = true;
}
int num_chars = text_run_end-text_run;
String text = para.substring(text_run, text_run + (new_line ? num_chars : num_chars+1));
element = eb.createTextRun(text);
if (cur_width + element.getTextLength() < para_width)
{
writer.writeElement(element);
cur_width += element.getTextLength();
}
else
{
writer.writeElement(eb.createTextNewLine()); // New line
text = para.substring(text_run, text_run + (text_run_end-text_run+1));
element = eb.createTextRun(text);
cur_width = element.getTextLength();
writer.writeElement(element);
}
if (new_line)
{
writer.writeElement(eb.createTextNewLine()); // New line
if (para.charAt(text_run_end) == '\r' && text_run_end + 1 < para_end && para.charAt(text_run_end+1) == '\n')
{ // treat carriage return / linefeed pair as a single new line character.
++text_run_end;
}
}
text_run = text_run_end+1;
}
// Finish the block of text
writer.writeElement(eb.createTextEnd());
writer.end(); // save changes to the current page
doc.pagePushBack(page);
}
catch(IOException e)
{
System.out.print("Unable to convert text file '");
System.out.print(text_file_path);
System.out.print("' to PDF, IOException:");
System.out.println(e);
}
catch(PDFNetException e)
{
System.out.print("Unable to convert text file '");
System.out.print(text_file_path);
System.out.print("' to PDF, PDFNetException:");
System.out.println(e);
}
return doc;
}
public static void main(String[] args)
{
boolean uninstallPrinterWhenDone = false; // change this to test the uninstallation functions
PDFNet.initialize();
// Relative path to the folder containing test files.
String input_path = "../../TestFiles/";
String output_path = "../../TestFiles/Output/";
String outputFile;
// Convert a TXT document to PDF
try
{
System.out.println("Converting TXT to PDF");
PDFDoc doc = textToPDF(input_path + "input.txt");
outputFile = output_path + "output.pdf";
doc.save(outputFile, 0, null);
System.out.println("Result saved in " + outputFile);
}
catch(PDFNetException e)
{
System.out.println("Unable to convert TXT document to PDF, error:");
System.out.println(e);
}
System.out.println("Done.");
PDFNet.terminate();
}
}
We will provide you shortly with a preview version of the new API that will allow for quick & simple text layout, but in the meantime, I wanted to point couple of alternative ways to accomplish the same task with the current API:
Option A) Use pdftron.PDF.HTML2PDF (perhaps
by surrounding bits of text in HTML string tags)
https://www.pdftron.com/pdfnet/samplecode.html#HTML2PDF
Option B) Load Text with help of .NET Flow API then serialize content to PDF. This is shown in Xaml2Pdf sample: