iText rotation creates pdf which displays out of memory exception - itext

Following is a code snippet creating a pdf file where pages could be rotated in the resulting file. This works fine for most pdf files. But one particualr pdf file of version 1.6 the page is already rotated by 180, on applying further rotation to it e.g. 90 degress and saving the file causes it to get corrupted. Infact even if you don't rotate the file and simply write it out to another file using iText the file the resulting pdf is corrupted and displays an out of memory exception when opened in Adobe reader.
Why would that happen? Am I missing some sort of compression in the file.
private String createPdfFileWithoutForms(final EditStateData[] editStateData, final String directory)
throws EditingException {
Long startTime = System.currentTimeMillis();
File pdfFileToReturn = new File(directory + File.separator + UidGenerator.generate() + ".pdf");
com.lowagie.text.Document document = null;
FileOutputStream outputStream = null;
PdfCopy pdfCopy = null;
PdfReader reader = null;
PdfDictionary pageDict = null;
int rotationAngle = 0;
Map<Integer, Integer> rotationQuadrants = null;
try {
document = new com.lowagie.text.Document();
outputStream = new FileOutputStream(pdfFileToReturn);
pdfCopy = new PdfCopy(document, outputStream);
pdfCopy.setFullCompression();
pdfCopy.setCompressionLevel(9);
document.open();
for (EditStateData state : editStateData) {
try {
reader = new PdfReader(state.getFileName());
reader.selectPages(state.getPages());
rotationQuadrants = state.getRotationQuadrants();
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
// Rotation quadrant key is the source page number
if (rotationQuadrants.containsKey(state.getPages().get(i - 1))) {
rotationAngle = reader.getPageRotation(i);
pageDict = reader.getPageN(i);
pageDict.put(PdfName.ROTATE,
new PdfNumber((rotationAngle
+ rotationQuadrants.get(state.getPages().get(i - 1))) % 360));
}
document.setPageSize(reader.getPageSizeWithRotation(i));
document.newPage();
// import the page from source pdf
PdfImportedPage page = pdfCopy.getImportedPage(reader, i);
// add the page to the destination pdf
pdfCopy.addPage(page);
}
} catch (final IOException e) {
LOGGER.error(e.getMessage(), e);
throw new EditingException(e.getMessage(), e);
} finally {
if (reader != null) {
reader.close();
}
}
}
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
throw new EditingException(e.getMessage(), e);
} finally {
if (document != null) {
document.close();
}
if (pdfCopy != null) {
pdfCopy.close();
}
IoUtils.closeQuietly(outputStream);
}
LOGGER.debug("Combining " + editStateData.length + " pdf files took "
+ ((System.currentTimeMillis() - startTime) / 1000) + " msecs");
return pdfFileToReturn.getAbsolutePath();
}

Related

“An error occured while executing doInBackground()”

i'm using the below code to get the file length and using which i display progress bar till i download the total file but i get an error from firebase crash report like below
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
#Override
protected String doInBackground(String...Url) {
try {
URL url = new URL(Url[0]);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
// Detect the file lenghth
fileLength = connection.getContentLength();
} else {
URLConnection connection = url.openConnection();
connection.connect();
// Detect the file lenghth
fileLength = connection.getContentLength();
}
/*
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("HEAD");
connection.connect();
int fileLength = connection.getContentLength();*/
// Locate storage location
// String filepath = Environment.getExternalStorageDirectory().getPath();
String filepath = getFilesDirectory(getApplicationContext()).getPath();
// File fo = getFilesDirectory(getApplicationContext());
// Download the file
InputStream input = new BufferedInputStream(url.openStream());
// Save the downloaded file
if (lang_slector == 0) {
// input stream to read file - with 8k buffer
File folder = new File(filepath, "/offlinedata/");
folder.mkdir();
File pdfFile = new File(folder, englishpdffilename);
try {
pdfFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
OutputStream output = new FileOutputStream(filepath + "/offlinedata/" +
pdffilename);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// Publish the progress
publishProgress((int)(total * 100 / fileLength));
output.write(data, 0, count);
}
// Close connection
output.flush();
output.close();
input.close();
} else if (lang_slector == 1) {
// input stream to read file - with 8k buffer
File folder = new File(filepath, "/offlinedata/");
folder.mkdir();
File pdfFile = new File(folder, englishpdffilename);
try {
pdfFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
OutputStream output = new FileOutputStream(filepath + "/offlinedata/" +
englishpdffilename);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// Publish the progress
publishProgress((int)(total * 100 / fileLength));
output.write(data, 0, count);
}
// Close connection
output.flush();
output.close();
input.close();
}
} catch (Exception e) {
// Error Log
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}

Issue in converting HTML to PDF containing <pre> tag with Flying Saucer and ITEXT

I am using Flying Saucer library to convert html to pdf. It is working fine with the all the HTML files.
But for some HTML files which include some tags in pre tag, generated PDF file has tags displayed.
If I remove pre tags then the formatting of data is lost.
My code is
org.w3c.dom.Document document = null;
try {
Document doc = Jsoup.parse(new File(htmlFile), "UTF-8", "");
Whitelist wl = new RelaxedPlusDataBase64Images();
Cleaner cleaner = new Cleaner(wl);
doc = cleaner.clean(doc);
Tidy tidy = new Tidy();
tidy.setShowWarnings(false);
tidy.setXmlTags(false);
tidy.setInputEncoding("UTF-8");
tidy.setOutputEncoding("UTF-8");
tidy.setPrintBodyOnly(true);
tidy.setXHTML(true);
tidy.setMakeClean(true);
tidy.setAsciiChars(true);
if (doc.select("pre").html().contains("</")) {
doc.select("pre").unwrap();
}
Reader reader = new StringReader(doc.html());
document = (tidy.parseDOM(reader, null));
Element element = (Element) document.getElementsByTagName("head").item(0);
element.getParentNode().removeChild(element);
NodeList elements = document.getElementsByTagName("img");
for (int i = 0; i < elements.getLength(); i++) {
String value = elements.item(i).getAttributes().getNamedItem("src").getNodeValue();
if (value != null && value.startsWith("cid:") && value.contains("#")) {
value = value.substring(value.indexOf("cid:") + 4, value.indexOf("#"));
elements.item(i).getAttributes().getNamedItem("src").setNodeValue(value);
System.out.println(value);
}
}
document.normalize();
System.out.println(getNiceLyFormattedXMLDocument(document));
} catch (Exception e) {
System.out.println(e);
}
Method to create PDF is :
try {
org.w3c.dom.Document doc = CleanHtml.cleanNTidyHTML("b.html");
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.setPDFVersion(new Character('7'));
String outputFile = "test.pdf";
OutputStream os = new FileOutputStream(outputFile);
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
By using itext XMLWorker :
try {
org.w3c.dom.Document doc = CleanHtml.cleanNTidyHTML("a.html");
String k = CleanHtml.getNiceLyFormattedXMLDocument(doc);
OutputStream file = new FileOutputStream(new File("test.pdf"));
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
ByteArrayInputStream is = new ByteArrayInputStream(k.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
document.close();
file.close();
} catch (Exception e) {
e.printStackTrace();
}
public static String getNiceLyFormattedXMLDocument(org.w3c.dom.Document doc) throws IOException, TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
// transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
Writer stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
transformer.transform(new DOMSource(doc), streamResult);
String result = stringWriter.toString();
return result;
}

How to specify parameters in box-view API header?

I am using the following piece of C# code to upload, convert and download a .pptx file, via Box view API.
var boxViewID = "";
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
String url_ = #"https://upload.view-api.box.com/1/documents";
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url_);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Headers.Add("Authorization:Token " + "MY_CODE"/*Configuration.BoxViewAPIKey*/);
wr.Method = "POST";
wr.KeepAlive = true;
wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
wr.Timeout = 1000000;
wr.SendChunked = true;
DateTime start = DateTime.Now;
Exception exc = null;
Stream rs = wr.GetRequestStream();
try
{
rs.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; non_svg=\"true\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate,"file", file, contentType);
Console.WriteLine(header);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[40960];
int bytesRead = 0;
int totalSent = 0;
int totalLength = (int)fileStream.Length;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
totalSent += bytesRead;
var percent = new decimal(100.0 * totalSent / totalLength);
if (progress != null)
{
progress("Box processing", percent);
}
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
}
catch(Exception ex)
{
exc = ex;
}
DateTime end = DateTime.Now;
int seconds = (int)(end - start).TotalSeconds;
if(seconds>=0)
{
if(exc!=null)
{
throw exc;
}
}
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
WebResponse wresp = null;
try
{
wresp = wr.GetResponse();
Stream stream2 = wresp.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
var res = reader2.ReadToEnd();
var docRes = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(res);
if (docRes["id"] != null)
boxViewID = docRes["id"];
}
catch (Exception ex)
{
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}
return boxViewID;
Specifying "non_svg" parameter should create .png images for every slide in the presentation (instead of .svg + .html pairs). However, the API seems to ignore this part of the request and I am always getting svg files.
Any idea on what am I doing wrong? Thanks!
The non_svg option causes PNG representations to be generated for each page, but the SVG representation is still generated. The viewer will only load the PNG files if SVG is not supported in the browser (basically only IE 8). Try changing page-1.svg to page-1.png in the browser (e.g., https://view-api.box.com/1/sessions/465c5d45caf04752a6113b0e5df593a5/assets/page-1.png vs https://view-api.box.com/1/sessions/465c5d45caf04752a6113b0e5df593a5/assets/page-1.svg). All of the assets will exist in content.zip if you use the documents content endpoint.

"Content can not be added to a PdfImportedPage." error

I am trying to download and merges multiple pdf files by using ITextSharp.
It used to working before but I being got an "Content can not be added to a PdfImportedPage." error message on the line:
importedPage = writer.GetImportedPage(reader, currentPageIndex);
The full code is below, any help will be very appreciated.
private string MergeDocuments(IList<string> fileUrls, string fileName)
{
var reportFolder = this.ReportFolder + "\\";
using (MemoryStream output = new MemoryStream())
{
Document document = new Document();
try
{
// Initialize pdf writer
PdfWriter writer = PdfWriter.GetInstance(document, output);
// Open document to write
document.Open();
PdfContentByte content = writer.DirectContent;
PdfImportedPage importedPage;
// Iterate through all pdf documents
foreach (var url in fileUrls)
{
// Create pdf reader
using (PdfReader reader = new PdfReader(new Uri(url)))
{
int numberOfPages = reader.NumberOfPages;
// Iterate through all pages
for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
{
// Determine page size for the current page
document.SetPageSize( reader.GetPageSizeWithRotation(currentPageIndex) );
// Create page
document.NewPage();
importedPage = writer.GetImportedPage(reader, currentPageIndex);
content.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
}
}
}
}
catch (Exception exception)
{
throw new Exception("Error occured", exception);
}
File.WriteAllBytes(reportFolder + fileName + ".pdf", output.GetBuffer());
}
return "Reports/" + fileName + ".pdf";
}
When I try the following code, I get a null pointer exception in the addDocument() method:
using (MemoryStream output = new MemoryStream()) {
Document document = new Document();
document.Open();
PdfCopy copy = new PdfSmartCopy(document, output);
foreach (var url in fileUrls) {
using (WebClient client = new WebClient()) {
var byteArray = client.DownloadData(url);
PdfReader reader = new PdfReader(byteArray);
copy.AddDocument(reader);
reader.Close();
}
}
}
I found the problem, the document object should be closed before writing memory stream to file.
Just added document.Close() as below.
document.Close();
File.WriteAllBytes(reportFolder + fileName + ".pdf", output.GetBuffer());

Android reading text fie from sdcsrd and store it in ArrayList

I need help on this one, I am trying to read a text file stored in sdcard and store the result in an ArrayList for subsequent usage, but the code is crashing.
public class Mytextreader extends Activity {
final ArrayList> dataList = new ArrayList>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loadData();
String text = "";
for (int i = 0; i < dataList.size(); i++) {
text = text + dataList.get(i).get("name") + ":"
+ dataList.get(i).get("image") + ":"
+ dataList.get(i).get("price") + ":"
+ dataList.get(i).get("barcode") + "\n";
}
TextView txv = (TextView) findViewById(R.id.textView01);
txv.setText(text);
}
private void loadData() {
File sdcardDir = Environment.getExternalStorageDirectory();
String sdcard = sdcardDir.getAbsolutePath();
File file = new File(sdcard + "/Downloads/data/data.txt");
// For each entry the following lines are repeated
HashMap<String, String> hmap = new HashMap<String, String>();
String text = "", line = "";
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
text = text + line + "\n";
}
br.close();
} catch (IOException e) {
Log.d("File Read test: Error= ", e.getMessage());
}
while (true) {
line = text.substring(0, text.indexOf('\n'));
text = text.substring(text.indexOf('\n') + 1);
hmap.put("name", line.substring(0, line.indexOf(';')));
line = line.substring(line.indexOf(';') + 1);
// Toast.makeText(this, line, Toast.LENGTH_LONG).show();
hmap.put("image", line.substring(0, line.indexOf(';')));
line = line.substring(line.indexOf(';') + 1);
// Toast.makeText(this, line, Toast.LENGTH_LONG).show();
hmap.put("price", line.substring(0, line.indexOf(';')));
line = line.substring(line.indexOf(';') + 1);
// Toast.makeText(this, line, Toast.LENGTH_LONG).show();
hmap.put("barcode", line);
dataList.add(hmap);
hmap.clear();
if (text.length() == 0)
break;
}
}
}
Ok fixed, the text file has an empty line, removed that line and the code is ok now