Java에서 이미지 형식을 PDF로 변환

Aspose.PDF for Java는 다양한 래스터 및 벡터 이미지 형식을 PDF 문서로 변환할 수 있습니다.

BMP를 PDF로 변환

BMP 이미지를 PDF 문서에 배치해야 하는 경우 이 예를 사용하세요.

  1. 출력 PDF를 보관할 빈 Document를 만드세요.
  2. Page를 추가하고 page.addImage(...)으로 BMP를 배치하세요.
  3. 래스터 콘텐츠가 PDF 페이지 영역을 채우도록 Rectangle로 대상 이미지 직사각형을 정의하세요.
  4. 출력 PDF 파일을 저장하세요.
public static void convertBmpToPdf(Path inputFile, Path outputFile) {
        try (Document document = new Document()) {
            try (Page page = document.getPages().add()) {
                page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
            }
            document.save(outputFile.toString());
        }
        System.out.println(inputFile + " converted into " + outputFile);
    }

CGM을 PDF로 변환

CGM 그래픽 파일을 PDF로 변환해야 하는 경우 이 예를 사용하세요.

  1. 파일 경로와 CgmLoadOptions를 Document 생성자에 전달하여 CGM 소스를 여세요.
  2. 문서를 로드하는 동안 Aspose.PDF가 CGM 그래픽 스트림을 해석하도록 하세요.
  3. 변환된 PDF를 대상 출력 경로에 저장하세요.
