gpt4 book ai didi

c# - 我的 PDF 语法坏了!如何将图像放入PDF?

转载 作者:行者123 更新时间:2023-11-30 12:45:20 25 4
gpt4 key购买 nike

我目前正在尝试在没有外部库的帮助下使用 c# 在 Windows Phone 上自行编码 PDF。我目前有这段代码,它会生成一个 PDF 文件,我可以使用 PowerTools 检索它。文本有效,但每当我尝试放入图像时,它就会完全失败。我可以检索文件,但其中没有图像。我有点不知所措,但我的问题是:这段代码哪里出了问题?如何正确地将图像文件流式传输为 PDF 文件?

编辑:将我的代码更新到最新;带有指向 pdf 对象的适当指针。

var stream = await System.IO.WindowsRuntimeStorageExtensions.OpenStreamForWriteAsync(pdfFile);
StreamWriter writer = new StreamWriter(stream, System.Text.Encoding.UTF8);
List<long> xrefs = new List<long>();

//Writing the actual PDF
writer.WriteLine("%");
writer.WriteLine("%PDF-1.5");
writer.WriteLine("%");

//1
writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);
writer.WriteLine("");
writer.WriteLine("%The PDF catalog");
writer.WriteLine(xrefs.Count + " 0 obj");
writer.WriteLine("<<");
writer.WriteLine(" /Type /Catalog");
writer.WriteLine(" /Pages 2 0 R");
writer.WriteLine(">>");
writer.WriteLine("endobj");

writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);

//2
//Declare the page list
writer.WriteLine("");
writer.WriteLine("%The page list");
writer.WriteLine(xrefs.Count + " 0 obj");
writer.WriteLine("<<");
writer.WriteLine(" /Type /Pages");
writer.WriteLine(" /Kids [3 0 R]");
//writer.WriteLine(" /Resources 3 0 R");
writer.WriteLine("/Count 1");
writer.WriteLine(">>");
writer.WriteLine("endobj");

writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);


//3
//Declare the page object
writer.WriteLine("");
writer.WriteLine("%Actual page, with references to all objects it uses (image and resources and whatnot)");
writer.WriteLine(xrefs.Count + " 0 obj");
writer.WriteLine("<<");
writer.WriteLine(" /Type /Page");
writer.WriteLine(" /Parent 2 0 R");
writer.WriteLine(" /Resources 4 0 R");
writer.WriteLine(" /MediaBox [0 0 " + currentImage.PixelWidth + " " + currentImage.PixelHeight + "]");
//612 792
writer.WriteLine(" /Contents 6 0 R");
writer.WriteLine(">>");
writer.WriteLine("endobj");

writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);

//4
//Declare the stuff this PDF has (Or something?)
writer.WriteLine("");
writer.WriteLine("%Resources this pdf uses");
writer.WriteLine(xrefs.Count + " 0 obj");
writer.WriteLine("<<");
writer.WriteLine("/ProcSet [/PDF /ImageC]");
writer.WriteLine("/XObject << Im1 5 0 R >>");
writer.WriteLine(">>");
writer.WriteLine("endobj");

writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);



//Writing the actual image stream
writer.WriteLine("stream");

byte[] jpegData = new byte[8192];
string streamString = "";
using (MemoryStream imageStream = new MemoryStream())
{
WriteableBitmap wBitmap = new WriteableBitmap(currentImage);

wBitmap.SaveJpeg(imageStream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
imageStream.Seek(0, SeekOrigin.Begin);
jpegData = imageStream.ToArray();

foreach (byte b in jpegData)
{
streamString += b + " ";
}
}



//5
writer.WriteLine("");
writer.WriteLine("%image declaration");
writer.WriteLine(xrefs.Count + " 0 obj");
writer.WriteLine("<<");
writer.WriteLine(" /Type /XObject"); //Specify the XOBject
writer.WriteLine(" /Subtype /Image"); //It is an image
writer.WriteLine(" /Width " + currentImage.PixelWidth); //The dimensions of the image
writer.WriteLine(" /Height " + currentImage.PixelHeight);
writer.WriteLine(" /ColorSpace /DeviceRGB");
writer.WriteLine(" /BitsPerComponent 8");
writer.WriteLine(" /Length " + streamString.Length);
writer.WriteLine(" /Filter /DCTDecode");
writer.WriteLine(">>");

writer.Flush();
stream.Flush();

writer.Write(streamString);

writer.Flush();

writer.WriteLine("");
writer.WriteLine("endstream");
writer.WriteLine("endobj");

writer.Flush();
stream.Flush();
xrefs.Add(stream.Position);

//6
writer.WriteLine("");
writer.WriteLine("%the placing of the image");
writer.WriteLine(xrefs.Count + " 0 obj");
writer.WriteLine(" <<");
writer.WriteLine(" /Length 8192");
writer.WriteLine(" >>");
writer.WriteLine(" stream");
writer.WriteLine(" q");
writer.WriteLine(" " + 256 + " 0 0 " + 256 + " 0 0 cm");
writer.WriteLine(" /Im1 Do");
writer.WriteLine(" Q");
writer.WriteLine(" endstream");
writer.WriteLine("endobj");
//writer.WriteLine(">>");

writer.Flush();
stream.Flush();
//xrefs.Add(stream.Position);

//Closing
//PDF-XREFS. This part of the PDF is an index table into every object #1..#7 that we defined.

long xref_pos = stream.Position;
writer.WriteLine("xref");
writer.WriteLine("1 " + (xrefs.Count));

foreach (long xref in xrefs)
{
writer.WriteLine("{0:0000000000} {1:00000} n", xref, 0);
}

// PDF-TRAILER. Every PDF ends with this trailer.
writer.WriteLine("trailer");
writer.WriteLine("<<");
writer.WriteLine(" /Size " + xrefs.Count);
writer.WriteLine(" /Root 1 0 R");
writer.WriteLine(">>");
writer.WriteLine("startxref");
writer.WriteLine(xref_pos);
writer.WriteLine("%%EOF");

writer.Dispose();

最佳答案

希望这对你有帮助..

using System;  
using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Graphics;

namespace DrawImage
{
class Program
{
static void Main(string[] args)
{
//Create PDF and Add Page
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

//Draw Image
PdfImage image = PdfImage.FromFile(@"D:\Images\Spring's Coming.png");
float width = image.Width * 0.75f;
float height = image.Height * 0.75f;
float x = (page.Canvas.ClientSize.Width - width) / 2;

page.Canvas.DrawImage(image, x, 60, width, height);

//Save and Launch
doc.SaveToFile("InsertImage.pdf");
doc.Close();
System.Diagnostics.Process.Start("InsertImage.pdf");
}
}
}

关于c# - 我的 PDF 语法坏了!如何将图像放入PDF?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25104661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com