ImportPage and get results in memory - RadaeePDF.com
Signin/Signup with: 
Welcome, Guest
Username: Password: Remember me
13 Jul 2017
Microsoft has ended support for Windows Phone 8.1
Read More...
Questions about Android development and PDF
  • Page:
  • 1
  • 2

TOPIC: ImportPage and get results in memory

ImportPage and get results in memory 5 months 2 weeks ago #13681

  • arlomedia
  • arlomedia's Avatar Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 41
  • Thank you received: 1
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:
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().

Please Log in or Create an account to join the conversation.

ImportPage and get results in memory 5 months 2 weeks ago #13685

  • Davide
  • Davide's Avatar
  • Offline
  • User is blocked
  • User is blocked
  • Posts: 814
  • Karma: 17
  • Thank you received: 65
Hi,
first of all I suggest you to check concat_pdf() method of PDFTestAct to import pdf page correctly.
You need something like that :
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

Please Log in or Create an account to join the conversation.

ImportPage and get results in memory 5 months 2 weeks ago #13686

  • arlomedia
  • arlomedia's Avatar Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 41
  • Thank you received: 1
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.

Please Log in or Create an account to join the conversation.

ImportPage and get results in memory 5 months 2 weeks ago #13687

  • Davide
  • Davide's Avatar
  • Offline
  • User is blocked
  • User is blocked
  • Posts: 814
  • Karma: 17
  • Thank you received: 65
Hi,
yes you can use the PDFMemStream or the byte array, but you don't have to close the pdf
doc_dst.Save();
//doc_dst.Close();

Please Log in or Create an account to join the conversation.

ImportPage and get results in memory 5 months 2 weeks ago #13688

  • arlomedia
  • arlomedia's Avatar Topic Author
  • Offline
  • Senior Boarder
  • Senior Boarder
  • Posts: 41
  • Thank you received: 1
Okay, I tried this, but myData is the same at the end as it was at the start:
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:
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?

Please Log in or Create an account to join the conversation.

ImportPage and get results in memory 5 months 2 weeks ago #13694

  • Davide
  • Davide's Avatar
  • Offline
  • User is blocked
  • User is blocked
  • Posts: 814
  • Karma: 17
  • Thank you received: 65
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 :
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.

Please Log in or Create an account to join the conversation.

  • Page:
  • 1
  • 2
Powered by Kunena Forum