(C#) PDFを読み込んで、
Stream内をすべて別ファイルに書き出す、サンプルソース
- 引数で、PDFファイル名を渡す
- PDFを、Byte列に、一度に読み込み
- PDF内、Byte列を順次上から下までナメて、stream~endstreamの中身を、ファイルに書き出し。
- "stream_out_" + 連番 + ".bin"
実行例
ソース
pdf_stream_out.cs
//c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe pdf_stream_out.cs
using System;
using System.IO;
class pdf_stream_out { //*1
static void Main(string[] args) { //*2
string p_fn = @"" + args[0];
byte[] data = File.ReadAllBytes(p_fn);
int strmCnt = 1;
int ARCN = 2; //array_count
string[] st = new string[ARCN];
st[0] = ">stream";
st[1] = "endstream";
byte[][] d = new byte[ARCN][];
for(int i=0; i<ARCN; ++i)
d[i] = System.Text.Encoding.ASCII.GetBytes(st[i]);
byte[][] dm = new byte[ARCN][];
for(int i=0; i<ARCN; ++i)
dm[i] = new byte[d[i].Length];
int stream_stt = 0;
int stream_size = 0;
for(int i=0;i<data.Length-st[1].Length;i++){ //-st[1]は、一番長い配列
for(int j=0; j<ARCN; ++j)
Array.Copy(data,i,dm[j],0,d[j].Length);
bool[] aEq = new bool[ARCN];
for(int j=0; j<ARCN; ++j){
aEq[j] = System.Linq.Enumerable.SequenceEqual(d[j], dm[j]);
if(aEq[j]){
//">stream"
if(j==0){
stream_stt = (int)(i+(int)d[j].Length+2);
Console.WriteLine("stream_start= " + stream_stt);
}
//"endstream"
if(j==1){
stream_size = (int)((int)i-3) - stream_stt +1;
Console.WriteLine("stream_size= " + stream_size);
byte[] data_out = new byte[stream_size];
Array.Copy(data,stream_stt,data_out,0,stream_size);
File.WriteAllBytes("stream_out_" +strmCnt+ ".bin", data_out);
strmCnt++;
}
string tx = System.Text.Encoding.ASCII.GetString(dm[j]);
Console.Write(i + "_" + tx + ":");
Console.WriteLine("_");
}
}
}
} //*2
} //*1
書き出し結果
参考
(C#) PDFを読み込んで、埋め込まれた画像をJPGファイルとして書き出す(DCTDecodeのStream内をママ書き出しているだけなのでかなり雑)
https://qiita.com/santarou6/items/5f011e266929558ede34
C# JpgからPDFへ変換(1Jpgファイルを1ページとしたPDFファイルの作成)
https://qiita.com/santarou6/items/ff24500c13d05b12a940