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

(C#) PDFを読み込んで、埋め込まれたTiff画像を書き出す(CCITTFaxDecode)

$
0
0
(C#) PDFを読み込んで、埋め込まれたTiff画像を書き出す(CCITTFaxDecode) 受信したFAXから生成されたPDFから、Tiff画像を書き出すサンプルソースです。 Tiff画像の情報は、幅高さ以外は、すべて決め打ち(固定値)にしているので、 対象のPDFの内容に応じて、変更してください。(解像度など) Tiffのタグについて タグ数15個(固定) ImageWidth[256] = ※画像より取得 ImageLength[257] = ※画像より取得 BitsPerSample[258] = 1(固定) Compression[259] = 4(固定) PhotometricInterpretation[262] = 0(固定) FillOrder[266] = 1(固定) StripOffsets[273] = 8(固定) Orientation[274] = 1(固定) SamplesPerPixel[277] = 1(固定) RowsPerStrip[278] = ※画像より取得 StripByteCounts[279] = ※画像より取得 XResolution[282] (192/1 = 192.000000)(固定)*適宜修正 …アドレス値 YResolution[283] (192/1 = 192.000000)(固定)*適宜修正 …アドレス値 PlanarConfiguration[284] = 1(固定) ResolutionUnit[296] = 2(固定) ※型 >3: SHORT型(2バイト短整数) >4: LONG型(4バイト長整数) >5: RATIONAL型(はじめの4バイトが分子で、残り4バイトが分母) pdf_CCITTFD_out1.cs //c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe pdf_CCITTFD_out1.cs using System; using System.IO; class pdf_CCITTFD_out1 { //*1 static byte[] rtIIBytes(int i){ byte[] rtBt = new byte[2]; string t0 = "0000" + Convert.ToString(i, 16); string t1 = t0.Substring(t0.Length - 4, 4); string t2_r = t1.Substring(t1.Length - 2, 2); string t2_l = t1.Substring(0, 2); rtBt[0] = Convert.ToByte(t2_r,16); rtBt[1] = Convert.ToByte(t2_l,16); return rtBt; } static byte[] rtTag(int i1,int i2,int i3,int i4,int i5,int i6){ byte[] rtBt = new byte[12]; Array.Copy(rtIIBytes(i1),0,rtBt,0,2); Array.Copy(rtIIBytes(i2),0,rtBt,2,2); Array.Copy(rtIIBytes(i3),0,rtBt,4,2); Array.Copy(rtIIBytes(i4),0,rtBt,6,2); Array.Copy(rtIIBytes(i5),0,rtBt,8,2); Array.Copy(rtIIBytes(i6),0,rtBt,10,2); return rtBt; } static void Main(string[] args) { //*2 string p_fn = @"" + args[0]; byte[] data = File.ReadAllBytes(p_fn); int ImgCnt = 1; int ARCN = 9; //array_count string[] st = new string[ARCN]; st[0] = "/CCITTFaxDecode"; st[1] = " obj"; st[2] = "endobj"; st[3] = ">stream"; st[4] = "endstream"; st[5] = "/Columns "; st[6] = "/Rows "; st[7] = "/Width "; st[8] = "/Height "; 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; int intColumns = 0; int intRows = 0; int intWidth = 0; int intHeight = 0; bool outFlg = false; byte[] TiffHeader = {0x49,0x49,0x2A,0x00}; for(int i=0;i<data.Length-st[0].Length;i++){ 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]){ //" obj" if(j==1){ outFlg = false; //リセット } //"/CCITTFaxDecode" if(j==0){ outFlg = true; } //">stream" if(j==3){ stream_stt = (int)(i+(int)d[j].Length+2); } //"endstream" if(j==4){ stream_size = (int)((int)i-3) - stream_stt +1; if(outFlg){ /* Console.WriteLine("intColumns= " + intColumns); Console.WriteLine("intRows= " + intRows); Console.WriteLine("intWidth= " + intWidth); Console.WriteLine("intHeight= " + intHeight); */ byte[] data_out = new byte[stream_size]; Array.Copy(data,stream_stt,data_out,0,stream_size); var fileName = @".\CCITTFD_stream_out" +ImgCnt+ ".tif"; using (var writer = new BinaryWriter(new FileStream(fileName, FileMode.Create))) { writer.Write(TiffHeader); writer.Write(rtIIBytes(stream_size+8)); writer.Write((byte)0x00); writer.Write((byte)0x00); writer.Write(data_out); writer.Write(rtIIBytes(15));//タグ数 writer.Write(rtTag(256,3,1,0,intWidth,0));//ImageWidth[256] writer.Write(rtTag(257,3,1,0,intHeight,0));//ImageLength[257] writer.Write(rtTag(258,3,1,0,1,0));//BitsPerSample[258] writer.Write(rtTag(259,3,1,0,4,0));//Compression[259] writer.Write(rtTag(262,3,1,0,0,0));//PhotometricInterpretation[262] writer.Write(rtTag(266,3,1,0,1,0));//FillOrder[266] writer.Write(rtTag(273,4,1,0,8,0));//StripOffsets[273] writer.Write(rtTag(274,3,1,0,1,0));//Orientation[274] writer.Write(rtTag(277,3,1,0,1,0));//SamplesPerPixel[277] writer.Write(rtTag(278,3,1,0,intHeight,0));//RowsPerStrip[278] writer.Write(rtTag(279,4,1,0,stream_size,0));//StripByteCounts[279] writer.Write(rtTag(282,5,1,0,stream_size+8+2+180+4,0));//XResolution[282] writer.Write(rtTag(282,5,1,0,stream_size+8+2+180+4+8,0));//YResolution[283] writer.Write(rtTag(284,3,1,0,1,0));//PlanarConfiguration[284] writer.Write(rtTag(296,3,1,0,2,0));//ResolutionUnit[296] writer.Write(rtIIBytes(0)); writer.Write(rtIIBytes(0)); //XResolution writer.Write(rtIIBytes(192)); writer.Write(rtIIBytes(0)); writer.Write(rtIIBytes(1)); writer.Write(rtIIBytes(0)); //YResolution writer.Write(rtIIBytes(192)); writer.Write(rtIIBytes(0)); writer.Write(rtIIBytes(1)); writer.Write(rtIIBytes(0)); //File.WriteAllBytes("CCITTFD_stream_out" +ImgCnt+ ".bin", TiffHeader, False); //File.WriteAllBytes("CCITTFD_stream_out" +ImgCnt+ ".bin", data_out, true); } ImgCnt++; } } string tx = System.Text.Encoding.ASCII.GetString(dm[j]); //Console.Write(i + "_" + tx + ":"); //Columns Rows Width Height なら、数値を拾いにいく if(j>=5){ //int r = BitConverter.ToInt32(data[i+1], 0); int c=0; for(int c1=0;c1<20;c1++){ int c2 =(int)data[c1+i+d[j].Length]; //数字なら文字コードが48~57 *改行考慮せず if(c2>=48&&c2<=57){ }else{c=c1;break;} } byte[] b = new byte[c]; Array.Copy(data,i+d[j].Length,b,0,c); string tx1 = System.Text.Encoding.ASCII.GetString(b); //Console.Write(tx1); switch (j) { case 5: intColumns = Int32.Parse(tx1); break; case 6: intRows = Int32.Parse(tx1); break; case 7: intWidth = Int32.Parse(tx1); break; case 8: intHeight = Int32.Parse(tx1); break; default: break; } } //Console.WriteLine("_"); } } } } //*2 } //*1 過去投稿 (C#) PDFを読み込んで、埋め込まれた画像をJPGファイルとして書き出す(DCTDecodeのStream内をママ書き出しているだけなのでかなり雑) https://qiita.com/santarou6/items/5f011e266929558ede34 (C#) PDFを読み込んで、Stream内をすべて別ファイルに書き出す、サンプルソース https://qiita.com/santarou6/items/5272458b1e9ff2058327 C# JpgからPDFへ変換(1Jpgファイルを1ページとしたPDFファイルの作成) https://qiita.com/santarou6/items/ff24500c13d05b12a940 外部サイト TIFFのフォーマット TIFFのフォーマット(その1) https://jprogramer.com/libtiffcate/3188 TIFFのフォーマット(その2) https://jprogramer.com/libtiffcate/3211 iTextSharp https://itextpdf.com/en/products/itext-7 https://github.com/itext/itext7-dotnet iText 7 Community for .NET (former iTextSharp) consists of several dlls. C#でPDFファイルから画像を抜き出す モノクロTIFF編 (iTextSharp) https://araramistudio.jimdo.com/2021/03/19/c-%E3%81%A7pdf%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%81%8B%E3%82%89%E7%94%BB%E5%83%8F%E3%82%92%E6%8A%9C%E3%81%8D%E5%87%BA%E3%81%99-%E3%83%A2%E3%83%8E%E3%82%AF%E3%83%ADtiff%E7%B7%A8-itextsharp/ Tiff Analyzer Tiff解析ソフト「Tiff Analyzer」 https://www.vector.co.jp/soft/dl/winnt/art/se251005.html TiffファイルのTAGを検出して表示とCSVファイルに保存

Viewing all articles
Browse latest Browse all 9353

Latest Images

Trending Articles