Question
How can Length of Bytes Written by Writer be Read
To generate a quality PDF document using the answer provided here, the length of written bytes should be calculated before the Writer
is done since it should be the value before w.write("%%EOF\n")
.
How can the length of written bytes be read Eg. before line w.write("xref\n")
in the answer?
EDIT
The following generates Test15.pdf with calculated xref
length according to the answer below.
private void createPdf15() {
try {
//Image image = Image.createImage("/Logo.jpg");
Image image = Image.createImage("/android_logo.png");
//Image image = Image.createImage("/ios_logo.png");
//create a jpeg encoded image
EncodedImage encodedImage = EncodedImage.createFromImage(image, true);
byte[] imageData = encodedImage.getImageData();
int imageWidth = encodedImage.getWidth();
int imageHeight = encodedImage.getHeight();
int imageLength = imageData.length;
//create ByteArrayInputStream from image byte array
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageData);
FileSystemStorage fss = FileSystemStorage.getInstance();
String pdfPath = fss.getAppHomePath() + "Test15.pdf";
StreamCounter streamCount = new StreamCounter(fss.openOutputStream(pdfPath));
try (Writer w = new OutputStreamWriter(streamCount, "ISO-8859-1")) {
w.write("%PDF-1.7\n");
w.write("1 0 obj<</Type/Catalog/Pages 2 0 R>>endobj\n");
w.write("2 0 obj<</Type/Pages/Kids [3 0 R]/Count 1>>endobj\n");
w.write("3 0 obj<</Type/Page/Parent 2 0 R/Resources 4 0 R/MediaBox [0 0 520 800]/Contents 6 0 R>>endobj\n");
w.write("4 0 obj<</Font<</F1 5 0 R>>/XObject<</Im0 7 0 R>>>>endobj\n");
w.write("5 0 obj<</Type/Font/Subtype/Type1/BaseFont/Helvetica>>endobj\n");
w.write("6 0 obj<</Length 219>>stream\n");
w.write("q\n");
w.write("BT /F1 24 Tf 175 720 Td (Codename One)Tj ET\n");
w.write("BT /F1 20 Tf 100 700 Td (Using one codebase, build)Tj ET\n");
w.write("BT /F1 20 Tf 0 660 Td (Android apps)Tj ET\n");
w.write("BT /F1 20 Tf 0 640 Td (iOS apps)Tj ET\n");
w.write("BT /F1 20 Tf 0 600 Td (UWP apps)Tj ET\n");
w.write("q 50 0 0 50 4 540 cm /Im0 Do Q\n");
w.write("Q\n");
w.write("endstream\n");
w.write("endobj\n");
int bytesWritten = streamCount.getWritten();
Log.p("1bytesWritten: " + bytesWritten); // Total1 = 0 -> Text1Length = 0
w.write("7 0 obj<</Type/XObject/Subtype/Image/Width " + imageWidth + "/Height " + imageHeight + "/ColorSpace/DeviceRGB/BitsPerComponent 8/Filter/DCTDecode/Length " + imageLength + ">>stream\n");
//read the next byte of data from ByteArrayInputStream in int range 0 - 255
int nextChar = byteArrayInputStream.read();
while (nextChar > -1) {
//convert int to char and add it to char array
char[] charArray = {(char) nextChar};
//write char array
w.write(charArray);
nextChar = byteArrayInputStream.read();
}
w.write("\nendstream\n");
w.write("endobj\n");
bytesWritten = streamCount.getWritten();
Log.p("2bytesWritten: " + bytesWritten); //Total2 = 16384 -> Image1Length = 7732 Text2Length = 8652 ??
w.write("xref\n");
w.write("0 8\n");
w.write("0000000000 65535 f \n");
w.write("0000000009 00000 n \n");
w.write("0000000052 00000 n \n");
w.write("0000000102 00000 n \n");
w.write("0000000197 00000 n \n");
w.write("0000000255 00000 n \n");
w.write("0000000316 00000 n \n");
w.write("0000000609 00000 n \n");
w.write("trailer\n");
w.write("<</Size 9/Root 1 0 R>>\n");
w.write("startxref\n");
//w.write("133724\n");
w.write(bytesWritten + "\n");
w.write("%%EOF\n");
w.flush();
bytesWritten = streamCount.getWritten();
Log.p("3bytesWritten: " + bytesWritten);//Total3 = 17466
} catch (Exception e) {
Log.p("Error " + e);
}
} catch (Exception e) {
Log.p("Error " + e);
}
}
2 61
2