亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將 JDF 文件轉換為 PDF(從多編碼文檔中刪除文本)

如何將 JDF 文件轉換為 PDF(從多編碼文檔中刪除文本)

C#
呼如林 2022-12-31 13:46:06
我正在嘗試使用 C# 將 JDF 文件轉換為 PDF 文件。查看JDF 格式后...我可以看到該文件只是一個放置在 PDF 文檔頂部的 XML。我已經嘗試使用StreamWriter / StreamReaderC# 中的功能,但由于 PDF 文檔還包含二進制數據和可變換行符(\r\t 和 \t),因此無法打開生成的文件,因為某些二進制數據在 PDF 上被銷毀了。這是我嘗試使用但沒有成功的一些代碼。using (StreamReader reader = new StreamReader(_jdf.FullName, Encoding.Default)){    using (StreamWriter writer = new StreamWriter(_pdf.FullName, false, Encoding.Default))    {        writer.NewLine = "\n"; //Tried without this and with \r\n        bool IsStartOfPDF = false;        while (!reader.EndOfStream)        {            var line = reader.ReadLine();            if (line.IndexOf("%PDF-") != -1)            {                IsStartOfPDF = true;            }            if (!IsStartOfPDF)            {                continue;            }            writer.WriteLine(line);        }    }}
查看完整描述

1 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

我正在自我回答這個問題,因為它可能是一個有點常見的問題,并且解決方案可以為其他人提供信息。


由于文檔包含二進制文件和文本,我們不能簡單地使用將StreamWriter二進制文件寫回另一個文件。即使當您使用StreamWriter讀取文件然后將所有內容寫入另一個文件時,您也會意識到文件之間的差異。


您可以使用BinaryWriter來搜索包含多個部分的文檔并將每個字節完全按照您在另一個文檔中找到的那樣寫入。


//Using a Binary Reader/Writer as the PDF is multitype

using (var reader = new BinaryReader(File.Open(_file.FullName, FileMode.Open)))

{

    using (var writer = new BinaryWriter(File.Open(tempFileName.FullName, FileMode.CreateNew)))

    {


        //We are searching for the start of the PDF 

        bool searchingForstartOfPDF = true;

        var startOfPDF = "%PDF-".ToCharArray();


        //While we haven't reached the end of the stream

        while (reader.BaseStream.Position != reader.BaseStream.Length)

        {

            //If we are still searching for the start of the PDF

            if (searchingForstartOfPDF)

            {

                //Read the current Char

                var str = reader.ReadChar();


                //If it matches the start of the PDF signiture

                if (str.Equals(startOfPDF[0]))

                {

                    //Check the next few characters to see if they match

                    //keeping an eye on our current position in the stream incase something goes wrong

                    var currBasePos = reader.BaseStream.Position;

                    for (var i = 1; i < startOfPDF.Length; i++)

                    {

                        //If we found a char that isn't in the PDF signiture, then resume the while loop

                        //to start searching again from the next position

                        if (!reader.ReadChar().Equals(startOfPDF[i]))

                        {

                            reader.BaseStream.Position = currBasePos;

                            break;

                        }

                        //If we've reached the end of the PDF signiture then we've found a match

                        if (i == startOfPDF.Length - 1)

                        {

                            //Success

                            //Set the Position to the start of the PDF signiture 

                            searchingForstartOfPDF = false;

                            reader.BaseStream.Position -= startOfPDF.Length;

                            //We are no longer searching for the PDF Signiture so 

                            //the remaining bytes in the file will be directly wrote

                            //using the stream writer

                        }

                    }

                }

            }

            else

            {

                //We are writing the binary now

                writer.Write(reader.ReadByte());

            }

        }


    }

}

此代碼示例使用BinaryReader1 對 1 地讀取每個字符,如果找到匹配的字符串%PDF-(PDF 開始簽名),它將閱讀器位置移回%,然后使用 寫入剩余文檔writer.Write(reader.ReadByte())。


查看完整回答
反對 回復 2022-12-31
  • 1 回答
  • 0 關注
  • 126 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號