บทช่วยสอนสั้นๆ นี้แนะนำวิธี ผสาน PDF กับ C# REST API คุณจะได้เรียนรู้วิธี รวม PDF ทั้งหมดเข้ากับอินเทอร์เฟซ C# REST โดยใช้ SDK โดยจะอธิบายขั้นตอนทั้งหมดที่จำเป็นในการพัฒนาแอปพลิเคชันสำหรับการรวมไฟล์ PDF หลายไฟล์
ข้อกำหนดเบื้องต้น
ดาวน์โหลด Aspose.PDF Cloud SDK for Dotnet
ตั้งค่าโครงการ C# ด้วย SDK ข้างต้น
ขั้นตอนในการรวม PDF เข้ากับ C# Low Code API
- ตั้งค่าคีย์ API และ SID สำหรับการเริ่มต้นวัตถุคลาส PdfApi
- สร้างออบเจ็กต์ MergeDocuments และจัดเตรียมรายการไฟล์ PDF ที่คุณต้องการผสาน
- อัปโหลดไฟล์ PDF ต้นฉบับทั้งหมดโดยใช้เมธอด UploadFile() ในคลาส PdfApi
- เรียกใช้เมธอด PutMergeDocuments() เพื่อรวมไฟล์ PDF ทั้งหมดเป็นไฟล์ PDF ปลายทางไฟล์เดียว
- ดาวน์โหลดและบันทึกไฟล์ PDF เอาต์พุตที่มีเนื้อหาของไฟล์ PDF ที่รวมเข้าด้วยกันทั้งหมด
ขั้นตอนเหล่านี้สรุปวิธีการ รวม PDF ออนไลน์ด้วย C# REST API ใช้คลาสออบเจ็กต์ MergeDocuments เพื่อสร้างรายการไฟล์ PDF ที่คุณต้องการผสาน และอัปโหลดไฟล์ PDF เหล่านี้ทั้งหมดไปยังคลาวด์โดยใช้วิธี PdfApi.UploadFile() และเรียกใช้วิธี PutMergeDocuments() เพื่อรวมไฟล์ PDF ออนไลน์ การเรียก API จะส่งคืนสถานะเป็น OK หลังจากนั้นคุณสามารถเรียกใช้เมธอด DownloadFile เพื่อดึงข้อมูลไฟล์ PDF เอาต์พุตได้
รหัสเพื่อรวมไฟล์ PDF เข้ากับ API ที่ใช้ C# .NET
using System; | |
using System.IO; | |
using Aspose.Pdf.Cloud.Sdk.Api; | |
using Aspose.Pdf.Cloud.Sdk.Model; | |
using System.Collections.Generic; | |
namespace Aspose.PDF.Cloud.Examples.Kb | |
{ | |
public class PdfTasks | |
{ | |
public static void SplitPDFFiles() | |
{ | |
PdfApi pdfApi = new PdfApi("API KEY", "API SID"); | |
String fileName = "sample.pdf"; | |
String format = "pdf"; | |
int from = 1; | |
int to = 2; | |
String storage = ""; | |
String folder = ""; | |
try | |
{ | |
// Upload source file to aspose cloud storage | |
pdfApi.UploadFile(fileName, new MemoryStream( System.IO.File.ReadAllBytes(fileName))); | |
// Invoke Aspose.PDF Cloud SDK API to split pdf files | |
SplitResultResponse apiResponse = pdfApi.PostSplitDocument(fileName, format, from, to, storage, folder); | |
if (apiResponse.Status.Equals("OK")) | |
{ | |
// Download created pdf file | |
foreach(var item in apiResponse.Result.Documents) | |
{ | |
Stream storageRes = pdfApi.DownloadFile(item.Href); | |
storageRes.Position = 0; | |
using (FileStream fileStream = new FileStream(item.Href, FileMode.Create, FileAccess.Write)) | |
{ | |
storageRes.CopyTo(fileStream); | |
} | |
} | |
Console.WriteLine("Split PDF Files, Done!"); | |
Console.ReadKey(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
} | |
} | |
public static void MergePDFFiles() | |
{ | |
PdfApi pdfApi = new PdfApi("API KEY", "API SID");// For merging PDF files | |
String fileName = "sample-merged.pdf"; | |
String storage = ""; | |
String folder = ""; | |
MergeDocuments body = new MergeDocuments(new List<string> { "sample.pdf", "input.pdf" }); | |
try | |
{ | |
// Load the input Word file | |
pdfApi.UploadFile("sample.pdf", new MemoryStream(System.IO.File.ReadAllBytes("sample.pdf"))); | |
pdfApi.UploadFile("input.pdf", new MemoryStream(System.IO.File.ReadAllBytes("input.pdf"))); | |
// Merge the PDF files | |
DocumentResponse apiResponse = pdfApi.PutMergeDocuments(fileName, body); | |
if (apiResponse != null && apiResponse.Status.Equals("OK")) | |
{ | |
// Download created pdf file | |
Stream storageRes = pdfApi.DownloadFile(fileName); | |
// Save response stream to a file | |
storageRes.Position = 0; | |
using (FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) | |
{ | |
storageRes.CopyTo(fileStream); | |
} | |
Console.WriteLine("Merge Multiple PDF Files, Done!"); | |
Console.ReadKey(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
} | |
} | |
public static void CreatePDF() | |
{ | |
try | |
{ | |
PdfApi pdfApi = new PdfApi("2086fd1a0db8175482a26f53c230483e", "fd43bbdc-4e3d-4a75-8847-2138b3ad167e"); | |
DocumentResponse response = pdfApi.PutCreateDocument("newPdfFile"); | |
if (response != null && response.Status.Equals("OK")) | |
{ | |
Paragraph para = PrepareParagraph("This is first text for new PDF"); | |
pdfApi.PutAddText("newPdfFile", 1, para); | |
var stream = pdfApi.DownloadFile("newPdfFile", null, null); | |
using (var fileStream = File.Create("output.pdf")) | |
{ | |
stream.Seek(0, SeekOrigin.Begin); | |
stream.CopyTo(fileStream); | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
Console.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
} | |
} | |
private static Paragraph PrepareParagraph(string text) | |
{ | |
List<TextLine> textLines = new List<TextLine>(); | |
Segment segment = new Segment(Value: text); | |
List<Segment> segments = new List<Segment>(); | |
segments.Add(segment); | |
TextLine textLine = new TextLine(Segments: segments); | |
textLines.Add(textLine); | |
Rectangle rectangle = new Rectangle(50.0, 800, 300.0, 850); | |
Paragraph paragraph = new Paragraph(Lines: textLines, Rectangle: rectangle); | |
return paragraph; | |
} | |
//THIS CODE IS CREATINg INVALID PDF FILE | |
public static void CreatePDFFromXML() | |
{ | |
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.Pdf-for-Cloud | |
PdfApi pdfApi = new PdfApi("2086fd1a0db8175482a26f53c230483e", "fd43bbdc-4e3d-4a75-8847-2138b3ad167e"); | |
String fileName = "input_out.pdf"; | |
String templateFile = "sample.xsl"; | |
String dataFile = "sample.xml"; | |
//String templateType = "xml"; | |
String storageRef = "my_xml"; | |
//String folder = ""; | |
try | |
{ | |
// Upload source file to aspose cloud storage | |
byte[] templateFileBytes = System.IO.File.ReadAllBytes(templateFile); | |
MemoryStream templateFileStream = new MemoryStream(templateFileBytes); | |
byte[] dataFileBytes = System.IO.File.ReadAllBytes(dataFile); | |
MemoryStream dataFileStream = new MemoryStream(dataFileBytes); | |
pdfApi.UploadFile(templateFile, templateFileStream); | |
pdfApi.UploadFile(dataFile, dataFileStream); | |
// Invoke Aspose.PDF Cloud SDK API to create Pdf from XML | |
//DocumentResponse apiResponse = pdfApi.PutCreateDocument(fileName, templateFile, dataFile); | |
AsposeResponse apiResponse = pdfApi.PutXmlInStorageToPdf(fileName, dataFile, xslFilePath: templateFile, storage: "my_xml"); | |
if (apiResponse != null && apiResponse.Status.Equals("OK")) | |
{ | |
// Download created pdf file | |
Stream storageRes = pdfApi.DownloadFile(fileName); | |
// Save response stream to a file | |
//storageRes.CopyTo(new FileStream("Sample_out.pdf", FileMode.Create, FileAccess.Write)); | |
storageRes.Position = 0; | |
SaveStreamToFile(storageRes, fileName); | |
//Console.WriteLine("Create PDF from XML, Done!"); | |
//Console.ReadKey(); | |
} | |
} | |
catch (Exception ex) | |
{ | |
System.Diagnostics.Debug.WriteLine("error:" + ex.Message + "\n" + ex.StackTrace); | |
} | |
} | |
private static void SaveStreamToFile(Stream sourceStream, string filePath) | |
{ | |
// Ensure the directory exists | |
//string directoryPath = Path.GetDirectoryName(filePath); | |
//if (!Directory.Exists(directoryPath)) | |
//{ | |
// Directory.CreateDirectory(directoryPath); | |
//} | |
// Create a FileStream to write the stream to the file | |
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) | |
{ | |
sourceStream.CopyTo(fileStream); | |
} | |
} | |
} | |
} |
รหัสนี้ได้สอนเราถึงวิธีการ เชื่อมไฟล์ PDF เข้ากับอินเทอร์เฟซ C# REST ใช้ชื่อที่คล้ายกันในเมธอด UploadFile() ตามที่ใช้ในรายการไฟล์ PDF ในออบเจ็กต์ MergeDocuments เมื่อคุณดาวน์โหลดไฟล์ PDF ที่เป็นผลลัพธ์ ให้ตั้งค่าพารามิเตอร์ Position เป็นศูนย์ ไม่เช่นนั้นคุณจะไม่สามารถบันทึกไฟล์ลงในดิสก์ได้
บทความนี้ได้อธิบายวิธี รวมเอกสาร PDF ออนไลน์ด้วย API ที่ใช้ C# .NET หากต้องการสร้างไฟล์ PDF เปล่า โปรดดูบทความต่อไปนี้: สร้างเอกสาร PDF ด้วย NET REST API