public static void convertCgmToPdf(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString(), new CgmLoadOptions())) {
        document.save(outputFile.toString());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

DICOM을 PDF로 변환

의료 DICOM 이미지를 PDF 문서로 래핑해야 하는 경우 이 예를 사용하세요.

  1. PDF 출력을 위해 빈 Document를 만드세요.
  2. Image 개체를 생성하고 해당 ImageFileType를 Dicom으로 설정하고 소스 파일 경로를 할당하세요.
  3. Page를 추가하고 DICOM 이미지를 페이지 단락 컬렉션에 추가하세요.
  4. 결과를 PDF로 저장하세요.
public static void convertDicomToPdf(Path inputFile, Path outputFile) {
    try (Document document = new Document()) {
        Image image = new Image();
        image.setFileType(ImageFileType.Dicom);
        image.setFile(inputFile.toString());

        try (Page page = document.getPages().add()) {
            page.getParagraphs().add(image);
        }

        document.save(outputFile.toString());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

직접 문서 로딩을 통해 EMF를 PDF로 변환

기본 EMF 로드 경로를 통해 EMF 파일을 PDF로 변환해야 하는 경우 이 예를 사용하세요.

  1. 빈 Document를 만들고 EMF 소스를 바이너리 스트림으로 여세요.
  2. Page를 추가하고 여백을 지워 EMF 아트워크가 전체 페이지 영역을 차지할 수 있도록 하세요.
  3. Image를 만들고 여기에 EMF 스트림을 바인딩한 다음 페이지 단락 컬렉션에 추가하세요.
  4. 출력 PDF 파일을 저장하세요.
public static void convertEmfToPdf01(Path inputFile, Path outputFile) throws IOException {
    try (Document document = new Document();
         FileInputStream imageStream = new FileInputStream(inputFile.toFile())) {
        try (Page page = document.getPages().add()) {
            page.getPageInfo().getMargin().setBottom(0);
            page.getPageInfo().getMargin().setTop(0);
            page.getPageInfo().getMargin().setLeft(0);
            page.getPageInfo().getMargin().setRight(0);

            Image image = new Image();
            image.setFileType(ImageFileType.Unknown);
            image.setImageStream(imageStream);
            page.getParagraphs().add(image);
        }
        document.save(outputFile.toString());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

대체 작업 흐름을 사용하여 EMF를 PDF로 변환

대체 설정 또는 페이지 구성 흐름을 사용하여 EMF 콘텐츠를 변환해야 하는 경우 이 예를 사용하세요.

  1. Aspose.Imaging을 사용하여 EMF 소스를 로드하고 PDF 배치 전에 메모리 내 PNG 스트림으로 렌더링하세요.
  2. 빈 Document를 만들고 Page를 추가하세요.
  3. 중간 바이트 스트림에서 Image를 생성하고 페이지에 추가하세요.
  4. 변환된 PDF를 저장하세요.
public static void convertEmfToPdf02(Path inputFile, Path outputFile) throws IOException {
    try (Document document = new Document();
         com.aspose.imaging.Image emfImage = com.aspose.imaging.Image.load(inputFile.toString());
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
        emfImage.save(byteArrayOutputStream, new PngOptions());

        try (Page page = document.getPages().add()) {
            Image image = new Image();
            image.setImageStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
            page.getParagraphs().add(image);
        }

        document.save(outputFile.toString());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

GIF를 PDF로 변환

PDF 페이지에 GIF 이미지를 추가해야 하는 경우 이 예를 사용하세요.

  1. PDF 출력을 위해 빈 Document를 만드세요.
  2. Page를 추가하고 page.addImage(...)로 GIF를 배치하세요.
  3. 이미지가 페이지 영역을 채우도록 Rectangle을 사용하여 배치 경계를 정의하세요.
  4. 출력 PDF를 저장하세요.
public static void convertGifToPdf(Path inputFile, Path outputFile) {
    try (Document document = new Document()) {
        try (Page page = document.getPages().add()) {
            page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
        }
        document.save(outputFile.toString());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

JPEG를 PDF로 변환

JPEG 이미지를 한 페이지짜리 PDF로 변환해야 하는 경우 이 예를 사용하세요.

  1. 출력 PDF에 대해 빈 Document를 만드세요.
  2. Page를 추가하고 page.addImage(...)으로 JPEG 이미지를 삽입하세요.
  3. 래스터 이미지가 페이지 좌표에 매핑되는 방식을 제어하려면 Rectangle을 사용하세요.
  4. 생성된 PDF 파일을 저장하세요.
public static void convertJpegToPdf(Path inputFile, Path outputFile) {
    try (Document document = new Document()) {
        try (Page page = document.getPages().add()) {
            page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
        }
        document.save(outputFile.toString());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

PNG를 PDF로 변환

PNG 이미지를 PDF 문서로 묶어야 하는 경우 이 예를 사용하세요.

  1. 변환 출력을 위해 빈 Document를 만드세요.
  2. Page를 추가하고 그 위에 page.addImage(...)를 사용하여 PNG 이미지를 배치하세요.
  3. Rectangle을 사용하여 페이지 캔버스에 맞춰 이미지 크기를 조정하세요.
  4. 출력 파일을 저장하세요.
public static void convertPngToPdf(Path inputFile, Path outputFile) {
    try (Document document = new Document()) {
        try (Page page = document.getPages().add()) {
            page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
        }
        document.save(outputFile.toString());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

SVG를 PDF로 변환

PDF 문서 내에서 SVG 아트웍을 렌더링해야 하는 경우 이 예를 사용하세요.

  1. 파일 경로와 SvgLoadOptions를 Document 생성자에 전달하여 SVG 소스를 여세요.
  2. Aspose.PDF가 SVG 마크업을 구문 분석하고 로드 중에 해당 PDF 그래픽 모델을 생성하도록 하세요.
  3. PDF 출력을 대상 파일 경로에 저장하세요.
public static void convertSvgToPdf(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString(), new SvgLoadOptions())) {
        document.save(outputFile.toString());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

TIFF를 PDF로 변환

TIFF 이미지를 PDF로 변환해야 하는 경우 이 예를 사용하세요.

  1. PDF 출력을 위해 빈 Document를 만드세요.
  2. Page를 추가하고 page.addImage(...)으로 TIFF 이미지를 배치하세요.
  3. TIFF 콘텐츠가 페이지 좌표에 매핑되도록 Rectangle로 배치 영역을 정의하세요.
  4. 결과를 PDF로 저장하세요.
public static void convertTiffToPdf(Path inputFile, Path outputFile) {
    try (Document document = new Document()) {
        try (Page page = document.getPages().add()) {
            page.addImage(inputFile.toString(), new Rectangle(0, 0, 595, 842, true));
        }
        document.save(outputFile.toString());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

CDR을 PDF로 변환

CorelDRAW CDR 파일을 PDF로 변환해야 하는 경우 이 예를 사용하세요.

  1. 파일 경로와 CdrLoadOptions를 Document 생성자에 전달하여 CDR 소스를 여세요.
  2. Aspose.PDF가 CorelDRAW 콘텐츠를 PDF 문서 모델에 로드하도록 하세요.
  3. 변환된 PDF 파일을 요청된 출력 경로에 저장하세요.
public static void convertCdrToPdf(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString(), new CdrLoadOptions())) {
        document.save(outputFile.toString());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}