Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6016881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:02:49+00:00 2026-05-23T03:02:49+00:00

M using the following code for printing but if there are some long lines

  • 0

M using the following code for printing but if there are some long lines in my text file ,they get cut from the sides while printing.What am i doing wrong?

  import java.io.FileInputStream;
    import java.io.FileNotFoundException;

    import javax.print.Doc;
    import javax.print.DocFlavor;
    import javax.print.DocPrintJob;
    import javax.print.PrintException;
    import javax.print.PrintService;
    import javax.print.PrintServiceLookup;
    import javax.print.SimpleDoc;
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.print.attribute.standard.Copies;
    import javax.print.attribute.standard.MediaSizeName;
    import javax.print.attribute.standard.OrientationRequested;

    public class PrintFileWithSpec {

        public static void printFile(String filename,String printerindx){

        FileInputStream psStream=null;
        int Printerinx=Integer.parseInt(printerindx);
        try {
           psStream = new FileInputStream(filename);
        } catch (FileNotFoundException ffne) {} 
        if (psStream == null) {
            return;
        }   
        DocFlavor psInFormat = null;

        int index=filename.lastIndexOf(".");
        String extension=filename.substring(index+1);

        if(extension.equals("txt"))//||extension.equals("log")||extension.equals("xml")||extension.equals("htm")||extension.equals("html"))
        psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;

        else if(extension.equals("jpg"))
            psInFormat = DocFlavor.INPUT_STREAM.JPEG;

        else if(extension.equals("png"))
            psInFormat = DocFlavor.INPUT_STREAM.PNG;

        else if(extension.equals("gif"))
            psInFormat = DocFlavor.INPUT_STREAM.GIF; 


        Doc myDoc = new SimpleDoc(psStream, psInFormat, null);  
        PrintRequestAttributeSet aset = 
            new HashPrintRequestAttributeSet();
        aset.add(new Copies(1));
        aset.add(MediaSizeName.ISO_A4); 

        //aset.add(Sides.DUPLEX);
         aset.add(OrientationRequested.PORTRAIT);
        PrintService[] services = 
        PrintServiceLookup.lookupPrintServices(psInFormat, null);
        System.out.println("Printer Selected "+services[Printerinx]);   

        //PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();

        DocFlavor[] docFalvor = services[Printerinx].getSupportedDocFlavors();
        for (int i = 0; i < docFalvor.length; i++) {
            System.out.println(docFalvor[i].getMimeType());
        }   
        if (services.length > 0) {
           DocPrintJob job = services[Printerinx].createPrintJob();
        try 
           {
            job.print(myDoc, aset);     
              System.out.print("Printing Doc");    
           } catch (PrintException pe)
           {           
               System.out.print(pe);          
           }
        }
        }   
        public static void main(String [] args)
        {       
            printFile("D:/testStream.txt","3");
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:02:50+00:00Added an answer on May 23, 2026 at 3:02 am

    You have to deal with line wrapping of text files yourself. The javadoc for DocFlavor says:

    Furthermore, every Java Print Service instance must fulfill these requirements for processing plain text print data:

    • The character pair carriage return-line feed (CR-LF) means “go to column 1 of the next line.”
    • A carriage return (CR) character standing by itself means “go to column 1 of the next line.”
    • A line feed (CR) character standing by itself means “go to column 1 of the next line.”
      *

    The client must itself perform all plain text print data formatting not addressed by the above requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using following code but cannot return data from MySQL. This is the output:
I have the following code: using System; using System.Diagnostics; using System.IO; using PdfSharp.Pdf.Printing; namespace
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
Using the following code, I'm retrieving a list of Integers from a DB and
I was using the following code to readn in an xml file into my
I'm using the following code to compress a small (~4kB) HTML file in C#.
Using System.Drawing.Printing, I want to print a set of lines on print document. But
I am using following code in rowdatabound function. Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object,
I am using following code to check whether a check box on my website
I am using following code to design my home page. The output (as shown

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.