Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9747

(C#) PDFを読み込んで、Stream内をすべて別ファイルに書き出す、サンプルソース

$
0
0
(C#) PDFを読み込んで、 Stream内をすべて別ファイルに書き出す、サンプルソース 1) 引数で、PDFファイル名を渡す 2) PDFを、Byte列に、一度に読み込み 3) PDF内、Byte列を順次上から下までナメて、stream~endstreamの中身を、ファイルに書き出し。 4) "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 書き出し結果

Viewing all articles
Browse latest Browse all 9747

Trending Articles