I'm building a program allows me to answer questions with radio buttons, and then save a list of the questions and the selected answers to a .txt file which I can convert to a .pdf from Word or some other program. Im using a PrintWriter to write the file, and everything is working as expected for all of that.

My problem is that I also need to write a .png image to the bottom of that text file. I used an imageReader to get the image into my code, and I know that part is working correctly because I can save the image to a different file than my text file...but I can't figure out a way to get it into my .txt file. I'll put the code below. Thanks in advance for any ideas.

private void writeToFile(PrintWriter out) {
	    	 list= new String[22];
				int ii  = 0;
				for(int i = 0;i< buttons.length;i++){
					if(buttons[i]){
						int j = i%3;
						if(j==2){
							list[ii]="N/A";
							ii++;
						}
						else if(j==1){
							list[ii]="No";
							ii++;
						}
						else{
							list[ii]="Yes";
							ii++;
						}
		
					}
				}
				img = null;
				try {
				    img = ImageIO.read(new File("resources/sig.png"));
				} catch (IOException e) {
					ta.setText("Error:	IO.read method"+ e);
				}
				
				try {
				    bi = img; // retrieve image
				    
				    outPutFile = new File("testFileToSeeIfImageIO.readLoadedTheImageAsExpected.png");
				    ImageIO.write(bi, "png", (outPutFile));
				} catch (IOException e) {
				    ta.setText("Error: IO.write method" + e);
				}
				
			

				 String format = "%1$-70s%2$-10s\n";
			out.println();
			out.println();
			out.println();
			out.println();
			out.println(" Integrated Construction");
			out.println("P.O. Box 0000");
			out.println("North Conway, NH 00000");
			out.println();
			out.println();
			out.println();
			out.println("Property Address:  "+ addressList.getSelectedItem() );
			out.println();
			out.println();
			out.println();
			for(int i =0;i<questions.length;i++){
				out.format(format, questions[i],list[i]);
			}
	
		}

Thanks
Scott

Scott,

Well, it depends what you want to do with the file after you're done.... however, once you write a PNG file to the back of a TXT file, it's really not a TXT file anymore. You could write out your data as binary, then tack the PNG onto the back...

In the end, with that kind of a file, you will need some form of interpreter/decoder to separate the text part of your data from the image part. Now, if you wrote out a PDF directly from your code, you could then combine the text and the image data into a single document, and that doc would be instantly viewable (since, of course, its a PDF).

There are some libraries for Java that allow you to monkey with PDFs (PDFBox, iText). Here is a link for some libraries that provide PDF manipulation capability:

http://java-source.net/open-source/pdf-libraries

Hopefully this is somewhat helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.