OEM販売のご相談
ご相談ください!

PDF Tool APIサンプルコード:レイヤーのフラット化

機能イメージ

レイヤーのフラット化をします。

概要

サンプルコードの概要

レイヤーをフラット化します。

  • PtlPDFDocument: PDF文書を表現したクラス
  • PtlPDFDocument.flatLayer(): レイヤーをフラット化

サンプルコード

/*
	Antenna House PDF Tool API V8.0
	C++ Interface sample program

	概要:レイヤーのフラット化

	Copyright 2025 Antenna House, Inc.
*/

#include 
#include 

using namespace std;
using namespace PdfTk;

int main(int argc, char* argv[]) {

	try {
		if (argc < 3) {
			printf("usage: FlatLayer.exe in-pdf-file out-pdf-file\n");
			return 1;
		}
		PtlParamInput input(argv[1]);
		PtlParamOutput output(argv[2]);

		PtlPDFDocument doc;

		// PDFファイルをロードします。
		doc.load(input);

		// レイヤーをフラット化します。
		doc.flatLayer();

		// ファイルに保存します。
		doc.save(output);

	}
	catch (PtlException e)
	{
		fprintf(stderr, "Error code : %d\n %s\n", e.getErrorCode(), e.getErrorMessage().c_str());
		return 1;
	}

	return 0;
}

            
/*
    Antenna House PDF Tool API V8.0
    Java Interface sample program

    概要:レイヤーのフラット化

    Copyright 2025 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class FlatLayer {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2)
        {
            System.out.println("usage: java FlatLayer in-pdf-file out-pdf-file");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlParamOutput outputFile = new PtlParamOutput(args[1]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // PDFファイルをロード
            doc.load(inputFile);

            // レイヤーをフラット化します。
            doc.flatLayer();

            // ファイルに保存します
            doc.save(outputFile);
        }
        catch (PtlException pex) {
            System.out.println("PtlException : ErrorCode = " + pex.getErrorCode() + "\n  " + pex.getErrorMessage());
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        catch (Error ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        finally {
            System.out.println("-- 完了 --");
        }
    }
}

            
/*
	Antenna House PDF Tool API V8.0
	.Net Interface sample program

	概要:レイヤーのフラット化

	Copyright 2025 Antenna House, Inc.
*/

using System;
using PdfTkNet;

namespace FlatLayer
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("usage: FlatLayer.exe in-pdf-file out-pdf-file");
                return;
            }

            try
            {
                using (PtlParamInput input = new PtlParamInput(args[0]))
                using (PtlParamOutput output = new PtlParamOutput(args[1]))
                using (PtlPDFDocument doc = new PtlPDFDocument())
                {

                    // PDFファイルをロードします。
                    doc.load(input);

                    doc.flatLayer();

                    doc.save(output);
                }
            }
            catch (PtlException pex)
            {
                Console.WriteLine(pex.getErrorCode() + " : " + pex.getErrorMessageJP());
                pex.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("-- 完了 --");
            }
        }
    }
}


            

サンプルコードのダウンロードはこちら

実行例

コマンドラインでの実行例

FlatLayer.exe C:\in\test_Layer.pdf C:\sav\outFlatLayer.pdf

java -jar FlatLayer.jar C:\in\test_Layer.pdf C:\sav\outFlatLayer.pdf
-- 完了 --
FlatLayer.exe C:\in\test_Layer.pdf C:\sav\outFlatLayer.pdf
-- 完了 --

出力結果イメージ

出力されたPDFでは各レイヤーが画像となって表示されています。
出力前にあったレイヤーがなくなっているのが確認できます。

出力イメージ

サンプルコードのダウンロード

サンプルコード

サンプルで使用した入出力PDF