Microsoft Windows Phone 8.1 support ends (13 Jul 2017)

Microsoft has ended support for Windows Phone 8.1

Questions about Android development and PDF

ImportPage and get results in memory

More
IP: 192.168.0.71 7 years 11 months ago #13681 by arlomedia
I have a PDF file I'm building in memory (using PdfDocument in the Android SDK) and I want to merge some existing PDF files (saved on disk) into it.

My data is in the "data" byte array, and this part seems to be working:
Code:
Document combinedDocument = new Document(); PDFMemStream stream = new PDFMemStream(data); combinedDocument.OpenStream(stream, ""); combinedDocument.SetCache(Global.tmp_path + "/ttt.dat"); for (Integer addPage : attachedDocuments.keySet()) { Document addDocument = new Document(); combinedDocument.Open(attachedDocuments.get(addPage), null); Document.ImportContext context = combinedDocument.ImportStart(addDocument); combinedDocument.ImportPage(context, 1, addPage); context.Destroy(); addDocument.Close(); } combinedDocument.Save(); combinedDocument.Close();

But now do I now get the new data from combinedDocument? I see a way to create a document from data in memory, but not a way to get the data from a document. I need something like combinedDocument.getData().
More
IP: 192.168.0.71 7 years 11 months ago #13685 by Davide
Hi,
first of all I suggest you to check concat_pdf() method of PDFTestAct to import pdf page correctly.
You need something like that :
Code:
private void concat_pdf( byte[] yourByteArray, String src ) { Document doc_dst = new Document(); Document doc_src = new Document(); PDFMemStream m_stream = new PDFMemStream(yourByteArray); doc_dst.OpenStream(m_stream, null); doc_dst.SetCache(Global.tmp_path + "/ttt.dat"); doc_src.Open(src, null); Document.ImportContext ctx = doc_dst.ImportStart(doc_src); int dstno = doc_dst.GetPageCount(); int srccnt = doc_src.GetPageCount(); int srcno = 0; while( srcno < srccnt ) { doc_dst.ImportPage(ctx, srcno, dstno); dstno++; srcno++; } ctx.Destroy(); doc_src.Close(); doc_dst.Save(); doc_dst.Close(); }

To "getData" from a document you have to open it using for example PDFMemStream and call get_data() method
More
IP: 192.168.0.71 7 years 11 months ago #13686 by arlomedia
Right, I adapted my code from the concat_pdf() method. But I don't see how to get from doc_dst, which is a Document object, back to a byte array. As I manipulate the Document object (doc_dst), does the underlying PDFMemStream (m_stream) or byte array (yourByteArray) also get updated? If so, I know what to do. If only the Document gets updated, then I need to get the new byte array from that somehow.
More
IP: 192.168.0.71 7 years 11 months ago #13687 by Davide
Hi,
yes you can use the PDFMemStream or the byte array, but you don't have to close the pdf
Code:
doc_dst.Save(); //doc_dst.Close();
More
IP: 192.168.0.71 7 years 11 months ago #13688 by arlomedia
Okay, I tried this, but myData is the same at the end as it was at the start:
Code:
Document combinedDocument = new Document(); PDFMemStream stream = new PDFMemStream(myData); combinedDocument.OpenStream(stream, ""); combinedDocument.SetCache(Global.tmp_path + "/ttt.dat"); for (Integer addPage : attachedDocuments.keySet()) { Document addDocument = new Document(); addDocument.Open(attachedDocuments.get(addPage), null); Document.ImportContext context = combinedDocument.ImportStart(addDocument); combinedDocument.ImportPage(context, 1, addPage); context.Destroy(); addDocument.Close(); } combinedDocument.Save(); //combinedDocument.Close(); myData = stream.get_data();

Then I went back to the original code from concat_pdf, using files saved on the device, and doc_dst was also unchanged:
Code:
Document doc_dst = new Document(); Document doc_src = new Document(); doc_dst.Open("/path/to/my/file/1", null); doc_dst.SetCache(Global.tmp_path + "/ttt.dat"); doc_src.Open("/path/to/my/file/2", null); Document.ImportContext ctx = doc_dst.ImportStart(doc_src); int dstno = doc_dst.GetPageCount(); int srccnt = doc_src.GetPageCount(); int srcno = 0; while( srcno < srccnt ) { doc_dst.ImportPage(ctx, srcno, dstno); dstno++; srcno++; } ctx.Destroy(); doc_src.Close(); doc_dst.Save(); doc_dst.Close();

I don't get any errors or crashes with these, but also nothing seems to happen -- nothing changes either in the data in memory in the first case, or the saved file in the second case. I'm using the demo Premium license while I test this. Am I missing something?
More
IP: 192.168.0.71 7 years 11 months ago #13694 by Davide
Hi,
what version of the library are you using?
Can you reproduce it with the last version of the library? www.radaeepdf.com/download/file/105-radaeepdf-3-17

Here a working code :
Code:
Document doc_dst = new Document(); Document doc_src = new Document(); PDFMemStream pdfMemStream = null; try { pdfMemStream = new PDFMemStream(readFile(new File(file))); doc_dst.OpenStream( pdfMemStream, null); } catch (IOException e) { e.printStackTrace(); } doc_dst.SetCache(Global.tmp_path + "/ttt.dat"); doc_src.Open(src, null); ImportContext ctx = doc_dst.ImportStart(doc_src); int dstno = doc_dst.GetPageCount(); int srccnt = doc_src.GetPageCount(); int srcno = 0; while( srcno < srccnt ) { doc_dst.ImportPage(ctx, srcno, dstno); dstno++; srcno++; } ctx.Destroy(); doc_src.Close(); doc_dst.Save(); //doc_dst.Close(); pdfMemStream.get_data();

You can use pdfMemStream if you want the stream or pdfMemStream.get_data() if you want the byte array.
Time to create page: 0.528 seconds
Powered by Kunena Forum