繁体   English   中英

如何使用java编写和读取.xps文件?

[英]how to write and read .xps file using java?

像下面的代码生成一个pdf文件

package write;

import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.List;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class CreatePdf {
    public static void main(String[] args) {
        try {
            File file = new File(/*pdf file location*/);
            FileOutputStream fileout = new FileOutputStream(file);
            Document document = new Document();
            PdfWriter.getInstance(document, fileout);
            document.addAuthor("Me");
            document.addTitle("My iText Test");
            document.open();
            Chunk chunk = new Chunk("iText Test");
            Font font = new Font(Font.COURIER);
            font.setStyle(Font.UNDERLINE);
            font.setStyle(Font.ITALIC);
            chunk.setFont(font);
            chunk.setBackground(Color.CYAN);
            document.add(chunk);
            Paragraph paragraph = new Paragraph();
            paragraph.add("Hello World");
            paragraph.setAlignment(Element.ALIGN_CENTER);
            document.add(paragraph);

            Image image;

            try {
                image = Image.getInstance(/*Image location*/);
                image.setAlignment(Image.MIDDLE);
                document.add(image);

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            List list = new List(true, 15);
            list.add("ABC");
            list.add("DEF");
            document.add(list); 

            PdfPTable table = new PdfPTable(3);
            table.addCell("Name");
             table.addCell("Age");
             table.addCell("Location");

             table.addCell("Name1");
             table.addCell("Age1");
             table.addCell("Location1");

             table.addCell("Name2");
             table.addCell("Age2");
             table.addCell("Location2");
             document.add(table);   

System.out.println("Pdf is created");
            document.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}

是否有任何 jar 文件可用于在 java 中读取和写入 .xps 文件? 是否可以读取现有的 .xps 文件并提取文本、图像、表格等.....

您可以使用 Microsoft 的 XPS Document Writer PrintService 打印成 XPS 文件。

class myReceipt implements Printable{

    @Override
    public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
    String temp;

    if (page > 0) { /* We have only one page, and 'page' is zero-based */
        return NO_SUCH_PAGE;
    }

    /* User (0,0) is typically outside the imageable area, so we must
     * translate by the X and Y values in the PageFormat to avoid clipping
     */
    Graphics2D g2d = (Graphics2D)g;
    g2d.translate(pf.getImageableX(), pf.getImageableY());
    int lineSize=20;

    Font testFont=new Font("Lucida Console", Font.BOLD, 20);
    // font name, style (0 for Plain), font size
    g.setFont(testFont);
    int line=20;

    g.drawString("        Fatura/Recibo nº"+nmrRec+"      ", 5, line);
    return PAGE_EXISTS;
}

}

你实际做的部分

Date data = new Date();                                         //Data
DateFormat dataform = new SimpleDateFormat("dd-MM-yyyy");       //Data

PrintService service=getPrinterService("Microsoft XPS Document Writer");
if(service!=null){
    try{
        File outputFile = new File(dataform.format(data)+"-Recibo"+nmrRec+".xps");

        Doc doc = new SimpleDoc(new myReceipt(), DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        attributes.add(new Destination(outputFile.toURI()));

        DocPrintJob job = service.createPrintJob();
        job.print(doc, attributes);
    } catch(Exception e){
        System.out.println("kaboom"+e);
    }
}
else{
    System.out.println("XPS Printer not found");
}

除了上面提到的写入部分,读取部分可以通过java-axp轻松完成,它可以让您阅读XPS。 https://code.google.com/p/java-axp/

这里要求的AS是另一种简单的写法 public class Main {

public static void main(String[] args) {

    Date data = new Date(); // Data
    DateFormat dataform = new SimpleDateFormat("dd-MM-yyyy"); // Data

    PrintService service = getPrinterService("Microsoft XPS Document Writer");
    PrinterJob.lookupPrintServices();//
    // System.out.println("negin");
    if (service != null) {
        try {
            File outputFile = new File(dataform.format(data)
                    + "-Recibo.xps");

            Doc doc = new SimpleDoc(new XlsExample(),
                    DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

            PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
            attributes.add(new Destination(outputFile.toURI()));

            DocPrintJob job = service.createPrintJob();
            job.print(doc, attributes);
        } catch (Exception e) {
            System.out.println("kaboom" + e);
        }
    } else {
        System.out.println("XPS Printer not found");
    }
}

public static PrintService getPrinterService(String name) {
    try {
        PrintService[] printServices = PrintServiceLookup
                .lookupPrintServices(null, null);
        PrintService printService = PrintServiceLookup
                .lookupDefaultPrintService();
        String printerName = "sss";// PrintConfig.getOsReceiptPrinterName();

        for (int i = 0; i < printServices.length; i++) {
            PrintService service = printServices[i];
            System.out.println("service.getName() " + service.getName());
            if ("Microsoft XPS Document Writer".equals(service.getName())) {
                return service;
            }
        }
        // job.setPrintService(printService);
        // printToKitchen=false;
    } catch (Exception e) {
    }
    return null;
}

}

请让我知道这是否符合您的目的,或者您是否需要其他东西

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM